sched: force update of blocked load of idle cpus
authorVincent Guittot <vincent.guittot@linaro.org>
Tue, 24 Oct 2017 12:25:55 +0000 (13:25 +0100)
committerLukasz Luba <l.luba@partner.samsung.com>
Mon, 10 Sep 2018 08:20:25 +0000 (10:20 +0200)
When idle, the blocked load of CPUs will be updated only when an idle
load balance is triggered which may never happen. Because of this
uncertainty on the execution of idle load balance, the utilization,
the load and the shares of idle cfs_rq can stay artificially high and
steal shares and running time to busy cfs_rqs of the task group.
Add a new light idle load balance state which ensures that blocked loads
are periodically updated and decayed but does not perform any task
migration.

The remote load udpates are rate-limited, so that they are not
performed with a shorter period than LOAD_AVG_PERIOD (i.e. PELT
half-life). This is the period after which we have a known 50% error
in stale load.

Cc: Dietmar Eggemann <dietmar.eggemann@arm.com>
Cc: Vincent Guittot <vincent.guittot@linaro.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Morten Rasmussen <morten.rasmussen@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Vincent Guittot <vincent.guittot@linaro.org>
[Switched remote update interval to use PELT half life]
[Moved update_blocked_averges call outside rebalance_domains
 to simplify code]
Signed-off-by: Brendan Jackman <brendan.jackman@arm.com>
Signed-off-by: Lukasz Luba <l.luba@partner.samsung.com>
kernel/sched/fair.c
kernel/sched/sched.h

index be7bbc12ef07f7ade05f0aafb27f162b50bd3bb9..b61599f9c9613e0fee45fa1f812830d4eb11794c 100644 (file)
@@ -6268,6 +6268,9 @@ static bool cpu_overutilized(int cpu)
        return (capacity_of(cpu) * 1024) < (cpu_util(cpu) * capacity_margin);
 }
 
+static inline bool nohz_kick_needed(struct rq *rq, bool only_update);
+static void nohz_balancer_kick(bool only_update);
+
 /*
  * select_task_rq_fair: Select target runqueue for the waking task in domains
  * that have the 'sd_flag' flag set. In practice, this is SD_BALANCE_WAKE,
@@ -6344,6 +6347,11 @@ pick_cpu:
        }
        rcu_read_unlock();
 
+#ifdef CONFIG_NO_HZ_COMMON
+       if (nohz_kick_needed(cpu_rq(new_cpu), true))
+               nohz_balancer_kick(true);
+#endif
+
        return new_cpu;
 }
 
@@ -9010,6 +9018,7 @@ static struct {
        cpumask_var_t idle_cpus_mask;
        atomic_t nr_cpus;
        unsigned long next_balance;     /* in jiffy units */
+       unsigned long next_update;     /* in jiffy units */
 } nohz ____cacheline_aligned;
 
 static inline int find_new_ilb(void)
@@ -9027,7 +9036,7 @@ static inline int find_new_ilb(void)
  * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
  * CPU (if there is one).
  */
-static void nohz_balancer_kick(void)
+static void nohz_balancer_kick(bool only_update)
 {
        int ilb_cpu;
 
@@ -9040,6 +9049,10 @@ static void nohz_balancer_kick(void)
 
        if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
                return;
+
+       if (only_update)
+               set_bit(NOHZ_STATS_KICK, nohz_flags(ilb_cpu));
+
        /*
         * Use smp_send_reschedule() instead of resched_cpu().
         * This way we generate a sched IPI on the target cpu which
@@ -9127,6 +9140,8 @@ void nohz_balance_enter_idle(int cpu)
        atomic_inc(&nohz.nr_cpus);
        set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
 }
+#else
+static inline void nohz_balancer_kick(bool only_update) {}
 #endif
 
 static DEFINE_SPINLOCK(balancing);
@@ -9158,8 +9173,6 @@ static void rebalance_domains(struct rq *rq, enum cpu_idle_type idle)
        int need_serialize, need_decay = 0;
        u64 max_cost = 0;
 
-       update_blocked_averages(cpu);
-
        rcu_read_lock();
        for_each_domain(cpu, sd) {
                /*
@@ -9258,6 +9271,7 @@ static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle)
 {
        int this_cpu = this_rq->cpu;
        struct rq *rq;
+       struct sched_domain *sd;
        int balance_cpu;
        /* Earliest time when we have to do rebalance again */
        unsigned long next_balance = jiffies + 60*HZ;
@@ -9267,6 +9281,23 @@ static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle)
            !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
                goto end;
 
