Skip to content

Commit fdf3e95

Browse files
Venkatesh PallipadiIngo Molnar
authored andcommitted
sched: Avoid side-effect of tickless idle on update_cpu_load
tickless idle has a negative side effect on update_cpu_load(), which in turn can affect load balancing behavior. update_cpu_load() is supposed to be called every tick, to keep track of various load indicies. With tickless idle, there are no scheduler ticks called on the idle CPUs. Idle CPUs may still do load balancing (with idle_load_balance CPU) using the stale cpu_load. It will also cause problems when all CPUs go idle for a while and become active again. In this case loads would not degrade as expected. This is how rq->nr_load_updates change looks like under different conditions: <cpu_num> <nr_load_updates change> All CPUS idle for 10 seconds (HZ=1000) 0 1621 10 496 11 139 12 875 13 1672 14 12 15 21 1 1472 2 2426 3 1161 4 2108 5 1525 6 701 7 249 8 766 9 1967 One CPU busy rest idle for 10 seconds 0 10003 10 601 11 95 12 966 13 1597 14 114 15 98 1 3457 2 93 3 6679 4 1425 5 1479 6 595 7 193 8 633 9 1687 All CPUs busy for 10 seconds 0 10026 10 10026 11 10026 12 10026 13 10025 14 10025 15 10025 1 10026 2 10026 3 10026 4 10026 5 10026 6 10026 7 10026 8 10026 9 10026 That is update_cpu_load works properly only when all CPUs are busy. If all are idle, all the CPUs get way lower updates. And when few CPUs are busy and rest are idle, only busy and ilb CPU does proper updates and rest of the idle CPUs will do lower updates. The patch keeps track of when a last update was done and fixes up the load avg based on current time. On one of my test system SPECjbb with warehouse 1..numcpus, patch improves throughput numbers by ~1% (average of 6 runs). On another test system (with different domain hierarchy) there is no noticable change in perf. Signed-off-by: Venkatesh Pallipadi <venki@google.com> Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Thomas Gleixner <tglx@linutronix.de> LKML-Reference: <AANLkTilLtDWQsAUrIxJ6s04WTgmw9GuOODc5AOrYsaR5@mail.gmail.com> Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent 246d86b commit fdf3e95

2 files changed

Lines changed: 99 additions & 6 deletions

File tree

‎kernel/sched.c‎

