Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH) |
| 3 | * |
| 4 | * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> |
| 5 | * |
| 6 | * Interactivity improvements by Mike Galbraith |
| 7 | * (C) 2007 Mike Galbraith <efault@gmx.de> |
| 8 | * |
| 9 | * Various enhancements by Dmitry Adamushko. |
| 10 | * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com> |
| 11 | * |
| 12 | * Group scheduling enhancements by Srivatsa Vaddagiri |
| 13 | * Copyright IBM Corporation, 2007 |
| 14 | * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com> |
| 15 | * |
| 16 | * Scaled math optimizations by Thomas Gleixner |
| 17 | * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de> |
Peter Zijlstra | 2180508 | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 18 | * |
| 19 | * Adaptive scheduling granularity, math enhancements by Peter Zijlstra |
| 20 | * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 21 | */ |
| 22 | |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 23 | #include <linux/latencytop.h> |
Christian Ehrhardt | 1983a92 | 2009-11-30 12:16:47 +0100 | [diff] [blame] | 24 | #include <linux/sched.h> |
Sisir Koppaka | 3436ae1 | 2011-03-26 18:22:55 +0530 | [diff] [blame] | 25 | #include <linux/cpumask.h> |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 26 | #include <linux/slab.h> |
| 27 | #include <linux/profile.h> |
| 28 | #include <linux/interrupt.h> |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 29 | #include <linux/mempolicy.h> |
Mel Gorman | e14808b | 2012-11-19 10:59:15 +0000 | [diff] [blame] | 30 | #include <linux/migrate.h> |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 31 | #include <linux/task_work.h> |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 32 | |
| 33 | #include <trace/events/sched.h> |
| 34 | |
| 35 | #include "sched.h" |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 36 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 37 | /* |
Peter Zijlstra | 2180508 | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 38 | * Targeted preemption latency for CPU-bound tasks: |
Takuya Yoshikawa | 864616e | 2010-10-14 16:09:13 +0900 | [diff] [blame] | 39 | * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 40 | * |
Peter Zijlstra | 2180508 | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 41 | * NOTE: this latency value is not the same as the concept of |
Ingo Molnar | d274a4c | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 42 | * 'timeslice length' - timeslices in CFS are of variable length |
| 43 | * and have no persistent notion like in traditional, time-slice |
| 44 | * based scheduling concepts. |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 45 | * |
Ingo Molnar | d274a4c | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 46 | * (to see the precise effective timeslice length of your workload, |
| 47 | * run vmstat and monitor the context-switches (cs) field) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 48 | */ |
Mike Galbraith | 2140692 | 2010-03-11 17:17:15 +0100 | [diff] [blame] | 49 | unsigned int sysctl_sched_latency = 6000000ULL; |
| 50 | unsigned int normalized_sysctl_sched_latency = 6000000ULL; |
Ingo Molnar | 2bd8e6d | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 51 | |
| 52 | /* |
Christian Ehrhardt | 1983a92 | 2009-11-30 12:16:47 +0100 | [diff] [blame] | 53 | * The initial- and re-scaling of tunables is configurable |
| 54 | * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus)) |
| 55 | * |
| 56 | * Options are: |
| 57 | * SCHED_TUNABLESCALING_NONE - unscaled, always *1 |
| 58 | * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus) |
| 59 | * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus |
| 60 | */ |
| 61 | enum sched_tunable_scaling sysctl_sched_tunable_scaling |
| 62 | = SCHED_TUNABLESCALING_LOG; |
| 63 | |
| 64 | /* |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 65 | * Minimal preemption granularity for CPU-bound tasks: |
Takuya Yoshikawa | 864616e | 2010-10-14 16:09:13 +0900 | [diff] [blame] | 66 | * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds) |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 67 | */ |
Ingo Molnar | 0bf377b | 2010-09-12 08:14:52 +0200 | [diff] [blame] | 68 | unsigned int sysctl_sched_min_granularity = 750000ULL; |
| 69 | unsigned int normalized_sysctl_sched_min_granularity = 750000ULL; |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 70 | |
| 71 | /* |
| 72 | * is kept at sysctl_sched_latency / sysctl_sched_min_granularity |
| 73 | */ |
Ingo Molnar | 0bf377b | 2010-09-12 08:14:52 +0200 | [diff] [blame] | 74 | static unsigned int sched_nr_latency = 8; |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 75 | |
| 76 | /* |
Mike Galbraith | 2bba22c | 2009-09-09 15:41:37 +0200 | [diff] [blame] | 77 | * After fork, child runs first. If set to 0 (default) then |
Ingo Molnar | 2bd8e6d | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 78 | * parent will (try to) run first. |
| 79 | */ |
Mike Galbraith | 2bba22c | 2009-09-09 15:41:37 +0200 | [diff] [blame] | 80 | unsigned int sysctl_sched_child_runs_first __read_mostly; |
Peter Zijlstra | 2180508 | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 81 | |
| 82 | /* |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 83 | * SCHED_OTHER wake-up granularity. |
Mike Galbraith | 172e082 | 2009-09-09 15:41:37 +0200 | [diff] [blame] | 84 | * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 85 | * |
| 86 | * This option delays the preemption effects of decoupled workloads |
| 87 | * and reduces their over-scheduling. Synchronous workloads will still |
| 88 | * have immediate wakeup/sleep latencies. |
| 89 | */ |
Mike Galbraith | 172e082 | 2009-09-09 15:41:37 +0200 | [diff] [blame] | 90 | unsigned int sysctl_sched_wakeup_granularity = 1000000UL; |
Christian Ehrhardt | 0bcdcf2 | 2009-11-30 12:16:46 +0100 | [diff] [blame] | 91 | unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 92 | |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 93 | const_debug unsigned int sysctl_sched_migration_cost = 500000UL; |
| 94 | |
Paul Turner | a7a4f8a | 2010-11-15 15:47:06 -0800 | [diff] [blame] | 95 | /* |
| 96 | * The exponential sliding window over which load is averaged for shares |
| 97 | * distribution. |
| 98 | * (default: 10msec) |
| 99 | */ |
| 100 | unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL; |
| 101 | |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 102 | #ifdef CONFIG_CFS_BANDWIDTH |
| 103 | /* |
| 104 | * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool |
| 105 | * each time a cfs_rq requests quota. |
| 106 | * |
| 107 | * Note: in the case that the slice exceeds the runtime remaining (either due |
| 108 | * to consumption or the quota being specified to be smaller than the slice) |
| 109 | * we will always only issue the remaining available time. |
| 110 | * |
| 111 | * default: 5 msec, units: microseconds |
| 112 | */ |
| 113 | unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL; |
| 114 | #endif |
| 115 | |
Paul Gortmaker | 8527632 | 2013-04-19 15:10:50 -0400 | [diff] [blame] | 116 | static inline void update_load_add(struct load_weight *lw, unsigned long inc) |
| 117 | { |
| 118 | lw->weight += inc; |
| 119 | lw->inv_weight = 0; |
| 120 | } |
| 121 | |
| 122 | static inline void update_load_sub(struct load_weight *lw, unsigned long dec) |
| 123 | { |
| 124 | lw->weight -= dec; |
| 125 | lw->inv_weight = 0; |
| 126 | } |
| 127 | |
| 128 | static inline void update_load_set(struct load_weight *lw, unsigned long w) |
| 129 | { |
| 130 | lw->weight = w; |
| 131 | lw->inv_weight = 0; |
| 132 | } |
| 133 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 134 | /* |
| 135 | * Increase the granularity value when there are more CPUs, |
| 136 | * because with more CPUs the 'effective latency' as visible |
| 137 | * to users decreases. But the relationship is not linear, |
| 138 | * so pick a second-best guess by going with the log2 of the |
| 139 | * number of CPUs. |
| 140 | * |
| 141 | * This idea comes from the SD scheduler of Con Kolivas: |
| 142 | */ |
| 143 | static int get_update_sysctl_factor(void) |
| 144 | { |
| 145 | unsigned int cpus = min_t(int, num_online_cpus(), 8); |
| 146 | unsigned int factor; |
| 147 | |
| 148 | switch (sysctl_sched_tunable_scaling) { |
| 149 | case SCHED_TUNABLESCALING_NONE: |
| 150 | factor = 1; |
| 151 | break; |
| 152 | case SCHED_TUNABLESCALING_LINEAR: |
| 153 | factor = cpus; |
| 154 | break; |
| 155 | case SCHED_TUNABLESCALING_LOG: |
| 156 | default: |
| 157 | factor = 1 + ilog2(cpus); |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | return factor; |
| 162 | } |
| 163 | |
| 164 | static void update_sysctl(void) |
| 165 | { |
| 166 | unsigned int factor = get_update_sysctl_factor(); |
| 167 | |
| 168 | #define SET_SYSCTL(name) \ |
| 169 | (sysctl_##name = (factor) * normalized_sysctl_##name) |
| 170 | SET_SYSCTL(sched_min_granularity); |
| 171 | SET_SYSCTL(sched_latency); |
| 172 | SET_SYSCTL(sched_wakeup_granularity); |
| 173 | #undef SET_SYSCTL |
| 174 | } |
| 175 | |
| 176 | void sched_init_granularity(void) |
| 177 | { |
| 178 | update_sysctl(); |
| 179 | } |
| 180 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 181 | #define WMULT_CONST (~0U) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 182 | #define WMULT_SHIFT 32 |
| 183 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 184 | static void __update_inv_weight(struct load_weight *lw) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 185 | { |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 186 | unsigned long w; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 187 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 188 | if (likely(lw->inv_weight)) |
| 189 | return; |
| 190 | |
| 191 | w = scale_load_down(lw->weight); |
| 192 | |
| 193 | if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST)) |
| 194 | lw->inv_weight = 1; |
| 195 | else if (unlikely(!w)) |
| 196 | lw->inv_weight = WMULT_CONST; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 197 | else |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 198 | lw->inv_weight = WMULT_CONST / w; |
| 199 | } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 200 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 201 | /* |
| 202 | * delta_exec * weight / lw.weight |
| 203 | * OR |
| 204 | * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT |
| 205 | * |
| 206 | * Either weight := NICE_0_LOAD and lw \e prio_to_wmult[], in which case |
| 207 | * we're guaranteed shift stays positive because inv_weight is guaranteed to |
| 208 | * fit 32 bits, and NICE_0_LOAD gives another 10 bits; therefore shift >= 22. |
| 209 | * |
| 210 | * Or, weight =< lw.weight (because lw.weight is the runqueue weight), thus |
| 211 | * weight/lw.weight <= 1, and therefore our shift will also be positive. |
| 212 | */ |
| 213 | static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weight *lw) |
| 214 | { |
| 215 | u64 fact = scale_load_down(weight); |
| 216 | int shift = WMULT_SHIFT; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 217 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 218 | __update_inv_weight(lw); |
| 219 | |
| 220 | if (unlikely(fact >> 32)) { |
| 221 | while (fact >> 32) { |
| 222 | fact >>= 1; |
| 223 | shift--; |
| 224 | } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 227 | /* hint to use a 32x32->64 mul */ |
| 228 | fact = (u64)(u32)fact * lw->inv_weight; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 229 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 230 | while (fact >> 32) { |
| 231 | fact >>= 1; |
| 232 | shift--; |
| 233 | } |
| 234 | |
| 235 | return mul_u64_u32_shr(delta_exec, fact, shift); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | |
| 239 | const struct sched_class fair_sched_class; |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 240 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 241 | /************************************************************** |
| 242 | * CFS operations on generic schedulable entities: |
| 243 | */ |
| 244 | |
| 245 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 246 | |
| 247 | /* cpu runqueue to which this cfs_rq is attached */ |
| 248 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) |
| 249 | { |
| 250 | return cfs_rq->rq; |
| 251 | } |
| 252 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 253 | /* An entity is a task if it doesn't "own" a runqueue */ |
| 254 | #define entity_is_task(se) (!se->my_q) |
| 255 | |
Peter Zijlstra | 8f48894 | 2009-07-24 12:25:30 +0200 | [diff] [blame] | 256 | static inline struct task_struct *task_of(struct sched_entity *se) |
| 257 | { |
| 258 | #ifdef CONFIG_SCHED_DEBUG |
| 259 | WARN_ON_ONCE(!entity_is_task(se)); |
| 260 | #endif |
| 261 | return container_of(se, struct task_struct, se); |
| 262 | } |
| 263 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 264 | /* Walk up scheduling entities hierarchy */ |
| 265 | #define for_each_sched_entity(se) \ |
| 266 | for (; se; se = se->parent) |
| 267 | |
| 268 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) |
| 269 | { |
| 270 | return p->se.cfs_rq; |
| 271 | } |
| 272 | |
| 273 | /* runqueue on which this entity is (to be) queued */ |
| 274 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) |
| 275 | { |
| 276 | return se->cfs_rq; |
| 277 | } |
| 278 | |
| 279 | /* runqueue "owned" by this group */ |
| 280 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) |
| 281 | { |
| 282 | return grp->my_q; |
| 283 | } |
| 284 | |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 285 | static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq, |
| 286 | int force_update); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 287 | |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 288 | static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
| 289 | { |
| 290 | if (!cfs_rq->on_list) { |
Paul Turner | 67e8625 | 2010-11-15 15:47:05 -0800 | [diff] [blame] | 291 | /* |
| 292 | * Ensure we either appear before our parent (if already |
| 293 | * enqueued) or force our parent to appear after us when it is |
| 294 | * enqueued. The fact that we always enqueue bottom-up |
| 295 | * reduces this to two cases. |
| 296 | */ |
| 297 | if (cfs_rq->tg->parent && |
| 298 | cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) { |
| 299 | list_add_rcu(&cfs_rq->leaf_cfs_rq_list, |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 300 | &rq_of(cfs_rq)->leaf_cfs_rq_list); |
Paul Turner | 67e8625 | 2010-11-15 15:47:05 -0800 | [diff] [blame] | 301 | } else { |
| 302 | list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, |
| 303 | &rq_of(cfs_rq)->leaf_cfs_rq_list); |
| 304 | } |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 305 | |
| 306 | cfs_rq->on_list = 1; |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 307 | /* We should have no load, but we need to update last_decay. */ |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 308 | update_cfs_rq_blocked_load(cfs_rq, 0); |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
| 312 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
| 313 | { |
| 314 | if (cfs_rq->on_list) { |
| 315 | list_del_rcu(&cfs_rq->leaf_cfs_rq_list); |
| 316 | cfs_rq->on_list = 0; |
| 317 | } |
| 318 | } |
| 319 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 320 | /* Iterate thr' all leaf cfs_rq's on a runqueue */ |
| 321 | #define for_each_leaf_cfs_rq(rq, cfs_rq) \ |
| 322 | list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list) |
| 323 | |
| 324 | /* Do the two (enqueued) entities belong to the same group ? */ |
| 325 | static inline int |
| 326 | is_same_group(struct sched_entity *se, struct sched_entity *pse) |
| 327 | { |
| 328 | if (se->cfs_rq == pse->cfs_rq) |
| 329 | return 1; |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static inline struct sched_entity *parent_entity(struct sched_entity *se) |
| 335 | { |
| 336 | return se->parent; |
| 337 | } |
| 338 | |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 339 | /* return depth at which a sched entity is present in the hierarchy */ |
| 340 | static inline int depth_se(struct sched_entity *se) |
| 341 | { |
| 342 | int depth = 0; |
| 343 | |
| 344 | for_each_sched_entity(se) |
| 345 | depth++; |
| 346 | |
| 347 | return depth; |
| 348 | } |
| 349 | |
| 350 | static void |
| 351 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) |
| 352 | { |
| 353 | int se_depth, pse_depth; |
| 354 | |
| 355 | /* |
| 356 | * preemption test can be made between sibling entities who are in the |
| 357 | * same cfs_rq i.e who have a common parent. Walk up the hierarchy of |
| 358 | * both tasks until we find their ancestors who are siblings of common |
| 359 | * parent. |
| 360 | */ |
| 361 | |
| 362 | /* First walk up until both entities are at same depth */ |
| 363 | se_depth = depth_se(*se); |
| 364 | pse_depth = depth_se(*pse); |
| 365 | |
| 366 | while (se_depth > pse_depth) { |
| 367 | se_depth--; |
| 368 | *se = parent_entity(*se); |
| 369 | } |
| 370 | |
| 371 | while (pse_depth > se_depth) { |
| 372 | pse_depth--; |
| 373 | *pse = parent_entity(*pse); |
| 374 | } |
| 375 | |
| 376 | while (!is_same_group(*se, *pse)) { |
| 377 | *se = parent_entity(*se); |
| 378 | *pse = parent_entity(*pse); |
| 379 | } |
| 380 | } |
| 381 | |
Peter Zijlstra | 8f48894 | 2009-07-24 12:25:30 +0200 | [diff] [blame] | 382 | #else /* !CONFIG_FAIR_GROUP_SCHED */ |
| 383 | |
| 384 | static inline struct task_struct *task_of(struct sched_entity *se) |
| 385 | { |
| 386 | return container_of(se, struct task_struct, se); |
| 387 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 388 | |
| 389 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) |
| 390 | { |
| 391 | return container_of(cfs_rq, struct rq, cfs); |
| 392 | } |
| 393 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 394 | #define entity_is_task(se) 1 |
| 395 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 396 | #define for_each_sched_entity(se) \ |
| 397 | for (; se; se = NULL) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 398 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 399 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 400 | { |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 401 | return &task_rq(p)->cfs; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 402 | } |
| 403 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 404 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) |
| 405 | { |
| 406 | struct task_struct *p = task_of(se); |
| 407 | struct rq *rq = task_rq(p); |
| 408 | |
| 409 | return &rq->cfs; |
| 410 | } |
| 411 | |
| 412 | /* runqueue "owned" by this group */ |
| 413 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) |
| 414 | { |
| 415 | return NULL; |
| 416 | } |
| 417 | |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 418 | static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
| 419 | { |
| 420 | } |
| 421 | |
| 422 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
| 423 | { |
| 424 | } |
| 425 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 426 | #define for_each_leaf_cfs_rq(rq, cfs_rq) \ |
| 427 | for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL) |
| 428 | |
| 429 | static inline int |
| 430 | is_same_group(struct sched_entity *se, struct sched_entity *pse) |
| 431 | { |
| 432 | return 1; |
| 433 | } |
| 434 | |
| 435 | static inline struct sched_entity *parent_entity(struct sched_entity *se) |
| 436 | { |
| 437 | return NULL; |
| 438 | } |
| 439 | |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 440 | static inline void |
| 441 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) |
| 442 | { |
| 443 | } |
| 444 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 445 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 446 | |
Peter Zijlstra | 6c16a6d | 2012-03-21 13:07:16 -0700 | [diff] [blame] | 447 | static __always_inline |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 448 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 449 | |
| 450 | /************************************************************** |
| 451 | * Scheduling class tree data structure manipulation methods: |
| 452 | */ |
| 453 | |
Andrei Epure | 1bf0823 | 2013-03-12 21:12:24 +0200 | [diff] [blame] | 454 | static inline u64 max_vruntime(u64 max_vruntime, u64 vruntime) |
Peter Zijlstra | 02e0431 | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 455 | { |
Andrei Epure | 1bf0823 | 2013-03-12 21:12:24 +0200 | [diff] [blame] | 456 | s64 delta = (s64)(vruntime - max_vruntime); |
Peter Zijlstra | 368059a | 2007-10-15 17:00:11 +0200 | [diff] [blame] | 457 | if (delta > 0) |
Andrei Epure | 1bf0823 | 2013-03-12 21:12:24 +0200 | [diff] [blame] | 458 | max_vruntime = vruntime; |
Peter Zijlstra | 02e0431 | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 459 | |
Andrei Epure | 1bf0823 | 2013-03-12 21:12:24 +0200 | [diff] [blame] | 460 | return max_vruntime; |
Peter Zijlstra | 02e0431 | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 461 | } |
| 462 | |
Ingo Molnar | 0702e3e | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 463 | static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime) |
Peter Zijlstra | b0ffd24 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 464 | { |
| 465 | s64 delta = (s64)(vruntime - min_vruntime); |
| 466 | if (delta < 0) |
| 467 | min_vruntime = vruntime; |
| 468 | |
| 469 | return min_vruntime; |
| 470 | } |
| 471 | |
Fabio Checconi | 54fdc58 | 2009-07-16 12:32:27 +0200 | [diff] [blame] | 472 | static inline int entity_before(struct sched_entity *a, |
| 473 | struct sched_entity *b) |
| 474 | { |
| 475 | return (s64)(a->vruntime - b->vruntime) < 0; |
| 476 | } |
| 477 | |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 478 | static void update_min_vruntime(struct cfs_rq *cfs_rq) |
| 479 | { |
| 480 | u64 vruntime = cfs_rq->min_vruntime; |
| 481 | |
| 482 | if (cfs_rq->curr) |
| 483 | vruntime = cfs_rq->curr->vruntime; |
| 484 | |
| 485 | if (cfs_rq->rb_leftmost) { |
| 486 | struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost, |
| 487 | struct sched_entity, |
| 488 | run_node); |
| 489 | |
Peter Zijlstra | e17036d | 2009-01-15 14:53:39 +0100 | [diff] [blame] | 490 | if (!cfs_rq->curr) |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 491 | vruntime = se->vruntime; |
| 492 | else |
| 493 | vruntime = min_vruntime(vruntime, se->vruntime); |
| 494 | } |
| 495 | |
Andrei Epure | 1bf0823 | 2013-03-12 21:12:24 +0200 | [diff] [blame] | 496 | /* ensure we never gain time by being placed backwards. */ |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 497 | cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime); |
Peter Zijlstra | 3fe1698 | 2011-04-05 17:23:48 +0200 | [diff] [blame] | 498 | #ifndef CONFIG_64BIT |
| 499 | smp_wmb(); |
| 500 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; |
| 501 | #endif |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 502 | } |
| 503 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 504 | /* |
| 505 | * Enqueue an entity into the rb-tree: |
| 506 | */ |
Ingo Molnar | 0702e3e | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 507 | 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] | 508 | { |
| 509 | struct rb_node **link = &cfs_rq->tasks_timeline.rb_node; |
| 510 | struct rb_node *parent = NULL; |
| 511 | struct sched_entity *entry; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 512 | int leftmost = 1; |
| 513 | |
| 514 | /* |
| 515 | * Find the right place in the rbtree: |
| 516 | */ |
| 517 | while (*link) { |
| 518 | parent = *link; |
| 519 | entry = rb_entry(parent, struct sched_entity, run_node); |
| 520 | /* |
| 521 | * We dont care about collisions. Nodes with |
| 522 | * the same key stay together. |
| 523 | */ |
Stephan Baerwolf | 2bd2d6f | 2011-07-20 14:46:59 +0200 | [diff] [blame] | 524 | if (entity_before(se, entry)) { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 525 | link = &parent->rb_left; |
| 526 | } else { |
| 527 | link = &parent->rb_right; |
| 528 | leftmost = 0; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /* |
| 533 | * Maintain a cache of leftmost tree entries (it is frequently |
| 534 | * used): |
| 535 | */ |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 536 | if (leftmost) |
Ingo Molnar | 57cb499 | 2007-10-15 17:00:11 +0200 | [diff] [blame] | 537 | cfs_rq->rb_leftmost = &se->run_node; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 538 | |
| 539 | rb_link_node(&se->run_node, parent, link); |
| 540 | rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 541 | } |
| 542 | |
Ingo Molnar | 0702e3e | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 543 | 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] | 544 | { |
Peter Zijlstra | 3fe6974 | 2008-03-14 20:55:51 +0100 | [diff] [blame] | 545 | if (cfs_rq->rb_leftmost == &se->run_node) { |
| 546 | struct rb_node *next_node; |
Peter Zijlstra | 3fe6974 | 2008-03-14 20:55:51 +0100 | [diff] [blame] | 547 | |
| 548 | next_node = rb_next(&se->run_node); |
| 549 | cfs_rq->rb_leftmost = next_node; |
Peter Zijlstra | 3fe6974 | 2008-03-14 20:55:51 +0100 | [diff] [blame] | 550 | } |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 551 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 552 | rb_erase(&se->run_node, &cfs_rq->tasks_timeline); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 553 | } |
| 554 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 555 | struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 556 | { |
Peter Zijlstra | f4b6755 | 2008-11-04 21:25:07 +0100 | [diff] [blame] | 557 | struct rb_node *left = cfs_rq->rb_leftmost; |
| 558 | |
| 559 | if (!left) |
| 560 | return NULL; |
| 561 | |
| 562 | return rb_entry(left, struct sched_entity, run_node); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 563 | } |
| 564 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 565 | static struct sched_entity *__pick_next_entity(struct sched_entity *se) |
| 566 | { |
| 567 | struct rb_node *next = rb_next(&se->run_node); |
| 568 | |
| 569 | if (!next) |
| 570 | return NULL; |
| 571 | |
| 572 | return rb_entry(next, struct sched_entity, run_node); |
| 573 | } |
| 574 | |
| 575 | #ifdef CONFIG_SCHED_DEBUG |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 576 | struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq) |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 577 | { |
Ingo Molnar | 7eee3e6 | 2008-02-22 10:32:21 +0100 | [diff] [blame] | 578 | struct rb_node *last = rb_last(&cfs_rq->tasks_timeline); |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 579 | |
Balbir Singh | 70eee74 | 2008-02-22 13:25:53 +0530 | [diff] [blame] | 580 | if (!last) |
| 581 | return NULL; |
Ingo Molnar | 7eee3e6 | 2008-02-22 10:32:21 +0100 | [diff] [blame] | 582 | |
| 583 | return rb_entry(last, struct sched_entity, run_node); |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 584 | } |
| 585 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 586 | /************************************************************** |
| 587 | * Scheduling class statistics methods: |
| 588 | */ |
| 589 | |
Christian Ehrhardt | acb4a84 | 2009-11-30 12:16:48 +0100 | [diff] [blame] | 590 | int sched_proc_update_handler(struct ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 591 | void __user *buffer, size_t *lenp, |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 592 | loff_t *ppos) |
| 593 | { |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 594 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
Christian Ehrhardt | acb4a84 | 2009-11-30 12:16:48 +0100 | [diff] [blame] | 595 | int factor = get_update_sysctl_factor(); |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 596 | |
| 597 | if (ret || !write) |
| 598 | return ret; |
| 599 | |
| 600 | sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency, |
| 601 | sysctl_sched_min_granularity); |
| 602 | |
Christian Ehrhardt | acb4a84 | 2009-11-30 12:16:48 +0100 | [diff] [blame] | 603 | #define WRT_SYSCTL(name) \ |
| 604 | (normalized_sysctl_##name = sysctl_##name / (factor)) |
| 605 | WRT_SYSCTL(sched_min_granularity); |
| 606 | WRT_SYSCTL(sched_latency); |
| 607 | WRT_SYSCTL(sched_wakeup_granularity); |
Christian Ehrhardt | acb4a84 | 2009-11-30 12:16:48 +0100 | [diff] [blame] | 608 | #undef WRT_SYSCTL |
| 609 | |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 610 | return 0; |
| 611 | } |
| 612 | #endif |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 613 | |
| 614 | /* |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 615 | * delta /= w |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 616 | */ |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 617 | static inline u64 calc_delta_fair(u64 delta, struct sched_entity *se) |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 618 | { |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 619 | if (unlikely(se->load.weight != NICE_0_LOAD)) |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 620 | delta = __calc_delta(delta, NICE_0_LOAD, &se->load); |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 621 | |
| 622 | return delta; |
| 623 | } |
| 624 | |
| 625 | /* |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 626 | * The idea is to set a period in which each task runs once. |
| 627 | * |
Borislav Petkov | 532b185 | 2012-08-08 16:16:04 +0200 | [diff] [blame] | 628 | * When there are too many tasks (sched_nr_latency) we have to stretch |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 629 | * this period because otherwise the slices get too small. |
| 630 | * |
| 631 | * p = (nr <= nl) ? l : l*nr/nl |
| 632 | */ |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 633 | static u64 __sched_period(unsigned long nr_running) |
| 634 | { |
| 635 | u64 period = sysctl_sched_latency; |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 636 | unsigned long nr_latency = sched_nr_latency; |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 637 | |
| 638 | if (unlikely(nr_running > nr_latency)) { |
Peter Zijlstra | 4bf0b77 | 2008-01-25 21:08:21 +0100 | [diff] [blame] | 639 | period = sysctl_sched_min_granularity; |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 640 | period *= nr_running; |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | return period; |
| 644 | } |
| 645 | |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 646 | /* |
| 647 | * We calculate the wall-time slice from the period by taking a part |
| 648 | * proportional to the weight. |
| 649 | * |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 650 | * s = p*P[w/rw] |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 651 | */ |
Peter Zijlstra | 6d0f0ebd | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 652 | 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] | 653 | { |
Mike Galbraith | 0a58244 | 2009-01-02 12:16:42 +0100 | [diff] [blame] | 654 | u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq); |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 655 | |
Mike Galbraith | 0a58244 | 2009-01-02 12:16:42 +0100 | [diff] [blame] | 656 | for_each_sched_entity(se) { |
Lin Ming | 6272d68 | 2009-01-15 17:17:15 +0100 | [diff] [blame] | 657 | struct load_weight *load; |
Christian Engelmayer | 3104bf0 | 2009-06-16 10:35:12 +0200 | [diff] [blame] | 658 | struct load_weight lw; |
Lin Ming | 6272d68 | 2009-01-15 17:17:15 +0100 | [diff] [blame] | 659 | |
| 660 | cfs_rq = cfs_rq_of(se); |
| 661 | load = &cfs_rq->load; |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 662 | |
Mike Galbraith | 0a58244 | 2009-01-02 12:16:42 +0100 | [diff] [blame] | 663 | if (unlikely(!se->on_rq)) { |
Christian Engelmayer | 3104bf0 | 2009-06-16 10:35:12 +0200 | [diff] [blame] | 664 | lw = cfs_rq->load; |
Mike Galbraith | 0a58244 | 2009-01-02 12:16:42 +0100 | [diff] [blame] | 665 | |
| 666 | update_load_add(&lw, se->load.weight); |
| 667 | load = &lw; |
| 668 | } |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 669 | slice = __calc_delta(slice, se->load.weight, load); |
Mike Galbraith | 0a58244 | 2009-01-02 12:16:42 +0100 | [diff] [blame] | 670 | } |
| 671 | return slice; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 672 | } |
| 673 | |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 674 | /* |
Andrei Epure | 660cc00 | 2013-03-11 12:03:20 +0200 | [diff] [blame] | 675 | * We calculate the vruntime slice of a to-be-inserted task. |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 676 | * |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 677 | * vs = s/w |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 678 | */ |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 679 | 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] | 680 | { |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 681 | return calc_delta_fair(sched_slice(cfs_rq, se), se); |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 682 | } |
| 683 | |
Alex Shi | a75cdaa | 2013-06-20 10:18:47 +0800 | [diff] [blame] | 684 | #ifdef CONFIG_SMP |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 685 | static unsigned long task_h_load(struct task_struct *p); |
| 686 | |
Alex Shi | a75cdaa | 2013-06-20 10:18:47 +0800 | [diff] [blame] | 687 | static inline void __update_task_entity_contrib(struct sched_entity *se); |
| 688 | |
| 689 | /* Give new task start runnable values to heavy its load in infant time */ |
| 690 | void init_task_runnable_average(struct task_struct *p) |
| 691 | { |
| 692 | u32 slice; |
| 693 | |
| 694 | p->se.avg.decay_count = 0; |
| 695 | slice = sched_slice(task_cfs_rq(p), &p->se) >> 10; |
| 696 | p->se.avg.runnable_avg_sum = slice; |
| 697 | p->se.avg.runnable_avg_period = slice; |
| 698 | __update_task_entity_contrib(&p->se); |
| 699 | } |
| 700 | #else |
| 701 | void init_task_runnable_average(struct task_struct *p) |
| 702 | { |
| 703 | } |
| 704 | #endif |
| 705 | |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 706 | /* |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 707 | * Update the current task's runtime statistics. |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 708 | */ |
Ingo Molnar | b7cc089 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 709 | static void update_curr(struct cfs_rq *cfs_rq) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 710 | { |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 711 | struct sched_entity *curr = cfs_rq->curr; |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 712 | u64 now = rq_clock_task(rq_of(cfs_rq)); |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 713 | u64 delta_exec; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 714 | |
| 715 | if (unlikely(!curr)) |
| 716 | return; |
| 717 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 718 | delta_exec = now - curr->exec_start; |
| 719 | if (unlikely((s64)delta_exec <= 0)) |
Peter Zijlstra | 34f28ec | 2008-12-16 08:45:31 +0100 | [diff] [blame] | 720 | return; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 721 | |
Ingo Molnar | 8ebc91d | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 722 | curr->exec_start = now; |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 723 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 724 | schedstat_set(curr->statistics.exec_max, |
| 725 | max(delta_exec, curr->statistics.exec_max)); |
| 726 | |
| 727 | curr->sum_exec_runtime += delta_exec; |
| 728 | schedstat_add(cfs_rq, exec_clock, delta_exec); |
| 729 | |
| 730 | curr->vruntime += calc_delta_fair(delta_exec, curr); |
| 731 | update_min_vruntime(cfs_rq); |
| 732 | |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 733 | if (entity_is_task(curr)) { |
| 734 | struct task_struct *curtask = task_of(curr); |
| 735 | |
Ingo Molnar | f977bb4 | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 736 | trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime); |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 737 | cpuacct_charge(curtask, delta_exec); |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 738 | account_group_exec_runtime(curtask, delta_exec); |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 739 | } |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 740 | |
| 741 | account_cfs_rq_runtime(cfs_rq, delta_exec); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | static inline void |
Ingo Molnar | 5870db5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 745 | 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] | 746 | { |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 747 | schedstat_set(se->statistics.wait_start, rq_clock(rq_of(cfs_rq))); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 748 | } |
| 749 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 750 | /* |
| 751 | * Task is being enqueued - update stats: |
| 752 | */ |
Ingo Molnar | d2417e5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 753 | 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] | 754 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 755 | /* |
| 756 | * Are we enqueueing a waiting task? (for current tasks |
| 757 | * a dequeue/enqueue event is a NOP) |
| 758 | */ |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 759 | if (se != cfs_rq->curr) |
Ingo Molnar | 5870db5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 760 | update_stats_wait_start(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 761 | } |
| 762 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 763 | static void |
Ingo Molnar | 9ef0a96 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 764 | 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] | 765 | { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 766 | schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max, |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 767 | rq_clock(rq_of(cfs_rq)) - se->statistics.wait_start)); |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 768 | schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1); |
| 769 | schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum + |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 770 | rq_clock(rq_of(cfs_rq)) - se->statistics.wait_start); |
Peter Zijlstra | 768d0c2 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 771 | #ifdef CONFIG_SCHEDSTATS |
| 772 | if (entity_is_task(se)) { |
| 773 | trace_sched_stat_wait(task_of(se), |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 774 | rq_clock(rq_of(cfs_rq)) - se->statistics.wait_start); |
Peter Zijlstra | 768d0c2 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 775 | } |
| 776 | #endif |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 777 | schedstat_set(se->statistics.wait_start, 0); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | static inline void |
Ingo Molnar | 19b6a2e | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 781 | update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 782 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 783 | /* |
| 784 | * Mark the end of the wait period if dequeueing a |
| 785 | * waiting task: |
| 786 | */ |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 787 | if (se != cfs_rq->curr) |
Ingo Molnar | 9ef0a96 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 788 | update_stats_wait_end(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | /* |
| 792 | * We are picking a new current task - update its stats: |
| 793 | */ |
| 794 | static inline void |
Ingo Molnar | 79303e9 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 795 | 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] | 796 | { |
| 797 | /* |
| 798 | * We are starting a new run period: |
| 799 | */ |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 800 | se->exec_start = rq_clock_task(rq_of(cfs_rq)); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 801 | } |
| 802 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 803 | /************************************************** |
| 804 | * Scheduling class queueing methods: |
| 805 | */ |
| 806 | |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 807 | #ifdef CONFIG_NUMA_BALANCING |
| 808 | /* |
Mel Gorman | 598f0ec | 2013-10-07 11:28:55 +0100 | [diff] [blame] | 809 | * Approximate time to scan a full NUMA task in ms. The task scan period is |
| 810 | * calculated based on the tasks virtual memory size and |
| 811 | * numa_balancing_scan_size. |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 812 | */ |
Mel Gorman | 598f0ec | 2013-10-07 11:28:55 +0100 | [diff] [blame] | 813 | unsigned int sysctl_numa_balancing_scan_period_min = 1000; |
| 814 | unsigned int sysctl_numa_balancing_scan_period_max = 60000; |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 815 | |
| 816 | /* Portion of address space to scan in MB */ |
| 817 | unsigned int sysctl_numa_balancing_scan_size = 256; |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 818 | |
Peter Zijlstra | 4b96a29 | 2012-10-25 14:16:47 +0200 | [diff] [blame] | 819 | /* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */ |
| 820 | unsigned int sysctl_numa_balancing_scan_delay = 1000; |
| 821 | |
Rik van Riel | de1c9ce | 2013-10-07 11:29:39 +0100 | [diff] [blame] | 822 | /* |
| 823 | * After skipping a page migration on a shared page, skip N more numa page |
| 824 | * migrations unconditionally. This reduces the number of NUMA migrations |
| 825 | * in shared memory workloads, and has the effect of pulling tasks towards |
| 826 | * where their memory lives, over pulling the memory towards the task. |
| 827 | */ |
| 828 | unsigned int sysctl_numa_balancing_migrate_deferred = 16; |
| 829 | |
Mel Gorman | 598f0ec | 2013-10-07 11:28:55 +0100 | [diff] [blame] | 830 | static unsigned int task_nr_scan_windows(struct task_struct *p) |
| 831 | { |
| 832 | unsigned long rss = 0; |
| 833 | unsigned long nr_scan_pages; |
| 834 | |
| 835 | /* |
| 836 | * Calculations based on RSS as non-present and empty pages are skipped |
| 837 | * by the PTE scanner and NUMA hinting faults should be trapped based |
| 838 | * on resident pages |
| 839 | */ |
| 840 | nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT); |
| 841 | rss = get_mm_rss(p->mm); |
| 842 | if (!rss) |
| 843 | rss = nr_scan_pages; |
| 844 | |
| 845 | rss = round_up(rss, nr_scan_pages); |
| 846 | return rss / nr_scan_pages; |
| 847 | } |
| 848 | |
| 849 | /* For sanitys sake, never scan more PTEs than MAX_SCAN_WINDOW MB/sec. */ |
| 850 | #define MAX_SCAN_WINDOW 2560 |
| 851 | |
| 852 | static unsigned int task_scan_min(struct task_struct *p) |
| 853 | { |
| 854 | unsigned int scan, floor; |
| 855 | unsigned int windows = 1; |
| 856 | |
| 857 | if (sysctl_numa_balancing_scan_size < MAX_SCAN_WINDOW) |
| 858 | windows = MAX_SCAN_WINDOW / sysctl_numa_balancing_scan_size; |
| 859 | floor = 1000 / windows; |
| 860 | |
| 861 | scan = sysctl_numa_balancing_scan_period_min / task_nr_scan_windows(p); |
| 862 | return max_t(unsigned int, floor, scan); |
| 863 | } |
| 864 | |
| 865 | static unsigned int task_scan_max(struct task_struct *p) |
| 866 | { |
| 867 | unsigned int smin = task_scan_min(p); |
| 868 | unsigned int smax; |
| 869 | |
| 870 | /* Watch for min being lower than max due to floor calculations */ |
| 871 | smax = sysctl_numa_balancing_scan_period_max / task_nr_scan_windows(p); |
| 872 | return max(smin, smax); |
| 873 | } |
| 874 | |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 875 | static void account_numa_enqueue(struct rq *rq, struct task_struct *p) |
| 876 | { |
| 877 | rq->nr_numa_running += (p->numa_preferred_nid != -1); |
| 878 | rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p)); |
| 879 | } |
| 880 | |
| 881 | static void account_numa_dequeue(struct rq *rq, struct task_struct *p) |
| 882 | { |
| 883 | rq->nr_numa_running -= (p->numa_preferred_nid != -1); |
| 884 | rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p)); |
| 885 | } |
| 886 | |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 887 | struct numa_group { |
| 888 | atomic_t refcount; |
| 889 | |
| 890 | spinlock_t lock; /* nr_tasks, tasks */ |
| 891 | int nr_tasks; |
Mel Gorman | e29cf08 | 2013-10-07 11:29:22 +0100 | [diff] [blame] | 892 | pid_t gid; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 893 | struct list_head task_list; |
| 894 | |
| 895 | struct rcu_head rcu; |
Mel Gorman | 989348b | 2013-10-07 11:29:40 +0100 | [diff] [blame] | 896 | unsigned long total_faults; |
| 897 | unsigned long faults[0]; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 898 | }; |
| 899 | |
Mel Gorman | e29cf08 | 2013-10-07 11:29:22 +0100 | [diff] [blame] | 900 | pid_t task_numa_group_id(struct task_struct *p) |
| 901 | { |
| 902 | return p->numa_group ? p->numa_group->gid : 0; |
| 903 | } |
| 904 | |
Mel Gorman | ac8e895 | 2013-10-07 11:29:03 +0100 | [diff] [blame] | 905 | static inline int task_faults_idx(int nid, int priv) |
| 906 | { |
| 907 | return 2 * nid + priv; |
| 908 | } |
| 909 | |
| 910 | static inline unsigned long task_faults(struct task_struct *p, int nid) |
| 911 | { |
| 912 | if (!p->numa_faults) |
| 913 | return 0; |
| 914 | |
| 915 | return p->numa_faults[task_faults_idx(nid, 0)] + |
| 916 | p->numa_faults[task_faults_idx(nid, 1)]; |
| 917 | } |
| 918 | |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 919 | static inline unsigned long group_faults(struct task_struct *p, int nid) |
| 920 | { |
| 921 | if (!p->numa_group) |
| 922 | return 0; |
| 923 | |
Wanpeng Li | 82897b4 | 2013-12-12 15:23:25 +0800 | [diff] [blame] | 924 | return p->numa_group->faults[task_faults_idx(nid, 0)] + |
| 925 | p->numa_group->faults[task_faults_idx(nid, 1)]; |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | /* |
| 929 | * These return the fraction of accesses done by a particular task, or |
| 930 | * task group, on a particular numa node. The group weight is given a |
| 931 | * larger multiplier, in order to group tasks together that are almost |
| 932 | * evenly spread out between numa nodes. |
| 933 | */ |
| 934 | static inline unsigned long task_weight(struct task_struct *p, int nid) |
| 935 | { |
| 936 | unsigned long total_faults; |
| 937 | |
| 938 | if (!p->numa_faults) |
| 939 | return 0; |
| 940 | |
| 941 | total_faults = p->total_numa_faults; |
| 942 | |
| 943 | if (!total_faults) |
| 944 | return 0; |
| 945 | |
| 946 | return 1000 * task_faults(p, nid) / total_faults; |
| 947 | } |
| 948 | |
| 949 | static inline unsigned long group_weight(struct task_struct *p, int nid) |
| 950 | { |
Mel Gorman | 989348b | 2013-10-07 11:29:40 +0100 | [diff] [blame] | 951 | if (!p->numa_group || !p->numa_group->total_faults) |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 952 | return 0; |
| 953 | |
Mel Gorman | 989348b | 2013-10-07 11:29:40 +0100 | [diff] [blame] | 954 | return 1000 * group_faults(p, nid) / p->numa_group->total_faults; |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 955 | } |
| 956 | |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 957 | static unsigned long weighted_cpuload(const int cpu); |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 958 | static unsigned long source_load(int cpu, int type); |
| 959 | static unsigned long target_load(int cpu, int type); |
| 960 | static unsigned long power_of(int cpu); |
| 961 | static long effective_load(struct task_group *tg, int cpu, long wl, long wg); |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 962 | |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 963 | /* Cached statistics for all CPUs within a node */ |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 964 | struct numa_stats { |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 965 | unsigned long nr_running; |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 966 | unsigned long load; |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 967 | |
| 968 | /* Total compute capacity of CPUs on a node */ |
| 969 | unsigned long power; |
| 970 | |
| 971 | /* Approximate capacity in terms of runnable tasks on a node */ |
| 972 | unsigned long capacity; |
| 973 | int has_capacity; |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 974 | }; |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 975 | |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 976 | /* |
| 977 | * XXX borrowed from update_sg_lb_stats |
| 978 | */ |
| 979 | static void update_numa_stats(struct numa_stats *ns, int nid) |
| 980 | { |
Peter Zijlstra | 5eca82a | 2013-11-06 18:47:57 +0100 | [diff] [blame] | 981 | int cpu, cpus = 0; |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 982 | |
| 983 | memset(ns, 0, sizeof(*ns)); |
| 984 | for_each_cpu(cpu, cpumask_of_node(nid)) { |
| 985 | struct rq *rq = cpu_rq(cpu); |
| 986 | |
| 987 | ns->nr_running += rq->nr_running; |
| 988 | ns->load += weighted_cpuload(cpu); |
| 989 | ns->power += power_of(cpu); |
Peter Zijlstra | 5eca82a | 2013-11-06 18:47:57 +0100 | [diff] [blame] | 990 | |
| 991 | cpus++; |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 992 | } |
| 993 | |
Peter Zijlstra | 5eca82a | 2013-11-06 18:47:57 +0100 | [diff] [blame] | 994 | /* |
| 995 | * If we raced with hotplug and there are no CPUs left in our mask |
| 996 | * the @ns structure is NULL'ed and task_numa_compare() will |
| 997 | * not find this node attractive. |
| 998 | * |
| 999 | * We'll either bail at !has_capacity, or we'll detect a huge imbalance |
| 1000 | * and bail there. |
| 1001 | */ |
| 1002 | if (!cpus) |
| 1003 | return; |
| 1004 | |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1005 | ns->load = (ns->load * SCHED_POWER_SCALE) / ns->power; |
| 1006 | ns->capacity = DIV_ROUND_CLOSEST(ns->power, SCHED_POWER_SCALE); |
| 1007 | ns->has_capacity = (ns->nr_running < ns->capacity); |
| 1008 | } |
| 1009 | |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1010 | struct task_numa_env { |
| 1011 | struct task_struct *p; |
| 1012 | |
| 1013 | int src_cpu, src_nid; |
| 1014 | int dst_cpu, dst_nid; |
| 1015 | |
| 1016 | struct numa_stats src_stats, dst_stats; |
| 1017 | |
Wanpeng Li | 40ea2b4 | 2013-12-05 19:10:17 +0800 | [diff] [blame] | 1018 | int imbalance_pct; |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1019 | |
| 1020 | struct task_struct *best_task; |
| 1021 | long best_imp; |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1022 | int best_cpu; |
| 1023 | }; |
| 1024 | |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1025 | static void task_numa_assign(struct task_numa_env *env, |
| 1026 | struct task_struct *p, long imp) |
| 1027 | { |
| 1028 | if (env->best_task) |
| 1029 | put_task_struct(env->best_task); |
| 1030 | if (p) |
| 1031 | get_task_struct(p); |
| 1032 | |
| 1033 | env->best_task = p; |
| 1034 | env->best_imp = imp; |
| 1035 | env->best_cpu = env->dst_cpu; |
| 1036 | } |
| 1037 | |
| 1038 | /* |
| 1039 | * This checks if the overall compute and NUMA accesses of the system would |
| 1040 | * be improved if the source tasks was migrated to the target dst_cpu taking |
| 1041 | * into account that it might be best if task running on the dst_cpu should |
| 1042 | * be exchanged with the source task |
| 1043 | */ |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1044 | static void task_numa_compare(struct task_numa_env *env, |
| 1045 | long taskimp, long groupimp) |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1046 | { |
| 1047 | struct rq *src_rq = cpu_rq(env->src_cpu); |
| 1048 | struct rq *dst_rq = cpu_rq(env->dst_cpu); |
| 1049 | struct task_struct *cur; |
| 1050 | long dst_load, src_load; |
| 1051 | long load; |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1052 | long imp = (groupimp > 0) ? groupimp : taskimp; |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1053 | |
| 1054 | rcu_read_lock(); |
| 1055 | cur = ACCESS_ONCE(dst_rq->curr); |
| 1056 | if (cur->pid == 0) /* idle */ |
| 1057 | cur = NULL; |
| 1058 | |
| 1059 | /* |
| 1060 | * "imp" is the fault differential for the source task between the |
| 1061 | * source and destination node. Calculate the total differential for |
| 1062 | * the source task and potential destination task. The more negative |
| 1063 | * the value is, the more rmeote accesses that would be expected to |
| 1064 | * be incurred if the tasks were swapped. |
| 1065 | */ |
| 1066 | if (cur) { |
| 1067 | /* Skip this swap candidate if cannot move to the source cpu */ |
| 1068 | if (!cpumask_test_cpu(env->src_cpu, tsk_cpus_allowed(cur))) |
| 1069 | goto unlock; |
| 1070 | |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1071 | /* |
| 1072 | * If dst and source tasks are in the same NUMA group, or not |
Rik van Riel | ca28aa5 | 2013-10-07 11:29:32 +0100 | [diff] [blame] | 1073 | * in any group then look only at task weights. |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1074 | */ |
Rik van Riel | ca28aa5 | 2013-10-07 11:29:32 +0100 | [diff] [blame] | 1075 | if (cur->numa_group == env->p->numa_group) { |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1076 | imp = taskimp + task_weight(cur, env->src_nid) - |
| 1077 | task_weight(cur, env->dst_nid); |
Rik van Riel | ca28aa5 | 2013-10-07 11:29:32 +0100 | [diff] [blame] | 1078 | /* |
| 1079 | * Add some hysteresis to prevent swapping the |
| 1080 | * tasks within a group over tiny differences. |
| 1081 | */ |
| 1082 | if (cur->numa_group) |
| 1083 | imp -= imp/16; |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1084 | } else { |
Rik van Riel | ca28aa5 | 2013-10-07 11:29:32 +0100 | [diff] [blame] | 1085 | /* |
| 1086 | * Compare the group weights. If a task is all by |
| 1087 | * itself (not part of a group), use the task weight |
| 1088 | * instead. |
| 1089 | */ |
| 1090 | if (env->p->numa_group) |
| 1091 | imp = groupimp; |
| 1092 | else |
| 1093 | imp = taskimp; |
| 1094 | |
| 1095 | if (cur->numa_group) |
| 1096 | imp += group_weight(cur, env->src_nid) - |
| 1097 | group_weight(cur, env->dst_nid); |
| 1098 | else |
| 1099 | imp += task_weight(cur, env->src_nid) - |
| 1100 | task_weight(cur, env->dst_nid); |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1101 | } |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | if (imp < env->best_imp) |
| 1105 | goto unlock; |
| 1106 | |
| 1107 | if (!cur) { |
| 1108 | /* Is there capacity at our destination? */ |
| 1109 | if (env->src_stats.has_capacity && |
| 1110 | !env->dst_stats.has_capacity) |
| 1111 | goto unlock; |
| 1112 | |
| 1113 | goto balance; |
| 1114 | } |
| 1115 | |
| 1116 | /* Balance doesn't matter much if we're running a task per cpu */ |
| 1117 | if (src_rq->nr_running == 1 && dst_rq->nr_running == 1) |
| 1118 | goto assign; |
| 1119 | |
| 1120 | /* |
| 1121 | * In the overloaded case, try and keep the load balanced. |
| 1122 | */ |
| 1123 | balance: |
| 1124 | dst_load = env->dst_stats.load; |
| 1125 | src_load = env->src_stats.load; |
| 1126 | |
| 1127 | /* XXX missing power terms */ |
| 1128 | load = task_h_load(env->p); |
| 1129 | dst_load += load; |
| 1130 | src_load -= load; |
| 1131 | |
| 1132 | if (cur) { |
| 1133 | load = task_h_load(cur); |
| 1134 | dst_load -= load; |
| 1135 | src_load += load; |
| 1136 | } |
| 1137 | |
| 1138 | /* make src_load the smaller */ |
| 1139 | if (dst_load < src_load) |
| 1140 | swap(dst_load, src_load); |
| 1141 | |
| 1142 | if (src_load * env->imbalance_pct < dst_load * 100) |
| 1143 | goto unlock; |
| 1144 | |
| 1145 | assign: |
| 1146 | task_numa_assign(env, cur, imp); |
| 1147 | unlock: |
| 1148 | rcu_read_unlock(); |
| 1149 | } |
| 1150 | |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1151 | static void task_numa_find_cpu(struct task_numa_env *env, |
| 1152 | long taskimp, long groupimp) |
Mel Gorman | 2c8a50a | 2013-10-07 11:29:18 +0100 | [diff] [blame] | 1153 | { |
| 1154 | int cpu; |
| 1155 | |
| 1156 | for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) { |
| 1157 | /* Skip this CPU if the source task cannot migrate */ |
| 1158 | if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(env->p))) |
| 1159 | continue; |
| 1160 | |
| 1161 | env->dst_cpu = cpu; |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1162 | task_numa_compare(env, taskimp, groupimp); |
Mel Gorman | 2c8a50a | 2013-10-07 11:29:18 +0100 | [diff] [blame] | 1163 | } |
| 1164 | } |
| 1165 | |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1166 | static int task_numa_migrate(struct task_struct *p) |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 1167 | { |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1168 | struct task_numa_env env = { |
| 1169 | .p = p, |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1170 | |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1171 | .src_cpu = task_cpu(p), |
Ingo Molnar | b32e86b | 2013-10-07 11:29:30 +0100 | [diff] [blame] | 1172 | .src_nid = task_node(p), |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1173 | |
| 1174 | .imbalance_pct = 112, |
| 1175 | |
| 1176 | .best_task = NULL, |
| 1177 | .best_imp = 0, |
| 1178 | .best_cpu = -1 |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1179 | }; |
| 1180 | struct sched_domain *sd; |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1181 | unsigned long taskweight, groupweight; |
Mel Gorman | 2c8a50a | 2013-10-07 11:29:18 +0100 | [diff] [blame] | 1182 | int nid, ret; |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1183 | long taskimp, groupimp; |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 1184 | |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1185 | /* |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1186 | * Pick the lowest SD_NUMA domain, as that would have the smallest |
| 1187 | * imbalance and would be the first to start moving tasks about. |
| 1188 | * |
| 1189 | * And we want to avoid any moving of tasks about, as that would create |
| 1190 | * random movement of tasks -- counter the numa conditions we're trying |
| 1191 | * to satisfy here. |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1192 | */ |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 1193 | rcu_read_lock(); |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1194 | sd = rcu_dereference(per_cpu(sd_numa, env.src_cpu)); |
Rik van Riel | 46a73e8 | 2013-11-11 19:29:25 -0500 | [diff] [blame] | 1195 | if (sd) |
| 1196 | env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2; |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 1197 | rcu_read_unlock(); |
| 1198 | |
Rik van Riel | 46a73e8 | 2013-11-11 19:29:25 -0500 | [diff] [blame] | 1199 | /* |
| 1200 | * Cpusets can break the scheduler domain tree into smaller |
| 1201 | * balance domains, some of which do not cross NUMA boundaries. |
| 1202 | * Tasks that are "trapped" in such domains cannot be migrated |
| 1203 | * elsewhere, so there is no point in (re)trying. |
| 1204 | */ |
| 1205 | if (unlikely(!sd)) { |
Wanpeng Li | de1b301 | 2013-12-12 15:23:24 +0800 | [diff] [blame] | 1206 | p->numa_preferred_nid = task_node(p); |
Rik van Riel | 46a73e8 | 2013-11-11 19:29:25 -0500 | [diff] [blame] | 1207 | return -EINVAL; |
| 1208 | } |
| 1209 | |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1210 | taskweight = task_weight(p, env.src_nid); |
| 1211 | groupweight = group_weight(p, env.src_nid); |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1212 | update_numa_stats(&env.src_stats, env.src_nid); |
Mel Gorman | 2c8a50a | 2013-10-07 11:29:18 +0100 | [diff] [blame] | 1213 | env.dst_nid = p->numa_preferred_nid; |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1214 | taskimp = task_weight(p, env.dst_nid) - taskweight; |
| 1215 | groupimp = group_weight(p, env.dst_nid) - groupweight; |
Mel Gorman | 2c8a50a | 2013-10-07 11:29:18 +0100 | [diff] [blame] | 1216 | update_numa_stats(&env.dst_stats, env.dst_nid); |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1217 | |
Rik van Riel | e1dda8a | 2013-10-07 11:29:19 +0100 | [diff] [blame] | 1218 | /* If the preferred nid has capacity, try to use it. */ |
| 1219 | if (env.dst_stats.has_capacity) |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1220 | task_numa_find_cpu(&env, taskimp, groupimp); |
Rik van Riel | e1dda8a | 2013-10-07 11:29:19 +0100 | [diff] [blame] | 1221 | |
| 1222 | /* No space available on the preferred nid. Look elsewhere. */ |
| 1223 | if (env.best_cpu == -1) { |
Mel Gorman | 2c8a50a | 2013-10-07 11:29:18 +0100 | [diff] [blame] | 1224 | for_each_online_node(nid) { |
| 1225 | if (nid == env.src_nid || nid == p->numa_preferred_nid) |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1226 | continue; |
| 1227 | |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 1228 | /* Only consider nodes where both task and groups benefit */ |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1229 | taskimp = task_weight(p, nid) - taskweight; |
| 1230 | groupimp = group_weight(p, nid) - groupweight; |
| 1231 | if (taskimp < 0 && groupimp < 0) |
Mel Gorman | 2c8a50a | 2013-10-07 11:29:18 +0100 | [diff] [blame] | 1232 | continue; |
| 1233 | |
| 1234 | env.dst_nid = nid; |
| 1235 | update_numa_stats(&env.dst_stats, env.dst_nid); |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1236 | task_numa_find_cpu(&env, taskimp, groupimp); |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1237 | } |
| 1238 | } |
| 1239 | |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1240 | /* No better CPU than the current one was found. */ |
| 1241 | if (env.best_cpu == -1) |
| 1242 | return -EAGAIN; |
| 1243 | |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 1244 | sched_setnuma(p, env.dst_nid); |
| 1245 | |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 1246 | /* |
| 1247 | * Reset the scan period if the task is being rescheduled on an |
| 1248 | * alternative node to recheck if the tasks is now properly placed. |
| 1249 | */ |
| 1250 | p->numa_scan_period = task_scan_min(p); |
| 1251 | |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1252 | if (env.best_task == NULL) { |
| 1253 | int ret = migrate_task_to(p, env.best_cpu); |
| 1254 | return ret; |
| 1255 | } |
| 1256 | |
| 1257 | ret = migrate_swap(p, env.best_task); |
| 1258 | put_task_struct(env.best_task); |
| 1259 | return ret; |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 1260 | } |
| 1261 | |
Mel Gorman | 6b9a746 | 2013-10-07 11:29:11 +0100 | [diff] [blame] | 1262 | /* Attempt to migrate a task to a CPU on the preferred node. */ |
| 1263 | static void numa_migrate_preferred(struct task_struct *p) |
| 1264 | { |
Rik van Riel | 2739d3e | 2013-10-07 11:29:41 +0100 | [diff] [blame] | 1265 | /* This task has no NUMA fault statistics yet */ |
| 1266 | if (unlikely(p->numa_preferred_nid == -1 || !p->numa_faults)) |
| 1267 | return; |
| 1268 | |
| 1269 | /* Periodically retry migrating the task to the preferred node */ |
| 1270 | p->numa_migrate_retry = jiffies + HZ; |
| 1271 | |
Mel Gorman | 6b9a746 | 2013-10-07 11:29:11 +0100 | [diff] [blame] | 1272 | /* Success if task is already running on preferred CPU */ |
Wanpeng Li | de1b301 | 2013-12-12 15:23:24 +0800 | [diff] [blame] | 1273 | if (task_node(p) == p->numa_preferred_nid) |
Mel Gorman | 6b9a746 | 2013-10-07 11:29:11 +0100 | [diff] [blame] | 1274 | return; |
| 1275 | |
Mel Gorman | 6b9a746 | 2013-10-07 11:29:11 +0100 | [diff] [blame] | 1276 | /* Otherwise, try migrate to a CPU on the preferred node */ |
Rik van Riel | 2739d3e | 2013-10-07 11:29:41 +0100 | [diff] [blame] | 1277 | task_numa_migrate(p); |
Mel Gorman | 6b9a746 | 2013-10-07 11:29:11 +0100 | [diff] [blame] | 1278 | } |
| 1279 | |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 1280 | /* |
| 1281 | * When adapting the scan rate, the period is divided into NUMA_PERIOD_SLOTS |
| 1282 | * increments. The more local the fault statistics are, the higher the scan |
| 1283 | * period will be for the next scan window. If local/remote ratio is below |
| 1284 | * NUMA_PERIOD_THRESHOLD (where range of ratio is 1..NUMA_PERIOD_SLOTS) the |
| 1285 | * scan period will decrease |
| 1286 | */ |
| 1287 | #define NUMA_PERIOD_SLOTS 10 |
| 1288 | #define NUMA_PERIOD_THRESHOLD 3 |
| 1289 | |
| 1290 | /* |
| 1291 | * Increase the scan period (slow down scanning) if the majority of |
| 1292 | * our memory is already on our local node, or if the majority of |
| 1293 | * the page accesses are shared with other processes. |
| 1294 | * Otherwise, decrease the scan period. |
| 1295 | */ |
| 1296 | static void update_task_scan_period(struct task_struct *p, |
| 1297 | unsigned long shared, unsigned long private) |
| 1298 | { |
| 1299 | unsigned int period_slot; |
| 1300 | int ratio; |
| 1301 | int diff; |
| 1302 | |
| 1303 | unsigned long remote = p->numa_faults_locality[0]; |
| 1304 | unsigned long local = p->numa_faults_locality[1]; |
| 1305 | |
| 1306 | /* |
| 1307 | * If there were no record hinting faults then either the task is |
| 1308 | * completely idle or all activity is areas that are not of interest |
| 1309 | * to automatic numa balancing. Scan slower |
| 1310 | */ |
| 1311 | if (local + shared == 0) { |
| 1312 | p->numa_scan_period = min(p->numa_scan_period_max, |
| 1313 | p->numa_scan_period << 1); |
| 1314 | |
| 1315 | p->mm->numa_next_scan = jiffies + |
| 1316 | msecs_to_jiffies(p->numa_scan_period); |
| 1317 | |
| 1318 | return; |
| 1319 | } |
| 1320 | |
| 1321 | /* |
| 1322 | * Prepare to scale scan period relative to the current period. |
| 1323 | * == NUMA_PERIOD_THRESHOLD scan period stays the same |
| 1324 | * < NUMA_PERIOD_THRESHOLD scan period decreases (scan faster) |
| 1325 | * >= NUMA_PERIOD_THRESHOLD scan period increases (scan slower) |
| 1326 | */ |
| 1327 | period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS); |
| 1328 | ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote); |
| 1329 | if (ratio >= NUMA_PERIOD_THRESHOLD) { |
| 1330 | int slot = ratio - NUMA_PERIOD_THRESHOLD; |
| 1331 | if (!slot) |
| 1332 | slot = 1; |
| 1333 | diff = slot * period_slot; |
| 1334 | } else { |
| 1335 | diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot; |
| 1336 | |
| 1337 | /* |
| 1338 | * Scale scan rate increases based on sharing. There is an |
| 1339 | * inverse relationship between the degree of sharing and |
| 1340 | * the adjustment made to the scanning period. Broadly |
| 1341 | * speaking the intent is that there is little point |
| 1342 | * scanning faster if shared accesses dominate as it may |
| 1343 | * simply bounce migrations uselessly |
| 1344 | */ |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 1345 | ratio = DIV_ROUND_UP(private * NUMA_PERIOD_SLOTS, (private + shared)); |
| 1346 | diff = (diff * ratio) / NUMA_PERIOD_SLOTS; |
| 1347 | } |
| 1348 | |
| 1349 | p->numa_scan_period = clamp(p->numa_scan_period + diff, |
| 1350 | task_scan_min(p), task_scan_max(p)); |
| 1351 | memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); |
| 1352 | } |
| 1353 | |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1354 | static void task_numa_placement(struct task_struct *p) |
| 1355 | { |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 1356 | int seq, nid, max_nid = -1, max_group_nid = -1; |
| 1357 | unsigned long max_faults = 0, max_group_faults = 0; |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 1358 | unsigned long fault_types[2] = { 0, 0 }; |
Mel Gorman | 7dbd13e | 2013-10-07 11:29:29 +0100 | [diff] [blame] | 1359 | spinlock_t *group_lock = NULL; |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1360 | |
Hugh Dickins | 2832bc1 | 2012-12-19 17:42:16 -0800 | [diff] [blame] | 1361 | seq = ACCESS_ONCE(p->mm->numa_scan_seq); |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1362 | if (p->numa_scan_seq == seq) |
| 1363 | return; |
| 1364 | p->numa_scan_seq = seq; |
Mel Gorman | 598f0ec | 2013-10-07 11:28:55 +0100 | [diff] [blame] | 1365 | p->numa_scan_period_max = task_scan_max(p); |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1366 | |
Mel Gorman | 7dbd13e | 2013-10-07 11:29:29 +0100 | [diff] [blame] | 1367 | /* If the task is part of a group prevent parallel updates to group stats */ |
| 1368 | if (p->numa_group) { |
| 1369 | group_lock = &p->numa_group->lock; |
| 1370 | spin_lock(group_lock); |
| 1371 | } |
| 1372 | |
Mel Gorman | 688b758 | 2013-10-07 11:28:58 +0100 | [diff] [blame] | 1373 | /* Find the node with the highest number of faults */ |
| 1374 | for_each_online_node(nid) { |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 1375 | unsigned long faults = 0, group_faults = 0; |
Mel Gorman | ac8e895 | 2013-10-07 11:29:03 +0100 | [diff] [blame] | 1376 | int priv, i; |
Mel Gorman | 745d614 | 2013-10-07 11:28:59 +0100 | [diff] [blame] | 1377 | |
Mel Gorman | ac8e895 | 2013-10-07 11:29:03 +0100 | [diff] [blame] | 1378 | for (priv = 0; priv < 2; priv++) { |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1379 | long diff; |
| 1380 | |
Mel Gorman | ac8e895 | 2013-10-07 11:29:03 +0100 | [diff] [blame] | 1381 | i = task_faults_idx(nid, priv); |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1382 | diff = -p->numa_faults[i]; |
Mel Gorman | 745d614 | 2013-10-07 11:28:59 +0100 | [diff] [blame] | 1383 | |
Mel Gorman | ac8e895 | 2013-10-07 11:29:03 +0100 | [diff] [blame] | 1384 | /* Decay existing window, copy faults since last scan */ |
| 1385 | p->numa_faults[i] >>= 1; |
| 1386 | p->numa_faults[i] += p->numa_faults_buffer[i]; |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 1387 | fault_types[priv] += p->numa_faults_buffer[i]; |
Mel Gorman | ac8e895 | 2013-10-07 11:29:03 +0100 | [diff] [blame] | 1388 | p->numa_faults_buffer[i] = 0; |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1389 | |
| 1390 | faults += p->numa_faults[i]; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1391 | diff += p->numa_faults[i]; |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 1392 | p->total_numa_faults += diff; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1393 | if (p->numa_group) { |
| 1394 | /* safe because we can only change our own group */ |
Mel Gorman | 989348b | 2013-10-07 11:29:40 +0100 | [diff] [blame] | 1395 | p->numa_group->faults[i] += diff; |
| 1396 | p->numa_group->total_faults += diff; |
| 1397 | group_faults += p->numa_group->faults[i]; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1398 | } |
Mel Gorman | ac8e895 | 2013-10-07 11:29:03 +0100 | [diff] [blame] | 1399 | } |
| 1400 | |
Mel Gorman | 688b758 | 2013-10-07 11:28:58 +0100 | [diff] [blame] | 1401 | if (faults > max_faults) { |
| 1402 | max_faults = faults; |
| 1403 | max_nid = nid; |
| 1404 | } |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 1405 | |
| 1406 | if (group_faults > max_group_faults) { |
| 1407 | max_group_faults = group_faults; |
| 1408 | max_group_nid = nid; |
| 1409 | } |
| 1410 | } |
| 1411 | |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 1412 | update_task_scan_period(p, fault_types[0], fault_types[1]); |
| 1413 | |
Mel Gorman | 7dbd13e | 2013-10-07 11:29:29 +0100 | [diff] [blame] | 1414 | if (p->numa_group) { |
| 1415 | /* |
| 1416 | * If the preferred task and group nids are different, |
| 1417 | * iterate over the nodes again to find the best place. |
| 1418 | */ |
| 1419 | if (max_nid != max_group_nid) { |
| 1420 | unsigned long weight, max_weight = 0; |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 1421 | |
Mel Gorman | 7dbd13e | 2013-10-07 11:29:29 +0100 | [diff] [blame] | 1422 | for_each_online_node(nid) { |
| 1423 | weight = task_weight(p, nid) + group_weight(p, nid); |
| 1424 | if (weight > max_weight) { |
| 1425 | max_weight = weight; |
| 1426 | max_nid = nid; |
| 1427 | } |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 1428 | } |
| 1429 | } |
Mel Gorman | 7dbd13e | 2013-10-07 11:29:29 +0100 | [diff] [blame] | 1430 | |
| 1431 | spin_unlock(group_lock); |
Mel Gorman | 688b758 | 2013-10-07 11:28:58 +0100 | [diff] [blame] | 1432 | } |
| 1433 | |
Mel Gorman | 6b9a746 | 2013-10-07 11:29:11 +0100 | [diff] [blame] | 1434 | /* Preferred node as the node with the most faults */ |
Mel Gorman | 3a7053b | 2013-10-07 11:29:00 +0100 | [diff] [blame] | 1435 | if (max_faults && max_nid != p->numa_preferred_nid) { |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 1436 | /* Update the preferred nid and migrate task if possible */ |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 1437 | sched_setnuma(p, max_nid); |
Mel Gorman | 6b9a746 | 2013-10-07 11:29:11 +0100 | [diff] [blame] | 1438 | numa_migrate_preferred(p); |
Mel Gorman | 3a7053b | 2013-10-07 11:29:00 +0100 | [diff] [blame] | 1439 | } |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1440 | } |
| 1441 | |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1442 | static inline int get_numa_group(struct numa_group *grp) |
| 1443 | { |
| 1444 | return atomic_inc_not_zero(&grp->refcount); |
| 1445 | } |
| 1446 | |
| 1447 | static inline void put_numa_group(struct numa_group *grp) |
| 1448 | { |
| 1449 | if (atomic_dec_and_test(&grp->refcount)) |
| 1450 | kfree_rcu(grp, rcu); |
| 1451 | } |
| 1452 | |
Mel Gorman | 3e6a941 | 2013-10-07 11:29:35 +0100 | [diff] [blame] | 1453 | static void task_numa_group(struct task_struct *p, int cpupid, int flags, |
| 1454 | int *priv) |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1455 | { |
| 1456 | struct numa_group *grp, *my_grp; |
| 1457 | struct task_struct *tsk; |
| 1458 | bool join = false; |
| 1459 | int cpu = cpupid_to_cpu(cpupid); |
| 1460 | int i; |
| 1461 | |
| 1462 | if (unlikely(!p->numa_group)) { |
| 1463 | unsigned int size = sizeof(struct numa_group) + |
Mel Gorman | 989348b | 2013-10-07 11:29:40 +0100 | [diff] [blame] | 1464 | 2*nr_node_ids*sizeof(unsigned long); |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1465 | |
| 1466 | grp = kzalloc(size, GFP_KERNEL | __GFP_NOWARN); |
| 1467 | if (!grp) |
| 1468 | return; |
| 1469 | |
| 1470 | atomic_set(&grp->refcount, 1); |
| 1471 | spin_lock_init(&grp->lock); |
| 1472 | INIT_LIST_HEAD(&grp->task_list); |
Mel Gorman | e29cf08 | 2013-10-07 11:29:22 +0100 | [diff] [blame] | 1473 | grp->gid = p->pid; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1474 | |
| 1475 | for (i = 0; i < 2*nr_node_ids; i++) |
Mel Gorman | 989348b | 2013-10-07 11:29:40 +0100 | [diff] [blame] | 1476 | grp->faults[i] = p->numa_faults[i]; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1477 | |
Mel Gorman | 989348b | 2013-10-07 11:29:40 +0100 | [diff] [blame] | 1478 | grp->total_faults = p->total_numa_faults; |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 1479 | |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1480 | list_add(&p->numa_entry, &grp->task_list); |
| 1481 | grp->nr_tasks++; |
| 1482 | rcu_assign_pointer(p->numa_group, grp); |
| 1483 | } |
| 1484 | |
| 1485 | rcu_read_lock(); |
| 1486 | tsk = ACCESS_ONCE(cpu_rq(cpu)->curr); |
| 1487 | |
| 1488 | if (!cpupid_match_pid(tsk, cpupid)) |
Peter Zijlstra | 3354781 | 2013-10-09 10:24:48 +0200 | [diff] [blame] | 1489 | goto no_join; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1490 | |
| 1491 | grp = rcu_dereference(tsk->numa_group); |
| 1492 | if (!grp) |
Peter Zijlstra | 3354781 | 2013-10-09 10:24:48 +0200 | [diff] [blame] | 1493 | goto no_join; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1494 | |
| 1495 | my_grp = p->numa_group; |
| 1496 | if (grp == my_grp) |
Peter Zijlstra | 3354781 | 2013-10-09 10:24:48 +0200 | [diff] [blame] | 1497 | goto no_join; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1498 | |
| 1499 | /* |
| 1500 | * Only join the other group if its bigger; if we're the bigger group, |
| 1501 | * the other task will join us. |
| 1502 | */ |
| 1503 | if (my_grp->nr_tasks > grp->nr_tasks) |
Peter Zijlstra | 3354781 | 2013-10-09 10:24:48 +0200 | [diff] [blame] | 1504 | goto no_join; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1505 | |
| 1506 | /* |
| 1507 | * Tie-break on the grp address. |
| 1508 | */ |
| 1509 | if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp) |
Peter Zijlstra | 3354781 | 2013-10-09 10:24:48 +0200 | [diff] [blame] | 1510 | goto no_join; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1511 | |
Rik van Riel | dabe1d9 | 2013-10-07 11:29:34 +0100 | [diff] [blame] | 1512 | /* Always join threads in the same process. */ |
| 1513 | if (tsk->mm == current->mm) |
| 1514 | join = true; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1515 | |
Rik van Riel | dabe1d9 | 2013-10-07 11:29:34 +0100 | [diff] [blame] | 1516 | /* Simple filter to avoid false positives due to PID collisions */ |
| 1517 | if (flags & TNF_SHARED) |
| 1518 | join = true; |
| 1519 | |
Mel Gorman | 3e6a941 | 2013-10-07 11:29:35 +0100 | [diff] [blame] | 1520 | /* Update priv based on whether false sharing was detected */ |
| 1521 | *priv = !join; |
| 1522 | |
Rik van Riel | dabe1d9 | 2013-10-07 11:29:34 +0100 | [diff] [blame] | 1523 | if (join && !get_numa_group(grp)) |
Peter Zijlstra | 3354781 | 2013-10-09 10:24:48 +0200 | [diff] [blame] | 1524 | goto no_join; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1525 | |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1526 | rcu_read_unlock(); |
| 1527 | |
| 1528 | if (!join) |
| 1529 | return; |
| 1530 | |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1531 | double_lock(&my_grp->lock, &grp->lock); |
| 1532 | |
Mel Gorman | 989348b | 2013-10-07 11:29:40 +0100 | [diff] [blame] | 1533 | for (i = 0; i < 2*nr_node_ids; i++) { |
| 1534 | my_grp->faults[i] -= p->numa_faults[i]; |
| 1535 | grp->faults[i] += p->numa_faults[i]; |
| 1536 | } |
| 1537 | my_grp->total_faults -= p->total_numa_faults; |
| 1538 | grp->total_faults += p->total_numa_faults; |
| 1539 | |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1540 | list_move(&p->numa_entry, &grp->task_list); |
| 1541 | my_grp->nr_tasks--; |
| 1542 | grp->nr_tasks++; |
| 1543 | |
| 1544 | spin_unlock(&my_grp->lock); |
| 1545 | spin_unlock(&grp->lock); |
| 1546 | |
| 1547 | rcu_assign_pointer(p->numa_group, grp); |
| 1548 | |
| 1549 | put_numa_group(my_grp); |
Peter Zijlstra | 3354781 | 2013-10-09 10:24:48 +0200 | [diff] [blame] | 1550 | return; |
| 1551 | |
| 1552 | no_join: |
| 1553 | rcu_read_unlock(); |
| 1554 | return; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | void task_numa_free(struct task_struct *p) |
| 1558 | { |
| 1559 | struct numa_group *grp = p->numa_group; |
| 1560 | int i; |
Rik van Riel | 8272701 | 2013-10-07 11:29:28 +0100 | [diff] [blame] | 1561 | void *numa_faults = p->numa_faults; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1562 | |
| 1563 | if (grp) { |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1564 | spin_lock(&grp->lock); |
Mel Gorman | 989348b | 2013-10-07 11:29:40 +0100 | [diff] [blame] | 1565 | for (i = 0; i < 2*nr_node_ids; i++) |
| 1566 | grp->faults[i] -= p->numa_faults[i]; |
| 1567 | grp->total_faults -= p->total_numa_faults; |
| 1568 | |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1569 | list_del(&p->numa_entry); |
| 1570 | grp->nr_tasks--; |
| 1571 | spin_unlock(&grp->lock); |
| 1572 | rcu_assign_pointer(p->numa_group, NULL); |
| 1573 | put_numa_group(grp); |
| 1574 | } |
| 1575 | |
Rik van Riel | 8272701 | 2013-10-07 11:29:28 +0100 | [diff] [blame] | 1576 | p->numa_faults = NULL; |
| 1577 | p->numa_faults_buffer = NULL; |
| 1578 | kfree(numa_faults); |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1579 | } |
| 1580 | |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1581 | /* |
| 1582 | * Got a PROT_NONE fault for a page on @node. |
| 1583 | */ |
Peter Zijlstra | 6688cc0 | 2013-10-07 11:29:24 +0100 | [diff] [blame] | 1584 | void task_numa_fault(int last_cpupid, int node, int pages, int flags) |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1585 | { |
| 1586 | struct task_struct *p = current; |
Peter Zijlstra | 6688cc0 | 2013-10-07 11:29:24 +0100 | [diff] [blame] | 1587 | bool migrated = flags & TNF_MIGRATED; |
Mel Gorman | ac8e895 | 2013-10-07 11:29:03 +0100 | [diff] [blame] | 1588 | int priv; |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1589 | |
Dave Kleikamp | 10e84b9 | 2013-07-31 13:53:35 -0700 | [diff] [blame] | 1590 | if (!numabalancing_enabled) |
Mel Gorman | 1a687c2 | 2012-11-22 11:16:36 +0000 | [diff] [blame] | 1591 | return; |
| 1592 | |
Mel Gorman | 9ff1d9f | 2013-10-07 11:29:04 +0100 | [diff] [blame] | 1593 | /* for example, ksmd faulting in a user's mm */ |
| 1594 | if (!p->mm) |
| 1595 | return; |
| 1596 | |
Rik van Riel | 8272701 | 2013-10-07 11:29:28 +0100 | [diff] [blame] | 1597 | /* Do not worry about placement if exiting */ |
| 1598 | if (p->state == TASK_DEAD) |
| 1599 | return; |
| 1600 | |
Mel Gorman | f809ca9 | 2013-10-07 11:28:57 +0100 | [diff] [blame] | 1601 | /* Allocate buffer to track faults on a per-node basis */ |
| 1602 | if (unlikely(!p->numa_faults)) { |
Mel Gorman | ac8e895 | 2013-10-07 11:29:03 +0100 | [diff] [blame] | 1603 | int size = sizeof(*p->numa_faults) * 2 * nr_node_ids; |
Mel Gorman | f809ca9 | 2013-10-07 11:28:57 +0100 | [diff] [blame] | 1604 | |
Mel Gorman | 745d614 | 2013-10-07 11:28:59 +0100 | [diff] [blame] | 1605 | /* numa_faults and numa_faults_buffer share the allocation */ |
| 1606 | p->numa_faults = kzalloc(size * 2, GFP_KERNEL|__GFP_NOWARN); |
Mel Gorman | f809ca9 | 2013-10-07 11:28:57 +0100 | [diff] [blame] | 1607 | if (!p->numa_faults) |
| 1608 | return; |
Mel Gorman | 745d614 | 2013-10-07 11:28:59 +0100 | [diff] [blame] | 1609 | |
| 1610 | BUG_ON(p->numa_faults_buffer); |
Mel Gorman | ac8e895 | 2013-10-07 11:29:03 +0100 | [diff] [blame] | 1611 | p->numa_faults_buffer = p->numa_faults + (2 * nr_node_ids); |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 1612 | p->total_numa_faults = 0; |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 1613 | memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); |
Mel Gorman | f809ca9 | 2013-10-07 11:28:57 +0100 | [diff] [blame] | 1614 | } |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1615 | |
Mel Gorman | fb003b8 | 2012-11-15 09:01:14 +0000 | [diff] [blame] | 1616 | /* |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1617 | * First accesses are treated as private, otherwise consider accesses |
| 1618 | * to be private if the accessing pid has not changed |
| 1619 | */ |
| 1620 | if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) { |
| 1621 | priv = 1; |
| 1622 | } else { |
| 1623 | priv = cpupid_match_pid(p, last_cpupid); |
Peter Zijlstra | 6688cc0 | 2013-10-07 11:29:24 +0100 | [diff] [blame] | 1624 | if (!priv && !(flags & TNF_NO_GROUP)) |
Mel Gorman | 3e6a941 | 2013-10-07 11:29:35 +0100 | [diff] [blame] | 1625 | task_numa_group(p, last_cpupid, flags, &priv); |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1626 | } |
| 1627 | |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1628 | task_numa_placement(p); |
Mel Gorman | f809ca9 | 2013-10-07 11:28:57 +0100 | [diff] [blame] | 1629 | |
Rik van Riel | 2739d3e | 2013-10-07 11:29:41 +0100 | [diff] [blame] | 1630 | /* |
| 1631 | * Retry task to preferred node migration periodically, in case it |
| 1632 | * case it previously failed, or the scheduler moved us. |
| 1633 | */ |
| 1634 | if (time_after(jiffies, p->numa_migrate_retry)) |
Mel Gorman | 6b9a746 | 2013-10-07 11:29:11 +0100 | [diff] [blame] | 1635 | numa_migrate_preferred(p); |
| 1636 | |
Ingo Molnar | b32e86b | 2013-10-07 11:29:30 +0100 | [diff] [blame] | 1637 | if (migrated) |
| 1638 | p->numa_pages_migrated += pages; |
| 1639 | |
Mel Gorman | ac8e895 | 2013-10-07 11:29:03 +0100 | [diff] [blame] | 1640 | p->numa_faults_buffer[task_faults_idx(node, priv)] += pages; |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 1641 | p->numa_faults_locality[!!(flags & TNF_FAULT_LOCAL)] += pages; |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1642 | } |
| 1643 | |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 1644 | static void reset_ptenuma_scan(struct task_struct *p) |
| 1645 | { |
| 1646 | ACCESS_ONCE(p->mm->numa_scan_seq)++; |
| 1647 | p->mm->numa_scan_offset = 0; |
| 1648 | } |
| 1649 | |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1650 | /* |
| 1651 | * The expensive part of numa migration is done from task_work context. |
| 1652 | * Triggered from task_tick_numa(). |
| 1653 | */ |
| 1654 | void task_numa_work(struct callback_head *work) |
| 1655 | { |
| 1656 | unsigned long migrate, next_scan, now = jiffies; |
| 1657 | struct task_struct *p = current; |
| 1658 | struct mm_struct *mm = p->mm; |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 1659 | struct vm_area_struct *vma; |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 1660 | unsigned long start, end; |
Mel Gorman | 598f0ec | 2013-10-07 11:28:55 +0100 | [diff] [blame] | 1661 | unsigned long nr_pte_updates = 0; |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 1662 | long pages; |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1663 | |
| 1664 | WARN_ON_ONCE(p != container_of(work, struct task_struct, numa_work)); |
| 1665 | |
| 1666 | work->next = work; /* protect against double add */ |
| 1667 | /* |
| 1668 | * Who cares about NUMA placement when they're dying. |
| 1669 | * |
| 1670 | * NOTE: make sure not to dereference p->mm before this check, |
| 1671 | * exit_task_work() happens _after_ exit_mm() so we could be called |
| 1672 | * without p->mm even though we still had it when we enqueued this |
| 1673 | * work. |
| 1674 | */ |
| 1675 | if (p->flags & PF_EXITING) |
| 1676 | return; |
| 1677 | |
Mel Gorman | 930aa17 | 2013-10-07 11:29:37 +0100 | [diff] [blame] | 1678 | if (!mm->numa_next_scan) { |
Mel Gorman | 7e8d16b | 2013-10-07 11:28:54 +0100 | [diff] [blame] | 1679 | mm->numa_next_scan = now + |
| 1680 | msecs_to_jiffies(sysctl_numa_balancing_scan_delay); |
Mel Gorman | b8593bf | 2012-11-21 01:18:23 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | /* |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1684 | * Enforce maximal scan/migration frequency.. |
| 1685 | */ |
| 1686 | migrate = mm->numa_next_scan; |
| 1687 | if (time_before(now, migrate)) |
| 1688 | return; |
| 1689 | |
Mel Gorman | 598f0ec | 2013-10-07 11:28:55 +0100 | [diff] [blame] | 1690 | if (p->numa_scan_period == 0) { |
| 1691 | p->numa_scan_period_max = task_scan_max(p); |
| 1692 | p->numa_scan_period = task_scan_min(p); |
| 1693 | } |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1694 | |
Mel Gorman | fb003b8 | 2012-11-15 09:01:14 +0000 | [diff] [blame] | 1695 | next_scan = now + msecs_to_jiffies(p->numa_scan_period); |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1696 | if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate) |
| 1697 | return; |
| 1698 | |
Mel Gorman | e14808b | 2012-11-19 10:59:15 +0000 | [diff] [blame] | 1699 | /* |
Peter Zijlstra | 19a78d1 | 2013-10-07 11:28:51 +0100 | [diff] [blame] | 1700 | * Delay this task enough that another task of this mm will likely win |
| 1701 | * the next time around. |
| 1702 | */ |
| 1703 | p->node_stamp += 2 * TICK_NSEC; |
| 1704 | |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 1705 | start = mm->numa_scan_offset; |
| 1706 | pages = sysctl_numa_balancing_scan_size; |
| 1707 | pages <<= 20 - PAGE_SHIFT; /* MB in pages */ |
| 1708 | if (!pages) |
| 1709 | return; |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1710 | |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 1711 | down_read(&mm->mmap_sem); |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 1712 | vma = find_vma(mm, start); |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 1713 | if (!vma) { |
| 1714 | reset_ptenuma_scan(p); |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 1715 | start = 0; |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 1716 | vma = mm->mmap; |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1717 | } |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 1718 | for (; vma; vma = vma->vm_next) { |
Mel Gorman | fc314724 | 2013-10-07 11:29:09 +0100 | [diff] [blame] | 1719 | if (!vma_migratable(vma) || !vma_policy_mof(p, vma)) |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 1720 | continue; |
| 1721 | |
Mel Gorman | 4591ce4f | 2013-10-07 11:29:13 +0100 | [diff] [blame] | 1722 | /* |
| 1723 | * Shared library pages mapped by multiple processes are not |
| 1724 | * migrated as it is expected they are cache replicated. Avoid |
| 1725 | * hinting faults in read-only file-backed mappings or the vdso |
| 1726 | * as migrating the pages will be of marginal benefit. |
| 1727 | */ |
| 1728 | if (!vma->vm_mm || |
| 1729 | (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ))) |
| 1730 | continue; |
| 1731 | |
Mel Gorman | 3c67f47 | 2013-12-18 17:08:40 -0800 | [diff] [blame] | 1732 | /* |
| 1733 | * Skip inaccessible VMAs to avoid any confusion between |
| 1734 | * PROT_NONE and NUMA hinting ptes |
| 1735 | */ |
| 1736 | if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))) |
| 1737 | continue; |
| 1738 | |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 1739 | do { |
| 1740 | start = max(start, vma->vm_start); |
| 1741 | end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE); |
| 1742 | end = min(end, vma->vm_end); |
Mel Gorman | 598f0ec | 2013-10-07 11:28:55 +0100 | [diff] [blame] | 1743 | nr_pte_updates += change_prot_numa(vma, start, end); |
| 1744 | |
| 1745 | /* |
| 1746 | * Scan sysctl_numa_balancing_scan_size but ensure that |
| 1747 | * at least one PTE is updated so that unused virtual |
| 1748 | * address space is quickly skipped. |
| 1749 | */ |
| 1750 | if (nr_pte_updates) |
| 1751 | pages -= (end - start) >> PAGE_SHIFT; |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 1752 | |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 1753 | start = end; |
| 1754 | if (pages <= 0) |
| 1755 | goto out; |
| 1756 | } while (end != vma->vm_end); |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 1757 | } |
| 1758 | |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 1759 | out: |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 1760 | /* |
Peter Zijlstra | c69307d | 2013-10-07 11:28:41 +0100 | [diff] [blame] | 1761 | * It is possible to reach the end of the VMA list but the last few |
| 1762 | * VMAs are not guaranteed to the vma_migratable. If they are not, we |
| 1763 | * would find the !migratable VMA on the next scan but not reset the |
| 1764 | * scanner to the start so check it now. |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 1765 | */ |
| 1766 | if (vma) |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 1767 | mm->numa_scan_offset = start; |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 1768 | else |
| 1769 | reset_ptenuma_scan(p); |
| 1770 | up_read(&mm->mmap_sem); |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | /* |
| 1774 | * Drive the periodic memory faults.. |
| 1775 | */ |
| 1776 | void task_tick_numa(struct rq *rq, struct task_struct *curr) |
| 1777 | { |
| 1778 | struct callback_head *work = &curr->numa_work; |
| 1779 | u64 period, now; |
| 1780 | |
| 1781 | /* |
| 1782 | * We don't care about NUMA placement if we don't have memory. |
| 1783 | */ |
| 1784 | if (!curr->mm || (curr->flags & PF_EXITING) || work->next != work) |
| 1785 | return; |
| 1786 | |
| 1787 | /* |
| 1788 | * Using runtime rather than walltime has the dual advantage that |
| 1789 | * we (mostly) drive the selection from busy threads and that the |
| 1790 | * task needs to have done some actual work before we bother with |
| 1791 | * NUMA placement. |
| 1792 | */ |
| 1793 | now = curr->se.sum_exec_runtime; |
| 1794 | period = (u64)curr->numa_scan_period * NSEC_PER_MSEC; |
| 1795 | |
| 1796 | if (now - curr->node_stamp > period) { |
Peter Zijlstra | 4b96a29 | 2012-10-25 14:16:47 +0200 | [diff] [blame] | 1797 | if (!curr->node_stamp) |
Mel Gorman | 598f0ec | 2013-10-07 11:28:55 +0100 | [diff] [blame] | 1798 | curr->numa_scan_period = task_scan_min(curr); |
Peter Zijlstra | 19a78d1 | 2013-10-07 11:28:51 +0100 | [diff] [blame] | 1799 | curr->node_stamp += period; |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1800 | |
| 1801 | if (!time_before(jiffies, curr->mm->numa_next_scan)) { |
| 1802 | init_task_work(work, task_numa_work); /* TODO: move this into sched_fork() */ |
| 1803 | task_work_add(curr, work, true); |
| 1804 | } |
| 1805 | } |
| 1806 | } |
| 1807 | #else |
| 1808 | static void task_tick_numa(struct rq *rq, struct task_struct *curr) |
| 1809 | { |
| 1810 | } |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 1811 | |
| 1812 | static inline void account_numa_enqueue(struct rq *rq, struct task_struct *p) |
| 1813 | { |
| 1814 | } |
| 1815 | |
| 1816 | static inline void account_numa_dequeue(struct rq *rq, struct task_struct *p) |
| 1817 | { |
| 1818 | } |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1819 | #endif /* CONFIG_NUMA_BALANCING */ |
| 1820 | |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1821 | static void |
| 1822 | account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 1823 | { |
| 1824 | update_load_add(&cfs_rq->load, se->load.weight); |
Peter Zijlstra | c09595f | 2008-06-27 13:41:14 +0200 | [diff] [blame] | 1825 | if (!parent_entity(se)) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1826 | update_load_add(&rq_of(cfs_rq)->load, se->load.weight); |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 1827 | #ifdef CONFIG_SMP |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 1828 | if (entity_is_task(se)) { |
| 1829 | struct rq *rq = rq_of(cfs_rq); |
| 1830 | |
| 1831 | account_numa_enqueue(rq, task_of(se)); |
| 1832 | list_add(&se->group_node, &rq->cfs_tasks); |
| 1833 | } |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 1834 | #endif |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1835 | cfs_rq->nr_running++; |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | static void |
| 1839 | account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 1840 | { |
| 1841 | update_load_sub(&cfs_rq->load, se->load.weight); |
Peter Zijlstra | c09595f | 2008-06-27 13:41:14 +0200 | [diff] [blame] | 1842 | if (!parent_entity(se)) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1843 | update_load_sub(&rq_of(cfs_rq)->load, se->load.weight); |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 1844 | if (entity_is_task(se)) { |
| 1845 | account_numa_dequeue(rq_of(cfs_rq), task_of(se)); |
Bharata B Rao | b87f172 | 2008-09-25 09:53:54 +0530 | [diff] [blame] | 1846 | list_del_init(&se->group_node); |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 1847 | } |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1848 | cfs_rq->nr_running--; |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1849 | } |
| 1850 | |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 1851 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 1852 | # ifdef CONFIG_SMP |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 1853 | static inline long calc_tg_weight(struct task_group *tg, struct cfs_rq *cfs_rq) |
| 1854 | { |
| 1855 | long tg_weight; |
| 1856 | |
| 1857 | /* |
| 1858 | * Use this CPU's actual weight instead of the last load_contribution |
| 1859 | * to gain a more accurate current total weight. See |
| 1860 | * update_cfs_rq_load_contribution(). |
| 1861 | */ |
Alex Shi | bf5b986 | 2013-06-20 10:18:54 +0800 | [diff] [blame] | 1862 | tg_weight = atomic_long_read(&tg->load_avg); |
Paul Turner | 8295836 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1863 | tg_weight -= cfs_rq->tg_load_contrib; |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 1864 | tg_weight += cfs_rq->load.weight; |
| 1865 | |
| 1866 | return tg_weight; |
| 1867 | } |
| 1868 | |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 1869 | 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] | 1870 | { |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 1871 | long tg_weight, load, shares; |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 1872 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 1873 | tg_weight = calc_tg_weight(tg, cfs_rq); |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 1874 | load = cfs_rq->load.weight; |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 1875 | |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 1876 | shares = (tg->shares * load); |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 1877 | if (tg_weight) |
| 1878 | shares /= tg_weight; |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 1879 | |
| 1880 | if (shares < MIN_SHARES) |
| 1881 | shares = MIN_SHARES; |
| 1882 | if (shares > tg->shares) |
| 1883 | shares = tg->shares; |
| 1884 | |
| 1885 | return shares; |
| 1886 | } |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 1887 | # else /* CONFIG_SMP */ |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 1888 | 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] | 1889 | { |
| 1890 | return tg->shares; |
| 1891 | } |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 1892 | # endif /* CONFIG_SMP */ |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 1893 | static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, |
| 1894 | unsigned long weight) |
| 1895 | { |
Paul Turner | 19e5eeb | 2010-12-15 19:10:18 -0800 | [diff] [blame] | 1896 | if (se->on_rq) { |
| 1897 | /* commit outstanding execution time */ |
| 1898 | if (cfs_rq->curr == se) |
| 1899 | update_curr(cfs_rq); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 1900 | account_entity_dequeue(cfs_rq, se); |
Paul Turner | 19e5eeb | 2010-12-15 19:10:18 -0800 | [diff] [blame] | 1901 | } |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 1902 | |
| 1903 | update_load_set(&se->load, weight); |
| 1904 | |
| 1905 | if (se->on_rq) |
| 1906 | account_entity_enqueue(cfs_rq, se); |
| 1907 | } |
| 1908 | |
Paul Turner | 8295836 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1909 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq); |
| 1910 | |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 1911 | static void update_cfs_shares(struct cfs_rq *cfs_rq) |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 1912 | { |
| 1913 | struct task_group *tg; |
| 1914 | struct sched_entity *se; |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 1915 | long shares; |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 1916 | |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 1917 | tg = cfs_rq->tg; |
| 1918 | se = tg->se[cpu_of(rq_of(cfs_rq))]; |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 1919 | if (!se || throttled_hierarchy(cfs_rq)) |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 1920 | return; |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 1921 | #ifndef CONFIG_SMP |
| 1922 | if (likely(se->load.weight == tg->shares)) |
| 1923 | return; |
| 1924 | #endif |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 1925 | shares = calc_cfs_shares(cfs_rq, tg); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 1926 | |
| 1927 | reweight_entity(cfs_rq_of(se), se, shares); |
| 1928 | } |
| 1929 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 1930 | static inline void update_cfs_shares(struct cfs_rq *cfs_rq) |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 1931 | { |
| 1932 | } |
| 1933 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 1934 | |
Alex Shi | 141965c | 2013-06-26 13:05:39 +0800 | [diff] [blame] | 1935 | #ifdef CONFIG_SMP |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 1936 | /* |
Paul Turner | 5b51f2f | 2012-10-04 13:18:32 +0200 | [diff] [blame] | 1937 | * We choose a half-life close to 1 scheduling period. |
| 1938 | * Note: The tables below are dependent on this value. |
| 1939 | */ |
| 1940 | #define LOAD_AVG_PERIOD 32 |
| 1941 | #define LOAD_AVG_MAX 47742 /* maximum possible load avg */ |
| 1942 | #define LOAD_AVG_MAX_N 345 /* number of full periods to produce LOAD_MAX_AVG */ |
| 1943 | |
| 1944 | /* Precomputed fixed inverse multiplies for multiplication by y^n */ |
| 1945 | static const u32 runnable_avg_yN_inv[] = { |
| 1946 | 0xffffffff, 0xfa83b2da, 0xf5257d14, 0xefe4b99a, 0xeac0c6e6, 0xe5b906e6, |
| 1947 | 0xe0ccdeeb, 0xdbfbb796, 0xd744fcc9, 0xd2a81d91, 0xce248c14, 0xc9b9bd85, |
| 1948 | 0xc5672a10, 0xc12c4cc9, 0xbd08a39e, 0xb8fbaf46, 0xb504f333, 0xb123f581, |
| 1949 | 0xad583ee9, 0xa9a15ab4, 0xa5fed6a9, 0xa2704302, 0x9ef5325f, 0x9b8d39b9, |
| 1950 | 0x9837f050, 0x94f4efa8, 0x91c3d373, 0x8ea4398a, 0x8b95c1e3, 0x88980e80, |
| 1951 | 0x85aac367, 0x82cd8698, |
| 1952 | }; |
| 1953 | |
| 1954 | /* |
| 1955 | * Precomputed \Sum y^k { 1<=k<=n }. These are floor(true_value) to prevent |
| 1956 | * over-estimates when re-combining. |
| 1957 | */ |
| 1958 | static const u32 runnable_avg_yN_sum[] = { |
| 1959 | 0, 1002, 1982, 2941, 3880, 4798, 5697, 6576, 7437, 8279, 9103, |
| 1960 | 9909,10698,11470,12226,12966,13690,14398,15091,15769,16433,17082, |
| 1961 | 17718,18340,18949,19545,20128,20698,21256,21802,22336,22859,23371, |
| 1962 | }; |
| 1963 | |
| 1964 | /* |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 1965 | * Approximate: |
| 1966 | * val * y^n, where y^32 ~= 0.5 (~1 scheduling period) |
| 1967 | */ |
| 1968 | static __always_inline u64 decay_load(u64 val, u64 n) |
| 1969 | { |
Paul Turner | 5b51f2f | 2012-10-04 13:18:32 +0200 | [diff] [blame] | 1970 | unsigned int local_n; |
| 1971 | |
| 1972 | if (!n) |
| 1973 | return val; |
| 1974 | else if (unlikely(n > LOAD_AVG_PERIOD * 63)) |
| 1975 | return 0; |
| 1976 | |
| 1977 | /* after bounds checking we can collapse to 32-bit */ |
| 1978 | local_n = n; |
| 1979 | |
| 1980 | /* |
| 1981 | * As y^PERIOD = 1/2, we can combine |
| 1982 | * y^n = 1/2^(n/PERIOD) * k^(n%PERIOD) |
| 1983 | * With a look-up table which covers k^n (n<PERIOD) |
| 1984 | * |
| 1985 | * To achieve constant time decay_load. |
| 1986 | */ |
| 1987 | if (unlikely(local_n >= LOAD_AVG_PERIOD)) { |
| 1988 | val >>= local_n / LOAD_AVG_PERIOD; |
| 1989 | local_n %= LOAD_AVG_PERIOD; |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 1990 | } |
| 1991 | |
Paul Turner | 5b51f2f | 2012-10-04 13:18:32 +0200 | [diff] [blame] | 1992 | val *= runnable_avg_yN_inv[local_n]; |
| 1993 | /* We don't use SRR here since we always want to round down. */ |
| 1994 | return val >> 32; |
| 1995 | } |
| 1996 | |
| 1997 | /* |
| 1998 | * For updates fully spanning n periods, the contribution to runnable |
| 1999 | * average will be: \Sum 1024*y^n |
| 2000 | * |
| 2001 | * We can compute this reasonably efficiently by combining: |
| 2002 | * y^PERIOD = 1/2 with precomputed \Sum 1024*y^n {for n <PERIOD} |
| 2003 | */ |
| 2004 | static u32 __compute_runnable_contrib(u64 n) |
| 2005 | { |
| 2006 | u32 contrib = 0; |
| 2007 | |
| 2008 | if (likely(n <= LOAD_AVG_PERIOD)) |
| 2009 | return runnable_avg_yN_sum[n]; |
| 2010 | else if (unlikely(n >= LOAD_AVG_MAX_N)) |
| 2011 | return LOAD_AVG_MAX; |
| 2012 | |
| 2013 | /* Compute \Sum k^n combining precomputed values for k^i, \Sum k^j */ |
| 2014 | do { |
| 2015 | contrib /= 2; /* y^LOAD_AVG_PERIOD = 1/2 */ |
| 2016 | contrib += runnable_avg_yN_sum[LOAD_AVG_PERIOD]; |
| 2017 | |
| 2018 | n -= LOAD_AVG_PERIOD; |
| 2019 | } while (n > LOAD_AVG_PERIOD); |
| 2020 | |
| 2021 | contrib = decay_load(contrib, n); |
| 2022 | return contrib + runnable_avg_yN_sum[n]; |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | /* |
| 2026 | * We can represent the historical contribution to runnable average as the |
| 2027 | * coefficients of a geometric series. To do this we sub-divide our runnable |
| 2028 | * history into segments of approximately 1ms (1024us); label the segment that |
| 2029 | * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g. |
| 2030 | * |
| 2031 | * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ... |
| 2032 | * p0 p1 p2 |
| 2033 | * (now) (~1ms ago) (~2ms ago) |
| 2034 | * |
| 2035 | * Let u_i denote the fraction of p_i that the entity was runnable. |
| 2036 | * |
| 2037 | * We then designate the fractions u_i as our co-efficients, yielding the |
| 2038 | * following representation of historical load: |
| 2039 | * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ... |
| 2040 | * |
| 2041 | * We choose y based on the with of a reasonably scheduling period, fixing: |
| 2042 | * y^32 = 0.5 |
| 2043 | * |
| 2044 | * This means that the contribution to load ~32ms ago (u_32) will be weighted |
| 2045 | * approximately half as much as the contribution to load within the last ms |
| 2046 | * (u_0). |
| 2047 | * |
| 2048 | * When a period "rolls over" and we have new u_0`, multiplying the previous |
| 2049 | * sum again by y is sufficient to update: |
| 2050 | * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... ) |
| 2051 | * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}] |
| 2052 | */ |
| 2053 | static __always_inline int __update_entity_runnable_avg(u64 now, |
| 2054 | struct sched_avg *sa, |
| 2055 | int runnable) |
| 2056 | { |
Paul Turner | 5b51f2f | 2012-10-04 13:18:32 +0200 | [diff] [blame] | 2057 | u64 delta, periods; |
| 2058 | u32 runnable_contrib; |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2059 | int delta_w, decayed = 0; |
| 2060 | |
| 2061 | delta = now - sa->last_runnable_update; |
| 2062 | /* |
| 2063 | * This should only happen when time goes backwards, which it |
| 2064 | * unfortunately does during sched clock init when we swap over to TSC. |
| 2065 | */ |
| 2066 | if ((s64)delta < 0) { |
| 2067 | sa->last_runnable_update = now; |
| 2068 | return 0; |
| 2069 | } |
| 2070 | |
| 2071 | /* |
| 2072 | * Use 1024ns as the unit of measurement since it's a reasonable |
| 2073 | * approximation of 1us and fast to compute. |
| 2074 | */ |
| 2075 | delta >>= 10; |
| 2076 | if (!delta) |
| 2077 | return 0; |
| 2078 | sa->last_runnable_update = now; |
| 2079 | |
| 2080 | /* delta_w is the amount already accumulated against our next period */ |
| 2081 | delta_w = sa->runnable_avg_period % 1024; |
| 2082 | if (delta + delta_w >= 1024) { |
| 2083 | /* period roll-over */ |
| 2084 | decayed = 1; |
| 2085 | |
| 2086 | /* |
| 2087 | * Now that we know we're crossing a period boundary, figure |
| 2088 | * out how much from delta we need to complete the current |
| 2089 | * period and accrue it. |
| 2090 | */ |
| 2091 | delta_w = 1024 - delta_w; |
Paul Turner | 5b51f2f | 2012-10-04 13:18:32 +0200 | [diff] [blame] | 2092 | if (runnable) |
| 2093 | sa->runnable_avg_sum += delta_w; |
| 2094 | sa->runnable_avg_period += delta_w; |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2095 | |
Paul Turner | 5b51f2f | 2012-10-04 13:18:32 +0200 | [diff] [blame] | 2096 | delta -= delta_w; |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2097 | |
Paul Turner | 5b51f2f | 2012-10-04 13:18:32 +0200 | [diff] [blame] | 2098 | /* Figure out how many additional periods this update spans */ |
| 2099 | periods = delta / 1024; |
| 2100 | delta %= 1024; |
| 2101 | |
| 2102 | sa->runnable_avg_sum = decay_load(sa->runnable_avg_sum, |
| 2103 | periods + 1); |
| 2104 | sa->runnable_avg_period = decay_load(sa->runnable_avg_period, |
| 2105 | periods + 1); |
| 2106 | |
| 2107 | /* Efficiently calculate \sum (1..n_period) 1024*y^i */ |
| 2108 | runnable_contrib = __compute_runnable_contrib(periods); |
| 2109 | if (runnable) |
| 2110 | sa->runnable_avg_sum += runnable_contrib; |
| 2111 | sa->runnable_avg_period += runnable_contrib; |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | /* Remainder of delta accrued against u_0` */ |
| 2115 | if (runnable) |
| 2116 | sa->runnable_avg_sum += delta; |
| 2117 | sa->runnable_avg_period += delta; |
| 2118 | |
| 2119 | return decayed; |
| 2120 | } |
| 2121 | |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2122 | /* Synchronize an entity's decay with its parenting cfs_rq.*/ |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2123 | static inline u64 __synchronize_entity_decay(struct sched_entity *se) |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2124 | { |
| 2125 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 2126 | u64 decays = atomic64_read(&cfs_rq->decay_counter); |
| 2127 | |
| 2128 | decays -= se->avg.decay_count; |
| 2129 | if (!decays) |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2130 | return 0; |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2131 | |
| 2132 | se->avg.load_avg_contrib = decay_load(se->avg.load_avg_contrib, decays); |
| 2133 | se->avg.decay_count = 0; |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2134 | |
| 2135 | return decays; |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2136 | } |
| 2137 | |
Paul Turner | c566e8e | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2138 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 2139 | static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq, |
| 2140 | int force_update) |
| 2141 | { |
| 2142 | struct task_group *tg = cfs_rq->tg; |
Alex Shi | bf5b986 | 2013-06-20 10:18:54 +0800 | [diff] [blame] | 2143 | long tg_contrib; |
Paul Turner | c566e8e | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2144 | |
| 2145 | tg_contrib = cfs_rq->runnable_load_avg + cfs_rq->blocked_load_avg; |
| 2146 | tg_contrib -= cfs_rq->tg_load_contrib; |
| 2147 | |
Alex Shi | bf5b986 | 2013-06-20 10:18:54 +0800 | [diff] [blame] | 2148 | if (force_update || abs(tg_contrib) > cfs_rq->tg_load_contrib / 8) { |
| 2149 | atomic_long_add(tg_contrib, &tg->load_avg); |
Paul Turner | c566e8e | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2150 | cfs_rq->tg_load_contrib += tg_contrib; |
| 2151 | } |
| 2152 | } |
Paul Turner | 8165e14 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2153 | |
Paul Turner | bb17f65 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2154 | /* |
| 2155 | * Aggregate cfs_rq runnable averages into an equivalent task_group |
| 2156 | * representation for computing load contributions. |
| 2157 | */ |
| 2158 | static inline void __update_tg_runnable_avg(struct sched_avg *sa, |
| 2159 | struct cfs_rq *cfs_rq) |
| 2160 | { |
| 2161 | struct task_group *tg = cfs_rq->tg; |
| 2162 | long contrib; |
| 2163 | |
| 2164 | /* The fraction of a cpu used by this cfs_rq */ |
Michal Nazarewicz | 85b088e | 2013-11-10 20:42:01 +0100 | [diff] [blame] | 2165 | contrib = div_u64((u64)sa->runnable_avg_sum << NICE_0_SHIFT, |
Paul Turner | bb17f65 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2166 | sa->runnable_avg_period + 1); |
| 2167 | contrib -= cfs_rq->tg_runnable_contrib; |
| 2168 | |
| 2169 | if (abs(contrib) > cfs_rq->tg_runnable_contrib / 64) { |
| 2170 | atomic_add(contrib, &tg->runnable_avg); |
| 2171 | cfs_rq->tg_runnable_contrib += contrib; |
| 2172 | } |
| 2173 | } |
| 2174 | |
Paul Turner | 8165e14 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2175 | static inline void __update_group_entity_contrib(struct sched_entity *se) |
| 2176 | { |
| 2177 | struct cfs_rq *cfs_rq = group_cfs_rq(se); |
| 2178 | struct task_group *tg = cfs_rq->tg; |
Paul Turner | bb17f65 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2179 | int runnable_avg; |
| 2180 | |
Paul Turner | 8165e14 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2181 | u64 contrib; |
| 2182 | |
| 2183 | contrib = cfs_rq->tg_load_contrib * tg->shares; |
Alex Shi | bf5b986 | 2013-06-20 10:18:54 +0800 | [diff] [blame] | 2184 | se->avg.load_avg_contrib = div_u64(contrib, |
| 2185 | atomic_long_read(&tg->load_avg) + 1); |
Paul Turner | bb17f65 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2186 | |
| 2187 | /* |
| 2188 | * For group entities we need to compute a correction term in the case |
| 2189 | * that they are consuming <1 cpu so that we would contribute the same |
| 2190 | * load as a task of equal weight. |
| 2191 | * |
| 2192 | * Explicitly co-ordinating this measurement would be expensive, but |
| 2193 | * fortunately the sum of each cpus contribution forms a usable |
| 2194 | * lower-bound on the true value. |
| 2195 | * |
| 2196 | * Consider the aggregate of 2 contributions. Either they are disjoint |
| 2197 | * (and the sum represents true value) or they are disjoint and we are |
| 2198 | * understating by the aggregate of their overlap. |
| 2199 | * |
| 2200 | * Extending this to N cpus, for a given overlap, the maximum amount we |
| 2201 | * understand is then n_i(n_i+1)/2 * w_i where n_i is the number of |
| 2202 | * cpus that overlap for this interval and w_i is the interval width. |
| 2203 | * |
| 2204 | * On a small machine; the first term is well-bounded which bounds the |
| 2205 | * total error since w_i is a subset of the period. Whereas on a |
| 2206 | * larger machine, while this first term can be larger, if w_i is the |
| 2207 | * of consequential size guaranteed to see n_i*w_i quickly converge to |
| 2208 | * our upper bound of 1-cpu. |
| 2209 | */ |
| 2210 | runnable_avg = atomic_read(&tg->runnable_avg); |
| 2211 | if (runnable_avg < NICE_0_LOAD) { |
| 2212 | se->avg.load_avg_contrib *= runnable_avg; |
| 2213 | se->avg.load_avg_contrib >>= NICE_0_SHIFT; |
| 2214 | } |
Paul Turner | 8165e14 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2215 | } |
Paul Turner | c566e8e | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2216 | #else |
| 2217 | static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq, |
| 2218 | int force_update) {} |
Paul Turner | bb17f65 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2219 | static inline void __update_tg_runnable_avg(struct sched_avg *sa, |
| 2220 | struct cfs_rq *cfs_rq) {} |
Paul Turner | 8165e14 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2221 | static inline void __update_group_entity_contrib(struct sched_entity *se) {} |
Paul Turner | c566e8e | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2222 | #endif |
| 2223 | |
Paul Turner | 8165e14 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2224 | static inline void __update_task_entity_contrib(struct sched_entity *se) |
| 2225 | { |
| 2226 | u32 contrib; |
| 2227 | |
| 2228 | /* avoid overflowing a 32-bit type w/ SCHED_LOAD_SCALE */ |
| 2229 | contrib = se->avg.runnable_avg_sum * scale_load_down(se->load.weight); |
| 2230 | contrib /= (se->avg.runnable_avg_period + 1); |
| 2231 | se->avg.load_avg_contrib = scale_load(contrib); |
| 2232 | } |
| 2233 | |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2234 | /* Compute the current contribution to load_avg by se, return any delta */ |
| 2235 | static long __update_entity_load_avg_contrib(struct sched_entity *se) |
| 2236 | { |
| 2237 | long old_contrib = se->avg.load_avg_contrib; |
| 2238 | |
Paul Turner | 8165e14 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2239 | if (entity_is_task(se)) { |
| 2240 | __update_task_entity_contrib(se); |
| 2241 | } else { |
Paul Turner | bb17f65 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2242 | __update_tg_runnable_avg(&se->avg, group_cfs_rq(se)); |
Paul Turner | 8165e14 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2243 | __update_group_entity_contrib(se); |
| 2244 | } |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2245 | |
| 2246 | return se->avg.load_avg_contrib - old_contrib; |
| 2247 | } |
| 2248 | |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2249 | static inline void subtract_blocked_load_contrib(struct cfs_rq *cfs_rq, |
| 2250 | long load_contrib) |
| 2251 | { |
| 2252 | if (likely(load_contrib < cfs_rq->blocked_load_avg)) |
| 2253 | cfs_rq->blocked_load_avg -= load_contrib; |
| 2254 | else |
| 2255 | cfs_rq->blocked_load_avg = 0; |
| 2256 | } |
| 2257 | |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2258 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq); |
| 2259 | |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2260 | /* Update a sched_entity's runnable average */ |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2261 | static inline void update_entity_load_avg(struct sched_entity *se, |
| 2262 | int update_cfs_rq) |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2263 | { |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2264 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 2265 | long contrib_delta; |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2266 | u64 now; |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2267 | |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2268 | /* |
| 2269 | * For a group entity we need to use their owned cfs_rq_clock_task() in |
| 2270 | * case they are the parent of a throttled hierarchy. |
| 2271 | */ |
| 2272 | if (entity_is_task(se)) |
| 2273 | now = cfs_rq_clock_task(cfs_rq); |
| 2274 | else |
| 2275 | now = cfs_rq_clock_task(group_cfs_rq(se)); |
| 2276 | |
| 2277 | if (!__update_entity_runnable_avg(now, &se->avg, se->on_rq)) |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2278 | return; |
| 2279 | |
| 2280 | contrib_delta = __update_entity_load_avg_contrib(se); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2281 | |
| 2282 | if (!update_cfs_rq) |
| 2283 | return; |
| 2284 | |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2285 | if (se->on_rq) |
| 2286 | cfs_rq->runnable_load_avg += contrib_delta; |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2287 | else |
| 2288 | subtract_blocked_load_contrib(cfs_rq, -contrib_delta); |
| 2289 | } |
| 2290 | |
| 2291 | /* |
| 2292 | * Decay the load contributed by all blocked children and account this so that |
| 2293 | * their contribution may appropriately discounted when they wake up. |
| 2294 | */ |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2295 | static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq, int force_update) |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2296 | { |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2297 | u64 now = cfs_rq_clock_task(cfs_rq) >> 20; |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2298 | u64 decays; |
| 2299 | |
| 2300 | decays = now - cfs_rq->last_decay; |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2301 | if (!decays && !force_update) |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2302 | return; |
| 2303 | |
Alex Shi | 2509940 | 2013-06-20 10:18:55 +0800 | [diff] [blame] | 2304 | if (atomic_long_read(&cfs_rq->removed_load)) { |
| 2305 | unsigned long removed_load; |
| 2306 | removed_load = atomic_long_xchg(&cfs_rq->removed_load, 0); |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2307 | subtract_blocked_load_contrib(cfs_rq, removed_load); |
| 2308 | } |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2309 | |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2310 | if (decays) { |
| 2311 | cfs_rq->blocked_load_avg = decay_load(cfs_rq->blocked_load_avg, |
| 2312 | decays); |
| 2313 | atomic64_add(decays, &cfs_rq->decay_counter); |
| 2314 | cfs_rq->last_decay = now; |
| 2315 | } |
Paul Turner | c566e8e | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2316 | |
| 2317 | __update_cfs_rq_tg_load_contrib(cfs_rq, force_update); |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2318 | } |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 2319 | |
| 2320 | static inline void update_rq_runnable_avg(struct rq *rq, int runnable) |
| 2321 | { |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 2322 | __update_entity_runnable_avg(rq_clock_task(rq), &rq->avg, runnable); |
Paul Turner | bb17f65 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2323 | __update_tg_runnable_avg(&rq->avg, &rq->cfs); |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 2324 | } |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2325 | |
| 2326 | /* Add the load generated by se into cfs_rq's child load-average */ |
| 2327 | static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq, |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2328 | struct sched_entity *se, |
| 2329 | int wakeup) |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2330 | { |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2331 | /* |
| 2332 | * We track migrations using entity decay_count <= 0, on a wake-up |
| 2333 | * migration we use a negative decay count to track the remote decays |
| 2334 | * accumulated while sleeping. |
Alex Shi | a75cdaa | 2013-06-20 10:18:47 +0800 | [diff] [blame] | 2335 | * |
| 2336 | * Newly forked tasks are enqueued with se->avg.decay_count == 0, they |
| 2337 | * are seen by enqueue_entity_load_avg() as a migration with an already |
| 2338 | * constructed load_avg_contrib. |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2339 | */ |
| 2340 | if (unlikely(se->avg.decay_count <= 0)) { |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 2341 | se->avg.last_runnable_update = rq_clock_task(rq_of(cfs_rq)); |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2342 | if (se->avg.decay_count) { |
| 2343 | /* |
| 2344 | * In a wake-up migration we have to approximate the |
| 2345 | * time sleeping. This is because we can't synchronize |
| 2346 | * clock_task between the two cpus, and it is not |
| 2347 | * guaranteed to be read-safe. Instead, we can |
| 2348 | * approximate this using our carried decays, which are |
| 2349 | * explicitly atomically readable. |
| 2350 | */ |
| 2351 | se->avg.last_runnable_update -= (-se->avg.decay_count) |
| 2352 | << 20; |
| 2353 | update_entity_load_avg(se, 0); |
| 2354 | /* Indicate that we're now synchronized and on-rq */ |
| 2355 | se->avg.decay_count = 0; |
| 2356 | } |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2357 | wakeup = 0; |
| 2358 | } else { |
Vincent Guittot | 9390675 | 2014-01-22 08:45:34 +0100 | [diff] [blame^] | 2359 | __synchronize_entity_decay(se); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2360 | } |
| 2361 | |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2362 | /* migrated tasks did not contribute to our blocked load */ |
| 2363 | if (wakeup) { |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2364 | subtract_blocked_load_contrib(cfs_rq, se->avg.load_avg_contrib); |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2365 | update_entity_load_avg(se, 0); |
| 2366 | } |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2367 | |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2368 | cfs_rq->runnable_load_avg += se->avg.load_avg_contrib; |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2369 | /* we force update consideration on load-balancer moves */ |
| 2370 | update_cfs_rq_blocked_load(cfs_rq, !wakeup); |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2371 | } |
| 2372 | |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2373 | /* |
| 2374 | * Remove se's load from this cfs_rq child load-average, if the entity is |
| 2375 | * transitioning to a blocked state we track its projected decay using |
| 2376 | * blocked_load_avg. |
| 2377 | */ |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2378 | static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq, |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2379 | struct sched_entity *se, |
| 2380 | int sleep) |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2381 | { |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2382 | update_entity_load_avg(se, 1); |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2383 | /* we force update consideration on load-balancer moves */ |
| 2384 | update_cfs_rq_blocked_load(cfs_rq, !sleep); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2385 | |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2386 | cfs_rq->runnable_load_avg -= se->avg.load_avg_contrib; |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2387 | if (sleep) { |
| 2388 | cfs_rq->blocked_load_avg += se->avg.load_avg_contrib; |
| 2389 | se->avg.decay_count = atomic64_read(&cfs_rq->decay_counter); |
| 2390 | } /* migrations, e.g. sleep=0 leave decay_count == 0 */ |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2391 | } |
Vincent Guittot | 642dbc3 | 2013-04-18 18:34:26 +0200 | [diff] [blame] | 2392 | |
| 2393 | /* |
| 2394 | * Update the rq's load with the elapsed running time before entering |
| 2395 | * idle. if the last scheduled task is not a CFS task, idle_enter will |
| 2396 | * be the only way to update the runnable statistic. |
| 2397 | */ |
| 2398 | void idle_enter_fair(struct rq *this_rq) |
| 2399 | { |
| 2400 | update_rq_runnable_avg(this_rq, 1); |
| 2401 | } |
| 2402 | |
| 2403 | /* |
| 2404 | * Update the rq's load with the elapsed idle time before a task is |
| 2405 | * scheduled. if the newly scheduled task is not a CFS task, idle_exit will |
| 2406 | * be the only way to update the runnable statistic. |
| 2407 | */ |
| 2408 | void idle_exit_fair(struct rq *this_rq) |
| 2409 | { |
| 2410 | update_rq_runnable_avg(this_rq, 0); |
| 2411 | } |
| 2412 | |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2413 | #else |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2414 | static inline void update_entity_load_avg(struct sched_entity *se, |
| 2415 | int update_cfs_rq) {} |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 2416 | static inline void update_rq_runnable_avg(struct rq *rq, int runnable) {} |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2417 | static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq, |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2418 | struct sched_entity *se, |
| 2419 | int wakeup) {} |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2420 | static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq, |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2421 | struct sched_entity *se, |
| 2422 | int sleep) {} |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2423 | static inline void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq, |
| 2424 | int force_update) {} |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2425 | #endif |
| 2426 | |
Ingo Molnar | 2396af6 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 2427 | 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] | 2428 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2429 | #ifdef CONFIG_SCHEDSTATS |
Peter Zijlstra | e414314 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 2430 | struct task_struct *tsk = NULL; |
| 2431 | |
| 2432 | if (entity_is_task(se)) |
| 2433 | tsk = task_of(se); |
| 2434 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 2435 | if (se->statistics.sleep_start) { |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 2436 | u64 delta = rq_clock(rq_of(cfs_rq)) - se->statistics.sleep_start; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2437 | |
| 2438 | if ((s64)delta < 0) |
| 2439 | delta = 0; |
| 2440 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 2441 | if (unlikely(delta > se->statistics.sleep_max)) |
| 2442 | se->statistics.sleep_max = delta; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2443 | |
Peter Zijlstra | 8c79a04 | 2012-01-30 14:51:37 +0100 | [diff] [blame] | 2444 | se->statistics.sleep_start = 0; |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 2445 | se->statistics.sum_sleep_runtime += delta; |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 2446 | |
Peter Zijlstra | 768d0c2 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 2447 | if (tsk) { |
Peter Zijlstra | e414314 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 2448 | account_scheduler_latency(tsk, delta >> 10, 1); |
Peter Zijlstra | 768d0c2 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 2449 | trace_sched_stat_sleep(tsk, delta); |
| 2450 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2451 | } |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 2452 | if (se->statistics.block_start) { |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 2453 | u64 delta = rq_clock(rq_of(cfs_rq)) - se->statistics.block_start; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2454 | |
| 2455 | if ((s64)delta < 0) |
| 2456 | delta = 0; |
| 2457 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 2458 | if (unlikely(delta > se->statistics.block_max)) |
| 2459 | se->statistics.block_max = delta; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2460 | |
Peter Zijlstra | 8c79a04 | 2012-01-30 14:51:37 +0100 | [diff] [blame] | 2461 | se->statistics.block_start = 0; |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 2462 | se->statistics.sum_sleep_runtime += delta; |
Ingo Molnar | 30084fb | 2007-10-02 14:13:08 +0200 | [diff] [blame] | 2463 | |
Peter Zijlstra | e414314 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 2464 | if (tsk) { |
Arjan van de Ven | 8f0dfc3 | 2009-07-20 11:26:58 -0700 | [diff] [blame] | 2465 | if (tsk->in_iowait) { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 2466 | se->statistics.iowait_sum += delta; |
| 2467 | se->statistics.iowait_count++; |
Peter Zijlstra | 768d0c2 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 2468 | trace_sched_stat_iowait(tsk, delta); |
Arjan van de Ven | 8f0dfc3 | 2009-07-20 11:26:58 -0700 | [diff] [blame] | 2469 | } |
| 2470 | |
Andrew Vagin | b781a60 | 2011-11-28 12:03:35 +0300 | [diff] [blame] | 2471 | trace_sched_stat_blocked(tsk, delta); |
| 2472 | |
Peter Zijlstra | e414314 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 2473 | /* |
| 2474 | * Blocking time is in units of nanosecs, so shift by |
| 2475 | * 20 to get a milliseconds-range estimation of the |
| 2476 | * amount of time that the task spent sleeping: |
| 2477 | */ |
| 2478 | if (unlikely(prof_on == SLEEP_PROFILING)) { |
| 2479 | profile_hits(SLEEP_PROFILING, |
| 2480 | (void *)get_wchan(tsk), |
| 2481 | delta >> 20); |
| 2482 | } |
| 2483 | account_scheduler_latency(tsk, delta >> 10, 0); |
Ingo Molnar | 30084fb | 2007-10-02 14:13:08 +0200 | [diff] [blame] | 2484 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2485 | } |
| 2486 | #endif |
| 2487 | } |
| 2488 | |
Peter Zijlstra | ddc9729 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 2489 | static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 2490 | { |
| 2491 | #ifdef CONFIG_SCHED_DEBUG |
| 2492 | s64 d = se->vruntime - cfs_rq->min_vruntime; |
| 2493 | |
| 2494 | if (d < 0) |
| 2495 | d = -d; |
| 2496 | |
| 2497 | if (d > 3*sysctl_sched_latency) |
| 2498 | schedstat_inc(cfs_rq, nr_spread_over); |
| 2499 | #endif |
| 2500 | } |
| 2501 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2502 | static void |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 2503 | place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial) |
| 2504 | { |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 2505 | u64 vruntime = cfs_rq->min_vruntime; |
Peter Zijlstra | 94dfb5e | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 2506 | |
Peter Zijlstra | 2cb8600 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 2507 | /* |
| 2508 | * The 'current' period is already promised to the current tasks, |
| 2509 | * however the extra weight of the new task will slow them down a |
| 2510 | * little, place the new task so that it fits in the slot that |
| 2511 | * stays open at the end. |
| 2512 | */ |
Peter Zijlstra | 94dfb5e | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 2513 | if (initial && sched_feat(START_DEBIT)) |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 2514 | vruntime += sched_vslice(cfs_rq, se); |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 2515 | |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 2516 | /* sleeps up to a single latency don't count. */ |
Mike Galbraith | 5ca9880 | 2010-03-11 17:17:17 +0100 | [diff] [blame] | 2517 | if (!initial) { |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 2518 | unsigned long thresh = sysctl_sched_latency; |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 2519 | |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 2520 | /* |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 2521 | * Halve their sleep time's effect, to allow |
| 2522 | * for a gentler effect of sleepers: |
| 2523 | */ |
| 2524 | if (sched_feat(GENTLE_FAIR_SLEEPERS)) |
| 2525 | thresh >>= 1; |
Ingo Molnar | 51e0304 | 2009-09-16 08:54:45 +0200 | [diff] [blame] | 2526 | |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 2527 | vruntime -= thresh; |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 2528 | } |
| 2529 | |
Mike Galbraith | b5d9d73 | 2009-09-08 11:12:28 +0200 | [diff] [blame] | 2530 | /* ensure we never gain time by being placed backwards. */ |
Viresh Kumar | 16c8f1c | 2012-11-08 13:33:46 +0530 | [diff] [blame] | 2531 | se->vruntime = max_vruntime(se->vruntime, vruntime); |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 2532 | } |
| 2533 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 2534 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq); |
| 2535 | |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 2536 | static void |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 2537 | 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] | 2538 | { |
| 2539 | /* |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 2540 | * Update the normalized vruntime before updating min_vruntime |
Kamalesh Babulal | 0fc576d | 2013-06-27 11:24:18 +0530 | [diff] [blame] | 2541 | * through calling update_curr(). |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 2542 | */ |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 2543 | if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING)) |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 2544 | se->vruntime += cfs_rq->min_vruntime; |
| 2545 | |
| 2546 | /* |
Dmitry Adamushko | a2a2d68 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 2547 | * Update run-time statistics of the 'current'. |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2548 | */ |
Ingo Molnar | b7cc089 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 2549 | update_curr(cfs_rq); |
Paul Turner | f269ae0 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2550 | enqueue_entity_load_avg(cfs_rq, se, flags & ENQUEUE_WAKEUP); |
Linus Torvalds | 17bc14b | 2012-12-14 07:20:43 -0800 | [diff] [blame] | 2551 | account_entity_enqueue(cfs_rq, se); |
| 2552 | update_cfs_shares(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2553 | |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 2554 | if (flags & ENQUEUE_WAKEUP) { |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 2555 | place_entity(cfs_rq, se, 0); |
Ingo Molnar | 2396af6 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 2556 | enqueue_sleeper(cfs_rq, se); |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 2557 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2558 | |
Ingo Molnar | d2417e5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 2559 | update_stats_enqueue(cfs_rq, se); |
Peter Zijlstra | ddc9729 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 2560 | check_spread(cfs_rq, se); |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 2561 | if (se != cfs_rq->curr) |
| 2562 | __enqueue_entity(cfs_rq, se); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2563 | se->on_rq = 1; |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 2564 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 2565 | if (cfs_rq->nr_running == 1) { |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 2566 | list_add_leaf_cfs_rq(cfs_rq); |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 2567 | check_enqueue_throttle(cfs_rq); |
| 2568 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2569 | } |
| 2570 | |
Rik van Riel | 2c13c919 | 2011-02-01 09:48:37 -0500 | [diff] [blame] | 2571 | static void __clear_buddies_last(struct sched_entity *se) |
Peter Zijlstra | 2002c69 | 2008-11-11 11:52:33 +0100 | [diff] [blame] | 2572 | { |
Rik van Riel | 2c13c919 | 2011-02-01 09:48:37 -0500 | [diff] [blame] | 2573 | for_each_sched_entity(se) { |
| 2574 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 2575 | if (cfs_rq->last == se) |
| 2576 | cfs_rq->last = NULL; |
| 2577 | else |
| 2578 | break; |
| 2579 | } |
| 2580 | } |
Peter Zijlstra | 2002c69 | 2008-11-11 11:52:33 +0100 | [diff] [blame] | 2581 | |
Rik van Riel | 2c13c919 | 2011-02-01 09:48:37 -0500 | [diff] [blame] | 2582 | static void __clear_buddies_next(struct sched_entity *se) |
| 2583 | { |
| 2584 | for_each_sched_entity(se) { |
| 2585 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 2586 | if (cfs_rq->next == se) |
| 2587 | cfs_rq->next = NULL; |
| 2588 | else |
| 2589 | break; |
| 2590 | } |
Peter Zijlstra | 2002c69 | 2008-11-11 11:52:33 +0100 | [diff] [blame] | 2591 | } |
| 2592 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 2593 | static void __clear_buddies_skip(struct sched_entity *se) |
| 2594 | { |
| 2595 | for_each_sched_entity(se) { |
| 2596 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 2597 | if (cfs_rq->skip == se) |
| 2598 | cfs_rq->skip = NULL; |
| 2599 | else |
| 2600 | break; |
| 2601 | } |
| 2602 | } |
| 2603 | |
Peter Zijlstra | a571bbe | 2009-01-28 14:51:40 +0100 | [diff] [blame] | 2604 | static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 2605 | { |
Rik van Riel | 2c13c919 | 2011-02-01 09:48:37 -0500 | [diff] [blame] | 2606 | if (cfs_rq->last == se) |
| 2607 | __clear_buddies_last(se); |
| 2608 | |
| 2609 | if (cfs_rq->next == se) |
| 2610 | __clear_buddies_next(se); |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 2611 | |
| 2612 | if (cfs_rq->skip == se) |
| 2613 | __clear_buddies_skip(se); |
Peter Zijlstra | a571bbe | 2009-01-28 14:51:40 +0100 | [diff] [blame] | 2614 | } |
| 2615 | |
Peter Zijlstra | 6c16a6d | 2012-03-21 13:07:16 -0700 | [diff] [blame] | 2616 | 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] | 2617 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2618 | static void |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 2619 | 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] | 2620 | { |
Dmitry Adamushko | a2a2d68 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 2621 | /* |
| 2622 | * Update run-time statistics of the 'current'. |
| 2623 | */ |
| 2624 | update_curr(cfs_rq); |
Linus Torvalds | 17bc14b | 2012-12-14 07:20:43 -0800 | [diff] [blame] | 2625 | dequeue_entity_load_avg(cfs_rq, se, flags & DEQUEUE_SLEEP); |
Dmitry Adamushko | a2a2d68 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 2626 | |
Ingo Molnar | 19b6a2e | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 2627 | update_stats_dequeue(cfs_rq, se); |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 2628 | if (flags & DEQUEUE_SLEEP) { |
Peter Zijlstra | 67e9fb2 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 2629 | #ifdef CONFIG_SCHEDSTATS |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2630 | if (entity_is_task(se)) { |
| 2631 | struct task_struct *tsk = task_of(se); |
| 2632 | |
| 2633 | if (tsk->state & TASK_INTERRUPTIBLE) |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 2634 | se->statistics.sleep_start = rq_clock(rq_of(cfs_rq)); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2635 | if (tsk->state & TASK_UNINTERRUPTIBLE) |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 2636 | se->statistics.block_start = rq_clock(rq_of(cfs_rq)); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2637 | } |
Dmitry Adamushko | db36cc7 | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 2638 | #endif |
Peter Zijlstra | 67e9fb2 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 2639 | } |
| 2640 | |
Peter Zijlstra | 2002c69 | 2008-11-11 11:52:33 +0100 | [diff] [blame] | 2641 | clear_buddies(cfs_rq, se); |
Peter Zijlstra | 4793241 | 2008-11-04 21:25:09 +0100 | [diff] [blame] | 2642 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 2643 | if (se != cfs_rq->curr) |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 2644 | __dequeue_entity(cfs_rq, se); |
Linus Torvalds | 17bc14b | 2012-12-14 07:20:43 -0800 | [diff] [blame] | 2645 | se->on_rq = 0; |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 2646 | account_entity_dequeue(cfs_rq, se); |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 2647 | |
| 2648 | /* |
| 2649 | * Normalize the entity after updating the min_vruntime because the |
| 2650 | * update can refer to the ->curr item and we need to reflect this |
| 2651 | * movement in our normalized position. |
| 2652 | */ |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 2653 | if (!(flags & DEQUEUE_SLEEP)) |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 2654 | se->vruntime -= cfs_rq->min_vruntime; |
Peter Zijlstra | 1e87623 | 2011-05-17 16:21:10 -0700 | [diff] [blame] | 2655 | |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 2656 | /* return excess runtime on last dequeue */ |
| 2657 | return_cfs_rq_runtime(cfs_rq); |
| 2658 | |
Peter Zijlstra | 1e87623 | 2011-05-17 16:21:10 -0700 | [diff] [blame] | 2659 | update_min_vruntime(cfs_rq); |
Linus Torvalds | 17bc14b | 2012-12-14 07:20:43 -0800 | [diff] [blame] | 2660 | update_cfs_shares(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2661 | } |
| 2662 | |
| 2663 | /* |
| 2664 | * Preempt the current task with a newly woken task if needed: |
| 2665 | */ |
Peter Zijlstra | 7c92e54 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 2666 | static void |
Ingo Molnar | 2e09bf5 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 2667 | check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2668 | { |
Peter Zijlstra | 1169783 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 2669 | unsigned long ideal_runtime, delta_exec; |
Wang Xingchao | f4cfb33 | 2011-09-16 13:35:52 -0400 | [diff] [blame] | 2670 | struct sched_entity *se; |
| 2671 | s64 delta; |
Peter Zijlstra | 1169783 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 2672 | |
Peter Zijlstra | 6d0f0ebd | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 2673 | ideal_runtime = sched_slice(cfs_rq, curr); |
Peter Zijlstra | 1169783 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 2674 | delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime; |
Mike Galbraith | a9f3e2b | 2009-01-28 14:51:39 +0100 | [diff] [blame] | 2675 | if (delta_exec > ideal_runtime) { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2676 | resched_task(rq_of(cfs_rq)->curr); |
Mike Galbraith | a9f3e2b | 2009-01-28 14:51:39 +0100 | [diff] [blame] | 2677 | /* |
| 2678 | * The current task ran long enough, ensure it doesn't get |
| 2679 | * re-elected due to buddy favours. |
| 2680 | */ |
| 2681 | clear_buddies(cfs_rq, curr); |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 2682 | return; |
| 2683 | } |
| 2684 | |
| 2685 | /* |
| 2686 | * Ensure that a task that missed wakeup preemption by a |
| 2687 | * narrow margin doesn't have to wait for a full slice. |
| 2688 | * This also mitigates buddy induced latencies under load. |
| 2689 | */ |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 2690 | if (delta_exec < sysctl_sched_min_granularity) |
| 2691 | return; |
| 2692 | |
Wang Xingchao | f4cfb33 | 2011-09-16 13:35:52 -0400 | [diff] [blame] | 2693 | se = __pick_first_entity(cfs_rq); |
| 2694 | delta = curr->vruntime - se->vruntime; |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 2695 | |
Wang Xingchao | f4cfb33 | 2011-09-16 13:35:52 -0400 | [diff] [blame] | 2696 | if (delta < 0) |
| 2697 | return; |
Mike Galbraith | d7d8294 | 2011-01-05 05:41:17 +0100 | [diff] [blame] | 2698 | |
Wang Xingchao | f4cfb33 | 2011-09-16 13:35:52 -0400 | [diff] [blame] | 2699 | if (delta > ideal_runtime) |
| 2700 | resched_task(rq_of(cfs_rq)->curr); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2701 | } |
| 2702 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 2703 | static void |
Ingo Molnar | 8494f41 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 2704 | set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2705 | { |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 2706 | /* 'current' is not kept within the tree. */ |
| 2707 | if (se->on_rq) { |
| 2708 | /* |
| 2709 | * Any task has to be enqueued before it get to execute on |
| 2710 | * a CPU. So account for the time it spent waiting on the |
| 2711 | * runqueue. |
| 2712 | */ |
| 2713 | update_stats_wait_end(cfs_rq, se); |
| 2714 | __dequeue_entity(cfs_rq, se); |
| 2715 | } |
| 2716 | |
Ingo Molnar | 79303e9 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 2717 | update_stats_curr_start(cfs_rq, se); |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 2718 | cfs_rq->curr = se; |
Ingo Molnar | eba1ed4 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 2719 | #ifdef CONFIG_SCHEDSTATS |
| 2720 | /* |
| 2721 | * Track our maximum slice length, if the CPU's load is at |
| 2722 | * least twice that of our own weight (i.e. dont track it |
| 2723 | * when there are only lesser-weight tasks around): |
| 2724 | */ |
Dmitry Adamushko | 495eca4 | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 2725 | if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 2726 | se->statistics.slice_max = max(se->statistics.slice_max, |
Ingo Molnar | eba1ed4 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 2727 | se->sum_exec_runtime - se->prev_sum_exec_runtime); |
| 2728 | } |
| 2729 | #endif |
Peter Zijlstra | 4a55b45 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 2730 | se->prev_sum_exec_runtime = se->sum_exec_runtime; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2731 | } |
| 2732 | |
Peter Zijlstra | 3f3a490 | 2008-10-24 11:06:16 +0200 | [diff] [blame] | 2733 | static int |
| 2734 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se); |
| 2735 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 2736 | /* |
| 2737 | * Pick the next process, keeping these things in mind, in this order: |
| 2738 | * 1) keep things fair between processes/task groups |
| 2739 | * 2) pick the "next" process, since someone really wants that to run |
| 2740 | * 3) pick the "last" process, for cache locality |
| 2741 | * 4) do not run the "skip" process, if something else is available |
| 2742 | */ |
Peter Zijlstra | f4b6755 | 2008-11-04 21:25:07 +0100 | [diff] [blame] | 2743 | static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq) |
Peter Zijlstra | aa2ac25 | 2008-03-14 21:12:12 +0100 | [diff] [blame] | 2744 | { |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 2745 | struct sched_entity *se = __pick_first_entity(cfs_rq); |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 2746 | struct sched_entity *left = se; |
Peter Zijlstra | f4b6755 | 2008-11-04 21:25:07 +0100 | [diff] [blame] | 2747 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 2748 | /* |
| 2749 | * Avoid running the skip buddy, if running something else can |
| 2750 | * be done without getting too unfair. |
| 2751 | */ |
| 2752 | if (cfs_rq->skip == se) { |
| 2753 | struct sched_entity *second = __pick_next_entity(se); |
| 2754 | if (second && wakeup_preempt_entity(second, left) < 1) |
| 2755 | se = second; |
| 2756 | } |
Peter Zijlstra | aa2ac25 | 2008-03-14 21:12:12 +0100 | [diff] [blame] | 2757 | |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 2758 | /* |
| 2759 | * Prefer last buddy, try to return the CPU to a preempted task. |
| 2760 | */ |
| 2761 | if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1) |
| 2762 | se = cfs_rq->last; |
| 2763 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 2764 | /* |
| 2765 | * Someone really wants this to run. If it's not unfair, run it. |
| 2766 | */ |
| 2767 | if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1) |
| 2768 | se = cfs_rq->next; |
| 2769 | |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 2770 | clear_buddies(cfs_rq, se); |
Peter Zijlstra | 4793241 | 2008-11-04 21:25:09 +0100 | [diff] [blame] | 2771 | |
| 2772 | return se; |
Peter Zijlstra | aa2ac25 | 2008-03-14 21:12:12 +0100 | [diff] [blame] | 2773 | } |
| 2774 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 2775 | static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
| 2776 | |
Ingo Molnar | ab6cde2 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 2777 | 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] | 2778 | { |
| 2779 | /* |
| 2780 | * If still on the runqueue then deactivate_task() |
| 2781 | * was not called and update_curr() has to be done: |
| 2782 | */ |
| 2783 | if (prev->on_rq) |
Ingo Molnar | b7cc089 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 2784 | update_curr(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2785 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 2786 | /* throttle cfs_rqs exceeding runtime */ |
| 2787 | check_cfs_rq_runtime(cfs_rq); |
| 2788 | |
Peter Zijlstra | ddc9729 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 2789 | check_spread(cfs_rq, prev); |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 2790 | if (prev->on_rq) { |
Ingo Molnar | 5870db5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 2791 | update_stats_wait_start(cfs_rq, prev); |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 2792 | /* Put 'current' back into the tree. */ |
| 2793 | __enqueue_entity(cfs_rq, prev); |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2794 | /* in !on_rq case, update occurred at dequeue */ |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2795 | update_entity_load_avg(prev, 1); |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 2796 | } |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 2797 | cfs_rq->curr = NULL; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2798 | } |
| 2799 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2800 | static void |
| 2801 | 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] | 2802 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2803 | /* |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 2804 | * Update run-time statistics of the 'current'. |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2805 | */ |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 2806 | update_curr(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2807 | |
Paul Turner | 43365bd | 2010-12-15 19:10:17 -0800 | [diff] [blame] | 2808 | /* |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2809 | * Ensure that runnable average is periodically updated. |
| 2810 | */ |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2811 | update_entity_load_avg(curr, 1); |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2812 | update_cfs_rq_blocked_load(cfs_rq, 1); |
Peter Zijlstra | bf0bd94 | 2013-07-26 23:48:42 +0200 | [diff] [blame] | 2813 | update_cfs_shares(cfs_rq); |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2814 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2815 | #ifdef CONFIG_SCHED_HRTICK |
| 2816 | /* |
| 2817 | * queued ticks are scheduled to match the slice, so don't bother |
| 2818 | * validating it and just reschedule. |
| 2819 | */ |
Harvey Harrison | 983ed7a | 2008-04-24 18:17:55 -0700 | [diff] [blame] | 2820 | if (queued) { |
| 2821 | resched_task(rq_of(cfs_rq)->curr); |
| 2822 | return; |
| 2823 | } |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2824 | /* |
| 2825 | * don't let the period tick interfere with the hrtick preemption |
| 2826 | */ |
| 2827 | if (!sched_feat(DOUBLE_TICK) && |
| 2828 | hrtimer_active(&rq_of(cfs_rq)->hrtick_timer)) |
| 2829 | return; |
| 2830 | #endif |
| 2831 | |
Yong Zhang | 2c2efae | 2011-07-29 16:20:33 +0800 | [diff] [blame] | 2832 | if (cfs_rq->nr_running > 1) |
Ingo Molnar | 2e09bf5 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 2833 | check_preempt_tick(cfs_rq, curr); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2834 | } |
| 2835 | |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 2836 | |
| 2837 | /************************************************** |
| 2838 | * CFS bandwidth control machinery |
| 2839 | */ |
| 2840 | |
| 2841 | #ifdef CONFIG_CFS_BANDWIDTH |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2842 | |
| 2843 | #ifdef HAVE_JUMP_LABEL |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 2844 | static struct static_key __cfs_bandwidth_used; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2845 | |
| 2846 | static inline bool cfs_bandwidth_used(void) |
| 2847 | { |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 2848 | return static_key_false(&__cfs_bandwidth_used); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2849 | } |
| 2850 | |
Ben Segall | 1ee14e6 | 2013-10-16 11:16:12 -0700 | [diff] [blame] | 2851 | void cfs_bandwidth_usage_inc(void) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2852 | { |
Ben Segall | 1ee14e6 | 2013-10-16 11:16:12 -0700 | [diff] [blame] | 2853 | static_key_slow_inc(&__cfs_bandwidth_used); |
| 2854 | } |
| 2855 | |
| 2856 | void cfs_bandwidth_usage_dec(void) |
| 2857 | { |
| 2858 | static_key_slow_dec(&__cfs_bandwidth_used); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2859 | } |
| 2860 | #else /* HAVE_JUMP_LABEL */ |
| 2861 | static bool cfs_bandwidth_used(void) |
| 2862 | { |
| 2863 | return true; |
| 2864 | } |
| 2865 | |
Ben Segall | 1ee14e6 | 2013-10-16 11:16:12 -0700 | [diff] [blame] | 2866 | void cfs_bandwidth_usage_inc(void) {} |
| 2867 | void cfs_bandwidth_usage_dec(void) {} |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2868 | #endif /* HAVE_JUMP_LABEL */ |
| 2869 | |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 2870 | /* |
| 2871 | * default period for cfs group bandwidth. |
| 2872 | * default: 0.1s, units: nanoseconds |
| 2873 | */ |
| 2874 | static inline u64 default_cfs_period(void) |
| 2875 | { |
| 2876 | return 100000000ULL; |
| 2877 | } |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 2878 | |
| 2879 | static inline u64 sched_cfs_bandwidth_slice(void) |
| 2880 | { |
| 2881 | return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC; |
| 2882 | } |
| 2883 | |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 2884 | /* |
| 2885 | * Replenish runtime according to assigned quota and update expiration time. |
| 2886 | * We use sched_clock_cpu directly instead of rq->clock to avoid adding |
| 2887 | * additional synchronization around rq->lock. |
| 2888 | * |
| 2889 | * requires cfs_b->lock |
| 2890 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2891 | void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b) |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 2892 | { |
| 2893 | u64 now; |
| 2894 | |
| 2895 | if (cfs_b->quota == RUNTIME_INF) |
| 2896 | return; |
| 2897 | |
| 2898 | now = sched_clock_cpu(smp_processor_id()); |
| 2899 | cfs_b->runtime = cfs_b->quota; |
| 2900 | cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period); |
| 2901 | } |
| 2902 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2903 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
| 2904 | { |
| 2905 | return &tg->cfs_bandwidth; |
| 2906 | } |
| 2907 | |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2908 | /* rq->task_clock normalized against any time this cfs_rq has spent throttled */ |
| 2909 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq) |
| 2910 | { |
| 2911 | if (unlikely(cfs_rq->throttle_count)) |
| 2912 | return cfs_rq->throttled_clock_task; |
| 2913 | |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 2914 | return rq_clock_task(rq_of(cfs_rq)) - cfs_rq->throttled_clock_task_time; |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2915 | } |
| 2916 | |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 2917 | /* returns 0 on failure to allocate runtime */ |
| 2918 | static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 2919 | { |
| 2920 | struct task_group *tg = cfs_rq->tg; |
| 2921 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg); |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 2922 | u64 amount = 0, min_amount, expires; |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 2923 | |
| 2924 | /* note: this is a positive sum as runtime_remaining <= 0 */ |
| 2925 | min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining; |
| 2926 | |
| 2927 | raw_spin_lock(&cfs_b->lock); |
| 2928 | if (cfs_b->quota == RUNTIME_INF) |
| 2929 | amount = min_amount; |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 2930 | else { |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 2931 | /* |
| 2932 | * If the bandwidth pool has become inactive, then at least one |
| 2933 | * period must have elapsed since the last consumption. |
| 2934 | * Refresh the global state and ensure bandwidth timer becomes |
| 2935 | * active. |
| 2936 | */ |
| 2937 | if (!cfs_b->timer_active) { |
| 2938 | __refill_cfs_bandwidth_runtime(cfs_b); |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 2939 | __start_cfs_bandwidth(cfs_b); |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 2940 | } |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 2941 | |
| 2942 | if (cfs_b->runtime > 0) { |
| 2943 | amount = min(cfs_b->runtime, min_amount); |
| 2944 | cfs_b->runtime -= amount; |
| 2945 | cfs_b->idle = 0; |
| 2946 | } |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 2947 | } |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 2948 | expires = cfs_b->runtime_expires; |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 2949 | raw_spin_unlock(&cfs_b->lock); |
| 2950 | |
| 2951 | cfs_rq->runtime_remaining += amount; |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 2952 | /* |
| 2953 | * we may have advanced our local expiration to account for allowed |
| 2954 | * spread between our sched_clock and the one on which runtime was |
| 2955 | * issued. |
| 2956 | */ |
| 2957 | if ((s64)(expires - cfs_rq->runtime_expires) > 0) |
| 2958 | cfs_rq->runtime_expires = expires; |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 2959 | |
| 2960 | return cfs_rq->runtime_remaining > 0; |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | /* |
| 2964 | * Note: This depends on the synchronization provided by sched_clock and the |
| 2965 | * fact that rq->clock snapshots this value. |
| 2966 | */ |
| 2967 | static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
| 2968 | { |
| 2969 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 2970 | |
| 2971 | /* if the deadline is ahead of our clock, nothing to do */ |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 2972 | if (likely((s64)(rq_clock(rq_of(cfs_rq)) - cfs_rq->runtime_expires) < 0)) |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 2973 | return; |
| 2974 | |
| 2975 | if (cfs_rq->runtime_remaining < 0) |
| 2976 | return; |
| 2977 | |
| 2978 | /* |
| 2979 | * If the local deadline has passed we have to consider the |
| 2980 | * possibility that our sched_clock is 'fast' and the global deadline |
| 2981 | * has not truly expired. |
| 2982 | * |
| 2983 | * Fortunately we can check determine whether this the case by checking |
| 2984 | * whether the global deadline has advanced. |
| 2985 | */ |
| 2986 | |
| 2987 | if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) { |
| 2988 | /* extend local deadline, drift is bounded above by 2 ticks */ |
| 2989 | cfs_rq->runtime_expires += TICK_NSEC; |
| 2990 | } else { |
| 2991 | /* global deadline is ahead, expiration has passed */ |
| 2992 | cfs_rq->runtime_remaining = 0; |
| 2993 | } |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 2994 | } |
| 2995 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 2996 | static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 2997 | { |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 2998 | /* dock delta_exec before expiring quota (as it could span periods) */ |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 2999 | cfs_rq->runtime_remaining -= delta_exec; |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 3000 | expire_cfs_rq_runtime(cfs_rq); |
| 3001 | |
| 3002 | if (likely(cfs_rq->runtime_remaining > 0)) |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 3003 | return; |
| 3004 | |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 3005 | /* |
| 3006 | * if we're unable to extend our runtime we resched so that the active |
| 3007 | * hierarchy can be throttled |
| 3008 | */ |
| 3009 | if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) |
| 3010 | resched_task(rq_of(cfs_rq)->curr); |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 3011 | } |
| 3012 | |
Peter Zijlstra | 6c16a6d | 2012-03-21 13:07:16 -0700 | [diff] [blame] | 3013 | static __always_inline |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 3014 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 3015 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 3016 | if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 3017 | return; |
| 3018 | |
| 3019 | __account_cfs_rq_runtime(cfs_rq, delta_exec); |
| 3020 | } |
| 3021 | |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 3022 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) |
| 3023 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 3024 | return cfs_bandwidth_used() && cfs_rq->throttled; |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 3025 | } |
| 3026 | |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 3027 | /* check whether cfs_rq, or any parent, is throttled */ |
| 3028 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) |
| 3029 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 3030 | return cfs_bandwidth_used() && cfs_rq->throttle_count; |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 3031 | } |
| 3032 | |
| 3033 | /* |
| 3034 | * Ensure that neither of the group entities corresponding to src_cpu or |
| 3035 | * dest_cpu are members of a throttled hierarchy when performing group |
| 3036 | * load-balance operations. |
| 3037 | */ |
| 3038 | static inline int throttled_lb_pair(struct task_group *tg, |
| 3039 | int src_cpu, int dest_cpu) |
| 3040 | { |
| 3041 | struct cfs_rq *src_cfs_rq, *dest_cfs_rq; |
| 3042 | |
| 3043 | src_cfs_rq = tg->cfs_rq[src_cpu]; |
| 3044 | dest_cfs_rq = tg->cfs_rq[dest_cpu]; |
| 3045 | |
| 3046 | return throttled_hierarchy(src_cfs_rq) || |
| 3047 | throttled_hierarchy(dest_cfs_rq); |
| 3048 | } |
| 3049 | |
| 3050 | /* updated child weight may affect parent so we have to do this bottom up */ |
| 3051 | static int tg_unthrottle_up(struct task_group *tg, void *data) |
| 3052 | { |
| 3053 | struct rq *rq = data; |
| 3054 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; |
| 3055 | |
| 3056 | cfs_rq->throttle_count--; |
| 3057 | #ifdef CONFIG_SMP |
| 3058 | if (!cfs_rq->throttle_count) { |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 3059 | /* adjust cfs_rq_clock_task() */ |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 3060 | cfs_rq->throttled_clock_task_time += rq_clock_task(rq) - |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 3061 | cfs_rq->throttled_clock_task; |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 3062 | } |
| 3063 | #endif |
| 3064 | |
| 3065 | return 0; |
| 3066 | } |
| 3067 | |
| 3068 | static int tg_throttle_down(struct task_group *tg, void *data) |
| 3069 | { |
| 3070 | struct rq *rq = data; |
| 3071 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; |
| 3072 | |
Paul Turner | 8295836 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 3073 | /* group is entering throttled state, stop time */ |
| 3074 | if (!cfs_rq->throttle_count) |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 3075 | cfs_rq->throttled_clock_task = rq_clock_task(rq); |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 3076 | cfs_rq->throttle_count++; |
| 3077 | |
| 3078 | return 0; |
| 3079 | } |
| 3080 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 3081 | static void throttle_cfs_rq(struct cfs_rq *cfs_rq) |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 3082 | { |
| 3083 | struct rq *rq = rq_of(cfs_rq); |
| 3084 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
| 3085 | struct sched_entity *se; |
| 3086 | long task_delta, dequeue = 1; |
| 3087 | |
| 3088 | se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; |
| 3089 | |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 3090 | /* freeze hierarchy runnable averages while throttled */ |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 3091 | rcu_read_lock(); |
| 3092 | walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); |
| 3093 | rcu_read_unlock(); |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 3094 | |
| 3095 | task_delta = cfs_rq->h_nr_running; |
| 3096 | for_each_sched_entity(se) { |
| 3097 | struct cfs_rq *qcfs_rq = cfs_rq_of(se); |
| 3098 | /* throttled entity or throttle-on-deactivate */ |
| 3099 | if (!se->on_rq) |
| 3100 | break; |
| 3101 | |
| 3102 | if (dequeue) |
| 3103 | dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP); |
| 3104 | qcfs_rq->h_nr_running -= task_delta; |
| 3105 | |
| 3106 | if (qcfs_rq->load.weight) |
| 3107 | dequeue = 0; |
| 3108 | } |
| 3109 | |
| 3110 | if (!se) |
| 3111 | rq->nr_running -= task_delta; |
| 3112 | |
| 3113 | cfs_rq->throttled = 1; |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 3114 | cfs_rq->throttled_clock = rq_clock(rq); |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 3115 | raw_spin_lock(&cfs_b->lock); |
| 3116 | list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq); |
Ben Segall | f9f9ffc | 2013-10-16 11:16:32 -0700 | [diff] [blame] | 3117 | if (!cfs_b->timer_active) |
| 3118 | __start_cfs_bandwidth(cfs_b); |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 3119 | raw_spin_unlock(&cfs_b->lock); |
| 3120 | } |
| 3121 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3122 | void unthrottle_cfs_rq(struct cfs_rq *cfs_rq) |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 3123 | { |
| 3124 | struct rq *rq = rq_of(cfs_rq); |
| 3125 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
| 3126 | struct sched_entity *se; |
| 3127 | int enqueue = 1; |
| 3128 | long task_delta; |
| 3129 | |
Michael Wang | 22b958d | 2013-06-04 14:23:39 +0800 | [diff] [blame] | 3130 | se = cfs_rq->tg->se[cpu_of(rq)]; |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 3131 | |
| 3132 | cfs_rq->throttled = 0; |
Frederic Weisbecker | 1a55af2 | 2013-04-12 01:51:01 +0200 | [diff] [blame] | 3133 | |
| 3134 | update_rq_clock(rq); |
| 3135 | |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 3136 | raw_spin_lock(&cfs_b->lock); |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 3137 | cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock; |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 3138 | list_del_rcu(&cfs_rq->throttled_list); |
| 3139 | raw_spin_unlock(&cfs_b->lock); |
| 3140 | |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 3141 | /* update hierarchical throttle state */ |
| 3142 | walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); |
| 3143 | |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 3144 | if (!cfs_rq->load.weight) |
| 3145 | return; |
| 3146 | |
| 3147 | task_delta = cfs_rq->h_nr_running; |
| 3148 | for_each_sched_entity(se) { |
| 3149 | if (se->on_rq) |
| 3150 | enqueue = 0; |
| 3151 | |
| 3152 | cfs_rq = cfs_rq_of(se); |
| 3153 | if (enqueue) |
| 3154 | enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP); |
| 3155 | cfs_rq->h_nr_running += task_delta; |
| 3156 | |
| 3157 | if (cfs_rq_throttled(cfs_rq)) |
| 3158 | break; |
| 3159 | } |
| 3160 | |
| 3161 | if (!se) |
| 3162 | rq->nr_running += task_delta; |
| 3163 | |
| 3164 | /* determine whether we need to wake up potentially idle cpu */ |
| 3165 | if (rq->curr == rq->idle && rq->cfs.nr_running) |
| 3166 | resched_task(rq->curr); |
| 3167 | } |
| 3168 | |
| 3169 | static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b, |
| 3170 | u64 remaining, u64 expires) |
| 3171 | { |
| 3172 | struct cfs_rq *cfs_rq; |
| 3173 | u64 runtime = remaining; |
| 3174 | |
| 3175 | rcu_read_lock(); |
| 3176 | list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, |
| 3177 | throttled_list) { |
| 3178 | struct rq *rq = rq_of(cfs_rq); |
| 3179 | |
| 3180 | raw_spin_lock(&rq->lock); |
| 3181 | if (!cfs_rq_throttled(cfs_rq)) |
| 3182 | goto next; |
| 3183 | |
| 3184 | runtime = -cfs_rq->runtime_remaining + 1; |
| 3185 | if (runtime > remaining) |
| 3186 | runtime = remaining; |
| 3187 | remaining -= runtime; |
| 3188 | |
| 3189 | cfs_rq->runtime_remaining += runtime; |
| 3190 | cfs_rq->runtime_expires = expires; |
| 3191 | |
| 3192 | /* we check whether we're throttled above */ |
| 3193 | if (cfs_rq->runtime_remaining > 0) |
| 3194 | unthrottle_cfs_rq(cfs_rq); |
| 3195 | |
| 3196 | next: |
| 3197 | raw_spin_unlock(&rq->lock); |
| 3198 | |
| 3199 | if (!remaining) |
| 3200 | break; |
| 3201 | } |
| 3202 | rcu_read_unlock(); |
| 3203 | |
| 3204 | return remaining; |
| 3205 | } |
| 3206 | |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 3207 | /* |
| 3208 | * Responsible for refilling a task_group's bandwidth and unthrottling its |
| 3209 | * cfs_rqs as appropriate. If there has been no activity within the last |
| 3210 | * period the timer is deactivated until scheduling resumes; cfs_b->idle is |
| 3211 | * used to track this state. |
| 3212 | */ |
| 3213 | static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun) |
| 3214 | { |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 3215 | u64 runtime, runtime_expires; |
| 3216 | int idle = 1, throttled; |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 3217 | |
| 3218 | raw_spin_lock(&cfs_b->lock); |
| 3219 | /* no need to continue the timer with no bandwidth constraint */ |
| 3220 | if (cfs_b->quota == RUNTIME_INF) |
| 3221 | goto out_unlock; |
| 3222 | |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 3223 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); |
| 3224 | /* idle depends on !throttled (for the case of a large deficit) */ |
| 3225 | idle = cfs_b->idle && !throttled; |
Nikhil Rao | e8da1b1 | 2011-07-21 09:43:40 -0700 | [diff] [blame] | 3226 | cfs_b->nr_periods += overrun; |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 3227 | |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 3228 | /* if we're going inactive then everything else can be deferred */ |
| 3229 | if (idle) |
| 3230 | goto out_unlock; |
| 3231 | |
Ben Segall | 927b54f | 2013-10-16 11:16:22 -0700 | [diff] [blame] | 3232 | /* |
| 3233 | * if we have relooped after returning idle once, we need to update our |
| 3234 | * status as actually running, so that other cpus doing |
| 3235 | * __start_cfs_bandwidth will stop trying to cancel us. |
| 3236 | */ |
| 3237 | cfs_b->timer_active = 1; |
| 3238 | |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 3239 | __refill_cfs_bandwidth_runtime(cfs_b); |
| 3240 | |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 3241 | if (!throttled) { |
| 3242 | /* mark as potentially idle for the upcoming period */ |
| 3243 | cfs_b->idle = 1; |
| 3244 | goto out_unlock; |
| 3245 | } |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 3246 | |
Nikhil Rao | e8da1b1 | 2011-07-21 09:43:40 -0700 | [diff] [blame] | 3247 | /* account preceding periods in which throttling occurred */ |
| 3248 | cfs_b->nr_throttled += overrun; |
| 3249 | |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 3250 | /* |
| 3251 | * There are throttled entities so we must first use the new bandwidth |
| 3252 | * to unthrottle them before making it generally available. This |
| 3253 | * ensures that all existing debts will be paid before a new cfs_rq is |
| 3254 | * allowed to run. |
| 3255 | */ |
| 3256 | runtime = cfs_b->runtime; |
| 3257 | runtime_expires = cfs_b->runtime_expires; |
| 3258 | cfs_b->runtime = 0; |
| 3259 | |
| 3260 | /* |
| 3261 | * This check is repeated as we are holding onto the new bandwidth |
| 3262 | * while we unthrottle. This can potentially race with an unthrottled |
| 3263 | * group trying to acquire new bandwidth from the global pool. |
| 3264 | */ |
| 3265 | while (throttled && runtime > 0) { |
| 3266 | raw_spin_unlock(&cfs_b->lock); |
| 3267 | /* we can't nest cfs_b->lock while distributing bandwidth */ |
| 3268 | runtime = distribute_cfs_runtime(cfs_b, runtime, |
| 3269 | runtime_expires); |
| 3270 | raw_spin_lock(&cfs_b->lock); |
| 3271 | |
| 3272 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); |
| 3273 | } |
| 3274 | |
| 3275 | /* return (any) remaining runtime */ |
| 3276 | cfs_b->runtime = runtime; |
| 3277 | /* |
| 3278 | * While we are ensured activity in the period following an |
| 3279 | * unthrottle, this also covers the case in which the new bandwidth is |
| 3280 | * insufficient to cover the existing bandwidth deficit. (Forcing the |
| 3281 | * timer to remain active while there are any throttled entities.) |
| 3282 | */ |
| 3283 | cfs_b->idle = 0; |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 3284 | out_unlock: |
| 3285 | if (idle) |
| 3286 | cfs_b->timer_active = 0; |
| 3287 | raw_spin_unlock(&cfs_b->lock); |
| 3288 | |
| 3289 | return idle; |
| 3290 | } |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 3291 | |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 3292 | /* a cfs_rq won't donate quota below this amount */ |
| 3293 | static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC; |
| 3294 | /* minimum remaining period time to redistribute slack quota */ |
| 3295 | static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC; |
| 3296 | /* how long we wait to gather additional slack before distributing */ |
| 3297 | static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC; |
| 3298 | |
Ben Segall | db06e78 | 2013-10-16 11:16:17 -0700 | [diff] [blame] | 3299 | /* |
| 3300 | * Are we near the end of the current quota period? |
| 3301 | * |
| 3302 | * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the |
| 3303 | * hrtimer base being cleared by __hrtimer_start_range_ns. In the case of |
| 3304 | * migrate_hrtimers, base is never cleared, so we are fine. |
| 3305 | */ |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 3306 | static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire) |
| 3307 | { |
| 3308 | struct hrtimer *refresh_timer = &cfs_b->period_timer; |
| 3309 | u64 remaining; |
| 3310 | |
| 3311 | /* if the call-back is running a quota refresh is already occurring */ |
| 3312 | if (hrtimer_callback_running(refresh_timer)) |
| 3313 | return 1; |
| 3314 | |
| 3315 | /* is a quota refresh about to occur? */ |
| 3316 | remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer)); |
| 3317 | if (remaining < min_expire) |
| 3318 | return 1; |
| 3319 | |
| 3320 | return 0; |
| 3321 | } |
| 3322 | |
| 3323 | static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b) |
| 3324 | { |
| 3325 | u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration; |
| 3326 | |
| 3327 | /* if there's a quota refresh soon don't bother with slack */ |
| 3328 | if (runtime_refresh_within(cfs_b, min_left)) |
| 3329 | return; |
| 3330 | |
| 3331 | start_bandwidth_timer(&cfs_b->slack_timer, |
| 3332 | ns_to_ktime(cfs_bandwidth_slack_period)); |
| 3333 | } |
| 3334 | |
| 3335 | /* we know any runtime found here is valid as update_curr() precedes return */ |
| 3336 | static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
| 3337 | { |
| 3338 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
| 3339 | s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; |
| 3340 | |
| 3341 | if (slack_runtime <= 0) |
| 3342 | return; |
| 3343 | |
| 3344 | raw_spin_lock(&cfs_b->lock); |
| 3345 | if (cfs_b->quota != RUNTIME_INF && |
| 3346 | cfs_rq->runtime_expires == cfs_b->runtime_expires) { |
| 3347 | cfs_b->runtime += slack_runtime; |
| 3348 | |
| 3349 | /* we are under rq->lock, defer unthrottling using a timer */ |
| 3350 | if (cfs_b->runtime > sched_cfs_bandwidth_slice() && |
| 3351 | !list_empty(&cfs_b->throttled_cfs_rq)) |
| 3352 | start_cfs_slack_bandwidth(cfs_b); |
| 3353 | } |
| 3354 | raw_spin_unlock(&cfs_b->lock); |
| 3355 | |
| 3356 | /* even if it's not valid for return we don't want to try again */ |
| 3357 | cfs_rq->runtime_remaining -= slack_runtime; |
| 3358 | } |
| 3359 | |
| 3360 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
| 3361 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 3362 | if (!cfs_bandwidth_used()) |
| 3363 | return; |
| 3364 | |
Paul Turner | fccfdc6 | 2011-11-07 20:26:34 -0800 | [diff] [blame] | 3365 | if (!cfs_rq->runtime_enabled || cfs_rq->nr_running) |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 3366 | return; |
| 3367 | |
| 3368 | __return_cfs_rq_runtime(cfs_rq); |
| 3369 | } |
| 3370 | |
| 3371 | /* |
| 3372 | * This is done with a timer (instead of inline with bandwidth return) since |
| 3373 | * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs. |
| 3374 | */ |
| 3375 | static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b) |
| 3376 | { |
| 3377 | u64 runtime = 0, slice = sched_cfs_bandwidth_slice(); |
| 3378 | u64 expires; |
| 3379 | |
| 3380 | /* confirm we're still not at a refresh boundary */ |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 3381 | raw_spin_lock(&cfs_b->lock); |
Ben Segall | db06e78 | 2013-10-16 11:16:17 -0700 | [diff] [blame] | 3382 | if (runtime_refresh_within(cfs_b, min_bandwidth_expiration)) { |
| 3383 | raw_spin_unlock(&cfs_b->lock); |
| 3384 | return; |
| 3385 | } |
| 3386 | |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 3387 | if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) { |
| 3388 | runtime = cfs_b->runtime; |
| 3389 | cfs_b->runtime = 0; |
| 3390 | } |
| 3391 | expires = cfs_b->runtime_expires; |
| 3392 | raw_spin_unlock(&cfs_b->lock); |
| 3393 | |
| 3394 | if (!runtime) |
| 3395 | return; |
| 3396 | |
| 3397 | runtime = distribute_cfs_runtime(cfs_b, runtime, expires); |
| 3398 | |
| 3399 | raw_spin_lock(&cfs_b->lock); |
| 3400 | if (expires == cfs_b->runtime_expires) |
| 3401 | cfs_b->runtime = runtime; |
| 3402 | raw_spin_unlock(&cfs_b->lock); |
| 3403 | } |
| 3404 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 3405 | /* |
| 3406 | * When a group wakes up we want to make sure that its quota is not already |
| 3407 | * expired/exceeded, otherwise it may be allowed to steal additional ticks of |
| 3408 | * runtime as update_curr() throttling can not not trigger until it's on-rq. |
| 3409 | */ |
| 3410 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) |
| 3411 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 3412 | if (!cfs_bandwidth_used()) |
| 3413 | return; |
| 3414 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 3415 | /* an active group must be handled by the update_curr()->put() path */ |
| 3416 | if (!cfs_rq->runtime_enabled || cfs_rq->curr) |
| 3417 | return; |
| 3418 | |
| 3419 | /* ensure the group is not already throttled */ |
| 3420 | if (cfs_rq_throttled(cfs_rq)) |
| 3421 | return; |
| 3422 | |
| 3423 | /* update runtime allocation */ |
| 3424 | account_cfs_rq_runtime(cfs_rq, 0); |
| 3425 | if (cfs_rq->runtime_remaining <= 0) |
| 3426 | throttle_cfs_rq(cfs_rq); |
| 3427 | } |
| 3428 | |
| 3429 | /* conditionally throttle active cfs_rq's from put_prev_entity() */ |
| 3430 | static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
| 3431 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 3432 | if (!cfs_bandwidth_used()) |
| 3433 | return; |
| 3434 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 3435 | if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) |
| 3436 | return; |
| 3437 | |
| 3438 | /* |
| 3439 | * it's possible for a throttled entity to be forced into a running |
| 3440 | * state (e.g. set_curr_task), in this case we're finished. |
| 3441 | */ |
| 3442 | if (cfs_rq_throttled(cfs_rq)) |
| 3443 | return; |
| 3444 | |
| 3445 | throttle_cfs_rq(cfs_rq); |
| 3446 | } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3447 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3448 | static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer) |
| 3449 | { |
| 3450 | struct cfs_bandwidth *cfs_b = |
| 3451 | container_of(timer, struct cfs_bandwidth, slack_timer); |
| 3452 | do_sched_cfs_slack_timer(cfs_b); |
| 3453 | |
| 3454 | return HRTIMER_NORESTART; |
| 3455 | } |
| 3456 | |
| 3457 | static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer) |
| 3458 | { |
| 3459 | struct cfs_bandwidth *cfs_b = |
| 3460 | container_of(timer, struct cfs_bandwidth, period_timer); |
| 3461 | ktime_t now; |
| 3462 | int overrun; |
| 3463 | int idle = 0; |
| 3464 | |
| 3465 | for (;;) { |
| 3466 | now = hrtimer_cb_get_time(timer); |
| 3467 | overrun = hrtimer_forward(timer, now, cfs_b->period); |
| 3468 | |
| 3469 | if (!overrun) |
| 3470 | break; |
| 3471 | |
| 3472 | idle = do_sched_cfs_period_timer(cfs_b, overrun); |
| 3473 | } |
| 3474 | |
| 3475 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; |
| 3476 | } |
| 3477 | |
| 3478 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) |
| 3479 | { |
| 3480 | raw_spin_lock_init(&cfs_b->lock); |
| 3481 | cfs_b->runtime = 0; |
| 3482 | cfs_b->quota = RUNTIME_INF; |
| 3483 | cfs_b->period = ns_to_ktime(default_cfs_period()); |
| 3484 | |
| 3485 | INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); |
| 3486 | hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 3487 | cfs_b->period_timer.function = sched_cfs_period_timer; |
| 3488 | hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 3489 | cfs_b->slack_timer.function = sched_cfs_slack_timer; |
| 3490 | } |
| 3491 | |
| 3492 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
| 3493 | { |
| 3494 | cfs_rq->runtime_enabled = 0; |
| 3495 | INIT_LIST_HEAD(&cfs_rq->throttled_list); |
| 3496 | } |
| 3497 | |
| 3498 | /* requires cfs_b->lock, may release to reprogram timer */ |
| 3499 | void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b) |
| 3500 | { |
| 3501 | /* |
| 3502 | * The timer may be active because we're trying to set a new bandwidth |
| 3503 | * period or because we're racing with the tear-down path |
| 3504 | * (timer_active==0 becomes visible before the hrtimer call-back |
| 3505 | * terminates). In either case we ensure that it's re-programmed |
| 3506 | */ |
Ben Segall | 927b54f | 2013-10-16 11:16:22 -0700 | [diff] [blame] | 3507 | while (unlikely(hrtimer_active(&cfs_b->period_timer)) && |
| 3508 | hrtimer_try_to_cancel(&cfs_b->period_timer) < 0) { |
| 3509 | /* bounce the lock to allow do_sched_cfs_period_timer to run */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3510 | raw_spin_unlock(&cfs_b->lock); |
Ben Segall | 927b54f | 2013-10-16 11:16:22 -0700 | [diff] [blame] | 3511 | cpu_relax(); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3512 | raw_spin_lock(&cfs_b->lock); |
| 3513 | /* if someone else restarted the timer then we're done */ |
| 3514 | if (cfs_b->timer_active) |
| 3515 | return; |
| 3516 | } |
| 3517 | |
| 3518 | cfs_b->timer_active = 1; |
| 3519 | start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period); |
| 3520 | } |
| 3521 | |
| 3522 | static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) |
| 3523 | { |
| 3524 | hrtimer_cancel(&cfs_b->period_timer); |
| 3525 | hrtimer_cancel(&cfs_b->slack_timer); |
| 3526 | } |
| 3527 | |
Arnd Bergmann | 38dc334 | 2013-01-25 14:14:22 +0000 | [diff] [blame] | 3528 | static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3529 | { |
| 3530 | struct cfs_rq *cfs_rq; |
| 3531 | |
| 3532 | for_each_leaf_cfs_rq(rq, cfs_rq) { |
| 3533 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
| 3534 | |
| 3535 | if (!cfs_rq->runtime_enabled) |
| 3536 | continue; |
| 3537 | |
| 3538 | /* |
| 3539 | * clock_task is not advancing so we just need to make sure |
| 3540 | * there's some valid quota amount |
| 3541 | */ |
| 3542 | cfs_rq->runtime_remaining = cfs_b->quota; |
| 3543 | if (cfs_rq_throttled(cfs_rq)) |
| 3544 | unthrottle_cfs_rq(cfs_rq); |
| 3545 | } |
| 3546 | } |
| 3547 | |
| 3548 | #else /* CONFIG_CFS_BANDWIDTH */ |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 3549 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq) |
| 3550 | { |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 3551 | return rq_clock_task(rq_of(cfs_rq)); |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 3552 | } |
| 3553 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 3554 | static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) {} |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 3555 | static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} |
| 3556 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {} |
Peter Zijlstra | 6c16a6d | 2012-03-21 13:07:16 -0700 | [diff] [blame] | 3557 | 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] | 3558 | |
| 3559 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) |
| 3560 | { |
| 3561 | return 0; |
| 3562 | } |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 3563 | |
| 3564 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) |
| 3565 | { |
| 3566 | return 0; |
| 3567 | } |
| 3568 | |
| 3569 | static inline int throttled_lb_pair(struct task_group *tg, |
| 3570 | int src_cpu, int dest_cpu) |
| 3571 | { |
| 3572 | return 0; |
| 3573 | } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3574 | |
| 3575 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} |
| 3576 | |
| 3577 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 3578 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 3579 | #endif |
| 3580 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3581 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
| 3582 | { |
| 3583 | return NULL; |
| 3584 | } |
| 3585 | static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} |
Peter Boonstoppel | a4c96ae | 2012-08-09 15:34:47 -0700 | [diff] [blame] | 3586 | static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {} |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3587 | |
| 3588 | #endif /* CONFIG_CFS_BANDWIDTH */ |
| 3589 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3590 | /************************************************** |
| 3591 | * CFS operations on tasks: |
| 3592 | */ |
| 3593 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3594 | #ifdef CONFIG_SCHED_HRTICK |
| 3595 | static void hrtick_start_fair(struct rq *rq, struct task_struct *p) |
| 3596 | { |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3597 | struct sched_entity *se = &p->se; |
| 3598 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 3599 | |
| 3600 | WARN_ON(task_rq(p) != rq); |
| 3601 | |
Mike Galbraith | b39e66e | 2011-11-22 15:20:07 +0100 | [diff] [blame] | 3602 | if (cfs_rq->nr_running > 1) { |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3603 | u64 slice = sched_slice(cfs_rq, se); |
| 3604 | u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; |
| 3605 | s64 delta = slice - ran; |
| 3606 | |
| 3607 | if (delta < 0) { |
| 3608 | if (rq->curr == p) |
| 3609 | resched_task(p); |
| 3610 | return; |
| 3611 | } |
| 3612 | |
| 3613 | /* |
| 3614 | * Don't schedule slices shorter than 10000ns, that just |
| 3615 | * doesn't make sense. Rely on vruntime for fairness. |
| 3616 | */ |
Peter Zijlstra | 3165651 | 2008-07-18 18:01:23 +0200 | [diff] [blame] | 3617 | if (rq->curr != p) |
Peter Zijlstra | 157124c | 2008-07-28 11:53:11 +0200 | [diff] [blame] | 3618 | delta = max_t(s64, 10000LL, delta); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3619 | |
Peter Zijlstra | 3165651 | 2008-07-18 18:01:23 +0200 | [diff] [blame] | 3620 | hrtick_start(rq, delta); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3621 | } |
| 3622 | } |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 3623 | |
| 3624 | /* |
| 3625 | * called from enqueue/dequeue and updates the hrtick when the |
| 3626 | * current task is from our class and nr_running is low enough |
| 3627 | * to matter. |
| 3628 | */ |
| 3629 | static void hrtick_update(struct rq *rq) |
| 3630 | { |
| 3631 | struct task_struct *curr = rq->curr; |
| 3632 | |
Mike Galbraith | b39e66e | 2011-11-22 15:20:07 +0100 | [diff] [blame] | 3633 | if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class) |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 3634 | return; |
| 3635 | |
| 3636 | if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency) |
| 3637 | hrtick_start_fair(rq, curr); |
| 3638 | } |
Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 3639 | #else /* !CONFIG_SCHED_HRTICK */ |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3640 | static inline void |
| 3641 | hrtick_start_fair(struct rq *rq, struct task_struct *p) |
| 3642 | { |
| 3643 | } |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 3644 | |
| 3645 | static inline void hrtick_update(struct rq *rq) |
| 3646 | { |
| 3647 | } |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3648 | #endif |
| 3649 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3650 | /* |
| 3651 | * The enqueue_task method is called before nr_running is |
| 3652 | * increased. Here we update the fair scheduling stats and |
| 3653 | * then put the task into the rbtree: |
| 3654 | */ |
Thomas Gleixner | ea87bb7 | 2010-01-20 20:58:57 +0000 | [diff] [blame] | 3655 | static void |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 3656 | enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3657 | { |
| 3658 | struct cfs_rq *cfs_rq; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 3659 | struct sched_entity *se = &p->se; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3660 | |
| 3661 | for_each_sched_entity(se) { |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 3662 | if (se->on_rq) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3663 | break; |
| 3664 | cfs_rq = cfs_rq_of(se); |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 3665 | enqueue_entity(cfs_rq, se, flags); |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 3666 | |
| 3667 | /* |
| 3668 | * end evaluation on encountering a throttled cfs_rq |
| 3669 | * |
| 3670 | * note: in the case of encountering a throttled cfs_rq we will |
| 3671 | * post the final h_nr_running increment below. |
| 3672 | */ |
| 3673 | if (cfs_rq_throttled(cfs_rq)) |
| 3674 | break; |
Paul Turner | 953bfcd | 2011-07-21 09:43:27 -0700 | [diff] [blame] | 3675 | cfs_rq->h_nr_running++; |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 3676 | |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 3677 | flags = ENQUEUE_WAKEUP; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3678 | } |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3679 | |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 3680 | for_each_sched_entity(se) { |
Lin Ming | 0f31714 | 2011-07-22 09:14:31 +0800 | [diff] [blame] | 3681 | cfs_rq = cfs_rq_of(se); |
Paul Turner | 953bfcd | 2011-07-21 09:43:27 -0700 | [diff] [blame] | 3682 | cfs_rq->h_nr_running++; |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 3683 | |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 3684 | if (cfs_rq_throttled(cfs_rq)) |
| 3685 | break; |
| 3686 | |
Linus Torvalds | 17bc14b | 2012-12-14 07:20:43 -0800 | [diff] [blame] | 3687 | update_cfs_shares(cfs_rq); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 3688 | update_entity_load_avg(se, 1); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 3689 | } |
| 3690 | |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 3691 | if (!se) { |
| 3692 | update_rq_runnable_avg(rq, rq->nr_running); |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 3693 | inc_nr_running(rq); |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 3694 | } |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 3695 | hrtick_update(rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3696 | } |
| 3697 | |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 3698 | static void set_next_buddy(struct sched_entity *se); |
| 3699 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3700 | /* |
| 3701 | * The dequeue_task method is called before nr_running is |
| 3702 | * decreased. We remove the task from the rbtree and |
| 3703 | * update the fair scheduling stats: |
| 3704 | */ |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 3705 | 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] | 3706 | { |
| 3707 | struct cfs_rq *cfs_rq; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 3708 | struct sched_entity *se = &p->se; |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 3709 | int task_sleep = flags & DEQUEUE_SLEEP; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3710 | |
| 3711 | for_each_sched_entity(se) { |
| 3712 | cfs_rq = cfs_rq_of(se); |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 3713 | dequeue_entity(cfs_rq, se, flags); |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 3714 | |
| 3715 | /* |
| 3716 | * end evaluation on encountering a throttled cfs_rq |
| 3717 | * |
| 3718 | * note: in the case of encountering a throttled cfs_rq we will |
| 3719 | * post the final h_nr_running decrement below. |
| 3720 | */ |
| 3721 | if (cfs_rq_throttled(cfs_rq)) |
| 3722 | break; |
Paul Turner | 953bfcd | 2011-07-21 09:43:27 -0700 | [diff] [blame] | 3723 | cfs_rq->h_nr_running--; |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 3724 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3725 | /* Don't dequeue parent if it has other entities besides us */ |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 3726 | if (cfs_rq->load.weight) { |
| 3727 | /* |
| 3728 | * Bias pick_next to pick a task from this cfs_rq, as |
| 3729 | * p is sleeping when it is within its sched_slice. |
| 3730 | */ |
| 3731 | if (task_sleep && parent_entity(se)) |
| 3732 | set_next_buddy(parent_entity(se)); |
Paul Turner | 9598c82 | 2011-07-06 22:30:37 -0700 | [diff] [blame] | 3733 | |
| 3734 | /* avoid re-evaluating load for this entity */ |
| 3735 | se = parent_entity(se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3736 | break; |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 3737 | } |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 3738 | flags |= DEQUEUE_SLEEP; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3739 | } |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3740 | |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 3741 | for_each_sched_entity(se) { |
Lin Ming | 0f31714 | 2011-07-22 09:14:31 +0800 | [diff] [blame] | 3742 | cfs_rq = cfs_rq_of(se); |
Paul Turner | 953bfcd | 2011-07-21 09:43:27 -0700 | [diff] [blame] | 3743 | cfs_rq->h_nr_running--; |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 3744 | |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 3745 | if (cfs_rq_throttled(cfs_rq)) |
| 3746 | break; |
| 3747 | |
Linus Torvalds | 17bc14b | 2012-12-14 07:20:43 -0800 | [diff] [blame] | 3748 | update_cfs_shares(cfs_rq); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 3749 | update_entity_load_avg(se, 1); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 3750 | } |
| 3751 | |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 3752 | if (!se) { |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 3753 | dec_nr_running(rq); |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 3754 | update_rq_runnable_avg(rq, 1); |
| 3755 | } |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 3756 | hrtick_update(rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3757 | } |
| 3758 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 3759 | #ifdef CONFIG_SMP |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3760 | /* Used instead of source_load when we know the type == 0 */ |
| 3761 | static unsigned long weighted_cpuload(const int cpu) |
| 3762 | { |
Alex Shi | b92486c | 2013-06-20 10:18:50 +0800 | [diff] [blame] | 3763 | return cpu_rq(cpu)->cfs.runnable_load_avg; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3764 | } |
| 3765 | |
| 3766 | /* |
| 3767 | * Return a low guess at the load of a migration-source cpu weighted |
| 3768 | * according to the scheduling class and "nice" value. |
| 3769 | * |
| 3770 | * We want to under-estimate the load of migration sources, to |
| 3771 | * balance conservatively. |
| 3772 | */ |
| 3773 | static unsigned long source_load(int cpu, int type) |
| 3774 | { |
| 3775 | struct rq *rq = cpu_rq(cpu); |
| 3776 | unsigned long total = weighted_cpuload(cpu); |
| 3777 | |
| 3778 | if (type == 0 || !sched_feat(LB_BIAS)) |
| 3779 | return total; |
| 3780 | |
| 3781 | return min(rq->cpu_load[type-1], total); |
| 3782 | } |
| 3783 | |
| 3784 | /* |
| 3785 | * Return a high guess at the load of a migration-target cpu weighted |
| 3786 | * according to the scheduling class and "nice" value. |
| 3787 | */ |
| 3788 | static unsigned long target_load(int cpu, int type) |
| 3789 | { |
| 3790 | struct rq *rq = cpu_rq(cpu); |
| 3791 | unsigned long total = weighted_cpuload(cpu); |
| 3792 | |
| 3793 | if (type == 0 || !sched_feat(LB_BIAS)) |
| 3794 | return total; |
| 3795 | |
| 3796 | return max(rq->cpu_load[type-1], total); |
| 3797 | } |
| 3798 | |
| 3799 | static unsigned long power_of(int cpu) |
| 3800 | { |
| 3801 | return cpu_rq(cpu)->cpu_power; |
| 3802 | } |
| 3803 | |
| 3804 | static unsigned long cpu_avg_load_per_task(int cpu) |
| 3805 | { |
| 3806 | struct rq *rq = cpu_rq(cpu); |
| 3807 | unsigned long nr_running = ACCESS_ONCE(rq->nr_running); |
Alex Shi | b92486c | 2013-06-20 10:18:50 +0800 | [diff] [blame] | 3808 | unsigned long load_avg = rq->cfs.runnable_load_avg; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3809 | |
| 3810 | if (nr_running) |
Alex Shi | b92486c | 2013-06-20 10:18:50 +0800 | [diff] [blame] | 3811 | return load_avg / nr_running; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3812 | |
| 3813 | return 0; |
| 3814 | } |
| 3815 | |
Michael Wang | 6247041 | 2013-07-04 12:55:51 +0800 | [diff] [blame] | 3816 | static void record_wakee(struct task_struct *p) |
| 3817 | { |
| 3818 | /* |
| 3819 | * Rough decay (wiping) for cost saving, don't worry |
| 3820 | * about the boundary, really active task won't care |
| 3821 | * about the loss. |
| 3822 | */ |
| 3823 | if (jiffies > current->wakee_flip_decay_ts + HZ) { |
| 3824 | current->wakee_flips = 0; |
| 3825 | current->wakee_flip_decay_ts = jiffies; |
| 3826 | } |
| 3827 | |
| 3828 | if (current->last_wakee != p) { |
| 3829 | current->last_wakee = p; |
| 3830 | current->wakee_flips++; |
| 3831 | } |
| 3832 | } |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 3833 | |
Peter Zijlstra | 74f8e4b | 2011-04-05 17:23:47 +0200 | [diff] [blame] | 3834 | static void task_waking_fair(struct task_struct *p) |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 3835 | { |
| 3836 | struct sched_entity *se = &p->se; |
| 3837 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
Peter Zijlstra | 3fe1698 | 2011-04-05 17:23:48 +0200 | [diff] [blame] | 3838 | u64 min_vruntime; |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 3839 | |
Peter Zijlstra | 3fe1698 | 2011-04-05 17:23:48 +0200 | [diff] [blame] | 3840 | #ifndef CONFIG_64BIT |
| 3841 | u64 min_vruntime_copy; |
Peter Zijlstra | 74f8e4b | 2011-04-05 17:23:47 +0200 | [diff] [blame] | 3842 | |
Peter Zijlstra | 3fe1698 | 2011-04-05 17:23:48 +0200 | [diff] [blame] | 3843 | do { |
| 3844 | min_vruntime_copy = cfs_rq->min_vruntime_copy; |
| 3845 | smp_rmb(); |
| 3846 | min_vruntime = cfs_rq->min_vruntime; |
| 3847 | } while (min_vruntime != min_vruntime_copy); |
| 3848 | #else |
| 3849 | min_vruntime = cfs_rq->min_vruntime; |
| 3850 | #endif |
| 3851 | |
| 3852 | se->vruntime -= min_vruntime; |
Michael Wang | 6247041 | 2013-07-04 12:55:51 +0800 | [diff] [blame] | 3853 | record_wakee(p); |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 3854 | } |
| 3855 | |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 3856 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | f5bfb7d | 2008-06-27 13:41:39 +0200 | [diff] [blame] | 3857 | /* |
| 3858 | * effective_load() calculates the load change as seen from the root_task_group |
| 3859 | * |
| 3860 | * Adding load to a group doesn't make a group heavier, but can cause movement |
| 3861 | * of group shares between cpus. Assuming the shares were perfectly aligned one |
| 3862 | * can calculate the shift in shares. |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 3863 | * |
| 3864 | * Calculate the effective load difference if @wl is added (subtracted) to @tg |
| 3865 | * on this @cpu and results in a total addition (subtraction) of @wg to the |
| 3866 | * total group weight. |
| 3867 | * |
| 3868 | * Given a runqueue weight distribution (rw_i) we can compute a shares |
| 3869 | * distribution (s_i) using: |
| 3870 | * |
| 3871 | * s_i = rw_i / \Sum rw_j (1) |
| 3872 | * |
| 3873 | * Suppose we have 4 CPUs and our @tg is a direct child of the root group and |
| 3874 | * has 7 equal weight tasks, distributed as below (rw_i), with the resulting |
| 3875 | * shares distribution (s_i): |
| 3876 | * |
| 3877 | * rw_i = { 2, 4, 1, 0 } |
| 3878 | * s_i = { 2/7, 4/7, 1/7, 0 } |
| 3879 | * |
| 3880 | * As per wake_affine() we're interested in the load of two CPUs (the CPU the |
| 3881 | * task used to run on and the CPU the waker is running on), we need to |
| 3882 | * compute the effect of waking a task on either CPU and, in case of a sync |
| 3883 | * wakeup, compute the effect of the current task going to sleep. |
| 3884 | * |
| 3885 | * So for a change of @wl to the local @cpu with an overall group weight change |
| 3886 | * of @wl we can compute the new shares distribution (s'_i) using: |
| 3887 | * |
| 3888 | * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2) |
| 3889 | * |
| 3890 | * Suppose we're interested in CPUs 0 and 1, and want to compute the load |
| 3891 | * differences in waking a task to CPU 0. The additional task changes the |
| 3892 | * weight and shares distributions like: |
| 3893 | * |
| 3894 | * rw'_i = { 3, 4, 1, 0 } |
| 3895 | * s'_i = { 3/8, 4/8, 1/8, 0 } |
| 3896 | * |
| 3897 | * We can then compute the difference in effective weight by using: |
| 3898 | * |
| 3899 | * dw_i = S * (s'_i - s_i) (3) |
| 3900 | * |
| 3901 | * Where 'S' is the group weight as seen by its parent. |
| 3902 | * |
| 3903 | * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7) |
| 3904 | * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 - |
| 3905 | * 4/7) times the weight of the group. |
Peter Zijlstra | f5bfb7d | 2008-06-27 13:41:39 +0200 | [diff] [blame] | 3906 | */ |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 3907 | 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] | 3908 | { |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 3909 | struct sched_entity *se = tg->se[cpu]; |
Peter Zijlstra | f1d239f | 2008-06-27 13:41:38 +0200 | [diff] [blame] | 3910 | |
Rik van Riel | 9722c2d | 2014-01-06 11:39:12 +0000 | [diff] [blame] | 3911 | if (!tg->parent) /* the trivial, non-cgroup case */ |
Peter Zijlstra | f1d239f | 2008-06-27 13:41:38 +0200 | [diff] [blame] | 3912 | return wl; |
| 3913 | |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 3914 | for_each_sched_entity(se) { |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 3915 | long w, W; |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 3916 | |
Paul Turner | 977dda7 | 2011-01-14 17:57:50 -0800 | [diff] [blame] | 3917 | tg = se->my_q->tg; |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 3918 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 3919 | /* |
| 3920 | * W = @wg + \Sum rw_j |
| 3921 | */ |
| 3922 | W = wg + calc_tg_weight(tg, se->my_q); |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 3923 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 3924 | /* |
| 3925 | * w = rw_i + @wl |
| 3926 | */ |
| 3927 | w = se->my_q->load.weight + wl; |
Peter Zijlstra | 940959e | 2008-09-23 15:33:42 +0200 | [diff] [blame] | 3928 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 3929 | /* |
| 3930 | * wl = S * s'_i; see (2) |
| 3931 | */ |
| 3932 | if (W > 0 && w < W) |
| 3933 | wl = (w * tg->shares) / W; |
Paul Turner | 977dda7 | 2011-01-14 17:57:50 -0800 | [diff] [blame] | 3934 | else |
| 3935 | wl = tg->shares; |
Peter Zijlstra | 940959e | 2008-09-23 15:33:42 +0200 | [diff] [blame] | 3936 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 3937 | /* |
| 3938 | * Per the above, wl is the new se->load.weight value; since |
| 3939 | * those are clipped to [MIN_SHARES, ...) do so now. See |
| 3940 | * calc_cfs_shares(). |
| 3941 | */ |
Paul Turner | 977dda7 | 2011-01-14 17:57:50 -0800 | [diff] [blame] | 3942 | if (wl < MIN_SHARES) |
| 3943 | wl = MIN_SHARES; |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 3944 | |
| 3945 | /* |
| 3946 | * wl = dw_i = S * (s'_i - s_i); see (3) |
| 3947 | */ |
Paul Turner | 977dda7 | 2011-01-14 17:57:50 -0800 | [diff] [blame] | 3948 | wl -= se->load.weight; |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 3949 | |
| 3950 | /* |
| 3951 | * Recursively apply this logic to all parent groups to compute |
| 3952 | * the final effective load change on the root group. Since |
| 3953 | * only the @tg group gets extra weight, all parent groups can |
| 3954 | * only redistribute existing shares. @wl is the shift in shares |
| 3955 | * resulting from this level per the above. |
| 3956 | */ |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 3957 | wg = 0; |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 3958 | } |
| 3959 | |
| 3960 | return wl; |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 3961 | } |
| 3962 | #else |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 3963 | |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 3964 | static long effective_load(struct task_group *tg, int cpu, long wl, long wg) |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 3965 | { |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 3966 | return wl; |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 3967 | } |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 3968 | |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 3969 | #endif |
| 3970 | |
Michael Wang | 6247041 | 2013-07-04 12:55:51 +0800 | [diff] [blame] | 3971 | static int wake_wide(struct task_struct *p) |
| 3972 | { |
Peter Zijlstra | 7d9ffa8 | 2013-07-04 12:56:46 +0800 | [diff] [blame] | 3973 | int factor = this_cpu_read(sd_llc_size); |
Michael Wang | 6247041 | 2013-07-04 12:55:51 +0800 | [diff] [blame] | 3974 | |
| 3975 | /* |
| 3976 | * Yeah, it's the switching-frequency, could means many wakee or |
| 3977 | * rapidly switch, use factor here will just help to automatically |
| 3978 | * adjust the loose-degree, so bigger node will lead to more pull. |
| 3979 | */ |
| 3980 | if (p->wakee_flips > factor) { |
| 3981 | /* |
| 3982 | * wakee is somewhat hot, it needs certain amount of cpu |
| 3983 | * resource, so if waker is far more hot, prefer to leave |
| 3984 | * it alone. |
| 3985 | */ |
| 3986 | if (current->wakee_flips > (factor * p->wakee_flips)) |
| 3987 | return 1; |
| 3988 | } |
| 3989 | |
| 3990 | return 0; |
| 3991 | } |
| 3992 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 3993 | 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] | 3994 | { |
Paul Turner | e37b6a7 | 2011-01-21 20:44:59 -0800 | [diff] [blame] | 3995 | s64 this_load, load; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 3996 | int idx, this_cpu, prev_cpu; |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 3997 | unsigned long tl_per_task; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 3998 | struct task_group *tg; |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 3999 | unsigned long weight; |
Mike Galbraith | b3137bc | 2008-05-29 11:11:41 +0200 | [diff] [blame] | 4000 | int balanced; |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 4001 | |
Michael Wang | 6247041 | 2013-07-04 12:55:51 +0800 | [diff] [blame] | 4002 | /* |
| 4003 | * If we wake multiple tasks be careful to not bounce |
| 4004 | * ourselves around too much. |
| 4005 | */ |
| 4006 | if (wake_wide(p)) |
| 4007 | return 0; |
| 4008 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 4009 | idx = sd->wake_idx; |
| 4010 | this_cpu = smp_processor_id(); |
| 4011 | prev_cpu = task_cpu(p); |
| 4012 | load = source_load(prev_cpu, idx); |
| 4013 | this_load = target_load(this_cpu, idx); |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 4014 | |
| 4015 | /* |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 4016 | * If sync wakeup then subtract the (maximum possible) |
| 4017 | * effect of the currently running task from the load |
| 4018 | * of the current CPU: |
| 4019 | */ |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 4020 | if (sync) { |
| 4021 | tg = task_group(current); |
| 4022 | weight = current->se.load.weight; |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 4023 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 4024 | this_load += effective_load(tg, this_cpu, -weight, -weight); |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 4025 | load += effective_load(tg, prev_cpu, 0, -weight); |
| 4026 | } |
| 4027 | |
| 4028 | tg = task_group(p); |
| 4029 | weight = p->se.load.weight; |
| 4030 | |
Peter Zijlstra | 71a29aa | 2009-09-07 18:28:05 +0200 | [diff] [blame] | 4031 | /* |
| 4032 | * 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] | 4033 | * due to the sync cause above having dropped this_load to 0, we'll |
| 4034 | * always have an imbalance, but there's really nothing you can do |
| 4035 | * about that, so that's good too. |
Peter Zijlstra | 71a29aa | 2009-09-07 18:28:05 +0200 | [diff] [blame] | 4036 | * |
| 4037 | * Otherwise check if either cpus are near enough in load to allow this |
| 4038 | * task to be woken on this_cpu. |
| 4039 | */ |
Paul Turner | e37b6a7 | 2011-01-21 20:44:59 -0800 | [diff] [blame] | 4040 | if (this_load > 0) { |
| 4041 | s64 this_eff_load, prev_eff_load; |
Peter Zijlstra | e51fd5e | 2010-05-31 12:37:30 +0200 | [diff] [blame] | 4042 | |
| 4043 | this_eff_load = 100; |
| 4044 | this_eff_load *= power_of(prev_cpu); |
| 4045 | this_eff_load *= this_load + |
| 4046 | effective_load(tg, this_cpu, weight, weight); |
| 4047 | |
| 4048 | prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2; |
| 4049 | prev_eff_load *= power_of(this_cpu); |
| 4050 | prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight); |
| 4051 | |
| 4052 | balanced = this_eff_load <= prev_eff_load; |
| 4053 | } else |
| 4054 | balanced = true; |
Mike Galbraith | b3137bc | 2008-05-29 11:11:41 +0200 | [diff] [blame] | 4055 | |
| 4056 | /* |
| 4057 | * If the currently running task will sleep within |
| 4058 | * a reasonable amount of time then attract this newly |
| 4059 | * woken task: |
| 4060 | */ |
Peter Zijlstra | 2fb7635 | 2008-10-08 09:16:04 +0200 | [diff] [blame] | 4061 | if (sync && balanced) |
| 4062 | return 1; |
Mike Galbraith | b3137bc | 2008-05-29 11:11:41 +0200 | [diff] [blame] | 4063 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 4064 | schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts); |
Mike Galbraith | b3137bc | 2008-05-29 11:11:41 +0200 | [diff] [blame] | 4065 | tl_per_task = cpu_avg_load_per_task(this_cpu); |
| 4066 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 4067 | if (balanced || |
| 4068 | (this_load <= load && |
| 4069 | this_load + target_load(prev_cpu, idx) <= tl_per_task)) { |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 4070 | /* |
| 4071 | * This domain has SD_WAKE_AFFINE and |
| 4072 | * p is cache cold in this domain, and |
| 4073 | * there is no bad imbalance. |
| 4074 | */ |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 4075 | schedstat_inc(sd, ttwu_move_affine); |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 4076 | schedstat_inc(p, se.statistics.nr_wakeups_affine); |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 4077 | |
| 4078 | return 1; |
| 4079 | } |
| 4080 | return 0; |
| 4081 | } |
| 4082 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4083 | /* |
| 4084 | * find_idlest_group finds and returns the least busy CPU group within the |
| 4085 | * domain. |
| 4086 | */ |
| 4087 | static struct sched_group * |
Peter Zijlstra | 78e7ed5 | 2009-09-03 13:16:51 +0200 | [diff] [blame] | 4088 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, |
Vincent Guittot | c44f2a0 | 2013-10-18 13:52:21 +0200 | [diff] [blame] | 4089 | int this_cpu, int sd_flag) |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 4090 | { |
Andi Kleen | b3bd3de | 2010-08-10 14:17:51 -0700 | [diff] [blame] | 4091 | struct sched_group *idlest = NULL, *group = sd->groups; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4092 | unsigned long min_load = ULONG_MAX, this_load = 0; |
Vincent Guittot | c44f2a0 | 2013-10-18 13:52:21 +0200 | [diff] [blame] | 4093 | int load_idx = sd->forkexec_idx; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4094 | int imbalance = 100 + (sd->imbalance_pct-100)/2; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 4095 | |
Vincent Guittot | c44f2a0 | 2013-10-18 13:52:21 +0200 | [diff] [blame] | 4096 | if (sd_flag & SD_BALANCE_WAKE) |
| 4097 | load_idx = sd->wake_idx; |
| 4098 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4099 | do { |
| 4100 | unsigned long load, avg_load; |
| 4101 | int local_group; |
| 4102 | int i; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 4103 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4104 | /* Skip over this group if it has no CPUs allowed */ |
| 4105 | if (!cpumask_intersects(sched_group_cpus(group), |
Peter Zijlstra | fa17b50 | 2011-06-16 12:23:22 +0200 | [diff] [blame] | 4106 | tsk_cpus_allowed(p))) |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4107 | continue; |
| 4108 | |
| 4109 | local_group = cpumask_test_cpu(this_cpu, |
| 4110 | sched_group_cpus(group)); |
| 4111 | |
| 4112 | /* Tally up the load of all CPUs in the group */ |
| 4113 | avg_load = 0; |
| 4114 | |
| 4115 | for_each_cpu(i, sched_group_cpus(group)) { |
| 4116 | /* Bias balancing toward cpus of our domain */ |
| 4117 | if (local_group) |
| 4118 | load = source_load(i, load_idx); |
| 4119 | else |
| 4120 | load = target_load(i, load_idx); |
| 4121 | |
| 4122 | avg_load += load; |
| 4123 | } |
| 4124 | |
| 4125 | /* Adjust by relative CPU power of the group */ |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4126 | avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4127 | |
| 4128 | if (local_group) { |
| 4129 | this_load = avg_load; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4130 | } else if (avg_load < min_load) { |
| 4131 | min_load = avg_load; |
| 4132 | idlest = group; |
| 4133 | } |
| 4134 | } while (group = group->next, group != sd->groups); |
| 4135 | |
| 4136 | if (!idlest || 100*this_load < imbalance*min_load) |
| 4137 | return NULL; |
| 4138 | return idlest; |
| 4139 | } |
| 4140 | |
| 4141 | /* |
| 4142 | * find_idlest_cpu - find the idlest cpu among the cpus in group. |
| 4143 | */ |
| 4144 | static int |
| 4145 | find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu) |
| 4146 | { |
| 4147 | unsigned long load, min_load = ULONG_MAX; |
| 4148 | int idlest = -1; |
| 4149 | int i; |
| 4150 | |
| 4151 | /* Traverse only the allowed CPUs */ |
Peter Zijlstra | fa17b50 | 2011-06-16 12:23:22 +0200 | [diff] [blame] | 4152 | 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] | 4153 | load = weighted_cpuload(i); |
| 4154 | |
| 4155 | if (load < min_load || (load == min_load && i == this_cpu)) { |
| 4156 | min_load = load; |
| 4157 | idlest = i; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 4158 | } |
| 4159 | } |
| 4160 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4161 | return idlest; |
| 4162 | } |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 4163 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4164 | /* |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 4165 | * Try and locate an idle CPU in the sched_domain. |
| 4166 | */ |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 4167 | static int select_idle_sibling(struct task_struct *p, int target) |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 4168 | { |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 4169 | struct sched_domain *sd; |
Linus Torvalds | 37407ea | 2012-09-16 12:29:43 -0700 | [diff] [blame] | 4170 | struct sched_group *sg; |
Mike Galbraith | e0a79f5 | 2013-01-28 12:19:25 +0100 | [diff] [blame] | 4171 | int i = task_cpu(p); |
| 4172 | |
| 4173 | if (idle_cpu(target)) |
| 4174 | return target; |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 4175 | |
| 4176 | /* |
Mike Galbraith | e0a79f5 | 2013-01-28 12:19:25 +0100 | [diff] [blame] | 4177 | * If the prevous cpu is cache affine and idle, don't be stupid. |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 4178 | */ |
Mike Galbraith | e0a79f5 | 2013-01-28 12:19:25 +0100 | [diff] [blame] | 4179 | if (i != target && cpus_share_cache(i, target) && idle_cpu(i)) |
| 4180 | return i; |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 4181 | |
| 4182 | /* |
Linus Torvalds | 37407ea | 2012-09-16 12:29:43 -0700 | [diff] [blame] | 4183 | * Otherwise, iterate the domains and find an elegible idle cpu. |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 4184 | */ |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 4185 | sd = rcu_dereference(per_cpu(sd_llc, target)); |
Suresh Siddha | 77e8136 | 2011-11-17 11:08:23 -0800 | [diff] [blame] | 4186 | for_each_lower_domain(sd) { |
Linus Torvalds | 37407ea | 2012-09-16 12:29:43 -0700 | [diff] [blame] | 4187 | sg = sd->groups; |
| 4188 | do { |
| 4189 | if (!cpumask_intersects(sched_group_cpus(sg), |
| 4190 | tsk_cpus_allowed(p))) |
| 4191 | goto next; |
Mike Galbraith | 970e178 | 2012-06-12 05:18:32 +0200 | [diff] [blame] | 4192 | |
Linus Torvalds | 37407ea | 2012-09-16 12:29:43 -0700 | [diff] [blame] | 4193 | for_each_cpu(i, sched_group_cpus(sg)) { |
Mike Galbraith | e0a79f5 | 2013-01-28 12:19:25 +0100 | [diff] [blame] | 4194 | if (i == target || !idle_cpu(i)) |
Linus Torvalds | 37407ea | 2012-09-16 12:29:43 -0700 | [diff] [blame] | 4195 | goto next; |
| 4196 | } |
| 4197 | |
| 4198 | target = cpumask_first_and(sched_group_cpus(sg), |
| 4199 | tsk_cpus_allowed(p)); |
| 4200 | goto done; |
| 4201 | next: |
| 4202 | sg = sg->next; |
| 4203 | } while (sg != sd->groups); |
| 4204 | } |
| 4205 | done: |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 4206 | return target; |
| 4207 | } |
| 4208 | |
| 4209 | /* |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4210 | * sched_balance_self: balance the current task (running on cpu) in domains |
| 4211 | * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and |
| 4212 | * SD_BALANCE_EXEC. |
| 4213 | * |
| 4214 | * Balance, ie. select the least loaded group. |
| 4215 | * |
| 4216 | * Returns the target CPU number, or the same CPU if no balancing is needed. |
| 4217 | * |
| 4218 | * preempt must be disabled. |
| 4219 | */ |
Peter Zijlstra | 0017d73 | 2010-03-24 18:34:10 +0100 | [diff] [blame] | 4220 | static int |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 4221 | select_task_rq_fair(struct task_struct *p, int prev_cpu, int sd_flag, int wake_flags) |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4222 | { |
Peter Zijlstra | 29cd8ba | 2009-09-17 09:01:14 +0200 | [diff] [blame] | 4223 | struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 4224 | int cpu = smp_processor_id(); |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 4225 | int new_cpu = cpu; |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 4226 | int want_affine = 0; |
Peter Zijlstra | 5158f4e | 2009-09-16 13:46:59 +0200 | [diff] [blame] | 4227 | int sync = wake_flags & WF_SYNC; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 4228 | |
Peter Zijlstra | 29baa74 | 2012-04-23 12:11:21 +0200 | [diff] [blame] | 4229 | if (p->nr_cpus_allowed == 1) |
Mike Galbraith | 76854c7 | 2011-11-22 15:18:24 +0100 | [diff] [blame] | 4230 | return prev_cpu; |
| 4231 | |
Peter Zijlstra | 0763a66 | 2009-09-14 19:37:39 +0200 | [diff] [blame] | 4232 | if (sd_flag & SD_BALANCE_WAKE) { |
Peter Zijlstra | fa17b50 | 2011-06-16 12:23:22 +0200 | [diff] [blame] | 4233 | if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 4234 | want_affine = 1; |
| 4235 | new_cpu = prev_cpu; |
| 4236 | } |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 4237 | |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 4238 | rcu_read_lock(); |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4239 | for_each_domain(cpu, tmp) { |
Peter Zijlstra | e4f42888 | 2009-12-16 18:04:34 +0100 | [diff] [blame] | 4240 | if (!(tmp->flags & SD_LOAD_BALANCE)) |
| 4241 | continue; |
| 4242 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4243 | /* |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 4244 | * If both cpu and prev_cpu are part of this domain, |
| 4245 | * cpu is a valid SD_WAKE_AFFINE target. |
Peter Zijlstra | fe3bcfe | 2009-11-12 15:55:29 +0100 | [diff] [blame] | 4246 | */ |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 4247 | if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && |
| 4248 | cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) { |
| 4249 | affine_sd = tmp; |
Alex Shi | f03542a | 2012-07-26 08:55:34 +0800 | [diff] [blame] | 4250 | break; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 4251 | } |
| 4252 | |
Alex Shi | f03542a | 2012-07-26 08:55:34 +0800 | [diff] [blame] | 4253 | if (tmp->flags & sd_flag) |
Peter Zijlstra | 29cd8ba | 2009-09-17 09:01:14 +0200 | [diff] [blame] | 4254 | sd = tmp; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 4255 | } |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4256 | |
Mike Galbraith | 8b911ac | 2010-03-11 17:17:16 +0100 | [diff] [blame] | 4257 | if (affine_sd) { |
Alex Shi | f03542a | 2012-07-26 08:55:34 +0800 | [diff] [blame] | 4258 | if (cpu != prev_cpu && wake_affine(affine_sd, p, sync)) |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 4259 | prev_cpu = cpu; |
| 4260 | |
| 4261 | new_cpu = select_idle_sibling(p, prev_cpu); |
| 4262 | goto unlock; |
Mike Galbraith | 8b911ac | 2010-03-11 17:17:16 +0100 | [diff] [blame] | 4263 | } |
Peter Zijlstra | 3b64089 | 2009-09-16 13:44:33 +0200 | [diff] [blame] | 4264 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4265 | while (sd) { |
| 4266 | struct sched_group *group; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 4267 | int weight; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4268 | |
Peter Zijlstra | 0763a66 | 2009-09-14 19:37:39 +0200 | [diff] [blame] | 4269 | if (!(sd->flags & sd_flag)) { |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4270 | sd = sd->child; |
| 4271 | continue; |
| 4272 | } |
| 4273 | |
Vincent Guittot | c44f2a0 | 2013-10-18 13:52:21 +0200 | [diff] [blame] | 4274 | group = find_idlest_group(sd, p, cpu, sd_flag); |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4275 | if (!group) { |
| 4276 | sd = sd->child; |
| 4277 | continue; |
| 4278 | } |
| 4279 | |
Peter Zijlstra | d7c33c4 | 2009-09-11 12:45:38 +0200 | [diff] [blame] | 4280 | new_cpu = find_idlest_cpu(group, p, cpu); |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4281 | if (new_cpu == -1 || new_cpu == cpu) { |
| 4282 | /* Now try balancing at a lower domain level of cpu */ |
| 4283 | sd = sd->child; |
| 4284 | continue; |
| 4285 | } |
| 4286 | |
| 4287 | /* Now try balancing at a lower domain level of new_cpu */ |
| 4288 | cpu = new_cpu; |
Peter Zijlstra | 669c55e | 2010-04-16 14:59:29 +0200 | [diff] [blame] | 4289 | weight = sd->span_weight; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4290 | sd = NULL; |
| 4291 | for_each_domain(cpu, tmp) { |
Peter Zijlstra | 669c55e | 2010-04-16 14:59:29 +0200 | [diff] [blame] | 4292 | if (weight <= tmp->span_weight) |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4293 | break; |
Peter Zijlstra | 0763a66 | 2009-09-14 19:37:39 +0200 | [diff] [blame] | 4294 | if (tmp->flags & sd_flag) |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 4295 | sd = tmp; |
| 4296 | } |
| 4297 | /* while loop will break here if sd == NULL */ |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 4298 | } |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 4299 | unlock: |
| 4300 | rcu_read_unlock(); |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 4301 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 4302 | return new_cpu; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 4303 | } |
Paul Turner | 0a74bef | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 4304 | |
| 4305 | /* |
| 4306 | * Called immediately before a task is migrated to a new cpu; task_cpu(p) and |
| 4307 | * cfs_rq_of(p) references at time of call are still valid and identify the |
| 4308 | * previous cpu. However, the caller only guarantees p->pi_lock is held; no |
| 4309 | * other assumptions, including the state of rq->lock, should be made. |
| 4310 | */ |
| 4311 | static void |
| 4312 | migrate_task_rq_fair(struct task_struct *p, int next_cpu) |
| 4313 | { |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 4314 | struct sched_entity *se = &p->se; |
| 4315 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 4316 | |
| 4317 | /* |
| 4318 | * Load tracking: accumulate removed load so that it can be processed |
| 4319 | * when we next update owning cfs_rq under rq->lock. Tasks contribute |
| 4320 | * to blocked load iff they have a positive decay-count. It can never |
| 4321 | * be negative here since on-rq tasks have decay-count == 0. |
| 4322 | */ |
| 4323 | if (se->avg.decay_count) { |
| 4324 | se->avg.decay_count = -__synchronize_entity_decay(se); |
Alex Shi | 2509940 | 2013-06-20 10:18:55 +0800 | [diff] [blame] | 4325 | atomic_long_add(se->avg.load_avg_contrib, |
| 4326 | &cfs_rq->removed_load); |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 4327 | } |
Paul Turner | 0a74bef | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 4328 | } |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 4329 | #endif /* CONFIG_SMP */ |
| 4330 | |
Peter Zijlstra | e52fb7c | 2009-01-14 12:39:19 +0100 | [diff] [blame] | 4331 | static unsigned long |
| 4332 | wakeup_gran(struct sched_entity *curr, struct sched_entity *se) |
Peter Zijlstra | 0bbd333 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 4333 | { |
| 4334 | unsigned long gran = sysctl_sched_wakeup_granularity; |
| 4335 | |
| 4336 | /* |
Peter Zijlstra | e52fb7c | 2009-01-14 12:39:19 +0100 | [diff] [blame] | 4337 | * Since its curr running now, convert the gran from real-time |
| 4338 | * to virtual-time in his units. |
Mike Galbraith | 13814d4 | 2010-03-11 17:17:04 +0100 | [diff] [blame] | 4339 | * |
| 4340 | * By using 'se' instead of 'curr' we penalize light tasks, so |
| 4341 | * they get preempted easier. That is, if 'se' < 'curr' then |
| 4342 | * the resulting gran will be larger, therefore penalizing the |
| 4343 | * lighter, if otoh 'se' > 'curr' then the resulting gran will |
| 4344 | * be smaller, again penalizing the lighter task. |
| 4345 | * |
| 4346 | * This is especially important for buddies when the leftmost |
| 4347 | * task is higher priority than the buddy. |
Peter Zijlstra | 0bbd333 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 4348 | */ |
Shaohua Li | f4ad9bd | 2011-04-08 12:53:09 +0800 | [diff] [blame] | 4349 | return calc_delta_fair(gran, se); |
Peter Zijlstra | 0bbd333 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 4350 | } |
| 4351 | |
| 4352 | /* |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 4353 | * Should 'se' preempt 'curr'. |
| 4354 | * |
| 4355 | * |s1 |
| 4356 | * |s2 |
| 4357 | * |s3 |
| 4358 | * g |
| 4359 | * |<--->|c |
| 4360 | * |
| 4361 | * w(c, s1) = -1 |
| 4362 | * w(c, s2) = 0 |
| 4363 | * w(c, s3) = 1 |
| 4364 | * |
| 4365 | */ |
| 4366 | static int |
| 4367 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se) |
| 4368 | { |
| 4369 | s64 gran, vdiff = curr->vruntime - se->vruntime; |
| 4370 | |
| 4371 | if (vdiff <= 0) |
| 4372 | return -1; |
| 4373 | |
Peter Zijlstra | e52fb7c | 2009-01-14 12:39:19 +0100 | [diff] [blame] | 4374 | gran = wakeup_gran(curr, se); |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 4375 | if (vdiff > gran) |
| 4376 | return 1; |
| 4377 | |
| 4378 | return 0; |
| 4379 | } |
| 4380 | |
Peter Zijlstra | 0247909 | 2008-11-04 21:25:10 +0100 | [diff] [blame] | 4381 | static void set_last_buddy(struct sched_entity *se) |
| 4382 | { |
Venkatesh Pallipadi | 69c80f3 | 2011-04-13 18:21:09 -0700 | [diff] [blame] | 4383 | if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE)) |
| 4384 | return; |
| 4385 | |
| 4386 | for_each_sched_entity(se) |
| 4387 | cfs_rq_of(se)->last = se; |
Peter Zijlstra | 0247909 | 2008-11-04 21:25:10 +0100 | [diff] [blame] | 4388 | } |
| 4389 | |
| 4390 | static void set_next_buddy(struct sched_entity *se) |
| 4391 | { |
Venkatesh Pallipadi | 69c80f3 | 2011-04-13 18:21:09 -0700 | [diff] [blame] | 4392 | if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE)) |
| 4393 | return; |
| 4394 | |
| 4395 | for_each_sched_entity(se) |
| 4396 | cfs_rq_of(se)->next = se; |
Peter Zijlstra | 0247909 | 2008-11-04 21:25:10 +0100 | [diff] [blame] | 4397 | } |
| 4398 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 4399 | static void set_skip_buddy(struct sched_entity *se) |
| 4400 | { |
Venkatesh Pallipadi | 69c80f3 | 2011-04-13 18:21:09 -0700 | [diff] [blame] | 4401 | for_each_sched_entity(se) |
| 4402 | cfs_rq_of(se)->skip = se; |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 4403 | } |
| 4404 | |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 4405 | /* |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4406 | * Preempt the current task with a newly woken task if needed: |
| 4407 | */ |
Peter Zijlstra | 5a9b86f | 2009-09-16 13:47:58 +0200 | [diff] [blame] | 4408 | 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] | 4409 | { |
| 4410 | struct task_struct *curr = rq->curr; |
Srivatsa Vaddagiri | 8651a86 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 4411 | struct sched_entity *se = &curr->se, *pse = &p->se; |
Mike Galbraith | 03e89e4 | 2008-12-16 08:45:30 +0100 | [diff] [blame] | 4412 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 4413 | int scale = cfs_rq->nr_running >= sched_nr_latency; |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 4414 | int next_buddy_marked = 0; |
Mike Galbraith | 03e89e4 | 2008-12-16 08:45:30 +0100 | [diff] [blame] | 4415 | |
Ingo Molnar | 4ae7d5c | 2008-03-19 01:42:00 +0100 | [diff] [blame] | 4416 | if (unlikely(se == pse)) |
| 4417 | return; |
| 4418 | |
Paul Turner | 5238cdd | 2011-07-21 09:43:37 -0700 | [diff] [blame] | 4419 | /* |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 4420 | * This is possible from callers such as move_task(), in which we |
Paul Turner | 5238cdd | 2011-07-21 09:43:37 -0700 | [diff] [blame] | 4421 | * unconditionally check_prempt_curr() after an enqueue (which may have |
| 4422 | * lead to a throttle). This both saves work and prevents false |
| 4423 | * next-buddy nomination below. |
| 4424 | */ |
| 4425 | if (unlikely(throttled_hierarchy(cfs_rq_of(pse)))) |
| 4426 | return; |
| 4427 | |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 4428 | if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) { |
Mike Galbraith | 3cb63d5 | 2009-09-11 12:01:17 +0200 | [diff] [blame] | 4429 | set_next_buddy(pse); |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 4430 | next_buddy_marked = 1; |
| 4431 | } |
Peter Zijlstra | 57fdc26 | 2008-09-23 15:33:45 +0200 | [diff] [blame] | 4432 | |
Bharata B Rao | aec0a51 | 2008-08-28 14:42:49 +0530 | [diff] [blame] | 4433 | /* |
| 4434 | * We can come here with TIF_NEED_RESCHED already set from new task |
| 4435 | * wake up path. |
Paul Turner | 5238cdd | 2011-07-21 09:43:37 -0700 | [diff] [blame] | 4436 | * |
| 4437 | * Note: this also catches the edge-case of curr being in a throttled |
| 4438 | * group (e.g. via set_curr_task), since update_curr() (in the |
| 4439 | * enqueue of curr) will have resulted in resched being set. This |
| 4440 | * prevents us from potentially nominating it as a false LAST_BUDDY |
| 4441 | * below. |
Bharata B Rao | aec0a51 | 2008-08-28 14:42:49 +0530 | [diff] [blame] | 4442 | */ |
| 4443 | if (test_tsk_need_resched(curr)) |
| 4444 | return; |
| 4445 | |
Darren Hart | a2f5c9a | 2011-02-22 13:04:33 -0800 | [diff] [blame] | 4446 | /* Idle tasks are by definition preempted by non-idle tasks. */ |
| 4447 | if (unlikely(curr->policy == SCHED_IDLE) && |
| 4448 | likely(p->policy != SCHED_IDLE)) |
| 4449 | goto preempt; |
| 4450 | |
Ingo Molnar | 91c234b | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 4451 | /* |
Darren Hart | a2f5c9a | 2011-02-22 13:04:33 -0800 | [diff] [blame] | 4452 | * Batch and idle tasks do not preempt non-idle tasks (their preemption |
| 4453 | * is driven by the tick): |
Ingo Molnar | 91c234b | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 4454 | */ |
Ingo Molnar | 8ed92e51 | 2012-10-14 14:28:50 +0200 | [diff] [blame] | 4455 | if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION)) |
Ingo Molnar | 91c234b | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 4456 | return; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4457 | |
Peter Zijlstra | 3a7e73a | 2009-11-28 18:51:02 +0100 | [diff] [blame] | 4458 | find_matching_se(&se, &pse); |
Paul Turner | 9bbd737 | 2011-07-05 19:07:21 -0700 | [diff] [blame] | 4459 | update_curr(cfs_rq_of(se)); |
Peter Zijlstra | 3a7e73a | 2009-11-28 18:51:02 +0100 | [diff] [blame] | 4460 | BUG_ON(!pse); |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 4461 | if (wakeup_preempt_entity(se, pse) == 1) { |
| 4462 | /* |
| 4463 | * Bias pick_next to pick the sched entity that is |
| 4464 | * triggering this preemption. |
| 4465 | */ |
| 4466 | if (!next_buddy_marked) |
| 4467 | set_next_buddy(pse); |
Peter Zijlstra | 3a7e73a | 2009-11-28 18:51:02 +0100 | [diff] [blame] | 4468 | goto preempt; |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 4469 | } |
Jupyung Lee | a65ac74 | 2009-11-17 18:51:40 +0900 | [diff] [blame] | 4470 | |
Peter Zijlstra | 3a7e73a | 2009-11-28 18:51:02 +0100 | [diff] [blame] | 4471 | return; |
| 4472 | |
| 4473 | preempt: |
| 4474 | resched_task(curr); |
| 4475 | /* |
| 4476 | * Only set the backward buddy when the current task is still |
| 4477 | * on the rq. This can happen when a wakeup gets interleaved |
| 4478 | * with schedule on the ->pre_schedule() or idle_balance() |
| 4479 | * point, either of which can * drop the rq lock. |
| 4480 | * |
| 4481 | * Also, during early boot the idle thread is in the fair class, |
| 4482 | * for obvious reasons its a bad idea to schedule back to it. |
| 4483 | */ |
| 4484 | if (unlikely(!se->on_rq || curr == rq->idle)) |
| 4485 | return; |
| 4486 | |
| 4487 | if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se)) |
| 4488 | set_last_buddy(se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4489 | } |
| 4490 | |
Ingo Molnar | fb8d472 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 4491 | static struct task_struct *pick_next_task_fair(struct rq *rq) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4492 | { |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4493 | struct task_struct *p; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4494 | struct cfs_rq *cfs_rq = &rq->cfs; |
| 4495 | struct sched_entity *se; |
| 4496 | |
Tim Blechmann | 36ace27 | 2009-11-24 11:55:45 +0100 | [diff] [blame] | 4497 | if (!cfs_rq->nr_running) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4498 | return NULL; |
| 4499 | |
| 4500 | do { |
Ingo Molnar | 9948f4b | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 4501 | se = pick_next_entity(cfs_rq); |
Peter Zijlstra | f4b6755 | 2008-11-04 21:25:07 +0100 | [diff] [blame] | 4502 | set_next_entity(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4503 | cfs_rq = group_cfs_rq(se); |
| 4504 | } while (cfs_rq); |
| 4505 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4506 | p = task_of(se); |
Mike Galbraith | b39e66e | 2011-11-22 15:20:07 +0100 | [diff] [blame] | 4507 | if (hrtick_enabled(rq)) |
| 4508 | hrtick_start_fair(rq, p); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4509 | |
| 4510 | return p; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4511 | } |
| 4512 | |
| 4513 | /* |
| 4514 | * Account for a descheduled task: |
| 4515 | */ |
Ingo Molnar | 31ee529 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 4516 | 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] | 4517 | { |
| 4518 | struct sched_entity *se = &prev->se; |
| 4519 | struct cfs_rq *cfs_rq; |
| 4520 | |
| 4521 | for_each_sched_entity(se) { |
| 4522 | cfs_rq = cfs_rq_of(se); |
Ingo Molnar | ab6cde2 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 4523 | put_prev_entity(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4524 | } |
| 4525 | } |
| 4526 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 4527 | /* |
| 4528 | * sched_yield() is very simple |
| 4529 | * |
| 4530 | * The magic of dealing with the ->skip buddy is in pick_next_entity. |
| 4531 | */ |
| 4532 | static void yield_task_fair(struct rq *rq) |
| 4533 | { |
| 4534 | struct task_struct *curr = rq->curr; |
| 4535 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); |
| 4536 | struct sched_entity *se = &curr->se; |
| 4537 | |
| 4538 | /* |
| 4539 | * Are we the only task in the tree? |
| 4540 | */ |
| 4541 | if (unlikely(rq->nr_running == 1)) |
| 4542 | return; |
| 4543 | |
| 4544 | clear_buddies(cfs_rq, se); |
| 4545 | |
| 4546 | if (curr->policy != SCHED_BATCH) { |
| 4547 | update_rq_clock(rq); |
| 4548 | /* |
| 4549 | * Update run-time statistics of the 'current'. |
| 4550 | */ |
| 4551 | update_curr(cfs_rq); |
Mike Galbraith | 916671c | 2011-11-22 15:21:26 +0100 | [diff] [blame] | 4552 | /* |
| 4553 | * Tell update_rq_clock() that we've just updated, |
| 4554 | * so we don't do microscopic update in schedule() |
| 4555 | * and double the fastpath cost. |
| 4556 | */ |
| 4557 | rq->skip_clock_update = 1; |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 4558 | } |
| 4559 | |
| 4560 | set_skip_buddy(se); |
| 4561 | } |
| 4562 | |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 4563 | static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt) |
| 4564 | { |
| 4565 | struct sched_entity *se = &p->se; |
| 4566 | |
Paul Turner | 5238cdd | 2011-07-21 09:43:37 -0700 | [diff] [blame] | 4567 | /* throttled hierarchies are not runnable */ |
| 4568 | if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se))) |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 4569 | return false; |
| 4570 | |
| 4571 | /* Tell the scheduler that we'd really like pse to run next. */ |
| 4572 | set_next_buddy(se); |
| 4573 | |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 4574 | yield_task_fair(rq); |
| 4575 | |
| 4576 | return true; |
| 4577 | } |
| 4578 | |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 4579 | #ifdef CONFIG_SMP |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4580 | /************************************************** |
Peter Zijlstra | e9c84cb | 2012-07-03 13:53:26 +0200 | [diff] [blame] | 4581 | * Fair scheduling class load-balancing methods. |
| 4582 | * |
| 4583 | * BASICS |
| 4584 | * |
| 4585 | * The purpose of load-balancing is to achieve the same basic fairness the |
| 4586 | * per-cpu scheduler provides, namely provide a proportional amount of compute |
| 4587 | * time to each task. This is expressed in the following equation: |
| 4588 | * |
| 4589 | * W_i,n/P_i == W_j,n/P_j for all i,j (1) |
| 4590 | * |
| 4591 | * Where W_i,n is the n-th weight average for cpu i. The instantaneous weight |
| 4592 | * W_i,0 is defined as: |
| 4593 | * |
| 4594 | * W_i,0 = \Sum_j w_i,j (2) |
| 4595 | * |
| 4596 | * Where w_i,j is the weight of the j-th runnable task on cpu i. This weight |
| 4597 | * is derived from the nice value as per prio_to_weight[]. |
| 4598 | * |
| 4599 | * The weight average is an exponential decay average of the instantaneous |
| 4600 | * weight: |
| 4601 | * |
| 4602 | * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3) |
| 4603 | * |
| 4604 | * P_i is the cpu power (or compute capacity) of cpu i, typically it is the |
| 4605 | * fraction of 'recent' time available for SCHED_OTHER task execution. But it |
| 4606 | * can also include other factors [XXX]. |
| 4607 | * |
| 4608 | * To achieve this balance we define a measure of imbalance which follows |
| 4609 | * directly from (1): |
| 4610 | * |
| 4611 | * imb_i,j = max{ avg(W/P), W_i/P_i } - min{ avg(W/P), W_j/P_j } (4) |
| 4612 | * |
| 4613 | * We them move tasks around to minimize the imbalance. In the continuous |
| 4614 | * function space it is obvious this converges, in the discrete case we get |
| 4615 | * a few fun cases generally called infeasible weight scenarios. |
| 4616 | * |
| 4617 | * [XXX expand on: |
| 4618 | * - infeasible weights; |
| 4619 | * - local vs global optima in the discrete case. ] |
| 4620 | * |
| 4621 | * |
| 4622 | * SCHED DOMAINS |
| 4623 | * |
| 4624 | * In order to solve the imbalance equation (4), and avoid the obvious O(n^2) |
| 4625 | * for all i,j solution, we create a tree of cpus that follows the hardware |
| 4626 | * topology where each level pairs two lower groups (or better). This results |
| 4627 | * in O(log n) layers. Furthermore we reduce the number of cpus going up the |
| 4628 | * tree to only the first of the previous level and we decrease the frequency |
| 4629 | * of load-balance at each level inv. proportional to the number of cpus in |
| 4630 | * the groups. |
| 4631 | * |
| 4632 | * This yields: |
| 4633 | * |
| 4634 | * log_2 n 1 n |
| 4635 | * \Sum { --- * --- * 2^i } = O(n) (5) |
| 4636 | * i = 0 2^i 2^i |
| 4637 | * `- size of each group |
| 4638 | * | | `- number of cpus doing load-balance |
| 4639 | * | `- freq |
| 4640 | * `- sum over all levels |
| 4641 | * |
| 4642 | * Coupled with a limit on how many tasks we can migrate every balance pass, |
| 4643 | * this makes (5) the runtime complexity of the balancer. |
| 4644 | * |
| 4645 | * An important property here is that each CPU is still (indirectly) connected |
| 4646 | * to every other cpu in at most O(log n) steps: |
| 4647 | * |
| 4648 | * The adjacency matrix of the resulting graph is given by: |
| 4649 | * |
| 4650 | * log_2 n |
| 4651 | * A_i,j = \Union (i % 2^k == 0) && i / 2^(k+1) == j / 2^(k+1) (6) |
| 4652 | * k = 0 |
| 4653 | * |
| 4654 | * And you'll find that: |
| 4655 | * |
| 4656 | * A^(log_2 n)_i,j != 0 for all i,j (7) |
| 4657 | * |
| 4658 | * Showing there's indeed a path between every cpu in at most O(log n) steps. |
| 4659 | * The task movement gives a factor of O(m), giving a convergence complexity |
| 4660 | * of: |
| 4661 | * |
| 4662 | * O(nm log n), n := nr_cpus, m := nr_tasks (8) |
| 4663 | * |
| 4664 | * |
| 4665 | * WORK CONSERVING |
| 4666 | * |
| 4667 | * In order to avoid CPUs going idle while there's still work to do, new idle |
| 4668 | * balancing is more aggressive and has the newly idle cpu iterate up the domain |
| 4669 | * tree itself instead of relying on other CPUs to bring it work. |
| 4670 | * |
| 4671 | * This adds some complexity to both (5) and (8) but it reduces the total idle |
| 4672 | * time. |
| 4673 | * |
| 4674 | * [XXX more?] |
| 4675 | * |
| 4676 | * |
| 4677 | * CGROUPS |
| 4678 | * |
| 4679 | * Cgroups make a horror show out of (2), instead of a simple sum we get: |
| 4680 | * |
| 4681 | * s_k,i |
| 4682 | * W_i,0 = \Sum_j \Prod_k w_k * ----- (9) |
| 4683 | * S_k |
| 4684 | * |
| 4685 | * Where |
| 4686 | * |
| 4687 | * s_k,i = \Sum_j w_i,j,k and S_k = \Sum_i s_k,i (10) |
| 4688 | * |
| 4689 | * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on cpu i. |
| 4690 | * |
| 4691 | * The big problem is S_k, its a global sum needed to compute a local (W_i) |
| 4692 | * property. |
| 4693 | * |
| 4694 | * [XXX write more on how we solve this.. _after_ merging pjt's patches that |
| 4695 | * rewrite all of this once again.] |
| 4696 | */ |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4697 | |
Hiroshi Shimamoto | ed387b7 | 2012-01-31 11:40:32 +0900 | [diff] [blame] | 4698 | static unsigned long __read_mostly max_load_balance_interval = HZ/10; |
| 4699 | |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 4700 | enum fbq_type { regular, remote, all }; |
| 4701 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 4702 | #define LBF_ALL_PINNED 0x01 |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 4703 | #define LBF_NEED_BREAK 0x02 |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 4704 | #define LBF_DST_PINNED 0x04 |
| 4705 | #define LBF_SOME_PINNED 0x08 |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 4706 | |
| 4707 | struct lb_env { |
| 4708 | struct sched_domain *sd; |
| 4709 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 4710 | struct rq *src_rq; |
Prashanth Nageshappa | 85c1e7d | 2012-06-19 17:47:34 +0530 | [diff] [blame] | 4711 | int src_cpu; |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 4712 | |
| 4713 | int dst_cpu; |
| 4714 | struct rq *dst_rq; |
| 4715 | |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 4716 | struct cpumask *dst_grpmask; |
| 4717 | int new_dst_cpu; |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 4718 | enum cpu_idle_type idle; |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4719 | long imbalance; |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4720 | /* The set of CPUs under consideration for load-balancing */ |
| 4721 | struct cpumask *cpus; |
| 4722 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 4723 | unsigned int flags; |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 4724 | |
| 4725 | unsigned int loop; |
| 4726 | unsigned int loop_break; |
| 4727 | unsigned int loop_max; |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 4728 | |
| 4729 | enum fbq_type fbq_type; |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 4730 | }; |
| 4731 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4732 | /* |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 4733 | * move_task - move a task from one runqueue to another runqueue. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4734 | * Both runqueues must be locked. |
| 4735 | */ |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 4736 | static void move_task(struct task_struct *p, struct lb_env *env) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4737 | { |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 4738 | deactivate_task(env->src_rq, p, 0); |
| 4739 | set_task_cpu(p, env->dst_cpu); |
| 4740 | activate_task(env->dst_rq, p, 0); |
| 4741 | check_preempt_curr(env->dst_rq, p, 0); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4742 | } |
| 4743 | |
| 4744 | /* |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4745 | * Is this task likely cache-hot: |
| 4746 | */ |
| 4747 | static int |
| 4748 | task_hot(struct task_struct *p, u64 now, struct sched_domain *sd) |
| 4749 | { |
| 4750 | s64 delta; |
| 4751 | |
| 4752 | if (p->sched_class != &fair_sched_class) |
| 4753 | return 0; |
| 4754 | |
| 4755 | if (unlikely(p->policy == SCHED_IDLE)) |
| 4756 | return 0; |
| 4757 | |
| 4758 | /* |
| 4759 | * Buddy candidates are cache hot: |
| 4760 | */ |
| 4761 | if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running && |
| 4762 | (&p->se == cfs_rq_of(&p->se)->next || |
| 4763 | &p->se == cfs_rq_of(&p->se)->last)) |
| 4764 | return 1; |
| 4765 | |
| 4766 | if (sysctl_sched_migration_cost == -1) |
| 4767 | return 1; |
| 4768 | if (sysctl_sched_migration_cost == 0) |
| 4769 | return 0; |
| 4770 | |
| 4771 | delta = now - p->se.exec_start; |
| 4772 | |
| 4773 | return delta < (s64)sysctl_sched_migration_cost; |
| 4774 | } |
| 4775 | |
Mel Gorman | 3a7053b | 2013-10-07 11:29:00 +0100 | [diff] [blame] | 4776 | #ifdef CONFIG_NUMA_BALANCING |
| 4777 | /* Returns true if the destination node has incurred more faults */ |
| 4778 | static bool migrate_improves_locality(struct task_struct *p, struct lb_env *env) |
| 4779 | { |
| 4780 | int src_nid, dst_nid; |
| 4781 | |
| 4782 | if (!sched_feat(NUMA_FAVOUR_HIGHER) || !p->numa_faults || |
| 4783 | !(env->sd->flags & SD_NUMA)) { |
| 4784 | return false; |
| 4785 | } |
| 4786 | |
| 4787 | src_nid = cpu_to_node(env->src_cpu); |
| 4788 | dst_nid = cpu_to_node(env->dst_cpu); |
| 4789 | |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 4790 | if (src_nid == dst_nid) |
Mel Gorman | 3a7053b | 2013-10-07 11:29:00 +0100 | [diff] [blame] | 4791 | return false; |
| 4792 | |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 4793 | /* Always encourage migration to the preferred node. */ |
| 4794 | if (dst_nid == p->numa_preferred_nid) |
| 4795 | return true; |
| 4796 | |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 4797 | /* If both task and group weight improve, this move is a winner. */ |
| 4798 | if (task_weight(p, dst_nid) > task_weight(p, src_nid) && |
| 4799 | group_weight(p, dst_nid) > group_weight(p, src_nid)) |
Mel Gorman | 3a7053b | 2013-10-07 11:29:00 +0100 | [diff] [blame] | 4800 | return true; |
| 4801 | |
| 4802 | return false; |
| 4803 | } |
Mel Gorman | 7a0f308 | 2013-10-07 11:29:01 +0100 | [diff] [blame] | 4804 | |
| 4805 | |
| 4806 | static bool migrate_degrades_locality(struct task_struct *p, struct lb_env *env) |
| 4807 | { |
| 4808 | int src_nid, dst_nid; |
| 4809 | |
| 4810 | if (!sched_feat(NUMA) || !sched_feat(NUMA_RESIST_LOWER)) |
| 4811 | return false; |
| 4812 | |
| 4813 | if (!p->numa_faults || !(env->sd->flags & SD_NUMA)) |
| 4814 | return false; |
| 4815 | |
| 4816 | src_nid = cpu_to_node(env->src_cpu); |
| 4817 | dst_nid = cpu_to_node(env->dst_cpu); |
| 4818 | |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 4819 | if (src_nid == dst_nid) |
Mel Gorman | 7a0f308 | 2013-10-07 11:29:01 +0100 | [diff] [blame] | 4820 | return false; |
| 4821 | |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 4822 | /* Migrating away from the preferred node is always bad. */ |
| 4823 | if (src_nid == p->numa_preferred_nid) |
| 4824 | return true; |
| 4825 | |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 4826 | /* If either task or group weight get worse, don't do it. */ |
| 4827 | if (task_weight(p, dst_nid) < task_weight(p, src_nid) || |
| 4828 | group_weight(p, dst_nid) < group_weight(p, src_nid)) |
Mel Gorman | 7a0f308 | 2013-10-07 11:29:01 +0100 | [diff] [blame] | 4829 | return true; |
| 4830 | |
| 4831 | return false; |
| 4832 | } |
| 4833 | |
Mel Gorman | 3a7053b | 2013-10-07 11:29:00 +0100 | [diff] [blame] | 4834 | #else |
| 4835 | static inline bool migrate_improves_locality(struct task_struct *p, |
| 4836 | struct lb_env *env) |
| 4837 | { |
| 4838 | return false; |
| 4839 | } |
Mel Gorman | 7a0f308 | 2013-10-07 11:29:01 +0100 | [diff] [blame] | 4840 | |
| 4841 | static inline bool migrate_degrades_locality(struct task_struct *p, |
| 4842 | struct lb_env *env) |
| 4843 | { |
| 4844 | return false; |
| 4845 | } |
Mel Gorman | 3a7053b | 2013-10-07 11:29:00 +0100 | [diff] [blame] | 4846 | #endif |
| 4847 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4848 | /* |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4849 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? |
| 4850 | */ |
| 4851 | static |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4852 | int can_migrate_task(struct task_struct *p, struct lb_env *env) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4853 | { |
| 4854 | int tsk_cache_hot = 0; |
| 4855 | /* |
| 4856 | * We do not migrate tasks that are: |
Joonsoo Kim | d319808 | 2013-04-23 17:27:40 +0900 | [diff] [blame] | 4857 | * 1) throttled_lb_pair, or |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4858 | * 2) cannot be migrated to this CPU due to cpus_allowed, or |
Joonsoo Kim | d319808 | 2013-04-23 17:27:40 +0900 | [diff] [blame] | 4859 | * 3) running (obviously), or |
| 4860 | * 4) are cache-hot on their current CPU. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4861 | */ |
Joonsoo Kim | d319808 | 2013-04-23 17:27:40 +0900 | [diff] [blame] | 4862 | if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu)) |
| 4863 | return 0; |
| 4864 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 4865 | if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) { |
Joonsoo Kim | e02e60c | 2013-04-23 17:27:42 +0900 | [diff] [blame] | 4866 | int cpu; |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 4867 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 4868 | schedstat_inc(p, se.statistics.nr_failed_migrations_affine); |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 4869 | |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 4870 | env->flags |= LBF_SOME_PINNED; |
| 4871 | |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 4872 | /* |
| 4873 | * Remember if this task can be migrated to any other cpu in |
| 4874 | * our sched_group. We may want to revisit it if we couldn't |
| 4875 | * meet load balance goals by pulling other tasks on src_cpu. |
| 4876 | * |
| 4877 | * Also avoid computing new_dst_cpu if we have already computed |
| 4878 | * one in current iteration. |
| 4879 | */ |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 4880 | if (!env->dst_grpmask || (env->flags & LBF_DST_PINNED)) |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 4881 | return 0; |
| 4882 | |
Joonsoo Kim | e02e60c | 2013-04-23 17:27:42 +0900 | [diff] [blame] | 4883 | /* Prevent to re-select dst_cpu via env's cpus */ |
| 4884 | for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) { |
| 4885 | if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) { |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 4886 | env->flags |= LBF_DST_PINNED; |
Joonsoo Kim | e02e60c | 2013-04-23 17:27:42 +0900 | [diff] [blame] | 4887 | env->new_dst_cpu = cpu; |
| 4888 | break; |
| 4889 | } |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 4890 | } |
Joonsoo Kim | e02e60c | 2013-04-23 17:27:42 +0900 | [diff] [blame] | 4891 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4892 | return 0; |
| 4893 | } |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 4894 | |
| 4895 | /* Record that we found atleast one task that could run on dst_cpu */ |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4896 | env->flags &= ~LBF_ALL_PINNED; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4897 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 4898 | if (task_running(env->src_rq, p)) { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 4899 | schedstat_inc(p, se.statistics.nr_failed_migrations_running); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4900 | return 0; |
| 4901 | } |
| 4902 | |
| 4903 | /* |
| 4904 | * Aggressive migration if: |
Mel Gorman | 3a7053b | 2013-10-07 11:29:00 +0100 | [diff] [blame] | 4905 | * 1) destination numa is preferred |
| 4906 | * 2) task is cache cold, or |
| 4907 | * 3) too many balance attempts have failed. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4908 | */ |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 4909 | tsk_cache_hot = task_hot(p, rq_clock_task(env->src_rq), env->sd); |
Mel Gorman | 7a0f308 | 2013-10-07 11:29:01 +0100 | [diff] [blame] | 4910 | if (!tsk_cache_hot) |
| 4911 | tsk_cache_hot = migrate_degrades_locality(p, env); |
Mel Gorman | 3a7053b | 2013-10-07 11:29:00 +0100 | [diff] [blame] | 4912 | |
| 4913 | if (migrate_improves_locality(p, env)) { |
| 4914 | #ifdef CONFIG_SCHEDSTATS |
| 4915 | if (tsk_cache_hot) { |
| 4916 | schedstat_inc(env->sd, lb_hot_gained[env->idle]); |
| 4917 | schedstat_inc(p, se.statistics.nr_forced_migrations); |
| 4918 | } |
| 4919 | #endif |
| 4920 | return 1; |
| 4921 | } |
| 4922 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4923 | if (!tsk_cache_hot || |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4924 | env->sd->nr_balance_failed > env->sd->cache_nice_tries) { |
Zhang Hang | 4e2dcb7 | 2013-04-10 14:04:55 +0800 | [diff] [blame] | 4925 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4926 | if (tsk_cache_hot) { |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4927 | schedstat_inc(env->sd, lb_hot_gained[env->idle]); |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 4928 | schedstat_inc(p, se.statistics.nr_forced_migrations); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4929 | } |
Zhang Hang | 4e2dcb7 | 2013-04-10 14:04:55 +0800 | [diff] [blame] | 4930 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4931 | return 1; |
| 4932 | } |
| 4933 | |
Zhang Hang | 4e2dcb7 | 2013-04-10 14:04:55 +0800 | [diff] [blame] | 4934 | schedstat_inc(p, se.statistics.nr_failed_migrations_hot); |
| 4935 | return 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4936 | } |
| 4937 | |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 4938 | /* |
| 4939 | * move_one_task tries to move exactly one task from busiest to this_rq, as |
| 4940 | * part of active balancing operations within "domain". |
| 4941 | * Returns 1 if successful and 0 otherwise. |
| 4942 | * |
| 4943 | * Called with both runqueues locked. |
| 4944 | */ |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4945 | static int move_one_task(struct lb_env *env) |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 4946 | { |
| 4947 | struct task_struct *p, *n; |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 4948 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 4949 | list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) { |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 4950 | if (!can_migrate_task(p, env)) |
| 4951 | continue; |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 4952 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 4953 | move_task(p, env); |
| 4954 | /* |
| 4955 | * Right now, this is only the second place move_task() |
| 4956 | * is called, so we can safely collect move_task() |
| 4957 | * stats here rather than inside move_task(). |
| 4958 | */ |
| 4959 | schedstat_inc(env->sd, lb_gained[env->idle]); |
| 4960 | return 1; |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 4961 | } |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 4962 | return 0; |
| 4963 | } |
| 4964 | |
Peter Zijlstra | eb95308 | 2012-04-17 13:38:40 +0200 | [diff] [blame] | 4965 | static const unsigned int sched_nr_migrate_break = 32; |
| 4966 | |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 4967 | /* |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4968 | * move_tasks tries to move up to imbalance weighted load from busiest to |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 4969 | * this_rq, as part of a balancing operation within domain "sd". |
| 4970 | * Returns 1 if successful and 0 otherwise. |
| 4971 | * |
| 4972 | * Called with both runqueues locked. |
| 4973 | */ |
| 4974 | static int move_tasks(struct lb_env *env) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4975 | { |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 4976 | struct list_head *tasks = &env->src_rq->cfs_tasks; |
| 4977 | struct task_struct *p; |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 4978 | unsigned long load; |
| 4979 | int pulled = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4980 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4981 | if (env->imbalance <= 0) |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 4982 | return 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4983 | |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 4984 | while (!list_empty(tasks)) { |
| 4985 | p = list_first_entry(tasks, struct task_struct, se.group_node); |
| 4986 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 4987 | env->loop++; |
| 4988 | /* 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] | 4989 | if (env->loop > env->loop_max) |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 4990 | break; |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 4991 | |
| 4992 | /* take a breather every nr_migrate tasks */ |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 4993 | if (env->loop > env->loop_break) { |
Peter Zijlstra | eb95308 | 2012-04-17 13:38:40 +0200 | [diff] [blame] | 4994 | env->loop_break += sched_nr_migrate_break; |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4995 | env->flags |= LBF_NEED_BREAK; |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 4996 | break; |
Peter Zijlstra | a195f00 | 2011-09-22 15:30:18 +0200 | [diff] [blame] | 4997 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4998 | |
Joonsoo Kim | d319808 | 2013-04-23 17:27:40 +0900 | [diff] [blame] | 4999 | if (!can_migrate_task(p, env)) |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 5000 | goto next; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5001 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 5002 | load = task_h_load(p); |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 5003 | |
Peter Zijlstra | eb95308 | 2012-04-17 13:38:40 +0200 | [diff] [blame] | 5004 | if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed) |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 5005 | goto next; |
| 5006 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5007 | if ((load / 2) > env->imbalance) |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 5008 | goto next; |
| 5009 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 5010 | move_task(p, env); |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 5011 | pulled++; |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5012 | env->imbalance -= load; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5013 | |
| 5014 | #ifdef CONFIG_PREEMPT |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 5015 | /* |
| 5016 | * NEWIDLE balancing is a source of latency, so preemptible |
| 5017 | * kernels will stop after the first task is pulled to minimize |
| 5018 | * the critical section. |
| 5019 | */ |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 5020 | if (env->idle == CPU_NEWLY_IDLE) |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 5021 | break; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5022 | #endif |
| 5023 | |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 5024 | /* |
| 5025 | * We only want to steal up to the prescribed amount of |
| 5026 | * weighted load. |
| 5027 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5028 | if (env->imbalance <= 0) |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 5029 | break; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5030 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 5031 | continue; |
| 5032 | next: |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 5033 | list_move_tail(&p->se.group_node, tasks); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5034 | } |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 5035 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5036 | /* |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 5037 | * Right now, this is one of only two places move_task() is called, |
| 5038 | * so we can safely collect move_task() stats here rather than |
| 5039 | * inside move_task(). |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5040 | */ |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 5041 | schedstat_add(env->sd, lb_gained[env->idle], pulled); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5042 | |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 5043 | return pulled; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5044 | } |
| 5045 | |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 5046 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 5047 | /* |
| 5048 | * update tg->load_weight by folding this cpu's load_avg |
| 5049 | */ |
Paul Turner | 48a1675 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 5050 | static void __update_blocked_averages_cpu(struct task_group *tg, int cpu) |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 5051 | { |
Paul Turner | 48a1675 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 5052 | struct sched_entity *se = tg->se[cpu]; |
| 5053 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu]; |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 5054 | |
Paul Turner | 48a1675 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 5055 | /* throttled entities do not contribute to load */ |
| 5056 | if (throttled_hierarchy(cfs_rq)) |
| 5057 | return; |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 5058 | |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 5059 | update_cfs_rq_blocked_load(cfs_rq, 1); |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 5060 | |
Paul Turner | 8295836 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 5061 | if (se) { |
| 5062 | update_entity_load_avg(se, 1); |
| 5063 | /* |
| 5064 | * We pivot on our runnable average having decayed to zero for |
| 5065 | * list removal. This generally implies that all our children |
| 5066 | * have also been removed (modulo rounding error or bandwidth |
| 5067 | * control); however, such cases are rare and we can fix these |
| 5068 | * at enqueue. |
| 5069 | * |
| 5070 | * TODO: fix up out-of-order children on enqueue. |
| 5071 | */ |
| 5072 | if (!se->avg.runnable_avg_sum && !cfs_rq->nr_running) |
| 5073 | list_del_leaf_cfs_rq(cfs_rq); |
| 5074 | } else { |
Paul Turner | 48a1675 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 5075 | struct rq *rq = rq_of(cfs_rq); |
Paul Turner | 8295836 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 5076 | update_rq_runnable_avg(rq, rq->nr_running); |
| 5077 | } |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 5078 | } |
| 5079 | |
Paul Turner | 48a1675 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 5080 | static void update_blocked_averages(int cpu) |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 5081 | { |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 5082 | struct rq *rq = cpu_rq(cpu); |
Paul Turner | 48a1675 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 5083 | struct cfs_rq *cfs_rq; |
| 5084 | unsigned long flags; |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 5085 | |
Paul Turner | 48a1675 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 5086 | raw_spin_lock_irqsave(&rq->lock, flags); |
| 5087 | update_rq_clock(rq); |
Peter Zijlstra | 9763b67 | 2011-07-13 13:09:25 +0200 | [diff] [blame] | 5088 | /* |
| 5089 | * Iterates the task_group tree in a bottom up fashion, see |
| 5090 | * list_add_leaf_cfs_rq() for details. |
| 5091 | */ |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 5092 | for_each_leaf_cfs_rq(rq, cfs_rq) { |
Paul Turner | 48a1675 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 5093 | /* |
| 5094 | * Note: We may want to consider periodically releasing |
| 5095 | * rq->lock about these updates so that creating many task |
| 5096 | * groups does not result in continually extending hold time. |
| 5097 | */ |
| 5098 | __update_blocked_averages_cpu(cfs_rq->tg, rq->cpu); |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 5099 | } |
Paul Turner | 48a1675 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 5100 | |
| 5101 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 5102 | } |
| 5103 | |
Peter Zijlstra | 9763b67 | 2011-07-13 13:09:25 +0200 | [diff] [blame] | 5104 | /* |
Vladimir Davydov | 6852079 | 2013-07-15 17:49:19 +0400 | [diff] [blame] | 5105 | * Compute the hierarchical load factor for cfs_rq and all its ascendants. |
Peter Zijlstra | 9763b67 | 2011-07-13 13:09:25 +0200 | [diff] [blame] | 5106 | * This needs to be done in a top-down fashion because the load of a child |
| 5107 | * group is a fraction of its parents load. |
| 5108 | */ |
Vladimir Davydov | 6852079 | 2013-07-15 17:49:19 +0400 | [diff] [blame] | 5109 | static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq) |
Peter Zijlstra | 9763b67 | 2011-07-13 13:09:25 +0200 | [diff] [blame] | 5110 | { |
Vladimir Davydov | 6852079 | 2013-07-15 17:49:19 +0400 | [diff] [blame] | 5111 | struct rq *rq = rq_of(cfs_rq); |
| 5112 | struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; |
Peter Zijlstra | a35b646 | 2012-08-08 21:46:40 +0200 | [diff] [blame] | 5113 | unsigned long now = jiffies; |
Vladimir Davydov | 6852079 | 2013-07-15 17:49:19 +0400 | [diff] [blame] | 5114 | unsigned long load; |
Peter Zijlstra | a35b646 | 2012-08-08 21:46:40 +0200 | [diff] [blame] | 5115 | |
Vladimir Davydov | 6852079 | 2013-07-15 17:49:19 +0400 | [diff] [blame] | 5116 | if (cfs_rq->last_h_load_update == now) |
Peter Zijlstra | a35b646 | 2012-08-08 21:46:40 +0200 | [diff] [blame] | 5117 | return; |
| 5118 | |
Vladimir Davydov | 6852079 | 2013-07-15 17:49:19 +0400 | [diff] [blame] | 5119 | cfs_rq->h_load_next = NULL; |
| 5120 | for_each_sched_entity(se) { |
| 5121 | cfs_rq = cfs_rq_of(se); |
| 5122 | cfs_rq->h_load_next = se; |
| 5123 | if (cfs_rq->last_h_load_update == now) |
| 5124 | break; |
| 5125 | } |
Peter Zijlstra | a35b646 | 2012-08-08 21:46:40 +0200 | [diff] [blame] | 5126 | |
Vladimir Davydov | 6852079 | 2013-07-15 17:49:19 +0400 | [diff] [blame] | 5127 | if (!se) { |
Vladimir Davydov | 7e3115e | 2013-09-14 19:39:46 +0400 | [diff] [blame] | 5128 | cfs_rq->h_load = cfs_rq->runnable_load_avg; |
Vladimir Davydov | 6852079 | 2013-07-15 17:49:19 +0400 | [diff] [blame] | 5129 | cfs_rq->last_h_load_update = now; |
| 5130 | } |
| 5131 | |
| 5132 | while ((se = cfs_rq->h_load_next) != NULL) { |
| 5133 | load = cfs_rq->h_load; |
| 5134 | load = div64_ul(load * se->avg.load_avg_contrib, |
| 5135 | cfs_rq->runnable_load_avg + 1); |
| 5136 | cfs_rq = group_cfs_rq(se); |
| 5137 | cfs_rq->h_load = load; |
| 5138 | cfs_rq->last_h_load_update = now; |
| 5139 | } |
Peter Zijlstra | 9763b67 | 2011-07-13 13:09:25 +0200 | [diff] [blame] | 5140 | } |
| 5141 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 5142 | static unsigned long task_h_load(struct task_struct *p) |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 5143 | { |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 5144 | struct cfs_rq *cfs_rq = task_cfs_rq(p); |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 5145 | |
Vladimir Davydov | 6852079 | 2013-07-15 17:49:19 +0400 | [diff] [blame] | 5146 | update_cfs_rq_h_load(cfs_rq); |
Alex Shi | a003a25 | 2013-06-20 10:18:51 +0800 | [diff] [blame] | 5147 | return div64_ul(p->se.avg.load_avg_contrib * cfs_rq->h_load, |
| 5148 | cfs_rq->runnable_load_avg + 1); |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 5149 | } |
| 5150 | #else |
Paul Turner | 48a1675 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 5151 | static inline void update_blocked_averages(int cpu) |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 5152 | { |
| 5153 | } |
| 5154 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 5155 | static unsigned long task_h_load(struct task_struct *p) |
| 5156 | { |
Alex Shi | a003a25 | 2013-06-20 10:18:51 +0800 | [diff] [blame] | 5157 | return p->se.avg.load_avg_contrib; |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 5158 | } |
| 5159 | #endif |
| 5160 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5161 | /********** Helpers for find_busiest_group ************************/ |
| 5162 | /* |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5163 | * sg_lb_stats - stats of a sched_group required for load_balancing |
| 5164 | */ |
| 5165 | struct sg_lb_stats { |
| 5166 | unsigned long avg_load; /*Avg load across the CPUs of the group */ |
| 5167 | unsigned long group_load; /* Total load over the CPUs of the group */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5168 | unsigned long sum_weighted_load; /* Weighted load of group's tasks */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5169 | unsigned long load_per_task; |
Peter Zijlstra | 3ae11c9 | 2013-08-15 20:37:48 +0200 | [diff] [blame] | 5170 | unsigned long group_power; |
Peter Zijlstra | 147c5fc | 2013-08-19 15:22:57 +0200 | [diff] [blame] | 5171 | unsigned int sum_nr_running; /* Nr tasks running in the group */ |
| 5172 | unsigned int group_capacity; |
| 5173 | unsigned int idle_cpus; |
| 5174 | unsigned int group_weight; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5175 | int group_imb; /* Is there an imbalance in the group ? */ |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 5176 | int group_has_capacity; /* Is there extra capacity in the group? */ |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 5177 | #ifdef CONFIG_NUMA_BALANCING |
| 5178 | unsigned int nr_numa_running; |
| 5179 | unsigned int nr_preferred_running; |
| 5180 | #endif |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5181 | }; |
| 5182 | |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5183 | /* |
| 5184 | * sd_lb_stats - Structure to store the statistics of a sched_domain |
| 5185 | * during load balancing. |
| 5186 | */ |
| 5187 | struct sd_lb_stats { |
| 5188 | struct sched_group *busiest; /* Busiest group in this sd */ |
| 5189 | struct sched_group *local; /* Local group in this sd */ |
| 5190 | unsigned long total_load; /* Total load of all groups in sd */ |
| 5191 | unsigned long total_pwr; /* Total power of all groups in sd */ |
| 5192 | unsigned long avg_load; /* Average load across all groups in sd */ |
| 5193 | |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5194 | struct sg_lb_stats busiest_stat;/* Statistics of the busiest group */ |
Peter Zijlstra | 147c5fc | 2013-08-19 15:22:57 +0200 | [diff] [blame] | 5195 | struct sg_lb_stats local_stat; /* Statistics of the local group */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5196 | }; |
| 5197 | |
Peter Zijlstra | 147c5fc | 2013-08-19 15:22:57 +0200 | [diff] [blame] | 5198 | static inline void init_sd_lb_stats(struct sd_lb_stats *sds) |
| 5199 | { |
| 5200 | /* |
| 5201 | * Skimp on the clearing to avoid duplicate work. We can avoid clearing |
| 5202 | * local_stat because update_sg_lb_stats() does a full clear/assignment. |
| 5203 | * We must however clear busiest_stat::avg_load because |
| 5204 | * update_sd_pick_busiest() reads this before assignment. |
| 5205 | */ |
| 5206 | *sds = (struct sd_lb_stats){ |
| 5207 | .busiest = NULL, |
| 5208 | .local = NULL, |
| 5209 | .total_load = 0UL, |
| 5210 | .total_pwr = 0UL, |
| 5211 | .busiest_stat = { |
| 5212 | .avg_load = 0UL, |
| 5213 | }, |
| 5214 | }; |
| 5215 | } |
| 5216 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5217 | /** |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5218 | * get_sd_load_idx - Obtain the load index for a given sched domain. |
| 5219 | * @sd: The sched_domain whose load_idx is to be obtained. |
Kamalesh Babulal | ed1b773 | 2013-10-13 23:06:15 +0530 | [diff] [blame] | 5220 | * @idle: The idle status of the CPU for whose sd load_idx is obtained. |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 5221 | * |
| 5222 | * Return: The load index. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5223 | */ |
| 5224 | static inline int get_sd_load_idx(struct sched_domain *sd, |
| 5225 | enum cpu_idle_type idle) |
| 5226 | { |
| 5227 | int load_idx; |
| 5228 | |
| 5229 | switch (idle) { |
| 5230 | case CPU_NOT_IDLE: |
| 5231 | load_idx = sd->busy_idx; |
| 5232 | break; |
| 5233 | |
| 5234 | case CPU_NEWLY_IDLE: |
| 5235 | load_idx = sd->newidle_idx; |
| 5236 | break; |
| 5237 | default: |
| 5238 | load_idx = sd->idle_idx; |
| 5239 | break; |
| 5240 | } |
| 5241 | |
| 5242 | return load_idx; |
| 5243 | } |
| 5244 | |
Li Zefan | 15f803c | 2013-03-05 16:07:11 +0800 | [diff] [blame] | 5245 | static unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5246 | { |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 5247 | return SCHED_POWER_SCALE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5248 | } |
| 5249 | |
| 5250 | unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu) |
| 5251 | { |
| 5252 | return default_scale_freq_power(sd, cpu); |
| 5253 | } |
| 5254 | |
Li Zefan | 15f803c | 2013-03-05 16:07:11 +0800 | [diff] [blame] | 5255 | static unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5256 | { |
Peter Zijlstra | 669c55e | 2010-04-16 14:59:29 +0200 | [diff] [blame] | 5257 | unsigned long weight = sd->span_weight; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5258 | unsigned long smt_gain = sd->smt_gain; |
| 5259 | |
| 5260 | smt_gain /= weight; |
| 5261 | |
| 5262 | return smt_gain; |
| 5263 | } |
| 5264 | |
| 5265 | unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu) |
| 5266 | { |
| 5267 | return default_scale_smt_power(sd, cpu); |
| 5268 | } |
| 5269 | |
Li Zefan | 15f803c | 2013-03-05 16:07:11 +0800 | [diff] [blame] | 5270 | static unsigned long scale_rt_power(int cpu) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5271 | { |
| 5272 | struct rq *rq = cpu_rq(cpu); |
Peter Zijlstra | b654f7d | 2012-05-22 14:04:28 +0200 | [diff] [blame] | 5273 | u64 total, available, age_stamp, avg; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5274 | |
Peter Zijlstra | b654f7d | 2012-05-22 14:04:28 +0200 | [diff] [blame] | 5275 | /* |
| 5276 | * Since we're reading these variables without serialization make sure |
| 5277 | * we read them once before doing sanity checks on them. |
| 5278 | */ |
| 5279 | age_stamp = ACCESS_ONCE(rq->age_stamp); |
| 5280 | avg = ACCESS_ONCE(rq->rt_avg); |
Venkatesh Pallipadi | aa48380 | 2010-10-04 17:03:22 -0700 | [diff] [blame] | 5281 | |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 5282 | total = sched_avg_period() + (rq_clock(rq) - age_stamp); |
Peter Zijlstra | b654f7d | 2012-05-22 14:04:28 +0200 | [diff] [blame] | 5283 | |
| 5284 | if (unlikely(total < avg)) { |
Venkatesh Pallipadi | aa48380 | 2010-10-04 17:03:22 -0700 | [diff] [blame] | 5285 | /* Ensures that power won't end up being negative */ |
| 5286 | available = 0; |
| 5287 | } else { |
Peter Zijlstra | b654f7d | 2012-05-22 14:04:28 +0200 | [diff] [blame] | 5288 | available = total - avg; |
Venkatesh Pallipadi | aa48380 | 2010-10-04 17:03:22 -0700 | [diff] [blame] | 5289 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5290 | |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 5291 | if (unlikely((s64)total < SCHED_POWER_SCALE)) |
| 5292 | total = SCHED_POWER_SCALE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5293 | |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 5294 | total >>= SCHED_POWER_SHIFT; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5295 | |
| 5296 | return div_u64(available, total); |
| 5297 | } |
| 5298 | |
| 5299 | static void update_cpu_power(struct sched_domain *sd, int cpu) |
| 5300 | { |
Peter Zijlstra | 669c55e | 2010-04-16 14:59:29 +0200 | [diff] [blame] | 5301 | unsigned long weight = sd->span_weight; |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 5302 | unsigned long power = SCHED_POWER_SCALE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5303 | struct sched_group *sdg = sd->groups; |
| 5304 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5305 | if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) { |
| 5306 | if (sched_feat(ARCH_POWER)) |
| 5307 | power *= arch_scale_smt_power(sd, cpu); |
| 5308 | else |
| 5309 | power *= default_scale_smt_power(sd, cpu); |
| 5310 | |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 5311 | power >>= SCHED_POWER_SHIFT; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5312 | } |
| 5313 | |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 5314 | sdg->sgp->power_orig = power; |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5315 | |
| 5316 | if (sched_feat(ARCH_POWER)) |
| 5317 | power *= arch_scale_freq_power(sd, cpu); |
| 5318 | else |
| 5319 | power *= default_scale_freq_power(sd, cpu); |
| 5320 | |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 5321 | power >>= SCHED_POWER_SHIFT; |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5322 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5323 | power *= scale_rt_power(cpu); |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 5324 | power >>= SCHED_POWER_SHIFT; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5325 | |
| 5326 | if (!power) |
| 5327 | power = 1; |
| 5328 | |
Peter Zijlstra | e51fd5e | 2010-05-31 12:37:30 +0200 | [diff] [blame] | 5329 | cpu_rq(cpu)->cpu_power = power; |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 5330 | sdg->sgp->power = power; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5331 | } |
| 5332 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5333 | void update_group_power(struct sched_domain *sd, int cpu) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5334 | { |
| 5335 | struct sched_domain *child = sd->child; |
| 5336 | struct sched_group *group, *sdg = sd->groups; |
Peter Zijlstra | 863bffc | 2013-08-28 11:44:39 +0200 | [diff] [blame] | 5337 | unsigned long power, power_orig; |
Vincent Guittot | 4ec4412 | 2011-12-12 20:21:08 +0100 | [diff] [blame] | 5338 | unsigned long interval; |
| 5339 | |
| 5340 | interval = msecs_to_jiffies(sd->balance_interval); |
| 5341 | interval = clamp(interval, 1UL, max_load_balance_interval); |
| 5342 | sdg->sgp->next_update = jiffies + interval; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5343 | |
| 5344 | if (!child) { |
| 5345 | update_cpu_power(sd, cpu); |
| 5346 | return; |
| 5347 | } |
| 5348 | |
Peter Zijlstra | 863bffc | 2013-08-28 11:44:39 +0200 | [diff] [blame] | 5349 | power_orig = power = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5350 | |
Peter Zijlstra | 74a5ce2 | 2012-05-23 18:00:43 +0200 | [diff] [blame] | 5351 | if (child->flags & SD_OVERLAP) { |
| 5352 | /* |
| 5353 | * SD_OVERLAP domains cannot assume that child groups |
| 5354 | * span the current group. |
| 5355 | */ |
| 5356 | |
Peter Zijlstra | 863bffc | 2013-08-28 11:44:39 +0200 | [diff] [blame] | 5357 | for_each_cpu(cpu, sched_group_cpus(sdg)) { |
Srikar Dronamraju | 9abf24d | 2013-11-12 22:11:26 +0530 | [diff] [blame] | 5358 | struct sched_group_power *sgp; |
| 5359 | struct rq *rq = cpu_rq(cpu); |
Peter Zijlstra | 863bffc | 2013-08-28 11:44:39 +0200 | [diff] [blame] | 5360 | |
Srikar Dronamraju | 9abf24d | 2013-11-12 22:11:26 +0530 | [diff] [blame] | 5361 | /* |
| 5362 | * build_sched_domains() -> init_sched_groups_power() |
| 5363 | * gets here before we've attached the domains to the |
| 5364 | * runqueues. |
| 5365 | * |
| 5366 | * Use power_of(), which is set irrespective of domains |
| 5367 | * in update_cpu_power(). |
| 5368 | * |
| 5369 | * This avoids power/power_orig from being 0 and |
| 5370 | * causing divide-by-zero issues on boot. |
| 5371 | * |
| 5372 | * Runtime updates will correct power_orig. |
| 5373 | */ |
| 5374 | if (unlikely(!rq->sd)) { |
| 5375 | power_orig += power_of(cpu); |
| 5376 | power += power_of(cpu); |
| 5377 | continue; |
| 5378 | } |
| 5379 | |
| 5380 | sgp = rq->sd->groups->sgp; |
| 5381 | power_orig += sgp->power_orig; |
| 5382 | power += sgp->power; |
Peter Zijlstra | 863bffc | 2013-08-28 11:44:39 +0200 | [diff] [blame] | 5383 | } |
Peter Zijlstra | 74a5ce2 | 2012-05-23 18:00:43 +0200 | [diff] [blame] | 5384 | } else { |
| 5385 | /* |
| 5386 | * !SD_OVERLAP domains can assume that child groups |
| 5387 | * span the current group. |
| 5388 | */ |
| 5389 | |
| 5390 | group = child->groups; |
| 5391 | do { |
Peter Zijlstra | 863bffc | 2013-08-28 11:44:39 +0200 | [diff] [blame] | 5392 | power_orig += group->sgp->power_orig; |
Peter Zijlstra | 74a5ce2 | 2012-05-23 18:00:43 +0200 | [diff] [blame] | 5393 | power += group->sgp->power; |
| 5394 | group = group->next; |
| 5395 | } while (group != child->groups); |
| 5396 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5397 | |
Peter Zijlstra | 863bffc | 2013-08-28 11:44:39 +0200 | [diff] [blame] | 5398 | sdg->sgp->power_orig = power_orig; |
| 5399 | sdg->sgp->power = power; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5400 | } |
| 5401 | |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5402 | /* |
| 5403 | * Try and fix up capacity for tiny siblings, this is needed when |
| 5404 | * things like SD_ASYM_PACKING need f_b_g to select another sibling |
| 5405 | * which on its own isn't powerful enough. |
| 5406 | * |
| 5407 | * See update_sd_pick_busiest() and check_asym_packing(). |
| 5408 | */ |
| 5409 | static inline int |
| 5410 | fix_small_capacity(struct sched_domain *sd, struct sched_group *group) |
| 5411 | { |
| 5412 | /* |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 5413 | * Only siblings can have significantly less than SCHED_POWER_SCALE |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5414 | */ |
Peter Zijlstra | a6c75f2 | 2011-04-07 14:09:52 +0200 | [diff] [blame] | 5415 | if (!(sd->flags & SD_SHARE_CPUPOWER)) |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5416 | return 0; |
| 5417 | |
| 5418 | /* |
| 5419 | * If ~90% of the cpu_power is still there, we're good. |
| 5420 | */ |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 5421 | if (group->sgp->power * 32 > group->sgp->power_orig * 29) |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5422 | return 1; |
| 5423 | |
| 5424 | return 0; |
| 5425 | } |
| 5426 | |
Peter Zijlstra | 30ce5da | 2013-08-15 20:29:29 +0200 | [diff] [blame] | 5427 | /* |
| 5428 | * Group imbalance indicates (and tries to solve) the problem where balancing |
| 5429 | * groups is inadequate due to tsk_cpus_allowed() constraints. |
| 5430 | * |
| 5431 | * Imagine a situation of two groups of 4 cpus each and 4 tasks each with a |
| 5432 | * cpumask covering 1 cpu of the first group and 3 cpus of the second group. |
| 5433 | * Something like: |
| 5434 | * |
| 5435 | * { 0 1 2 3 } { 4 5 6 7 } |
| 5436 | * * * * * |
| 5437 | * |
| 5438 | * If we were to balance group-wise we'd place two tasks in the first group and |
| 5439 | * two tasks in the second group. Clearly this is undesired as it will overload |
| 5440 | * cpu 3 and leave one of the cpus in the second group unused. |
| 5441 | * |
| 5442 | * The current solution to this issue is detecting the skew in the first group |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 5443 | * by noticing the lower domain failed to reach balance and had difficulty |
| 5444 | * moving tasks due to affinity constraints. |
Peter Zijlstra | 30ce5da | 2013-08-15 20:29:29 +0200 | [diff] [blame] | 5445 | * |
| 5446 | * When this is so detected; this group becomes a candidate for busiest; see |
Kamalesh Babulal | ed1b773 | 2013-10-13 23:06:15 +0530 | [diff] [blame] | 5447 | * update_sd_pick_busiest(). And calculate_imbalance() and |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 5448 | * find_busiest_group() avoid some of the usual balance conditions to allow it |
Peter Zijlstra | 30ce5da | 2013-08-15 20:29:29 +0200 | [diff] [blame] | 5449 | * to create an effective group imbalance. |
| 5450 | * |
| 5451 | * This is a somewhat tricky proposition since the next run might not find the |
| 5452 | * group imbalance and decide the groups need to be balanced again. A most |
| 5453 | * subtle and fragile situation. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5454 | */ |
Peter Zijlstra | 30ce5da | 2013-08-15 20:29:29 +0200 | [diff] [blame] | 5455 | |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 5456 | static inline int sg_imbalanced(struct sched_group *group) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5457 | { |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 5458 | return group->sgp->imbalance; |
Peter Zijlstra | 30ce5da | 2013-08-15 20:29:29 +0200 | [diff] [blame] | 5459 | } |
| 5460 | |
Peter Zijlstra | b37d931 | 2013-08-28 11:50:34 +0200 | [diff] [blame] | 5461 | /* |
| 5462 | * Compute the group capacity. |
| 5463 | * |
Peter Zijlstra | c61037e | 2013-08-28 12:40:38 +0200 | [diff] [blame] | 5464 | * Avoid the issue where N*frac(smt_power) >= 1 creates 'phantom' cores by |
| 5465 | * first dividing out the smt factor and computing the actual number of cores |
| 5466 | * and limit power unit capacity with that. |
Peter Zijlstra | b37d931 | 2013-08-28 11:50:34 +0200 | [diff] [blame] | 5467 | */ |
| 5468 | static inline int sg_capacity(struct lb_env *env, struct sched_group *group) |
| 5469 | { |
Peter Zijlstra | c61037e | 2013-08-28 12:40:38 +0200 | [diff] [blame] | 5470 | unsigned int capacity, smt, cpus; |
| 5471 | unsigned int power, power_orig; |
Peter Zijlstra | b37d931 | 2013-08-28 11:50:34 +0200 | [diff] [blame] | 5472 | |
Peter Zijlstra | c61037e | 2013-08-28 12:40:38 +0200 | [diff] [blame] | 5473 | power = group->sgp->power; |
| 5474 | power_orig = group->sgp->power_orig; |
| 5475 | cpus = group->group_weight; |
Peter Zijlstra | b37d931 | 2013-08-28 11:50:34 +0200 | [diff] [blame] | 5476 | |
Peter Zijlstra | c61037e | 2013-08-28 12:40:38 +0200 | [diff] [blame] | 5477 | /* smt := ceil(cpus / power), assumes: 1 < smt_power < 2 */ |
| 5478 | smt = DIV_ROUND_UP(SCHED_POWER_SCALE * cpus, power_orig); |
| 5479 | capacity = cpus / smt; /* cores */ |
| 5480 | |
| 5481 | capacity = min_t(unsigned, capacity, DIV_ROUND_CLOSEST(power, SCHED_POWER_SCALE)); |
Peter Zijlstra | b37d931 | 2013-08-28 11:50:34 +0200 | [diff] [blame] | 5482 | if (!capacity) |
| 5483 | capacity = fix_small_capacity(env->sd, group); |
| 5484 | |
| 5485 | return capacity; |
| 5486 | } |
| 5487 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5488 | /** |
| 5489 | * update_sg_lb_stats - Update sched_group's statistics for load balancing. |
| 5490 | * @env: The load balancing environment. |
| 5491 | * @group: sched_group whose statistics are to be updated. |
| 5492 | * @load_idx: Load index of sched_domain of this_cpu for load calc. |
| 5493 | * @local_group: Does group contain this_cpu. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5494 | * @sgs: variable to hold the statistics for this group. |
| 5495 | */ |
| 5496 | static inline void update_sg_lb_stats(struct lb_env *env, |
| 5497 | struct sched_group *group, int load_idx, |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 5498 | int local_group, struct sg_lb_stats *sgs) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5499 | { |
Peter Zijlstra | 30ce5da | 2013-08-15 20:29:29 +0200 | [diff] [blame] | 5500 | unsigned long load; |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5501 | int i; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5502 | |
Peter Zijlstra | b72ff13 | 2013-08-28 10:32:32 +0200 | [diff] [blame] | 5503 | memset(sgs, 0, sizeof(*sgs)); |
| 5504 | |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 5505 | for_each_cpu_and(i, sched_group_cpus(group), env->cpus) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5506 | struct rq *rq = cpu_rq(i); |
| 5507 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5508 | /* Bias balancing toward cpus of our domain */ |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 5509 | if (local_group) |
Peter Zijlstra | 04f733b | 2012-05-11 00:12:02 +0200 | [diff] [blame] | 5510 | load = target_load(i, load_idx); |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 5511 | else |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5512 | load = source_load(i, load_idx); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5513 | |
| 5514 | sgs->group_load += load; |
Kamalesh Babulal | 380c907 | 2013-11-15 15:06:52 +0530 | [diff] [blame] | 5515 | sgs->sum_nr_running += rq->nr_running; |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 5516 | #ifdef CONFIG_NUMA_BALANCING |
| 5517 | sgs->nr_numa_running += rq->nr_numa_running; |
| 5518 | sgs->nr_preferred_running += rq->nr_preferred_running; |
| 5519 | #endif |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5520 | sgs->sum_weighted_load += weighted_cpuload(i); |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 5521 | if (idle_cpu(i)) |
| 5522 | sgs->idle_cpus++; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5523 | } |
| 5524 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5525 | /* Adjust by relative CPU power of the group */ |
Peter Zijlstra | 3ae11c9 | 2013-08-15 20:37:48 +0200 | [diff] [blame] | 5526 | sgs->group_power = group->sgp->power; |
| 5527 | sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / sgs->group_power; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5528 | |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 5529 | if (sgs->sum_nr_running) |
Peter Zijlstra | 38d0f77 | 2013-08-15 19:47:56 +0200 | [diff] [blame] | 5530 | sgs->load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5531 | |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 5532 | sgs->group_weight = group->group_weight; |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 5533 | |
Peter Zijlstra | b37d931 | 2013-08-28 11:50:34 +0200 | [diff] [blame] | 5534 | sgs->group_imb = sg_imbalanced(group); |
| 5535 | sgs->group_capacity = sg_capacity(env, group); |
| 5536 | |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 5537 | if (sgs->group_capacity > sgs->sum_nr_running) |
| 5538 | sgs->group_has_capacity = 1; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5539 | } |
| 5540 | |
| 5541 | /** |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5542 | * update_sd_pick_busiest - return 1 on busiest group |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 5543 | * @env: The load balancing environment. |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5544 | * @sds: sched_domain statistics |
| 5545 | * @sg: sched_group candidate to be checked for being the busiest |
Michael Neuling | b6b1229 | 2010-06-10 12:06:21 +1000 | [diff] [blame] | 5546 | * @sgs: sched_group statistics |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5547 | * |
| 5548 | * Determine if @sg is a busier group than the previously selected |
| 5549 | * busiest group. |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 5550 | * |
| 5551 | * Return: %true if @sg is a busier group than the previously selected |
| 5552 | * busiest group. %false otherwise. |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5553 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5554 | static bool update_sd_pick_busiest(struct lb_env *env, |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5555 | struct sd_lb_stats *sds, |
| 5556 | struct sched_group *sg, |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5557 | struct sg_lb_stats *sgs) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5558 | { |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5559 | if (sgs->avg_load <= sds->busiest_stat.avg_load) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5560 | return false; |
| 5561 | |
| 5562 | if (sgs->sum_nr_running > sgs->group_capacity) |
| 5563 | return true; |
| 5564 | |
| 5565 | if (sgs->group_imb) |
| 5566 | return true; |
| 5567 | |
| 5568 | /* |
| 5569 | * ASYM_PACKING needs to move all the work to the lowest |
| 5570 | * numbered CPUs in the group, therefore mark all groups |
| 5571 | * higher than ourself as busy. |
| 5572 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5573 | if ((env->sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running && |
| 5574 | env->dst_cpu < group_first_cpu(sg)) { |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5575 | if (!sds->busiest) |
| 5576 | return true; |
| 5577 | |
| 5578 | if (group_first_cpu(sds->busiest) > group_first_cpu(sg)) |
| 5579 | return true; |
| 5580 | } |
| 5581 | |
| 5582 | return false; |
| 5583 | } |
| 5584 | |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 5585 | #ifdef CONFIG_NUMA_BALANCING |
| 5586 | static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs) |
| 5587 | { |
| 5588 | if (sgs->sum_nr_running > sgs->nr_numa_running) |
| 5589 | return regular; |
| 5590 | if (sgs->sum_nr_running > sgs->nr_preferred_running) |
| 5591 | return remote; |
| 5592 | return all; |
| 5593 | } |
| 5594 | |
| 5595 | static inline enum fbq_type fbq_classify_rq(struct rq *rq) |
| 5596 | { |
| 5597 | if (rq->nr_running > rq->nr_numa_running) |
| 5598 | return regular; |
| 5599 | if (rq->nr_running > rq->nr_preferred_running) |
| 5600 | return remote; |
| 5601 | return all; |
| 5602 | } |
| 5603 | #else |
| 5604 | static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs) |
| 5605 | { |
| 5606 | return all; |
| 5607 | } |
| 5608 | |
| 5609 | static inline enum fbq_type fbq_classify_rq(struct rq *rq) |
| 5610 | { |
| 5611 | return regular; |
| 5612 | } |
| 5613 | #endif /* CONFIG_NUMA_BALANCING */ |
| 5614 | |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5615 | /** |
Hui Kang | 461819a | 2011-10-11 23:00:59 -0400 | [diff] [blame] | 5616 | * update_sd_lb_stats - Update sched_domain's statistics for load balancing. |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 5617 | * @env: The load balancing environment. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5618 | * @sds: variable to hold the statistics for this sched_domain. |
| 5619 | */ |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 5620 | static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sds) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5621 | { |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5622 | struct sched_domain *child = env->sd->child; |
| 5623 | struct sched_group *sg = env->sd->groups; |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5624 | struct sg_lb_stats tmp_sgs; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5625 | int load_idx, prefer_sibling = 0; |
| 5626 | |
| 5627 | if (child && child->flags & SD_PREFER_SIBLING) |
| 5628 | prefer_sibling = 1; |
| 5629 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5630 | load_idx = get_sd_load_idx(env->sd, env->idle); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5631 | |
| 5632 | do { |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5633 | struct sg_lb_stats *sgs = &tmp_sgs; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5634 | int local_group; |
| 5635 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5636 | local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg)); |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5637 | if (local_group) { |
| 5638 | sds->local = sg; |
| 5639 | sgs = &sds->local_stat; |
Peter Zijlstra | b72ff13 | 2013-08-28 10:32:32 +0200 | [diff] [blame] | 5640 | |
| 5641 | if (env->idle != CPU_NEWLY_IDLE || |
| 5642 | time_after_eq(jiffies, sg->sgp->next_update)) |
| 5643 | update_group_power(env->sd, env->dst_cpu); |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5644 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5645 | |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5646 | update_sg_lb_stats(env, sg, load_idx, local_group, sgs); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5647 | |
Peter Zijlstra | b72ff13 | 2013-08-28 10:32:32 +0200 | [diff] [blame] | 5648 | if (local_group) |
| 5649 | goto next_group; |
| 5650 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5651 | /* |
| 5652 | * In case the child domain prefers tasks go to siblings |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5653 | * first, lower the sg capacity to one so that we'll try |
Nikhil Rao | 75dd321 | 2010-10-15 13:12:30 -0700 | [diff] [blame] | 5654 | * and move all the excess tasks away. We lower the capacity |
| 5655 | * of a group only if the local group has the capacity to fit |
| 5656 | * these excess tasks, i.e. nr_running < group_capacity. The |
| 5657 | * extra check prevents the case where you always pull from the |
| 5658 | * heaviest group when it is already under-utilized (possible |
| 5659 | * with a large weight task outweighs the tasks on the system). |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5660 | */ |
Peter Zijlstra | b72ff13 | 2013-08-28 10:32:32 +0200 | [diff] [blame] | 5661 | if (prefer_sibling && sds->local && |
| 5662 | sds->local_stat.group_has_capacity) |
Peter Zijlstra | 147c5fc | 2013-08-19 15:22:57 +0200 | [diff] [blame] | 5663 | sgs->group_capacity = min(sgs->group_capacity, 1U); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5664 | |
Peter Zijlstra | b72ff13 | 2013-08-28 10:32:32 +0200 | [diff] [blame] | 5665 | if (update_sd_pick_busiest(env, sds, sg, sgs)) { |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5666 | sds->busiest = sg; |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5667 | sds->busiest_stat = *sgs; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5668 | } |
| 5669 | |
Peter Zijlstra | b72ff13 | 2013-08-28 10:32:32 +0200 | [diff] [blame] | 5670 | next_group: |
| 5671 | /* Now, start updating sd_lb_stats */ |
| 5672 | sds->total_load += sgs->group_load; |
| 5673 | sds->total_pwr += sgs->group_power; |
| 5674 | |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5675 | sg = sg->next; |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5676 | } while (sg != env->sd->groups); |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 5677 | |
| 5678 | if (env->sd->flags & SD_NUMA) |
| 5679 | env->fbq_type = fbq_classify_group(&sds->busiest_stat); |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5680 | } |
| 5681 | |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5682 | /** |
| 5683 | * check_asym_packing - Check to see if the group is packed into the |
| 5684 | * sched doman. |
| 5685 | * |
| 5686 | * This is primarily intended to used at the sibling level. Some |
| 5687 | * cores like POWER7 prefer to use lower numbered SMT threads. In the |
| 5688 | * case of POWER7, it can move to lower SMT modes only when higher |
| 5689 | * threads are idle. When in lower SMT modes, the threads will |
| 5690 | * perform better since they share less core resources. Hence when we |
| 5691 | * have idle threads, we want them to be the higher ones. |
| 5692 | * |
| 5693 | * This packing function is run on idle threads. It checks to see if |
| 5694 | * the busiest CPU in this domain (core in the P7 case) has a higher |
| 5695 | * CPU number than the packing function is being run on. Here we are |
| 5696 | * assuming lower CPU number will be equivalent to lower a SMT thread |
| 5697 | * number. |
| 5698 | * |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 5699 | * Return: 1 when packing is required and a task should be moved to |
Michael Neuling | b6b1229 | 2010-06-10 12:06:21 +1000 | [diff] [blame] | 5700 | * this CPU. The amount of the imbalance is returned in *imbalance. |
| 5701 | * |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 5702 | * @env: The load balancing environment. |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5703 | * @sds: Statistics of the sched_domain which is to be packed |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5704 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5705 | static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5706 | { |
| 5707 | int busiest_cpu; |
| 5708 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5709 | if (!(env->sd->flags & SD_ASYM_PACKING)) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5710 | return 0; |
| 5711 | |
| 5712 | if (!sds->busiest) |
| 5713 | return 0; |
| 5714 | |
| 5715 | busiest_cpu = group_first_cpu(sds->busiest); |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5716 | if (env->dst_cpu > busiest_cpu) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5717 | return 0; |
| 5718 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5719 | env->imbalance = DIV_ROUND_CLOSEST( |
Peter Zijlstra | 3ae11c9 | 2013-08-15 20:37:48 +0200 | [diff] [blame] | 5720 | sds->busiest_stat.avg_load * sds->busiest_stat.group_power, |
| 5721 | SCHED_POWER_SCALE); |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5722 | |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5723 | return 1; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5724 | } |
| 5725 | |
| 5726 | /** |
| 5727 | * fix_small_imbalance - Calculate the minor imbalance that exists |
| 5728 | * amongst the groups of a sched_domain, during |
| 5729 | * load balancing. |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 5730 | * @env: The load balancing environment. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5731 | * @sds: Statistics of the sched_domain whose imbalance is to be calculated. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5732 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5733 | static inline |
| 5734 | void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5735 | { |
| 5736 | unsigned long tmp, pwr_now = 0, pwr_move = 0; |
| 5737 | unsigned int imbn = 2; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 5738 | unsigned long scaled_busy_load_per_task; |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5739 | struct sg_lb_stats *local, *busiest; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5740 | |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5741 | local = &sds->local_stat; |
| 5742 | busiest = &sds->busiest_stat; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5743 | |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5744 | if (!local->sum_nr_running) |
| 5745 | local->load_per_task = cpu_avg_load_per_task(env->dst_cpu); |
| 5746 | else if (busiest->load_per_task > local->load_per_task) |
| 5747 | imbn = 1; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 5748 | |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5749 | scaled_busy_load_per_task = |
| 5750 | (busiest->load_per_task * SCHED_POWER_SCALE) / |
Peter Zijlstra | 3ae11c9 | 2013-08-15 20:37:48 +0200 | [diff] [blame] | 5751 | busiest->group_power; |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5752 | |
Vladimir Davydov | 3029ede | 2013-09-15 17:49:14 +0400 | [diff] [blame] | 5753 | if (busiest->avg_load + scaled_busy_load_per_task >= |
| 5754 | local->avg_load + (scaled_busy_load_per_task * imbn)) { |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5755 | env->imbalance = busiest->load_per_task; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5756 | return; |
| 5757 | } |
| 5758 | |
| 5759 | /* |
| 5760 | * OK, we don't have enough imbalance to justify moving tasks, |
| 5761 | * however we may be able to increase total CPU power used by |
| 5762 | * moving them. |
| 5763 | */ |
| 5764 | |
Peter Zijlstra | 3ae11c9 | 2013-08-15 20:37:48 +0200 | [diff] [blame] | 5765 | pwr_now += busiest->group_power * |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5766 | min(busiest->load_per_task, busiest->avg_load); |
Peter Zijlstra | 3ae11c9 | 2013-08-15 20:37:48 +0200 | [diff] [blame] | 5767 | pwr_now += local->group_power * |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5768 | min(local->load_per_task, local->avg_load); |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 5769 | pwr_now /= SCHED_POWER_SCALE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5770 | |
| 5771 | /* Amount of load we'd subtract */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5772 | tmp = (busiest->load_per_task * SCHED_POWER_SCALE) / |
Peter Zijlstra | 3ae11c9 | 2013-08-15 20:37:48 +0200 | [diff] [blame] | 5773 | busiest->group_power; |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5774 | if (busiest->avg_load > tmp) { |
Peter Zijlstra | 3ae11c9 | 2013-08-15 20:37:48 +0200 | [diff] [blame] | 5775 | pwr_move += busiest->group_power * |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5776 | min(busiest->load_per_task, |
| 5777 | busiest->avg_load - tmp); |
| 5778 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5779 | |
| 5780 | /* Amount of load we'd add */ |
Peter Zijlstra | 3ae11c9 | 2013-08-15 20:37:48 +0200 | [diff] [blame] | 5781 | if (busiest->avg_load * busiest->group_power < |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5782 | busiest->load_per_task * SCHED_POWER_SCALE) { |
Peter Zijlstra | 3ae11c9 | 2013-08-15 20:37:48 +0200 | [diff] [blame] | 5783 | tmp = (busiest->avg_load * busiest->group_power) / |
| 5784 | local->group_power; |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5785 | } else { |
| 5786 | tmp = (busiest->load_per_task * SCHED_POWER_SCALE) / |
Peter Zijlstra | 3ae11c9 | 2013-08-15 20:37:48 +0200 | [diff] [blame] | 5787 | local->group_power; |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5788 | } |
Peter Zijlstra | 3ae11c9 | 2013-08-15 20:37:48 +0200 | [diff] [blame] | 5789 | pwr_move += local->group_power * |
| 5790 | min(local->load_per_task, local->avg_load + tmp); |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 5791 | pwr_move /= SCHED_POWER_SCALE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5792 | |
| 5793 | /* Move if we gain throughput */ |
| 5794 | if (pwr_move > pwr_now) |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5795 | env->imbalance = busiest->load_per_task; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5796 | } |
| 5797 | |
| 5798 | /** |
| 5799 | * calculate_imbalance - Calculate the amount of imbalance present within the |
| 5800 | * groups of a given sched_domain during load balance. |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5801 | * @env: load balance environment |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5802 | * @sds: statistics of the sched_domain whose imbalance is to be calculated. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5803 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5804 | static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5805 | { |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 5806 | unsigned long max_pull, load_above_capacity = ~0UL; |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5807 | struct sg_lb_stats *local, *busiest; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 5808 | |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5809 | local = &sds->local_stat; |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5810 | busiest = &sds->busiest_stat; |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5811 | |
| 5812 | if (busiest->group_imb) { |
Peter Zijlstra | 30ce5da | 2013-08-15 20:29:29 +0200 | [diff] [blame] | 5813 | /* |
| 5814 | * In the group_imb case we cannot rely on group-wide averages |
| 5815 | * to ensure cpu-load equilibrium, look at wider averages. XXX |
| 5816 | */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5817 | busiest->load_per_task = |
| 5818 | min(busiest->load_per_task, sds->avg_load); |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 5819 | } |
| 5820 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5821 | /* |
| 5822 | * In the presence of smp nice balancing, certain scenarios can have |
| 5823 | * max load less than avg load(as we skip the groups at or below |
| 5824 | * its cpu_power, while calculating max_load..) |
| 5825 | */ |
Vladimir Davydov | b188555 | 2013-09-15 17:49:13 +0400 | [diff] [blame] | 5826 | if (busiest->avg_load <= sds->avg_load || |
| 5827 | local->avg_load >= sds->avg_load) { |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5828 | env->imbalance = 0; |
| 5829 | return fix_small_imbalance(env, sds); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5830 | } |
| 5831 | |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5832 | if (!busiest->group_imb) { |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 5833 | /* |
| 5834 | * Don't want to pull so many tasks that a group would go idle. |
Peter Zijlstra | 30ce5da | 2013-08-15 20:29:29 +0200 | [diff] [blame] | 5835 | * Except of course for the group_imb case, since then we might |
| 5836 | * have to drop below capacity to reach cpu-load equilibrium. |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 5837 | */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5838 | load_above_capacity = |
| 5839 | (busiest->sum_nr_running - busiest->group_capacity); |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 5840 | |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 5841 | load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE); |
Peter Zijlstra | 3ae11c9 | 2013-08-15 20:37:48 +0200 | [diff] [blame] | 5842 | load_above_capacity /= busiest->group_power; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 5843 | } |
| 5844 | |
| 5845 | /* |
| 5846 | * We're trying to get all the cpus to the average_load, so we don't |
| 5847 | * want to push ourselves above the average load, nor do we wish to |
| 5848 | * reduce the max loaded cpu below the average load. At the same time, |
| 5849 | * we also don't want to reduce the group load below the group capacity |
| 5850 | * (so that we can implement power-savings policies etc). Thus we look |
| 5851 | * for the minimum possible imbalance. |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 5852 | */ |
Peter Zijlstra | 30ce5da | 2013-08-15 20:29:29 +0200 | [diff] [blame] | 5853 | max_pull = min(busiest->avg_load - sds->avg_load, load_above_capacity); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5854 | |
| 5855 | /* How much load to actually move to equalise the imbalance */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5856 | env->imbalance = min( |
Peter Zijlstra | 3ae11c9 | 2013-08-15 20:37:48 +0200 | [diff] [blame] | 5857 | max_pull * busiest->group_power, |
| 5858 | (sds->avg_load - local->avg_load) * local->group_power |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5859 | ) / SCHED_POWER_SCALE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5860 | |
| 5861 | /* |
| 5862 | * if *imbalance is less than the average load per runnable task |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 5863 | * 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] | 5864 | * a think about bumping its value to force at least one task to be |
| 5865 | * moved |
| 5866 | */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5867 | if (env->imbalance < busiest->load_per_task) |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5868 | return fix_small_imbalance(env, sds); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5869 | } |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 5870 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5871 | /******* find_busiest_group() helpers end here *********************/ |
| 5872 | |
| 5873 | /** |
| 5874 | * find_busiest_group - Returns the busiest group within the sched_domain |
| 5875 | * if there is an imbalance. If there isn't an imbalance, and |
| 5876 | * the user has opted for power-savings, it returns a group whose |
| 5877 | * CPUs can be put to idle by rebalancing those tasks elsewhere, if |
| 5878 | * such a group exists. |
| 5879 | * |
| 5880 | * Also calculates the amount of weighted load which should be moved |
| 5881 | * to restore balance. |
| 5882 | * |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 5883 | * @env: The load balancing environment. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5884 | * |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 5885 | * Return: - The busiest group if imbalance exists. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5886 | * - If no imbalance and user has opted for power-savings balance, |
| 5887 | * return the least loaded group whose CPUs can be |
| 5888 | * put to idle by rebalancing its tasks onto our group. |
| 5889 | */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5890 | static struct sched_group *find_busiest_group(struct lb_env *env) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5891 | { |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5892 | struct sg_lb_stats *local, *busiest; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5893 | struct sd_lb_stats sds; |
| 5894 | |
Peter Zijlstra | 147c5fc | 2013-08-19 15:22:57 +0200 | [diff] [blame] | 5895 | init_sd_lb_stats(&sds); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5896 | |
| 5897 | /* |
| 5898 | * Compute the various statistics relavent for load balancing at |
| 5899 | * this level. |
| 5900 | */ |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 5901 | update_sd_lb_stats(env, &sds); |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5902 | local = &sds.local_stat; |
| 5903 | busiest = &sds.busiest_stat; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5904 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5905 | if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) && |
| 5906 | check_asym_packing(env, &sds)) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 5907 | return sds.busiest; |
| 5908 | |
Peter Zijlstra | cc57aa8 | 2011-02-21 18:55:32 +0100 | [diff] [blame] | 5909 | /* There is no busy sibling group to pull tasks from */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5910 | if (!sds.busiest || busiest->sum_nr_running == 0) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5911 | goto out_balanced; |
| 5912 | |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 5913 | sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr; |
Ken Chen | b0432d8 | 2011-04-07 17:23:22 -0700 | [diff] [blame] | 5914 | |
Peter Zijlstra | 866ab43 | 2011-02-21 18:56:47 +0100 | [diff] [blame] | 5915 | /* |
| 5916 | * If the busiest group is imbalanced the below checks don't |
Peter Zijlstra | 30ce5da | 2013-08-15 20:29:29 +0200 | [diff] [blame] | 5917 | * work because they assume all things are equal, which typically |
Peter Zijlstra | 866ab43 | 2011-02-21 18:56:47 +0100 | [diff] [blame] | 5918 | * isn't true due to cpus_allowed constraints and the like. |
| 5919 | */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5920 | if (busiest->group_imb) |
Peter Zijlstra | 866ab43 | 2011-02-21 18:56:47 +0100 | [diff] [blame] | 5921 | goto force_balance; |
| 5922 | |
Peter Zijlstra | cc57aa8 | 2011-02-21 18:55:32 +0100 | [diff] [blame] | 5923 | /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5924 | if (env->idle == CPU_NEWLY_IDLE && local->group_has_capacity && |
| 5925 | !busiest->group_has_capacity) |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 5926 | goto force_balance; |
| 5927 | |
Peter Zijlstra | cc57aa8 | 2011-02-21 18:55:32 +0100 | [diff] [blame] | 5928 | /* |
| 5929 | * If the local group is more busy than the selected busiest group |
| 5930 | * don't try and pull any tasks. |
| 5931 | */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5932 | if (local->avg_load >= busiest->avg_load) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5933 | goto out_balanced; |
| 5934 | |
Peter Zijlstra | cc57aa8 | 2011-02-21 18:55:32 +0100 | [diff] [blame] | 5935 | /* |
| 5936 | * Don't pull any tasks if this group is already above the domain |
| 5937 | * average load. |
| 5938 | */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5939 | if (local->avg_load >= sds.avg_load) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5940 | goto out_balanced; |
| 5941 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5942 | if (env->idle == CPU_IDLE) { |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 5943 | /* |
| 5944 | * This cpu is idle. If the busiest group load doesn't |
| 5945 | * have more tasks than the number of available cpu's and |
| 5946 | * there is no imbalance between this and busiest group |
| 5947 | * wrt to idle cpu's, it is balanced. |
| 5948 | */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5949 | if ((local->idle_cpus < busiest->idle_cpus) && |
| 5950 | busiest->sum_nr_running <= busiest->group_weight) |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 5951 | goto out_balanced; |
Peter Zijlstra | c186faf | 2011-02-21 18:52:53 +0100 | [diff] [blame] | 5952 | } else { |
| 5953 | /* |
| 5954 | * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use |
| 5955 | * imbalance_pct to be conservative. |
| 5956 | */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 5957 | if (100 * busiest->avg_load <= |
| 5958 | env->sd->imbalance_pct * local->avg_load) |
Peter Zijlstra | c186faf | 2011-02-21 18:52:53 +0100 | [diff] [blame] | 5959 | goto out_balanced; |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 5960 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5961 | |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 5962 | force_balance: |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5963 | /* Looks like there is an imbalance. Compute it */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5964 | calculate_imbalance(env, &sds); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5965 | return sds.busiest; |
| 5966 | |
| 5967 | out_balanced: |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5968 | env->imbalance = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5969 | return NULL; |
| 5970 | } |
| 5971 | |
| 5972 | /* |
| 5973 | * find_busiest_queue - find the busiest runqueue among the cpus in group. |
| 5974 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 5975 | static struct rq *find_busiest_queue(struct lb_env *env, |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 5976 | struct sched_group *group) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5977 | { |
| 5978 | struct rq *busiest = NULL, *rq; |
Joonsoo Kim | 95a79b8 | 2013-08-06 17:36:41 +0900 | [diff] [blame] | 5979 | unsigned long busiest_load = 0, busiest_power = 1; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5980 | int i; |
| 5981 | |
Peter Zijlstra | 6906a40 | 2013-08-19 15:20:21 +0200 | [diff] [blame] | 5982 | for_each_cpu_and(i, sched_group_cpus(group), env->cpus) { |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 5983 | unsigned long power, capacity, wl; |
| 5984 | enum fbq_type rt; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5985 | |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 5986 | rq = cpu_rq(i); |
| 5987 | rt = fbq_classify_rq(rq); |
| 5988 | |
| 5989 | /* |
| 5990 | * We classify groups/runqueues into three groups: |
| 5991 | * - regular: there are !numa tasks |
| 5992 | * - remote: there are numa tasks that run on the 'wrong' node |
| 5993 | * - all: there is no distinction |
| 5994 | * |
| 5995 | * In order to avoid migrating ideally placed numa tasks, |
| 5996 | * ignore those when there's better options. |
| 5997 | * |
| 5998 | * If we ignore the actual busiest queue to migrate another |
| 5999 | * task, the next balance pass can still reduce the busiest |
| 6000 | * queue by moving tasks around inside the node. |
| 6001 | * |
| 6002 | * If we cannot move enough load due to this classification |
| 6003 | * the next pass will adjust the group classification and |
| 6004 | * allow migration of more tasks. |
| 6005 | * |
| 6006 | * Both cases only affect the total convergence complexity. |
| 6007 | */ |
| 6008 | if (rt > env->fbq_type) |
| 6009 | continue; |
| 6010 | |
| 6011 | power = power_of(i); |
| 6012 | capacity = DIV_ROUND_CLOSEST(power, SCHED_POWER_SCALE); |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 6013 | if (!capacity) |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 6014 | capacity = fix_small_capacity(env->sd, group); |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 6015 | |
Thomas Gleixner | 6e40f5b | 2010-02-16 16:48:56 +0100 | [diff] [blame] | 6016 | wl = weighted_cpuload(i); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6017 | |
Thomas Gleixner | 6e40f5b | 2010-02-16 16:48:56 +0100 | [diff] [blame] | 6018 | /* |
| 6019 | * When comparing with imbalance, use weighted_cpuload() |
| 6020 | * which is not scaled with the cpu power. |
| 6021 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 6022 | if (capacity && rq->nr_running == 1 && wl > env->imbalance) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6023 | continue; |
| 6024 | |
Thomas Gleixner | 6e40f5b | 2010-02-16 16:48:56 +0100 | [diff] [blame] | 6025 | /* |
| 6026 | * For the load comparisons with the other cpu's, consider |
| 6027 | * the weighted_cpuload() scaled with the cpu power, so that |
| 6028 | * the load can be moved away from the cpu that is potentially |
| 6029 | * running at a lower capacity. |
Joonsoo Kim | 95a79b8 | 2013-08-06 17:36:41 +0900 | [diff] [blame] | 6030 | * |
| 6031 | * Thus we're looking for max(wl_i / power_i), crosswise |
| 6032 | * multiplication to rid ourselves of the division works out |
| 6033 | * to: wl_i * power_j > wl_j * power_i; where j is our |
| 6034 | * previous maximum. |
Thomas Gleixner | 6e40f5b | 2010-02-16 16:48:56 +0100 | [diff] [blame] | 6035 | */ |
Joonsoo Kim | 95a79b8 | 2013-08-06 17:36:41 +0900 | [diff] [blame] | 6036 | if (wl * busiest_power > busiest_load * power) { |
| 6037 | busiest_load = wl; |
| 6038 | busiest_power = power; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6039 | busiest = rq; |
| 6040 | } |
| 6041 | } |
| 6042 | |
| 6043 | return busiest; |
| 6044 | } |
| 6045 | |
| 6046 | /* |
| 6047 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but |
| 6048 | * so long as it is large enough. |
| 6049 | */ |
| 6050 | #define MAX_PINNED_INTERVAL 512 |
| 6051 | |
| 6052 | /* Working cpumask for load_balance and load_balance_newidle. */ |
Joonsoo Kim | e6252c3 | 2013-04-23 17:27:41 +0900 | [diff] [blame] | 6053 | DEFINE_PER_CPU(cpumask_var_t, load_balance_mask); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6054 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 6055 | static int need_active_balance(struct lb_env *env) |
Peter Zijlstra | 1af3ed3 | 2009-12-23 15:10:31 +0100 | [diff] [blame] | 6056 | { |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 6057 | struct sched_domain *sd = env->sd; |
| 6058 | |
| 6059 | if (env->idle == CPU_NEWLY_IDLE) { |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 6060 | |
| 6061 | /* |
| 6062 | * ASYM_PACKING needs to force migrate tasks from busy but |
| 6063 | * higher numbered CPUs in order to pack all tasks in the |
| 6064 | * lowest numbered CPUs. |
| 6065 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 6066 | if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 6067 | return 1; |
Peter Zijlstra | 1af3ed3 | 2009-12-23 15:10:31 +0100 | [diff] [blame] | 6068 | } |
| 6069 | |
| 6070 | return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2); |
| 6071 | } |
| 6072 | |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 6073 | static int active_load_balance_cpu_stop(void *data); |
| 6074 | |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 6075 | static int should_we_balance(struct lb_env *env) |
| 6076 | { |
| 6077 | struct sched_group *sg = env->sd->groups; |
| 6078 | struct cpumask *sg_cpus, *sg_mask; |
| 6079 | int cpu, balance_cpu = -1; |
| 6080 | |
| 6081 | /* |
| 6082 | * In the newly idle case, we will allow all the cpu's |
| 6083 | * to do the newly idle load balance. |
| 6084 | */ |
| 6085 | if (env->idle == CPU_NEWLY_IDLE) |
| 6086 | return 1; |
| 6087 | |
| 6088 | sg_cpus = sched_group_cpus(sg); |
| 6089 | sg_mask = sched_group_mask(sg); |
| 6090 | /* Try to find first idle cpu */ |
| 6091 | for_each_cpu_and(cpu, sg_cpus, env->cpus) { |
| 6092 | if (!cpumask_test_cpu(cpu, sg_mask) || !idle_cpu(cpu)) |
| 6093 | continue; |
| 6094 | |
| 6095 | balance_cpu = cpu; |
| 6096 | break; |
| 6097 | } |
| 6098 | |
| 6099 | if (balance_cpu == -1) |
| 6100 | balance_cpu = group_balance_cpu(sg); |
| 6101 | |
| 6102 | /* |
| 6103 | * First idle cpu or the first cpu(busiest) in this sched group |
| 6104 | * is eligible for doing load balancing at this and above domains. |
| 6105 | */ |
Joonsoo Kim | b0cff9d | 2013-09-10 15:54:49 +0900 | [diff] [blame] | 6106 | return balance_cpu == env->dst_cpu; |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 6107 | } |
| 6108 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6109 | /* |
| 6110 | * Check this_cpu to ensure it is balanced within domain. Attempt to move |
| 6111 | * tasks if there is an imbalance. |
| 6112 | */ |
| 6113 | static int load_balance(int this_cpu, struct rq *this_rq, |
| 6114 | struct sched_domain *sd, enum cpu_idle_type idle, |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 6115 | int *continue_balancing) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6116 | { |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 6117 | int ld_moved, cur_ld_moved, active_balance = 0; |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 6118 | struct sched_domain *sd_parent = sd->parent; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6119 | struct sched_group *group; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6120 | struct rq *busiest; |
| 6121 | unsigned long flags; |
Joonsoo Kim | e6252c3 | 2013-04-23 17:27:41 +0900 | [diff] [blame] | 6122 | struct cpumask *cpus = __get_cpu_var(load_balance_mask); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6123 | |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 6124 | struct lb_env env = { |
| 6125 | .sd = sd, |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 6126 | .dst_cpu = this_cpu, |
| 6127 | .dst_rq = this_rq, |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 6128 | .dst_grpmask = sched_group_cpus(sd->groups), |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 6129 | .idle = idle, |
Peter Zijlstra | eb95308 | 2012-04-17 13:38:40 +0200 | [diff] [blame] | 6130 | .loop_break = sched_nr_migrate_break, |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 6131 | .cpus = cpus, |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 6132 | .fbq_type = all, |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 6133 | }; |
| 6134 | |
Joonsoo Kim | cfc0311 | 2013-04-23 17:27:39 +0900 | [diff] [blame] | 6135 | /* |
| 6136 | * For NEWLY_IDLE load_balancing, we don't need to consider |
| 6137 | * other cpus in our group |
| 6138 | */ |
Joonsoo Kim | e02e60c | 2013-04-23 17:27:42 +0900 | [diff] [blame] | 6139 | if (idle == CPU_NEWLY_IDLE) |
Joonsoo Kim | cfc0311 | 2013-04-23 17:27:39 +0900 | [diff] [blame] | 6140 | env.dst_grpmask = NULL; |
Joonsoo Kim | cfc0311 | 2013-04-23 17:27:39 +0900 | [diff] [blame] | 6141 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6142 | cpumask_copy(cpus, cpu_active_mask); |
| 6143 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6144 | schedstat_inc(sd, lb_count[idle]); |
| 6145 | |
| 6146 | redo: |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 6147 | if (!should_we_balance(&env)) { |
| 6148 | *continue_balancing = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6149 | goto out_balanced; |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 6150 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6151 | |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 6152 | group = find_busiest_group(&env); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6153 | if (!group) { |
| 6154 | schedstat_inc(sd, lb_nobusyg[idle]); |
| 6155 | goto out_balanced; |
| 6156 | } |
| 6157 | |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 6158 | busiest = find_busiest_queue(&env, group); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6159 | if (!busiest) { |
| 6160 | schedstat_inc(sd, lb_nobusyq[idle]); |
| 6161 | goto out_balanced; |
| 6162 | } |
| 6163 | |
Michael Wang | 78feefc | 2012-08-06 16:41:59 +0800 | [diff] [blame] | 6164 | BUG_ON(busiest == env.dst_rq); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6165 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 6166 | schedstat_add(sd, lb_imbalance[idle], env.imbalance); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6167 | |
| 6168 | ld_moved = 0; |
| 6169 | if (busiest->nr_running > 1) { |
| 6170 | /* |
| 6171 | * Attempt to move tasks. If find_busiest_group has found |
| 6172 | * an imbalance but busiest->nr_running <= 1, the group is |
| 6173 | * still unbalanced. ld_moved simply stays zero, so it is |
| 6174 | * correctly treated as an imbalance. |
| 6175 | */ |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 6176 | env.flags |= LBF_ALL_PINNED; |
Peter Zijlstra | c82513e | 2012-04-26 13:12:27 +0200 | [diff] [blame] | 6177 | env.src_cpu = busiest->cpu; |
| 6178 | env.src_rq = busiest; |
| 6179 | env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 6180 | |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 6181 | more_balance: |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6182 | local_irq_save(flags); |
Michael Wang | 78feefc | 2012-08-06 16:41:59 +0800 | [diff] [blame] | 6183 | double_rq_lock(env.dst_rq, busiest); |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 6184 | |
| 6185 | /* |
| 6186 | * cur_ld_moved - load moved in current iteration |
| 6187 | * ld_moved - cumulative load moved across iterations |
| 6188 | */ |
| 6189 | cur_ld_moved = move_tasks(&env); |
| 6190 | ld_moved += cur_ld_moved; |
Michael Wang | 78feefc | 2012-08-06 16:41:59 +0800 | [diff] [blame] | 6191 | double_rq_unlock(env.dst_rq, busiest); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6192 | local_irq_restore(flags); |
| 6193 | |
| 6194 | /* |
| 6195 | * some other cpu did the load balance for us. |
| 6196 | */ |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 6197 | if (cur_ld_moved && env.dst_cpu != smp_processor_id()) |
| 6198 | resched_cpu(env.dst_cpu); |
| 6199 | |
Joonsoo Kim | f1cd085 | 2013-04-23 17:27:37 +0900 | [diff] [blame] | 6200 | if (env.flags & LBF_NEED_BREAK) { |
| 6201 | env.flags &= ~LBF_NEED_BREAK; |
| 6202 | goto more_balance; |
| 6203 | } |
| 6204 | |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 6205 | /* |
| 6206 | * Revisit (affine) tasks on src_cpu that couldn't be moved to |
| 6207 | * us and move them to an alternate dst_cpu in our sched_group |
| 6208 | * where they can run. The upper limit on how many times we |
| 6209 | * iterate on same src_cpu is dependent on number of cpus in our |
| 6210 | * sched_group. |
| 6211 | * |
| 6212 | * This changes load balance semantics a bit on who can move |
| 6213 | * load to a given_cpu. In addition to the given_cpu itself |
| 6214 | * (or a ilb_cpu acting on its behalf where given_cpu is |
| 6215 | * nohz-idle), we now have balance_cpu in a position to move |
| 6216 | * load to given_cpu. In rare situations, this may cause |
| 6217 | * conflicts (balance_cpu and given_cpu/ilb_cpu deciding |
| 6218 | * _independently_ and at _same_ time to move some load to |
| 6219 | * given_cpu) causing exceess load to be moved to given_cpu. |
| 6220 | * This however should not happen so much in practice and |
| 6221 | * moreover subsequent load balance cycles should correct the |
| 6222 | * excess load moved. |
| 6223 | */ |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 6224 | if ((env.flags & LBF_DST_PINNED) && env.imbalance > 0) { |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 6225 | |
Vladimir Davydov | 7aff2e3 | 2013-09-15 21:30:13 +0400 | [diff] [blame] | 6226 | /* Prevent to re-select dst_cpu via env's cpus */ |
| 6227 | cpumask_clear_cpu(env.dst_cpu, env.cpus); |
| 6228 | |
Michael Wang | 78feefc | 2012-08-06 16:41:59 +0800 | [diff] [blame] | 6229 | env.dst_rq = cpu_rq(env.new_dst_cpu); |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 6230 | env.dst_cpu = env.new_dst_cpu; |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 6231 | env.flags &= ~LBF_DST_PINNED; |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 6232 | env.loop = 0; |
| 6233 | env.loop_break = sched_nr_migrate_break; |
Joonsoo Kim | e02e60c | 2013-04-23 17:27:42 +0900 | [diff] [blame] | 6234 | |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 6235 | /* |
| 6236 | * Go back to "more_balance" rather than "redo" since we |
| 6237 | * need to continue with same src_cpu. |
| 6238 | */ |
| 6239 | goto more_balance; |
| 6240 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6241 | |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 6242 | /* |
| 6243 | * We failed to reach balance because of affinity. |
| 6244 | */ |
| 6245 | if (sd_parent) { |
| 6246 | int *group_imbalance = &sd_parent->groups->sgp->imbalance; |
| 6247 | |
| 6248 | if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0) { |
| 6249 | *group_imbalance = 1; |
| 6250 | } else if (*group_imbalance) |
| 6251 | *group_imbalance = 0; |
| 6252 | } |
| 6253 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6254 | /* All tasks on this runqueue were pinned by CPU affinity */ |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 6255 | if (unlikely(env.flags & LBF_ALL_PINNED)) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6256 | cpumask_clear_cpu(cpu_of(busiest), cpus); |
Prashanth Nageshappa | bbf18b1 | 2012-06-19 17:52:07 +0530 | [diff] [blame] | 6257 | if (!cpumask_empty(cpus)) { |
| 6258 | env.loop = 0; |
| 6259 | env.loop_break = sched_nr_migrate_break; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6260 | goto redo; |
Prashanth Nageshappa | bbf18b1 | 2012-06-19 17:52:07 +0530 | [diff] [blame] | 6261 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6262 | goto out_balanced; |
| 6263 | } |
| 6264 | } |
| 6265 | |
| 6266 | if (!ld_moved) { |
| 6267 | schedstat_inc(sd, lb_failed[idle]); |
Venkatesh Pallipadi | 58b26c4 | 2010-09-10 18:19:17 -0700 | [diff] [blame] | 6268 | /* |
| 6269 | * Increment the failure counter only on periodic balance. |
| 6270 | * We do not want newidle balance, which can be very |
| 6271 | * frequent, pollute the failure counter causing |
| 6272 | * excessive cache_hot migrations and active balances. |
| 6273 | */ |
| 6274 | if (idle != CPU_NEWLY_IDLE) |
| 6275 | sd->nr_balance_failed++; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6276 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 6277 | if (need_active_balance(&env)) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6278 | raw_spin_lock_irqsave(&busiest->lock, flags); |
| 6279 | |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 6280 | /* don't kick the active_load_balance_cpu_stop, |
| 6281 | * if the curr task on busiest cpu can't be |
| 6282 | * moved to this_cpu |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6283 | */ |
| 6284 | if (!cpumask_test_cpu(this_cpu, |
Peter Zijlstra | fa17b50 | 2011-06-16 12:23:22 +0200 | [diff] [blame] | 6285 | tsk_cpus_allowed(busiest->curr))) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6286 | raw_spin_unlock_irqrestore(&busiest->lock, |
| 6287 | flags); |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 6288 | env.flags |= LBF_ALL_PINNED; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6289 | goto out_one_pinned; |
| 6290 | } |
| 6291 | |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 6292 | /* |
| 6293 | * ->active_balance synchronizes accesses to |
| 6294 | * ->active_balance_work. Once set, it's cleared |
| 6295 | * only after active load balance is finished. |
| 6296 | */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6297 | if (!busiest->active_balance) { |
| 6298 | busiest->active_balance = 1; |
| 6299 | busiest->push_cpu = this_cpu; |
| 6300 | active_balance = 1; |
| 6301 | } |
| 6302 | raw_spin_unlock_irqrestore(&busiest->lock, flags); |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 6303 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 6304 | if (active_balance) { |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 6305 | stop_one_cpu_nowait(cpu_of(busiest), |
| 6306 | active_load_balance_cpu_stop, busiest, |
| 6307 | &busiest->active_balance_work); |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 6308 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6309 | |
| 6310 | /* |
| 6311 | * We've kicked active balancing, reset the failure |
| 6312 | * counter. |
| 6313 | */ |
| 6314 | sd->nr_balance_failed = sd->cache_nice_tries+1; |
| 6315 | } |
| 6316 | } else |
| 6317 | sd->nr_balance_failed = 0; |
| 6318 | |
| 6319 | if (likely(!active_balance)) { |
| 6320 | /* We were unbalanced, so reset the balancing interval */ |
| 6321 | sd->balance_interval = sd->min_interval; |
| 6322 | } else { |
| 6323 | /* |
| 6324 | * If we've begun active balancing, start to back off. This |
| 6325 | * case may not be covered by the all_pinned logic if there |
| 6326 | * is only 1 task on the busy runqueue (because we don't call |
| 6327 | * move_tasks). |
| 6328 | */ |
| 6329 | if (sd->balance_interval < sd->max_interval) |
| 6330 | sd->balance_interval *= 2; |
| 6331 | } |
| 6332 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6333 | goto out; |
| 6334 | |
| 6335 | out_balanced: |
| 6336 | schedstat_inc(sd, lb_balanced[idle]); |
| 6337 | |
| 6338 | sd->nr_balance_failed = 0; |
| 6339 | |
| 6340 | out_one_pinned: |
| 6341 | /* tune up the balancing interval */ |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 6342 | if (((env.flags & LBF_ALL_PINNED) && |
Peter Zijlstra | 5b54b56 | 2011-09-22 15:23:13 +0200 | [diff] [blame] | 6343 | sd->balance_interval < MAX_PINNED_INTERVAL) || |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6344 | (sd->balance_interval < sd->max_interval)) |
| 6345 | sd->balance_interval *= 2; |
| 6346 | |
Venkatesh Pallipadi | 46e49b3 | 2011-02-14 14:38:50 -0800 | [diff] [blame] | 6347 | ld_moved = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6348 | out: |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6349 | return ld_moved; |
| 6350 | } |
| 6351 | |
| 6352 | /* |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6353 | * idle_balance is called by schedule() if this_cpu is about to become |
| 6354 | * idle. Attempts to pull tasks from other CPUs. |
| 6355 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 6356 | void idle_balance(int this_cpu, struct rq *this_rq) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6357 | { |
| 6358 | struct sched_domain *sd; |
| 6359 | int pulled_task = 0; |
| 6360 | unsigned long next_balance = jiffies + HZ; |
Jason Low | 9bd721c | 2013-09-13 11:26:52 -0700 | [diff] [blame] | 6361 | u64 curr_cost = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6362 | |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 6363 | this_rq->idle_stamp = rq_clock(this_rq); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6364 | |
| 6365 | if (this_rq->avg_idle < sysctl_sched_migration_cost) |
| 6366 | return; |
| 6367 | |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 6368 | /* |
| 6369 | * Drop the rq->lock, but keep IRQ/preempt disabled. |
| 6370 | */ |
| 6371 | raw_spin_unlock(&this_rq->lock); |
| 6372 | |
Paul Turner | 48a1675 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 6373 | update_blocked_averages(this_cpu); |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 6374 | rcu_read_lock(); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6375 | for_each_domain(this_cpu, sd) { |
| 6376 | unsigned long interval; |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 6377 | int continue_balancing = 1; |
Jason Low | 9bd721c | 2013-09-13 11:26:52 -0700 | [diff] [blame] | 6378 | u64 t0, domain_cost; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6379 | |
| 6380 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 6381 | continue; |
| 6382 | |
Jason Low | 9bd721c | 2013-09-13 11:26:52 -0700 | [diff] [blame] | 6383 | if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) |
| 6384 | break; |
| 6385 | |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 6386 | if (sd->flags & SD_BALANCE_NEWIDLE) { |
Jason Low | 9bd721c | 2013-09-13 11:26:52 -0700 | [diff] [blame] | 6387 | t0 = sched_clock_cpu(this_cpu); |
| 6388 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6389 | /* If we've pulled tasks over stop searching: */ |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 6390 | pulled_task = load_balance(this_cpu, this_rq, |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 6391 | sd, CPU_NEWLY_IDLE, |
| 6392 | &continue_balancing); |
Jason Low | 9bd721c | 2013-09-13 11:26:52 -0700 | [diff] [blame] | 6393 | |
| 6394 | domain_cost = sched_clock_cpu(this_cpu) - t0; |
| 6395 | if (domain_cost > sd->max_newidle_lb_cost) |
| 6396 | sd->max_newidle_lb_cost = domain_cost; |
| 6397 | |
| 6398 | curr_cost += domain_cost; |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 6399 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6400 | |
| 6401 | interval = msecs_to_jiffies(sd->balance_interval); |
| 6402 | if (time_after(next_balance, sd->last_balance + interval)) |
| 6403 | next_balance = sd->last_balance + interval; |
Nikhil Rao | d5ad140 | 2010-11-17 11:42:04 -0800 | [diff] [blame] | 6404 | if (pulled_task) { |
| 6405 | this_rq->idle_stamp = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6406 | break; |
Nikhil Rao | d5ad140 | 2010-11-17 11:42:04 -0800 | [diff] [blame] | 6407 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6408 | } |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 6409 | rcu_read_unlock(); |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 6410 | |
| 6411 | raw_spin_lock(&this_rq->lock); |
| 6412 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6413 | if (pulled_task || time_after(jiffies, this_rq->next_balance)) { |
| 6414 | /* |
| 6415 | * We are going idle. next_balance may be set based on |
| 6416 | * a busy processor. So reset next_balance. |
| 6417 | */ |
| 6418 | this_rq->next_balance = next_balance; |
| 6419 | } |
Jason Low | 9bd721c | 2013-09-13 11:26:52 -0700 | [diff] [blame] | 6420 | |
| 6421 | if (curr_cost > this_rq->max_idle_balance_cost) |
| 6422 | this_rq->max_idle_balance_cost = curr_cost; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6423 | } |
| 6424 | |
| 6425 | /* |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 6426 | * active_load_balance_cpu_stop is run by cpu stopper. It pushes |
| 6427 | * running tasks off the busiest CPU onto idle CPUs. It requires at |
| 6428 | * least 1 task to be running on each physical CPU where possible, and |
| 6429 | * avoids physical / logical imbalances. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6430 | */ |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 6431 | static int active_load_balance_cpu_stop(void *data) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6432 | { |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 6433 | struct rq *busiest_rq = data; |
| 6434 | int busiest_cpu = cpu_of(busiest_rq); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6435 | int target_cpu = busiest_rq->push_cpu; |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 6436 | struct rq *target_rq = cpu_rq(target_cpu); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6437 | struct sched_domain *sd; |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 6438 | |
| 6439 | raw_spin_lock_irq(&busiest_rq->lock); |
| 6440 | |
| 6441 | /* make sure the requested cpu hasn't gone down in the meantime */ |
| 6442 | if (unlikely(busiest_cpu != smp_processor_id() || |
| 6443 | !busiest_rq->active_balance)) |
| 6444 | goto out_unlock; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6445 | |
| 6446 | /* Is there any task to move? */ |
| 6447 | if (busiest_rq->nr_running <= 1) |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 6448 | goto out_unlock; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6449 | |
| 6450 | /* |
| 6451 | * This condition is "impossible", if it occurs |
| 6452 | * we need to fix it. Originally reported by |
| 6453 | * Bjorn Helgaas on a 128-cpu setup. |
| 6454 | */ |
| 6455 | BUG_ON(busiest_rq == target_rq); |
| 6456 | |
| 6457 | /* move a task from busiest_rq to target_rq */ |
| 6458 | double_lock_balance(busiest_rq, target_rq); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6459 | |
| 6460 | /* Search for an sd spanning us and the target CPU. */ |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 6461 | rcu_read_lock(); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6462 | for_each_domain(target_cpu, sd) { |
| 6463 | if ((sd->flags & SD_LOAD_BALANCE) && |
| 6464 | cpumask_test_cpu(busiest_cpu, sched_domain_span(sd))) |
| 6465 | break; |
| 6466 | } |
| 6467 | |
| 6468 | if (likely(sd)) { |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 6469 | struct lb_env env = { |
| 6470 | .sd = sd, |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 6471 | .dst_cpu = target_cpu, |
| 6472 | .dst_rq = target_rq, |
| 6473 | .src_cpu = busiest_rq->cpu, |
| 6474 | .src_rq = busiest_rq, |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 6475 | .idle = CPU_IDLE, |
| 6476 | }; |
| 6477 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6478 | schedstat_inc(sd, alb_count); |
| 6479 | |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 6480 | if (move_one_task(&env)) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6481 | schedstat_inc(sd, alb_pushed); |
| 6482 | else |
| 6483 | schedstat_inc(sd, alb_failed); |
| 6484 | } |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 6485 | rcu_read_unlock(); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6486 | double_unlock_balance(busiest_rq, target_rq); |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 6487 | out_unlock: |
| 6488 | busiest_rq->active_balance = 0; |
| 6489 | raw_spin_unlock_irq(&busiest_rq->lock); |
| 6490 | return 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6491 | } |
| 6492 | |
Frederic Weisbecker | 3451d02 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 6493 | #ifdef CONFIG_NO_HZ_COMMON |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6494 | /* |
| 6495 | * idle load balancing details |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6496 | * - When one of the busy CPUs notice that there may be an idle rebalancing |
| 6497 | * needed, they will kick the idle load balancer, which then does idle |
| 6498 | * load balancing for all the idle CPUs. |
| 6499 | */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6500 | static struct { |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6501 | cpumask_var_t idle_cpus_mask; |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 6502 | atomic_t nr_cpus; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6503 | unsigned long next_balance; /* in jiffy units */ |
| 6504 | } nohz ____cacheline_aligned; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6505 | |
Daniel Lezcano | 3dd0337 | 2014-01-06 12:34:41 +0100 | [diff] [blame] | 6506 | static inline int find_new_ilb(void) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6507 | { |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 6508 | int ilb = cpumask_first(nohz.idle_cpus_mask); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6509 | |
Suresh Siddha | 786d6dc7 | 2011-12-01 17:07:35 -0800 | [diff] [blame] | 6510 | if (ilb < nr_cpu_ids && idle_cpu(ilb)) |
| 6511 | return ilb; |
| 6512 | |
| 6513 | return nr_cpu_ids; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6514 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6515 | |
| 6516 | /* |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6517 | * Kick a CPU to do the nohz balancing, if it is time for it. We pick the |
| 6518 | * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle |
| 6519 | * CPU (if there is one). |
| 6520 | */ |
Daniel Lezcano | 0aeeeeb | 2014-01-06 12:34:42 +0100 | [diff] [blame] | 6521 | static void nohz_balancer_kick(void) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6522 | { |
| 6523 | int ilb_cpu; |
| 6524 | |
| 6525 | nohz.next_balance++; |
| 6526 | |
Daniel Lezcano | 3dd0337 | 2014-01-06 12:34:41 +0100 | [diff] [blame] | 6527 | ilb_cpu = find_new_ilb(); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6528 | |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 6529 | if (ilb_cpu >= nr_cpu_ids) |
| 6530 | return; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6531 | |
Suresh Siddha | cd490c5 | 2011-12-06 11:26:34 -0800 | [diff] [blame] | 6532 | if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu))) |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 6533 | return; |
| 6534 | /* |
| 6535 | * Use smp_send_reschedule() instead of resched_cpu(). |
| 6536 | * This way we generate a sched IPI on the target cpu which |
| 6537 | * is idle. And the softirq performing nohz idle load balance |
| 6538 | * will be run before returning from the IPI. |
| 6539 | */ |
| 6540 | smp_send_reschedule(ilb_cpu); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6541 | return; |
| 6542 | } |
| 6543 | |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 6544 | static inline void nohz_balance_exit_idle(int cpu) |
Suresh Siddha | 7132596 | 2012-01-19 18:28:57 -0800 | [diff] [blame] | 6545 | { |
| 6546 | if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) { |
| 6547 | cpumask_clear_cpu(cpu, nohz.idle_cpus_mask); |
| 6548 | atomic_dec(&nohz.nr_cpus); |
| 6549 | clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)); |
| 6550 | } |
| 6551 | } |
| 6552 | |
Suresh Siddha | 69e1e81 | 2011-12-01 17:07:33 -0800 | [diff] [blame] | 6553 | static inline void set_cpu_sd_state_busy(void) |
| 6554 | { |
| 6555 | struct sched_domain *sd; |
Preeti U Murthy | 37dc6b5 | 2013-10-30 08:42:52 +0530 | [diff] [blame] | 6556 | int cpu = smp_processor_id(); |
Suresh Siddha | 69e1e81 | 2011-12-01 17:07:33 -0800 | [diff] [blame] | 6557 | |
Suresh Siddha | 69e1e81 | 2011-12-01 17:07:33 -0800 | [diff] [blame] | 6558 | rcu_read_lock(); |
Preeti U Murthy | 37dc6b5 | 2013-10-30 08:42:52 +0530 | [diff] [blame] | 6559 | sd = rcu_dereference(per_cpu(sd_busy, cpu)); |
Vincent Guittot | 25f55d9 | 2013-04-23 16:59:02 +0200 | [diff] [blame] | 6560 | |
| 6561 | if (!sd || !sd->nohz_idle) |
| 6562 | goto unlock; |
| 6563 | sd->nohz_idle = 0; |
| 6564 | |
Preeti U Murthy | 37dc6b5 | 2013-10-30 08:42:52 +0530 | [diff] [blame] | 6565 | atomic_inc(&sd->groups->sgp->nr_busy_cpus); |
Vincent Guittot | 25f55d9 | 2013-04-23 16:59:02 +0200 | [diff] [blame] | 6566 | unlock: |
Suresh Siddha | 69e1e81 | 2011-12-01 17:07:33 -0800 | [diff] [blame] | 6567 | rcu_read_unlock(); |
| 6568 | } |
| 6569 | |
| 6570 | void set_cpu_sd_state_idle(void) |
| 6571 | { |
| 6572 | struct sched_domain *sd; |
Preeti U Murthy | 37dc6b5 | 2013-10-30 08:42:52 +0530 | [diff] [blame] | 6573 | int cpu = smp_processor_id(); |
Suresh Siddha | 69e1e81 | 2011-12-01 17:07:33 -0800 | [diff] [blame] | 6574 | |
Suresh Siddha | 69e1e81 | 2011-12-01 17:07:33 -0800 | [diff] [blame] | 6575 | rcu_read_lock(); |
Preeti U Murthy | 37dc6b5 | 2013-10-30 08:42:52 +0530 | [diff] [blame] | 6576 | sd = rcu_dereference(per_cpu(sd_busy, cpu)); |
Vincent Guittot | 25f55d9 | 2013-04-23 16:59:02 +0200 | [diff] [blame] | 6577 | |
| 6578 | if (!sd || sd->nohz_idle) |
| 6579 | goto unlock; |
| 6580 | sd->nohz_idle = 1; |
| 6581 | |
Preeti U Murthy | 37dc6b5 | 2013-10-30 08:42:52 +0530 | [diff] [blame] | 6582 | atomic_dec(&sd->groups->sgp->nr_busy_cpus); |
Vincent Guittot | 25f55d9 | 2013-04-23 16:59:02 +0200 | [diff] [blame] | 6583 | unlock: |
Suresh Siddha | 69e1e81 | 2011-12-01 17:07:33 -0800 | [diff] [blame] | 6584 | rcu_read_unlock(); |
| 6585 | } |
| 6586 | |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6587 | /* |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 6588 | * This routine will record that the cpu is going idle with tick stopped. |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 6589 | * 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] | 6590 | */ |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 6591 | void nohz_balance_enter_idle(int cpu) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6592 | { |
Suresh Siddha | 7132596 | 2012-01-19 18:28:57 -0800 | [diff] [blame] | 6593 | /* |
| 6594 | * If this cpu is going down, then nothing needs to be done. |
| 6595 | */ |
| 6596 | if (!cpu_active(cpu)) |
| 6597 | return; |
| 6598 | |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 6599 | if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu))) |
| 6600 | return; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6601 | |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 6602 | cpumask_set_cpu(cpu, nohz.idle_cpus_mask); |
| 6603 | atomic_inc(&nohz.nr_cpus); |
| 6604 | set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6605 | } |
Suresh Siddha | 7132596 | 2012-01-19 18:28:57 -0800 | [diff] [blame] | 6606 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 6607 | static int sched_ilb_notifier(struct notifier_block *nfb, |
Suresh Siddha | 7132596 | 2012-01-19 18:28:57 -0800 | [diff] [blame] | 6608 | unsigned long action, void *hcpu) |
| 6609 | { |
| 6610 | switch (action & ~CPU_TASKS_FROZEN) { |
| 6611 | case CPU_DYING: |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 6612 | nohz_balance_exit_idle(smp_processor_id()); |
Suresh Siddha | 7132596 | 2012-01-19 18:28:57 -0800 | [diff] [blame] | 6613 | return NOTIFY_OK; |
| 6614 | default: |
| 6615 | return NOTIFY_DONE; |
| 6616 | } |
| 6617 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6618 | #endif |
| 6619 | |
| 6620 | static DEFINE_SPINLOCK(balancing); |
| 6621 | |
Peter Zijlstra | 49c022e | 2011-04-05 10:14:25 +0200 | [diff] [blame] | 6622 | /* |
| 6623 | * Scale the max load_balance interval with the number of CPUs in the system. |
| 6624 | * This trades load-balance latency on larger machines for less cross talk. |
| 6625 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 6626 | void update_max_interval(void) |
Peter Zijlstra | 49c022e | 2011-04-05 10:14:25 +0200 | [diff] [blame] | 6627 | { |
| 6628 | max_load_balance_interval = HZ*num_online_cpus()/10; |
| 6629 | } |
| 6630 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6631 | /* |
| 6632 | * It checks each scheduling domain to see if it is due to be balanced, |
| 6633 | * and initiates a balancing operation if so. |
| 6634 | * |
Libin | b9b0853 | 2013-04-01 19:14:01 +0800 | [diff] [blame] | 6635 | * Balancing parameters are set up in init_sched_domains. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6636 | */ |
Daniel Lezcano | f7ed0a8 | 2014-01-06 12:34:43 +0100 | [diff] [blame] | 6637 | static void rebalance_domains(struct rq *rq, enum cpu_idle_type idle) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6638 | { |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 6639 | int continue_balancing = 1; |
Daniel Lezcano | f7ed0a8 | 2014-01-06 12:34:43 +0100 | [diff] [blame] | 6640 | int cpu = rq->cpu; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6641 | unsigned long interval; |
Peter Zijlstra | 04f733b | 2012-05-11 00:12:02 +0200 | [diff] [blame] | 6642 | struct sched_domain *sd; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6643 | /* Earliest time when we have to do rebalance again */ |
| 6644 | unsigned long next_balance = jiffies + 60*HZ; |
| 6645 | int update_next_balance = 0; |
Jason Low | f48627e | 2013-09-13 11:26:53 -0700 | [diff] [blame] | 6646 | int need_serialize, need_decay = 0; |
| 6647 | u64 max_cost = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6648 | |
Paul Turner | 48a1675 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 6649 | update_blocked_averages(cpu); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 6650 | |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 6651 | rcu_read_lock(); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6652 | for_each_domain(cpu, sd) { |
Jason Low | f48627e | 2013-09-13 11:26:53 -0700 | [diff] [blame] | 6653 | /* |
| 6654 | * Decay the newidle max times here because this is a regular |
| 6655 | * visit to all the domains. Decay ~1% per second. |
| 6656 | */ |
| 6657 | if (time_after(jiffies, sd->next_decay_max_lb_cost)) { |
| 6658 | sd->max_newidle_lb_cost = |
| 6659 | (sd->max_newidle_lb_cost * 253) / 256; |
| 6660 | sd->next_decay_max_lb_cost = jiffies + HZ; |
| 6661 | need_decay = 1; |
| 6662 | } |
| 6663 | max_cost += sd->max_newidle_lb_cost; |
| 6664 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6665 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 6666 | continue; |
| 6667 | |
Jason Low | f48627e | 2013-09-13 11:26:53 -0700 | [diff] [blame] | 6668 | /* |
| 6669 | * Stop the load balance at this level. There is another |
| 6670 | * CPU in our sched group which is doing load balancing more |
| 6671 | * actively. |
| 6672 | */ |
| 6673 | if (!continue_balancing) { |
| 6674 | if (need_decay) |
| 6675 | continue; |
| 6676 | break; |
| 6677 | } |
| 6678 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6679 | interval = sd->balance_interval; |
| 6680 | if (idle != CPU_IDLE) |
| 6681 | interval *= sd->busy_factor; |
| 6682 | |
| 6683 | /* scale ms to jiffies */ |
| 6684 | interval = msecs_to_jiffies(interval); |
Peter Zijlstra | 49c022e | 2011-04-05 10:14:25 +0200 | [diff] [blame] | 6685 | interval = clamp(interval, 1UL, max_load_balance_interval); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6686 | |
| 6687 | need_serialize = sd->flags & SD_SERIALIZE; |
| 6688 | |
| 6689 | if (need_serialize) { |
| 6690 | if (!spin_trylock(&balancing)) |
| 6691 | goto out; |
| 6692 | } |
| 6693 | |
| 6694 | if (time_after_eq(jiffies, sd->last_balance + interval)) { |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 6695 | if (load_balance(cpu, rq, sd, idle, &continue_balancing)) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6696 | /* |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 6697 | * The LBF_DST_PINNED logic could have changed |
Joonsoo Kim | de5eb2d | 2013-04-23 17:27:38 +0900 | [diff] [blame] | 6698 | * env->dst_cpu, so we can't know our idle |
| 6699 | * state even if we migrated tasks. Update it. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6700 | */ |
Joonsoo Kim | de5eb2d | 2013-04-23 17:27:38 +0900 | [diff] [blame] | 6701 | idle = idle_cpu(cpu) ? CPU_IDLE : CPU_NOT_IDLE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6702 | } |
| 6703 | sd->last_balance = jiffies; |
| 6704 | } |
| 6705 | if (need_serialize) |
| 6706 | spin_unlock(&balancing); |
| 6707 | out: |
| 6708 | if (time_after(next_balance, sd->last_balance + interval)) { |
| 6709 | next_balance = sd->last_balance + interval; |
| 6710 | update_next_balance = 1; |
| 6711 | } |
Jason Low | f48627e | 2013-09-13 11:26:53 -0700 | [diff] [blame] | 6712 | } |
| 6713 | if (need_decay) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6714 | /* |
Jason Low | f48627e | 2013-09-13 11:26:53 -0700 | [diff] [blame] | 6715 | * Ensure the rq-wide value also decays but keep it at a |
| 6716 | * reasonable floor to avoid funnies with rq->avg_idle. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6717 | */ |
Jason Low | f48627e | 2013-09-13 11:26:53 -0700 | [diff] [blame] | 6718 | rq->max_idle_balance_cost = |
| 6719 | max((u64)sysctl_sched_migration_cost, max_cost); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6720 | } |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 6721 | rcu_read_unlock(); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6722 | |
| 6723 | /* |
| 6724 | * next_balance will be updated only when there is a need. |
| 6725 | * When the cpu is attached to null domain for ex, it will not be |
| 6726 | * updated. |
| 6727 | */ |
| 6728 | if (likely(update_next_balance)) |
| 6729 | rq->next_balance = next_balance; |
| 6730 | } |
| 6731 | |
Frederic Weisbecker | 3451d02 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 6732 | #ifdef CONFIG_NO_HZ_COMMON |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6733 | /* |
Frederic Weisbecker | 3451d02 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 6734 | * In CONFIG_NO_HZ_COMMON case, the idle balance kickee will do the |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6735 | * rebalancing for all the cpus for whom scheduler ticks are stopped. |
| 6736 | */ |
Daniel Lezcano | 208cb16 | 2014-01-06 12:34:44 +0100 | [diff] [blame] | 6737 | static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6738 | { |
Daniel Lezcano | 208cb16 | 2014-01-06 12:34:44 +0100 | [diff] [blame] | 6739 | int this_cpu = this_rq->cpu; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6740 | struct rq *rq; |
| 6741 | int balance_cpu; |
| 6742 | |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 6743 | if (idle != CPU_IDLE || |
| 6744 | !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu))) |
| 6745 | goto end; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6746 | |
| 6747 | for_each_cpu(balance_cpu, nohz.idle_cpus_mask) { |
Suresh Siddha | 8a6d42d | 2011-12-06 11:19:37 -0800 | [diff] [blame] | 6748 | if (balance_cpu == this_cpu || !idle_cpu(balance_cpu)) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6749 | continue; |
| 6750 | |
| 6751 | /* |
| 6752 | * If this cpu gets work to do, stop the load balancing |
| 6753 | * work being done for other cpus. Next load |
| 6754 | * balancing owner will pick it up. |
| 6755 | */ |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 6756 | if (need_resched()) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6757 | break; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6758 | |
Vincent Guittot | 5ed4f1d | 2012-09-13 06:11:26 +0200 | [diff] [blame] | 6759 | rq = cpu_rq(balance_cpu); |
| 6760 | |
| 6761 | raw_spin_lock_irq(&rq->lock); |
| 6762 | update_rq_clock(rq); |
| 6763 | update_idle_cpu_load(rq); |
| 6764 | raw_spin_unlock_irq(&rq->lock); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6765 | |
Daniel Lezcano | f7ed0a8 | 2014-01-06 12:34:43 +0100 | [diff] [blame] | 6766 | rebalance_domains(rq, CPU_IDLE); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6767 | |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6768 | if (time_after(this_rq->next_balance, rq->next_balance)) |
| 6769 | this_rq->next_balance = rq->next_balance; |
| 6770 | } |
| 6771 | nohz.next_balance = this_rq->next_balance; |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 6772 | end: |
| 6773 | clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6774 | } |
| 6775 | |
| 6776 | /* |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 6777 | * Current heuristic for kicking the idle load balancer in the presence |
| 6778 | * of an idle cpu is the system. |
| 6779 | * - This rq has more than one task. |
| 6780 | * - At any scheduler domain level, this cpu's scheduler group has multiple |
| 6781 | * busy cpu's exceeding the group's power. |
| 6782 | * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler |
| 6783 | * domain span are idle. |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6784 | */ |
Daniel Lezcano | 4a72562 | 2014-01-06 12:34:39 +0100 | [diff] [blame] | 6785 | static inline int nohz_kick_needed(struct rq *rq) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6786 | { |
| 6787 | unsigned long now = jiffies; |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 6788 | struct sched_domain *sd; |
Preeti U Murthy | 37dc6b5 | 2013-10-30 08:42:52 +0530 | [diff] [blame] | 6789 | struct sched_group_power *sgp; |
Daniel Lezcano | 4a72562 | 2014-01-06 12:34:39 +0100 | [diff] [blame] | 6790 | int nr_busy, cpu = rq->cpu; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6791 | |
Daniel Lezcano | 4a72562 | 2014-01-06 12:34:39 +0100 | [diff] [blame] | 6792 | if (unlikely(rq->idle_balance)) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6793 | return 0; |
| 6794 | |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 6795 | /* |
| 6796 | * We may be recently in ticked or tickless idle mode. At the first |
| 6797 | * busy tick after returning from idle, we will update the busy stats. |
| 6798 | */ |
Suresh Siddha | 69e1e81 | 2011-12-01 17:07:33 -0800 | [diff] [blame] | 6799 | set_cpu_sd_state_busy(); |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 6800 | nohz_balance_exit_idle(cpu); |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 6801 | |
| 6802 | /* |
| 6803 | * None are in tickless mode and hence no need for NOHZ idle load |
| 6804 | * balancing. |
| 6805 | */ |
| 6806 | if (likely(!atomic_read(&nohz.nr_cpus))) |
| 6807 | return 0; |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 6808 | |
| 6809 | if (time_before(now, nohz.next_balance)) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6810 | return 0; |
| 6811 | |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 6812 | if (rq->nr_running >= 2) |
| 6813 | goto need_kick; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6814 | |
Peter Zijlstra | 067491b | 2011-12-07 14:32:08 +0100 | [diff] [blame] | 6815 | rcu_read_lock(); |
Preeti U Murthy | 37dc6b5 | 2013-10-30 08:42:52 +0530 | [diff] [blame] | 6816 | sd = rcu_dereference(per_cpu(sd_busy, cpu)); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6817 | |
Preeti U Murthy | 37dc6b5 | 2013-10-30 08:42:52 +0530 | [diff] [blame] | 6818 | if (sd) { |
| 6819 | sgp = sd->groups->sgp; |
| 6820 | nr_busy = atomic_read(&sgp->nr_busy_cpus); |
| 6821 | |
| 6822 | if (nr_busy > 1) |
Peter Zijlstra | 067491b | 2011-12-07 14:32:08 +0100 | [diff] [blame] | 6823 | goto need_kick_unlock; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6824 | } |
Preeti U Murthy | 37dc6b5 | 2013-10-30 08:42:52 +0530 | [diff] [blame] | 6825 | |
| 6826 | sd = rcu_dereference(per_cpu(sd_asym, cpu)); |
| 6827 | |
| 6828 | if (sd && (cpumask_first_and(nohz.idle_cpus_mask, |
| 6829 | sched_domain_span(sd)) < cpu)) |
| 6830 | goto need_kick_unlock; |
| 6831 | |
Peter Zijlstra | 067491b | 2011-12-07 14:32:08 +0100 | [diff] [blame] | 6832 | rcu_read_unlock(); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6833 | return 0; |
Peter Zijlstra | 067491b | 2011-12-07 14:32:08 +0100 | [diff] [blame] | 6834 | |
| 6835 | need_kick_unlock: |
| 6836 | rcu_read_unlock(); |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 6837 | need_kick: |
| 6838 | return 1; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6839 | } |
| 6840 | #else |
Daniel Lezcano | 208cb16 | 2014-01-06 12:34:44 +0100 | [diff] [blame] | 6841 | static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) { } |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6842 | #endif |
| 6843 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6844 | /* |
| 6845 | * run_rebalance_domains is triggered when needed from the scheduler tick. |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6846 | * Also triggered for nohz idle balancing (with nohz_balancing_kick set). |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6847 | */ |
| 6848 | static void run_rebalance_domains(struct softirq_action *h) |
| 6849 | { |
Daniel Lezcano | 208cb16 | 2014-01-06 12:34:44 +0100 | [diff] [blame] | 6850 | struct rq *this_rq = this_rq(); |
Suresh Siddha | 6eb57e0 | 2011-10-03 15:09:01 -0700 | [diff] [blame] | 6851 | enum cpu_idle_type idle = this_rq->idle_balance ? |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6852 | CPU_IDLE : CPU_NOT_IDLE; |
| 6853 | |
Daniel Lezcano | f7ed0a8 | 2014-01-06 12:34:43 +0100 | [diff] [blame] | 6854 | rebalance_domains(this_rq, idle); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6855 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6856 | /* |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6857 | * If this cpu has a pending nohz_balance_kick, then do the |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6858 | * balancing on behalf of the other idle cpus whose ticks are |
| 6859 | * stopped. |
| 6860 | */ |
Daniel Lezcano | 208cb16 | 2014-01-06 12:34:44 +0100 | [diff] [blame] | 6861 | nohz_idle_balance(this_rq, idle); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6862 | } |
| 6863 | |
Daniel Lezcano | 63f609b | 2014-01-06 12:34:40 +0100 | [diff] [blame] | 6864 | static inline int on_null_domain(struct rq *rq) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6865 | { |
Daniel Lezcano | 63f609b | 2014-01-06 12:34:40 +0100 | [diff] [blame] | 6866 | return !rcu_dereference_sched(rq->sd); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6867 | } |
| 6868 | |
| 6869 | /* |
| 6870 | * 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] | 6871 | */ |
Daniel Lezcano | 7caff66 | 2014-01-06 12:34:38 +0100 | [diff] [blame] | 6872 | void trigger_load_balance(struct rq *rq) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6873 | { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6874 | /* Don't need to rebalance while attached to NULL domain */ |
Daniel Lezcano | c726099 | 2014-01-06 12:34:45 +0100 | [diff] [blame] | 6875 | if (unlikely(on_null_domain(rq))) |
| 6876 | return; |
| 6877 | |
| 6878 | if (time_after_eq(jiffies, rq->next_balance)) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6879 | raise_softirq(SCHED_SOFTIRQ); |
Frederic Weisbecker | 3451d02 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 6880 | #ifdef CONFIG_NO_HZ_COMMON |
Daniel Lezcano | c726099 | 2014-01-06 12:34:45 +0100 | [diff] [blame] | 6881 | if (nohz_kick_needed(rq)) |
Daniel Lezcano | 0aeeeeb | 2014-01-06 12:34:42 +0100 | [diff] [blame] | 6882 | nohz_balancer_kick(); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 6883 | #endif |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6884 | } |
| 6885 | |
Christian Ehrhardt | 0bcdcf2 | 2009-11-30 12:16:46 +0100 | [diff] [blame] | 6886 | static void rq_online_fair(struct rq *rq) |
| 6887 | { |
| 6888 | update_sysctl(); |
| 6889 | } |
| 6890 | |
| 6891 | static void rq_offline_fair(struct rq *rq) |
| 6892 | { |
| 6893 | update_sysctl(); |
Peter Boonstoppel | a4c96ae | 2012-08-09 15:34:47 -0700 | [diff] [blame] | 6894 | |
| 6895 | /* Ensure any throttled groups are reachable by pick_next_task */ |
| 6896 | unthrottle_offline_cfs_rqs(rq); |
Christian Ehrhardt | 0bcdcf2 | 2009-11-30 12:16:46 +0100 | [diff] [blame] | 6897 | } |
| 6898 | |
Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 6899 | #endif /* CONFIG_SMP */ |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 6900 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6901 | /* |
| 6902 | * scheduler tick hitting a task of our scheduling class: |
| 6903 | */ |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 6904 | 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] | 6905 | { |
| 6906 | struct cfs_rq *cfs_rq; |
| 6907 | struct sched_entity *se = &curr->se; |
| 6908 | |
| 6909 | for_each_sched_entity(se) { |
| 6910 | cfs_rq = cfs_rq_of(se); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 6911 | entity_tick(cfs_rq, se, queued); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6912 | } |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 6913 | |
Dave Kleikamp | 10e84b9 | 2013-07-31 13:53:35 -0700 | [diff] [blame] | 6914 | if (numabalancing_enabled) |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 6915 | task_tick_numa(rq, curr); |
Linus Torvalds | 3d59eeb | 2012-12-16 14:33:25 -0800 | [diff] [blame] | 6916 | |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 6917 | update_rq_runnable_avg(rq, 1); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6918 | } |
| 6919 | |
| 6920 | /* |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 6921 | * called on fork with the child task as argument from the parent's context |
| 6922 | * - child not yet on the tasklist |
| 6923 | * - preemption disabled |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6924 | */ |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 6925 | static void task_fork_fair(struct task_struct *p) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6926 | { |
Daisuke Nishimura | 4fc420c | 2011-12-15 14:36:55 +0900 | [diff] [blame] | 6927 | struct cfs_rq *cfs_rq; |
| 6928 | struct sched_entity *se = &p->se, *curr; |
Ingo Molnar | 00bf7bf | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 6929 | int this_cpu = smp_processor_id(); |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 6930 | struct rq *rq = this_rq(); |
| 6931 | unsigned long flags; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6932 | |
Thomas Gleixner | 05fa785 | 2009-11-17 14:28:38 +0100 | [diff] [blame] | 6933 | raw_spin_lock_irqsave(&rq->lock, flags); |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 6934 | |
Peter Zijlstra | 861d034 | 2010-08-19 13:31:43 +0200 | [diff] [blame] | 6935 | update_rq_clock(rq); |
| 6936 | |
Daisuke Nishimura | 4fc420c | 2011-12-15 14:36:55 +0900 | [diff] [blame] | 6937 | cfs_rq = task_cfs_rq(current); |
| 6938 | curr = cfs_rq->curr; |
| 6939 | |
Daisuke Nishimura | 6c9a27f | 2013-09-10 18:16:36 +0900 | [diff] [blame] | 6940 | /* |
| 6941 | * Not only the cpu but also the task_group of the parent might have |
| 6942 | * been changed after parent->se.parent,cfs_rq were copied to |
| 6943 | * child->se.parent,cfs_rq. So call __set_task_cpu() to make those |
| 6944 | * of child point to valid ones. |
| 6945 | */ |
| 6946 | rcu_read_lock(); |
| 6947 | __set_task_cpu(p, this_cpu); |
| 6948 | rcu_read_unlock(); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6949 | |
Ting Yang | 7109c44 | 2007-08-28 12:53:24 +0200 | [diff] [blame] | 6950 | update_curr(cfs_rq); |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 6951 | |
Mike Galbraith | b5d9d73 | 2009-09-08 11:12:28 +0200 | [diff] [blame] | 6952 | if (curr) |
| 6953 | se->vruntime = curr->vruntime; |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 6954 | place_entity(cfs_rq, se, 1); |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 6955 | |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 6956 | if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) { |
Dmitry Adamushko | 87fefa3 | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 6957 | /* |
Ingo Molnar | edcb60a | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 6958 | * Upon rescheduling, sched_class::put_prev_task() will place |
| 6959 | * 'current' within the tree based on its new key value. |
| 6960 | */ |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 6961 | swap(curr->vruntime, se->vruntime); |
Bharata B Rao | aec0a51 | 2008-08-28 14:42:49 +0530 | [diff] [blame] | 6962 | resched_task(rq->curr); |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 6963 | } |
| 6964 | |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 6965 | se->vruntime -= cfs_rq->min_vruntime; |
| 6966 | |
Thomas Gleixner | 05fa785 | 2009-11-17 14:28:38 +0100 | [diff] [blame] | 6967 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6968 | } |
| 6969 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 6970 | /* |
| 6971 | * Priority of the task has changed. Check to see if we preempt |
| 6972 | * the current task. |
| 6973 | */ |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 6974 | static void |
| 6975 | prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio) |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 6976 | { |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 6977 | if (!p->se.on_rq) |
| 6978 | return; |
| 6979 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 6980 | /* |
| 6981 | * Reschedule if we are currently running on this runqueue and |
| 6982 | * our priority decreased, or if we are not currently running on |
| 6983 | * this runqueue and our priority is higher than the current's |
| 6984 | */ |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 6985 | if (rq->curr == p) { |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 6986 | if (p->prio > oldprio) |
| 6987 | resched_task(rq->curr); |
| 6988 | } else |
Peter Zijlstra | 15afe09 | 2008-09-20 23:38:02 +0200 | [diff] [blame] | 6989 | check_preempt_curr(rq, p, 0); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 6990 | } |
| 6991 | |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 6992 | static void switched_from_fair(struct rq *rq, struct task_struct *p) |
| 6993 | { |
| 6994 | struct sched_entity *se = &p->se; |
| 6995 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 6996 | |
| 6997 | /* |
| 6998 | * Ensure the task's vruntime is normalized, so that when its |
| 6999 | * switched back to the fair class the enqueue_entity(.flags=0) will |
| 7000 | * do the right thing. |
| 7001 | * |
| 7002 | * If it was on_rq, then the dequeue_entity(.flags=0) will already |
| 7003 | * have normalized the vruntime, if it was !on_rq, then only when |
| 7004 | * the task is sleeping will it still have non-normalized vruntime. |
| 7005 | */ |
| 7006 | if (!se->on_rq && p->state != TASK_RUNNING) { |
| 7007 | /* |
| 7008 | * Fix up our vruntime so that the current sleep doesn't |
| 7009 | * cause 'unlimited' sleep bonus. |
| 7010 | */ |
| 7011 | place_entity(cfs_rq, se, 0); |
| 7012 | se->vruntime -= cfs_rq->min_vruntime; |
| 7013 | } |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 7014 | |
Alex Shi | 141965c | 2013-06-26 13:05:39 +0800 | [diff] [blame] | 7015 | #ifdef CONFIG_SMP |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 7016 | /* |
| 7017 | * Remove our load from contribution when we leave sched_fair |
| 7018 | * and ensure we don't carry in an old decay_count if we |
| 7019 | * switch back. |
| 7020 | */ |
Kirill Tkhai | 87e3c8a | 2013-07-21 04:32:07 +0400 | [diff] [blame] | 7021 | if (se->avg.decay_count) { |
| 7022 | __synchronize_entity_decay(se); |
| 7023 | subtract_blocked_load_contrib(cfs_rq, se->avg.load_avg_contrib); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 7024 | } |
| 7025 | #endif |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 7026 | } |
| 7027 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 7028 | /* |
| 7029 | * We switched to the sched_fair class. |
| 7030 | */ |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 7031 | static void switched_to_fair(struct rq *rq, struct task_struct *p) |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 7032 | { |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 7033 | if (!p->se.on_rq) |
| 7034 | return; |
| 7035 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 7036 | /* |
| 7037 | * We were most likely switched from sched_rt, so |
| 7038 | * kick off the schedule if running, otherwise just see |
| 7039 | * if we can still preempt the current task. |
| 7040 | */ |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 7041 | if (rq->curr == p) |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 7042 | resched_task(rq->curr); |
| 7043 | else |
Peter Zijlstra | 15afe09 | 2008-09-20 23:38:02 +0200 | [diff] [blame] | 7044 | check_preempt_curr(rq, p, 0); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 7045 | } |
| 7046 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 7047 | /* Account for a task changing its policy or group. |
| 7048 | * |
| 7049 | * This routine is mostly called to set cfs_rq->curr field when a task |
| 7050 | * migrates between groups/classes. |
| 7051 | */ |
| 7052 | static void set_curr_task_fair(struct rq *rq) |
| 7053 | { |
| 7054 | struct sched_entity *se = &rq->curr->se; |
| 7055 | |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 7056 | for_each_sched_entity(se) { |
| 7057 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 7058 | |
| 7059 | set_next_entity(cfs_rq, se); |
| 7060 | /* ensure bandwidth has been allocated on our new cfs_rq */ |
| 7061 | account_cfs_rq_runtime(cfs_rq, 0); |
| 7062 | } |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 7063 | } |
| 7064 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 7065 | void init_cfs_rq(struct cfs_rq *cfs_rq) |
| 7066 | { |
| 7067 | cfs_rq->tasks_timeline = RB_ROOT; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 7068 | cfs_rq->min_vruntime = (u64)(-(1LL << 20)); |
| 7069 | #ifndef CONFIG_64BIT |
| 7070 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; |
| 7071 | #endif |
Alex Shi | 141965c | 2013-06-26 13:05:39 +0800 | [diff] [blame] | 7072 | #ifdef CONFIG_SMP |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 7073 | atomic64_set(&cfs_rq->decay_counter, 1); |
Alex Shi | 2509940 | 2013-06-20 10:18:55 +0800 | [diff] [blame] | 7074 | atomic_long_set(&cfs_rq->removed_load, 0); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 7075 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 7076 | } |
| 7077 | |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 7078 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | b2b5ce0 | 2010-10-15 15:24:15 +0200 | [diff] [blame] | 7079 | 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] | 7080 | { |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 7081 | struct cfs_rq *cfs_rq; |
Peter Zijlstra | b2b5ce0 | 2010-10-15 15:24:15 +0200 | [diff] [blame] | 7082 | /* |
| 7083 | * If the task was not on the rq at the time of this cgroup movement |
| 7084 | * it must have been asleep, sleeping tasks keep their ->vruntime |
| 7085 | * absolute on their old rq until wakeup (needed for the fair sleeper |
| 7086 | * bonus in place_entity()). |
| 7087 | * |
| 7088 | * If it was on the rq, we've just 'preempted' it, which does convert |
| 7089 | * ->vruntime to a relative base. |
| 7090 | * |
| 7091 | * Make sure both cases convert their relative position when migrating |
| 7092 | * to another cgroup's rq. This does somewhat interfere with the |
| 7093 | * fair sleeper stuff for the first placement, but who cares. |
| 7094 | */ |
Daisuke Nishimura | 7ceff01 | 2011-12-15 14:36:07 +0900 | [diff] [blame] | 7095 | /* |
| 7096 | * When !on_rq, vruntime of the task has usually NOT been normalized. |
| 7097 | * But there are some cases where it has already been normalized: |
| 7098 | * |
| 7099 | * - Moving a forked child which is waiting for being woken up by |
| 7100 | * wake_up_new_task(). |
Daisuke Nishimura | 62af378 | 2011-12-15 14:37:41 +0900 | [diff] [blame] | 7101 | * - Moving a task which has been woken up by try_to_wake_up() and |
| 7102 | * waiting for actually being woken up by sched_ttwu_pending(). |
Daisuke Nishimura | 7ceff01 | 2011-12-15 14:36:07 +0900 | [diff] [blame] | 7103 | * |
| 7104 | * To prevent boost or penalty in the new cfs_rq caused by delta |
| 7105 | * min_vruntime between the two cfs_rqs, we skip vruntime adjustment. |
| 7106 | */ |
Daisuke Nishimura | 62af378 | 2011-12-15 14:37:41 +0900 | [diff] [blame] | 7107 | if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING)) |
Daisuke Nishimura | 7ceff01 | 2011-12-15 14:36:07 +0900 | [diff] [blame] | 7108 | on_rq = 1; |
| 7109 | |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 7110 | if (!on_rq) |
Peter Zijlstra | b2b5ce0 | 2010-10-15 15:24:15 +0200 | [diff] [blame] | 7111 | p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime; |
| 7112 | set_task_rq(p, task_cpu(p)); |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 7113 | if (!on_rq) { |
| 7114 | cfs_rq = cfs_rq_of(&p->se); |
| 7115 | p->se.vruntime += cfs_rq->min_vruntime; |
| 7116 | #ifdef CONFIG_SMP |
| 7117 | /* |
| 7118 | * migrate_task_rq_fair() will have removed our previous |
| 7119 | * contribution, but we must synchronize for ongoing future |
| 7120 | * decay. |
| 7121 | */ |
| 7122 | p->se.avg.decay_count = atomic64_read(&cfs_rq->decay_counter); |
| 7123 | cfs_rq->blocked_load_avg += p->se.avg.load_avg_contrib; |
| 7124 | #endif |
| 7125 | } |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 7126 | } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 7127 | |
| 7128 | void free_fair_sched_group(struct task_group *tg) |
| 7129 | { |
| 7130 | int i; |
| 7131 | |
| 7132 | destroy_cfs_bandwidth(tg_cfs_bandwidth(tg)); |
| 7133 | |
| 7134 | for_each_possible_cpu(i) { |
| 7135 | if (tg->cfs_rq) |
| 7136 | kfree(tg->cfs_rq[i]); |
| 7137 | if (tg->se) |
| 7138 | kfree(tg->se[i]); |
| 7139 | } |
| 7140 | |
| 7141 | kfree(tg->cfs_rq); |
| 7142 | kfree(tg->se); |
| 7143 | } |
| 7144 | |
| 7145 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) |
| 7146 | { |
| 7147 | struct cfs_rq *cfs_rq; |
| 7148 | struct sched_entity *se; |
| 7149 | int i; |
| 7150 | |
| 7151 | tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL); |
| 7152 | if (!tg->cfs_rq) |
| 7153 | goto err; |
| 7154 | tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL); |
| 7155 | if (!tg->se) |
| 7156 | goto err; |
| 7157 | |
| 7158 | tg->shares = NICE_0_LOAD; |
| 7159 | |
| 7160 | init_cfs_bandwidth(tg_cfs_bandwidth(tg)); |
| 7161 | |
| 7162 | for_each_possible_cpu(i) { |
| 7163 | cfs_rq = kzalloc_node(sizeof(struct cfs_rq), |
| 7164 | GFP_KERNEL, cpu_to_node(i)); |
| 7165 | if (!cfs_rq) |
| 7166 | goto err; |
| 7167 | |
| 7168 | se = kzalloc_node(sizeof(struct sched_entity), |
| 7169 | GFP_KERNEL, cpu_to_node(i)); |
| 7170 | if (!se) |
| 7171 | goto err_free_rq; |
| 7172 | |
| 7173 | init_cfs_rq(cfs_rq); |
| 7174 | init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); |
| 7175 | } |
| 7176 | |
| 7177 | return 1; |
| 7178 | |
| 7179 | err_free_rq: |
| 7180 | kfree(cfs_rq); |
| 7181 | err: |
| 7182 | return 0; |
| 7183 | } |
| 7184 | |
| 7185 | void unregister_fair_sched_group(struct task_group *tg, int cpu) |
| 7186 | { |
| 7187 | struct rq *rq = cpu_rq(cpu); |
| 7188 | unsigned long flags; |
| 7189 | |
| 7190 | /* |
| 7191 | * Only empty task groups can be destroyed; so we can speculatively |
| 7192 | * check on_list without danger of it being re-added. |
| 7193 | */ |
| 7194 | if (!tg->cfs_rq[cpu]->on_list) |
| 7195 | return; |
| 7196 | |
| 7197 | raw_spin_lock_irqsave(&rq->lock, flags); |
| 7198 | list_del_leaf_cfs_rq(tg->cfs_rq[cpu]); |
| 7199 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
| 7200 | } |
| 7201 | |
| 7202 | void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, |
| 7203 | struct sched_entity *se, int cpu, |
| 7204 | struct sched_entity *parent) |
| 7205 | { |
| 7206 | struct rq *rq = cpu_rq(cpu); |
| 7207 | |
| 7208 | cfs_rq->tg = tg; |
| 7209 | cfs_rq->rq = rq; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 7210 | init_cfs_rq_runtime(cfs_rq); |
| 7211 | |
| 7212 | tg->cfs_rq[cpu] = cfs_rq; |
| 7213 | tg->se[cpu] = se; |
| 7214 | |
| 7215 | /* se could be NULL for root_task_group */ |
| 7216 | if (!se) |
| 7217 | return; |
| 7218 | |
| 7219 | if (!parent) |
| 7220 | se->cfs_rq = &rq->cfs; |
| 7221 | else |
| 7222 | se->cfs_rq = parent->my_q; |
| 7223 | |
| 7224 | se->my_q = cfs_rq; |
Paul Turner | 0ac9b1c | 2013-10-16 11:16:27 -0700 | [diff] [blame] | 7225 | /* guarantee group entities always have weight */ |
| 7226 | update_load_set(&se->load, NICE_0_LOAD); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 7227 | se->parent = parent; |
| 7228 | } |
| 7229 | |
| 7230 | static DEFINE_MUTEX(shares_mutex); |
| 7231 | |
| 7232 | int sched_group_set_shares(struct task_group *tg, unsigned long shares) |
| 7233 | { |
| 7234 | int i; |
| 7235 | unsigned long flags; |
| 7236 | |
| 7237 | /* |
| 7238 | * We can't change the weight of the root cgroup. |
| 7239 | */ |
| 7240 | if (!tg->se[0]) |
| 7241 | return -EINVAL; |
| 7242 | |
| 7243 | shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES)); |
| 7244 | |
| 7245 | mutex_lock(&shares_mutex); |
| 7246 | if (tg->shares == shares) |
| 7247 | goto done; |
| 7248 | |
| 7249 | tg->shares = shares; |
| 7250 | for_each_possible_cpu(i) { |
| 7251 | struct rq *rq = cpu_rq(i); |
| 7252 | struct sched_entity *se; |
| 7253 | |
| 7254 | se = tg->se[i]; |
| 7255 | /* Propagate contribution to hierarchy */ |
| 7256 | raw_spin_lock_irqsave(&rq->lock, flags); |
Frederic Weisbecker | 71b1da4 | 2013-04-12 01:50:59 +0200 | [diff] [blame] | 7257 | |
| 7258 | /* Possible calls to update_curr() need rq clock */ |
| 7259 | update_rq_clock(rq); |
Linus Torvalds | 17bc14b | 2012-12-14 07:20:43 -0800 | [diff] [blame] | 7260 | for_each_sched_entity(se) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 7261 | update_cfs_shares(group_cfs_rq(se)); |
| 7262 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
| 7263 | } |
| 7264 | |
| 7265 | done: |
| 7266 | mutex_unlock(&shares_mutex); |
| 7267 | return 0; |
| 7268 | } |
| 7269 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
| 7270 | |
| 7271 | void free_fair_sched_group(struct task_group *tg) { } |
| 7272 | |
| 7273 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) |
| 7274 | { |
| 7275 | return 1; |
| 7276 | } |
| 7277 | |
| 7278 | void unregister_fair_sched_group(struct task_group *tg, int cpu) { } |
| 7279 | |
| 7280 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 7281 | |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 7282 | |
H Hartley Sweeten | 6d686f4 | 2010-01-13 20:21:52 -0700 | [diff] [blame] | 7283 | 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] | 7284 | { |
| 7285 | struct sched_entity *se = &task->se; |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 7286 | unsigned int rr_interval = 0; |
| 7287 | |
| 7288 | /* |
| 7289 | * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise |
| 7290 | * idle runqueue: |
| 7291 | */ |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 7292 | if (rq->cfs.load.weight) |
Zhu Yanhai | a59f4e0 | 2013-01-08 12:56:52 +0800 | [diff] [blame] | 7293 | rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se)); |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 7294 | |
| 7295 | return rr_interval; |
| 7296 | } |
| 7297 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 7298 | /* |
| 7299 | * All the scheduling class methods: |
| 7300 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 7301 | const struct sched_class fair_sched_class = { |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 7302 | .next = &idle_sched_class, |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 7303 | .enqueue_task = enqueue_task_fair, |
| 7304 | .dequeue_task = dequeue_task_fair, |
| 7305 | .yield_task = yield_task_fair, |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 7306 | .yield_to_task = yield_to_task_fair, |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 7307 | |
Ingo Molnar | 2e09bf5 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 7308 | .check_preempt_curr = check_preempt_wakeup, |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 7309 | |
| 7310 | .pick_next_task = pick_next_task_fair, |
| 7311 | .put_prev_task = put_prev_task_fair, |
| 7312 | |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 7313 | #ifdef CONFIG_SMP |
Li Zefan | 4ce72a2 | 2008-10-22 15:25:26 +0800 | [diff] [blame] | 7314 | .select_task_rq = select_task_rq_fair, |
Paul Turner | 0a74bef | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 7315 | .migrate_task_rq = migrate_task_rq_fair, |
Alex Shi | 141965c | 2013-06-26 13:05:39 +0800 | [diff] [blame] | 7316 | |
Christian Ehrhardt | 0bcdcf2 | 2009-11-30 12:16:46 +0100 | [diff] [blame] | 7317 | .rq_online = rq_online_fair, |
| 7318 | .rq_offline = rq_offline_fair, |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 7319 | |
| 7320 | .task_waking = task_waking_fair, |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 7321 | #endif |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 7322 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 7323 | .set_curr_task = set_curr_task_fair, |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 7324 | .task_tick = task_tick_fair, |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 7325 | .task_fork = task_fork_fair, |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 7326 | |
| 7327 | .prio_changed = prio_changed_fair, |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 7328 | .switched_from = switched_from_fair, |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 7329 | .switched_to = switched_to_fair, |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 7330 | |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 7331 | .get_rr_interval = get_rr_interval_fair, |
| 7332 | |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 7333 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | b2b5ce0 | 2010-10-15 15:24:15 +0200 | [diff] [blame] | 7334 | .task_move_group = task_move_group_fair, |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 7335 | #endif |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 7336 | }; |
| 7337 | |
| 7338 | #ifdef CONFIG_SCHED_DEBUG |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 7339 | void print_cfs_stats(struct seq_file *m, int cpu) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 7340 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 7341 | struct cfs_rq *cfs_rq; |
| 7342 | |
Peter Zijlstra | 5973e5b | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 7343 | rcu_read_lock(); |
Ingo Molnar | c3b64f1 | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 7344 | for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq) |
Ingo Molnar | 5cef9ec | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 7345 | print_cfs_rq(m, cpu, cfs_rq); |
Peter Zijlstra | 5973e5b | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 7346 | rcu_read_unlock(); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 7347 | } |
| 7348 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 7349 | |
| 7350 | __init void init_sched_fair_class(void) |
| 7351 | { |
| 7352 | #ifdef CONFIG_SMP |
| 7353 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains); |
| 7354 | |
Frederic Weisbecker | 3451d02 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 7355 | #ifdef CONFIG_NO_HZ_COMMON |
Diwakar Tundlam | 554ceca | 2012-03-07 14:44:26 -0800 | [diff] [blame] | 7356 | nohz.next_balance = jiffies; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 7357 | zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT); |
Suresh Siddha | 7132596 | 2012-01-19 18:28:57 -0800 | [diff] [blame] | 7358 | cpu_notifier(sched_ilb_notifier, 0); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 7359 | #endif |
| 7360 | #endif /* SMP */ |
| 7361 | |
| 7362 | } |