+       /*
+        * This cpu is going to update the blocked load of idle CPUs either
+        * before doing a rebalancing or just to keep metrics up to date. we
+        * can safely update the next update timestamp
+        */
+       rcu_read_lock();
+       sd = rcu_dereference(this_rq->sd);
+       /*
+        * Check whether there is a sched_domain available for this cpu.
+        * The last other cpu can have been unplugged since the ILB has been
+        * triggered and the sched_domain can now be null. The idle balance
+        * sequence will quickly be aborted as there is no more idle CPUs
+        */
+       if (sd)
+               nohz.next_update = jiffies + msecs_to_jiffies(LOAD_AVG_PERIOD);
+       rcu_read_unlock();
+
        for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
                if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
                        continue;
@@ -9293,7 +9324,15 @@ static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle)
                        cpu_load_update_idle(rq);
                        rq_unlock_irq(rq, &rf);
 
-                       rebalance_domains(rq, CPU_IDLE);
+                       update_blocked_averages(balance_cpu);
+                       /*
+                        * This idle load balance softirq may have been
+                        * triggered only to update the blocked load and shares
+                        * of idle CPUs (which we have just done for
+                        * balance_cpu). In that case skip the actual balance.
+                        */
+                       if (!test_bit(NOHZ_STATS_KICK, nohz_flags(this_cpu)))
+                               rebalance_domains(rq, idle);
                }
 
                if (time_after(next_balance, rq->next_balance)) {
@@ -9324,7 +9363,7 @@ end:
  *   - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
  *     domain span are idle.
  */
-static inline bool nohz_kick_needed(struct rq *rq)
+static inline bool nohz_kick_needed(struct rq *rq, bool only_update)
 {
        unsigned long now = jiffies;
        struct sched_domain_shared *sds;
@@ -9332,7 +9371,7 @@ static inline bool nohz_kick_needed(struct rq *rq)
        int nr_busy, i, cpu = rq->cpu;
        bool kick = false;
 
-       if (unlikely(rq->idle_balance))
+       if (unlikely(rq->idle_balance) && !only_update)
                return false;
 
        /*
@@ -9349,6 +9388,13 @@ static inline bool nohz_kick_needed(struct rq *rq)
        if (likely(!atomic_read(&nohz.nr_cpus)))
                return false;
 
+       if (only_update) {
+               if (time_before(now, nohz.next_update))
+                       return false;
+               else
+                       return true;
+       }
+
        if (time_before(now, nohz.next_balance))
                return false;
 
@@ -9398,6 +9444,7 @@ unlock:
 }
 #else
 static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) { }
+static inline bool nohz_kick_needed(struct rq *rq, bool only_update) { return false; }
 #endif
 
 /*
@@ -9419,7 +9466,12 @@ static __latent_entropy void run_rebalance_domains(struct softirq_action *h)
         * and abort nohz_idle_balance altogether if we pull some load.
         */
        nohz_idle_balance(this_rq, idle);
-       rebalance_domains(this_rq, idle);
+       update_blocked_averages(this_rq->cpu);
+       if (!test_bit(NOHZ_STATS_KICK, nohz_flags(this_rq->cpu)))
+               rebalance_domains(this_rq, idle);
+#ifdef CONFIG_NO_HZ_COMMON
+       clear_bit(NOHZ_STATS_KICK, nohz_flags(this_rq->cpu));
+#endif
 }
 
 /*
@@ -9434,8 +9486,8 @@ void trigger_load_balance(struct rq *rq)
        if (time_after_eq(jiffies, rq->next_balance))
                raise_softirq(SCHED_SOFTIRQ);
 #ifdef CONFIG_NO_HZ_COMMON
-       if (nohz_kick_needed(rq))
-               nohz_balancer_kick();
+       if (nohz_kick_needed(rq, false))
+               nohz_balancer_kick(false);
 #endif
 }
 
@@ -10012,6 +10064,7 @@ __init void init_sched_fair_class(void)
 
 #ifdef CONFIG_NO_HZ_COMMON
        nohz.next_balance = jiffies;
+       nohz.next_update = jiffies;
        zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
 #endif
 #endif /* SMP */
index e0048c2d8fa6d859e94e4ad0f244523342a2cee7..9ccbf14d0dd7aa31fe9ef5e6e80a9bf2367c2809 100644 (file)
@@ -2018,6 +2018,7 @@ extern void cfs_bandwidth_usage_dec(void);
 enum rq_nohz_flag_bits {
        NOHZ_TICK_STOPPED,
        NOHZ_BALANCE_KICK,
+       NOHZ_STATS_KICK
 };
 
 #define nohz_flags(cpu)        (&cpu_rq(cpu)->nohz_flags)