Lines changed: 95 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ struct rq {
457457
unsigned long nr_running;
458458
#define CPU_LOAD_IDX_MAX 5
459459
unsigned long cpu_load[CPU_LOAD_IDX_MAX];
460+
unsigned long last_load_update_tick;
460461
#ifdef CONFIG_NO_HZ
461462
u64 nohz_stamp;
462463
unsigned char in_nohz_recently;
@@ -1803,6 +1804,7 @@ static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
18031804
static void calc_load_account_idle(struct rq *this_rq);
18041805
static void update_sysctl(void);
18051806
static int get_update_sysctl_factor(void);
1807+
static void update_cpu_load(struct rq *this_rq);
18061808

18071809
static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
18081810
{
@@ -3049,34 +3051,119 @@ static void calc_load_account_active(struct rq *this_rq)
30493051
this_rq->calc_load_update += LOAD_FREQ;
30503052
}
30513053

3054+
/*
3055+
* The exact cpuload at various idx values, calculated at every tick would be
3056+
* load = (2^idx - 1) / 2^idx * load + 1 / 2^idx * cur_load
3057+
*
3058+
* If a cpu misses updates for n-1 ticks (as it was idle) and update gets called
3059+
* on nth tick when cpu may be busy, then we have:
3060+
* load = ((2^idx - 1) / 2^idx)^(n-1) * load
3061+
* load = (2^idx - 1) / 2^idx) * load + 1 / 2^idx * cur_load
3062+
*
3063+
* decay_load_missed() below does efficient calculation of
3064+
* load = ((2^idx - 1) / 2^idx)^(n-1) * load
3065+
* avoiding 0..n-1 loop doing load = ((2^idx - 1) / 2^idx) * load
3066+
*
3067+
* The calculation is approximated on a 128 point scale.
3068+
* degrade_zero_ticks is the number of ticks after which load at any
3069+
* particular idx is approximated to be zero.
3070+
* degrade_factor is a precomputed table, a row for each load idx.
3071+
* Each column corresponds to degradation factor for a power of two ticks,
3072+
* based on 128 point scale.
3073+
* Example:
3074+
* row 2, col 3 (=12) says that the degradation at load idx 2 after
3075+
* 8 ticks is 12/128 (which is an approximation of exact factor 3^8/4^8).
3076+
*
3077+
* With this power of 2 load factors, we can degrade the load n times
3078+
* by looking at 1 bits in n and doing as many mult/shift instead of
3079+
* n mult/shifts needed by the exact degradation.
3080+
*/
3081+
#define DEGRADE_SHIFT 7
3082+
static const unsigned char
3083+
degrade_zero_ticks[CPU_LOAD_IDX_MAX] = {0, 8, 32, 64, 128};
3084+
static const unsigned char
3085+
degrade_factor[CPU_LOAD_IDX_MAX][DEGRADE_SHIFT + 1] = {
3086+
{0, 0, 0, 0, 0, 0, 0, 0},
3087+
{64, 32, 8, 0, 0, 0, 0, 0},
3088+
{96, 72, 40, 12, 1, 0, 0},
3089+
{112, 98, 75, 43, 15, 1, 0},
3090+
{120, 112, 98, 76, 45, 16, 2} };
3091+
3092+
/*
3093+
* Update cpu_load for any missed ticks, due to tickless idle. The backlog
3094+
* would be when CPU is idle and so we just decay the old load without
3095+
* adding any new load.
3096+
*/
3097+
static unsigned long
3098+
decay_load_missed(unsigned long load, unsigned long missed_updates, int idx)
3099+
{
3100+
int j = 0;
3101+
3102+
if (!missed_updates)
3103+
return load;
3104+
3105+
if (missed_updates >= degrade_zero_ticks[idx])
3106+
return 0;
3107+
3108+
if (idx == 1)
3109+
return load >> missed_updates;
3110+
3111+
while (missed_updates) {
3112+
if (missed_updates % 2)
3113+
load = (load * degrade_factor[idx][j]) >> DEGRADE_SHIFT;
3114+
3115+
missed_updates >>= 1;
3116+
j++;
3117+
}
3118+
return load;
3119+
}
3120+
30523121
/*
30533122
* Update rq->cpu_load[] statistics. This function is usually called every
3054-
* scheduler tick (TICK_NSEC).
3123+
* scheduler tick (TICK_NSEC). With tickless idle this will not be called
3124+
* every tick. We fix it up based on jiffies.
30553125
*/
30563126
static void update_cpu_load(struct rq *this_rq)
30573127
{
30583128
unsigned long this_load = this_rq->load.weight;
3129+
unsigned long curr_jiffies = jiffies;
3130+
unsigned long pending_updates;
30593131
int i, scale;
30603132

30613133
this_rq->nr_load_updates++;
30623134

3135+
/* Avoid repeated calls on same jiffy, when moving in and out of idle */
3136+
if (curr_jiffies == this_rq->last_load_update_tick)
3137+
return;
3138+
3139+
pending_updates = curr_jiffies - this_rq->last_load_update_tick;
3140+
this_rq->last_load_update_tick = curr_jiffies;
3141+
30633142
/* Update our load: */
3064-
for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
3143+
this_rq->cpu_load[0] = this_load; /* Fasttrack for idx 0 */
3144+
for (i = 1, scale = 2; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
30653145
unsigned long old_load, new_load;
30663146

30673147
/* scale is effectively 1 << i now, and >> i divides by scale */
30683148

30693149
old_load = this_rq->cpu_load[i];
3150+
old_load = decay_load_missed(old_load, pending_updates - 1, i);
30703151
new_load = this_load;
30713152
/*
30723153
* Round up the averaging division if load is increasing. This
30733154
* prevents us from getting stuck on 9 if the load is 10, for
30743155
* example.
30753156
*/
30763157
if (new_load > old_load)
3077-
new_load += scale-1;
3078-
this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
3158+
new_load += scale - 1;
3159+
3160+
this_rq->cpu_load[i] = (old_load * (scale - 1) + new_load) >> i;
30793161
}
3162+
}
3163+
3164+
static void update_cpu_load_active(struct rq *this_rq)
3165+
{
3166+
update_cpu_load(this_rq);
30803167

30813168
calc_load_account_active(this_rq);
30823169
}
@@ -3464,7 +3551,7 @@ void scheduler_tick(void)
34643551

34653552
raw_spin_lock(&rq->lock);
34663553
update_rq_clock(rq);
3467-
update_cpu_load(rq);
3554+
update_cpu_load_active(rq);
34683555
curr->sched_class->task_tick(rq, curr, 0);
34693556
raw_spin_unlock(&rq->lock);
34703557

@@ -7688,6 +7775,9 @@ void __init sched_init(void)
76887775

76897776
for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
76907777
rq->cpu_load[j] = 0;
7778+
7779+
rq->last_load_update_tick = jiffies;
7780+
76917781
#ifdef CONFIG_SMP
76927782
rq->sd = NULL;
76937783
rq->rd = NULL;

‎kernel/sched_fair.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3420,9 +3420,12 @@ static void run_rebalance_domains(struct softirq_action *h)
34203420
if (need_resched())
34213421
break;
34223422

3423+
rq = cpu_rq(balance_cpu);
3424+
raw_spin_lock_irq(&rq->lock);
3425+
update_cpu_load(rq);
3426+
raw_spin_unlock_irq(&rq->lock);
34233427
rebalance_domains(balance_cpu, CPU_IDLE);
34243428

3425-
rq = cpu_rq(balance_cpu);
34263429
if (time_after(this_rq->next_balance, rq->next_balance))
34273430
this_rq->next_balance = rq->next_balance;
34283431
}

0 commit comments

Comments
 (0)