Skip to contentSkip to conversation

sched: Fix/improve nohz cpu load updates v2

PATCH In review Frederic Weisbecker 3 participants 14 messages Apr 8, 2016 – Apr 12, 2016

Subsystems (5)
lore
Share & tools
lore
#2 lore raw

PATCH 2/3 sched: Correctly handle nohz ticks cpu load accounting

In reply to Frederic Weisbecker

Ticks can happen while the CPU is in dynticks-idle or dynticks-singletask mode. In fact "nohz" or "dynticks" only mean that we exit the periodic mode and we try to minimize the ticks as much as possible. The nohz subsystem uses a confusing terminology with the internal state "ts->tick_stopped" which is also available through its public interface with tick_nohz_tick_stopped(). This is a misnomer as the tick is instead reduced with the best effort rather than stopped. In the best case the tick can indeed be actually stopped but there is no guarantee about that. If a timer needs to fire one second later, a tick will fire while the CPU is in nohz mode and this is a very common scenario.

Now this confusion happens to be a problem with cpu load updates:
cpu_load_update_active() doesn't handle nohz ticks correctly because it assumes that ticks are completely stopped in nohz mode and that cpu_load_update_active() can't be called in dynticks mode. When that happens, the whole previous tickless load is ignored and the function just records the load for the current tick, ignoring potentially long idle periods behind.

In order to solve this, we could account the current load for the previous nohz time but there is a risk that we account the load of a task that got freshly enqueued for the whole nohz period.

So instead, lets record the dynticks load on nohz frame entry so we know what to record in case of nohz ticks, then use this record to account the tickless load on nohz ticks and nohz frame end.

Cc Byungchul Park <[email protected]>
Cc Chris Metcalf <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Luiz Capitulino <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 include/linux/sched.h    |  6 ++-
 kernel/sched/fair.c      | 95 +++++++++++++++++++++++++++++++-----------------
 kernel/time/tick-sched.c |  9 +++--
 3 files changed, 70 insertions(+), 40 deletions(-)
include/linux/sched.h +4 -2
index aa5ee0d..07b1b1e 100644
@@ -178,9 +178,11 @@ extern void get_iowait_load(unsigned long *nr_waiters, unsigned long *load);
 extern void calc_global_load(unsigned long ticks);
 
 #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
-extern void cpu_load_update_nohz(int active);
+extern void cpu_load_update_nohz_start(void);
+extern void cpu_load_update_nohz_stop(void);
 #else
-static inline void cpu_load_update_nohz(int active) { }
+static inline void cpu_load_update_nohz_start(void) { }
+static inline void cpu_load_update_nohz_stop(void) { }
 #endif
 
 extern void dump_cpu_task(int cpu);
kernel/sched/fair.c +61 -34

151 lines not on this page · raw message

kernel/time/tick-sched.c +5 -4
index 66bdc9a..31872bc 100644
@@ -776,6 +776,7 @@ static ktime_t tick_nohz_stop_sched_tick(struct tick_sched *ts,
 	if (!ts->tick_stopped) {
 		nohz_balance_enter_idle(cpu);
 		calc_load_enter_idle();
+		cpu_load_update_nohz_start();
 
 		ts->last_tick = hrtimer_get_expires(&ts->sched_timer);
 		ts->tick_stopped = 1;
@@ -802,11 +803,11 @@ out:
 	return tick;
 }
 
-static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now, int active)
+static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now)
 {
 	/* Update jiffies first */
 	tick_do_update_jiffies64(now);
-	cpu_load_update_nohz(active);
+	cpu_load_update_nohz_stop();
 
 	calc_load_exit_idle();
 	touch_softlockup_watchdog_sched();
@@ -833,7 +834,7 @@ static void tick_nohz_full_update_tick(struct tick_sched *ts)
 	if (can_stop_full_tick(ts))
 		tick_nohz_stop_sched_tick(ts, ktime_get(), cpu);
 	else if (ts->tick_stopped)
-		tick_nohz_restart_sched_tick(ts, ktime_get(), 1);
+		tick_nohz_restart_sched_tick(ts, ktime_get());
 #endif
 }
 
@@ -1024,7 +1025,7 @@ void tick_nohz_idle_exit(void)
 		tick_nohz_stop_idle(ts, now);
 
 	if (ts->tick_stopped) {
-		tick_nohz_restart_sched_tick(ts, now, 0);
+		tick_nohz_restart_sched_tick(ts, now);
 		tick_nohz_account_idle_ticks(ts);
 	}
 
Signature · 1 line
2.7.0
#6 lore raw

PATCH 3/3 sched: Optimize !CONFIG_NO_HZ_COMMON cpu load updates

In reply to Frederic Weisbecker

Some code in cpu load update only concern NO_HZ configs but it is built on all configurations. When NO_HZ isn't built, that code is harmless but just happens to take some useless ressources in CPU and memory:

1) one useless field in struct rq
2) jiffies record on every tick that is never used (cpu_load_update_periodic)
3) decay_load_missed is called two times on every tick to eventually return immediately with no action taken. And that function is dead code.

For pure optimization purposes, lets conditionally build the NO_HZ related code.

Cc Byungchul Park <[email protected]>
Cc Chris Metcalf <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Luiz Capitulino <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 kernel/sched/core.c  |  3 ++-
 kernel/sched/fair.c  | 43 ++++++++++++++++++++++++++++++++-----------
 kernel/sched/sched.h |  6 ++++--
 3 files changed, 38 insertions(+), 14 deletions(-)
kernel/sched/core.c +2 -1
index 4c522a7..59a2821 100644
@@ -7327,8 +7327,9 @@ void __init sched_init(void)
 
 		for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
 			rq->cpu_load[j] = 0;
-
+#ifdef CONFIG_NO_HZ_COMMON
 		rq->last_load_update_tick = jiffies;
+#endif
 
 #ifdef CONFIG_SMP
 		rq->sd = NULL;
kernel/sched/fair.c +32 -11

69 lines not on this page · raw message

kernel/sched/sched.h +4 -2
index 1802013..2302bb6 100644
@@ -585,11 +585,13 @@ struct rq {
 #endif
 	#define CPU_LOAD_IDX_MAX 5
 	unsigned long cpu_load[CPU_LOAD_IDX_MAX];
+#ifdef CONFIG_NO_HZ_COMMON
+#ifdef CONFIG_SMP
 	unsigned long last_load_update_tick;
-#ifdef CONFIG_NO_HZ_COMMON
+#endif /* CONFIG_SMP */
 	u64 nohz_stamp;
 	unsigned long nohz_flags;
-#endif
+#endif /* CONFIG_NO_HZ_COMMON */
 #ifdef CONFIG_NO_HZ_FULL
 	unsigned long last_sched_tick;
 #endif
Signature · 1 line
2.7.0
#0 lore raw

cover sched: Fix/improve nohz cpu load updates v2

This series tries to fix issues against CFS cpu load accounting in nohz configs (both idle and full nohz). Some optimizations coming along.

Following Peterz reviews, I've tried to improve the changelogs. Comments have been updated as well and some changes have been refactored.

git://git.kernel.org/pub/scm/linux/kernel/git/frederic/linux-dynticks.git
sched/nohz-v2

HEAD: 55cb7c1a25acb140253ca4e55f8d43cd31404b55

Thanks,
Frederic
---

Frederic Weisbecker (3):
sched: Gather cpu load functions under a more conventional namespace
sched: Correctly handle nohz ticks cpu load accounting
sched: Optimize !CONFIG_NO_HZ_COMMON cpu load updates

 Documentation/trace/ftrace.txt |  10 +--
 include/linux/sched.h          |   6 +-
 kernel/sched/core.c            |   5 +-
 kernel/sched/fair.c            | 146 +++++++++++++++++++++++++++--------------
 kernel/sched/sched.h           |  10 +--
 kernel/time/tick-sched.c       |   9 +--
 6 files changed, 120 insertions(+), 66 deletions(-)
#1 lore raw

PATCH 1/3 sched: Gather cpu load functions under a more conventional namespace

The cpu load update related functions have a weak naming convention currently, starting with update_cpu_load_*() which isn't ideal as "update" is a very generic concept.

Since two of these functions are public already (and a third is to come) that's enough to introduce a more conventional naming scheme. So let's do the following rename instead:

	update_cpu_load_*() -> cpu_load_update_*()
Cc Byungchul Park <[email protected]>
Cc Chris Metcalf <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Luiz Capitulino <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 Documentation/trace/ftrace.txt | 10 +++++-----
 include/linux/sched.h          |  4 ++--
 kernel/sched/core.c            |  2 +-
 kernel/sched/fair.c            | 24 ++++++++++++------------
 kernel/sched/sched.h           |  4 ++--
 kernel/time/tick-sched.c       |  2 +-
 6 files changed, 23 insertions(+), 23 deletions(-)
Documentation/trace/ftrace.txt +5 -5
index f52f297..9857606 100644
@@ -1562,12 +1562,12 @@ Doing the same with chrt -r 5 and function-trace set.
   <idle>-0       3dN.1   12us : menu_hrtimer_cancel <-tick_nohz_idle_exit
   <idle>-0       3dN.1   12us : ktime_get <-tick_nohz_idle_exit
   <idle>-0       3dN.1   12us : tick_do_update_jiffies64 <-tick_nohz_idle_exit
-  <idle>-0       3dN.1   13us : update_cpu_load_nohz <-tick_nohz_idle_exit
-  <idle>-0       3dN.1   13us : _raw_spin_lock <-update_cpu_load_nohz
+  <idle>-0       3dN.1   13us : cpu_load_update_nohz <-tick_nohz_idle_exit
+  <idle>-0       3dN.1   13us : _raw_spin_lock <-cpu_load_update_nohz
   <idle>-0       3dN.1   13us : add_preempt_count <-_raw_spin_lock
-  <idle>-0       3dN.2   13us : __update_cpu_load <-update_cpu_load_nohz
-  <idle>-0       3dN.2   14us : sched_avg_update <-__update_cpu_load
-  <idle>-0       3dN.2   14us : _raw_spin_unlock <-update_cpu_load_nohz
+  <idle>-0       3dN.2   13us : __cpu_load_update <-cpu_load_update_nohz
+  <idle>-0       3dN.2   14us : sched_avg_update <-__cpu_load_update
+  <idle>-0       3dN.2   14us : _raw_spin_unlock <-cpu_load_update_nohz
   <idle>-0       3dN.2   14us : sub_preempt_count <-_raw_spin_unlock
   <idle>-0       3dN.1   15us : calc_load_exit_idle <-tick_nohz_idle_exit
   <idle>-0       3dN.1   15us : touch_softlockup_watchdog <-tick_nohz_idle_exit
include/linux/sched.h +2 -2
index 52c4847..aa5ee0d 100644
@@ -178,9 +178,9 @@ extern void get_iowait_load(unsigned long *nr_waiters, unsigned long *load);
 extern void calc_global_load(unsigned long ticks);
 
 #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
-extern void update_cpu_load_nohz(int active);
+extern void cpu_load_update_nohz(int active);
 #else
-static inline void update_cpu_load_nohz(int active) { }
+static inline void cpu_load_update_nohz(int active) { }
 #endif
 
 extern void dump_cpu_task(int cpu);
kernel/sched/core.c +1 -1
index 8b489fc..4c522a7 100644
@@ -2915,7 +2915,7 @@ void scheduler_tick(void)
 	raw_spin_lock(&rq->lock);
 	update_rq_clock(rq);
 	curr->sched_class->task_tick(rq, curr, 0);
-	update_cpu_load_active(rq);
+	cpu_load_update_active(rq);
 	calc_global_load_tick(rq);
 	raw_spin_unlock(&rq->lock);
 
kernel/sched/fair.c +12 -12
index 0fe30e66..f33764d 100644
@@ -4491,7 +4491,7 @@ decay_load_missed(unsigned long load, unsigned long missed_updates, int idx)
 }
 
 /**
- * __update_cpu_load - update the rq->cpu_load[] statistics
+ * __cpu_load_update - update the rq->cpu_load[] statistics
  * @this_rq: The rq to update statistics for
  * @this_load: The current load
  * @pending_updates: The number of missed updates
@@ -4526,7 +4526,7 @@ decay_load_missed(unsigned long load, unsigned long missed_updates, int idx)
  * see decay_load_misses(). For NOHZ_FULL we get to subtract and add the extra
  * term. See the @active paramter.
  */
-static void __update_cpu_load(struct rq *this_rq, unsigned long this_load,
+static void __cpu_load_update(struct rq *this_rq, unsigned long this_load,
 			      unsigned long pending_updates, int active)
 {
 	unsigned long tickless_load = active ? this_rq->cpu_load[0] : 0;
@@ -4574,7 +4574,7 @@ static unsigned long weighted_cpuload(const int cpu)
 }
 
 #ifdef CONFIG_NO_HZ_COMMON
-static void __update_cpu_load_nohz(struct rq *this_rq,
+static void __cpu_load_update_nohz(struct rq *this_rq,
 				   unsigned long curr_jiffies,
 				   unsigned long load,
 				   int active)
@@ -4589,7 +4589,7 @@ static void __update_cpu_load_nohz(struct rq *this_rq,
 		 * In the NOHZ_FULL case, we were non-idle, we should consider
 		 * its weighted load.
 		 */
-		__update_cpu_load(this_rq, load, pending_updates, active);
+		__cpu_load_update(this_rq, load, pending_updates, active);
 	}
 }
 
@@ -4610,7 +4610,7 @@ static void __update_cpu_load_nohz(struct rq *this_rq,
  * Called from nohz_idle_balance() to update the load ratings before doing the
  * idle balance.
  */
-static void update_cpu_load_idle(struct rq *this_rq)
+static void cpu_load_update_idle(struct rq *this_rq)
 {
 	/*
 	 * bail if there's load or we're actually up-to-date.
@@ -4618,13 +4618,13 @@ static void update_cpu_load_idle(struct rq *this_rq)
 	if (weighted_cpuload(cpu_of(this_rq)))
 		return;
 
-	__update_cpu_load_nohz(this_rq, READ_ONCE(jiffies), 0, 0);
+	__cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), 0, 0);
 }
 
 /*
  * Called from tick_nohz_idle_exit() -- try and fix up the ticks we missed.
  */
-void update_cpu_load_nohz(int active)
+void cpu_load_update_nohz(int active)
 {
 	struct rq *this_rq = this_rq();
 	unsigned long curr_jiffies = READ_ONCE(jiffies);
@@ -4634,7 +4634,7 @@ void update_cpu_load_nohz(int active)
 		return;
 
 	raw_spin_lock(&this_rq->lock);
-	__update_cpu_load_nohz(this_rq, curr_jiffies, load, active);
+	__cpu_load_update_nohz(this_rq, curr_jiffies, load, active);
 	raw_spin_unlock(&this_rq->lock);
 }
 #endif /* CONFIG_NO_HZ */
@@ -4642,14 +4642,14 @@ void update_cpu_load_nohz(int active)
 /*
  * Called from scheduler_tick()
  */
-void update_cpu_load_active(struct rq *this_rq)
+void cpu_load_update_active(struct rq *this_rq)
 {
 	unsigned long load = weighted_cpuload(cpu_of(this_rq));
 	/*
-	 * See the mess around update_cpu_load_idle() / update_cpu_load_nohz().
+	 * See the mess around cpu_load_update_idle() / cpu_load_update_nohz().
 	 */
 	this_rq->last_load_update_tick = jiffies;
-	__update_cpu_load(this_rq, load, 1, 1);
+	__cpu_load_update(this_rq, load, 1, 1);
 }
 
 /*
@@ -7957,7 +7957,7 @@ static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle)
 		if (time_after_eq(jiffies, rq->next_balance)) {
 			raw_spin_lock_irq(&rq->lock);
 			update_rq_clock(rq);
-			update_cpu_load_idle(rq);
+			cpu_load_update_idle(rq);
 			raw_spin_unlock_irq(&rq->lock);
 			rebalance_domains(rq, CPU_IDLE);
 		}
kernel/sched/sched.h +2 -2
index ec2e8d2..1802013 100644
@@ -31,9 +31,9 @@ extern void calc_global_load_tick(struct rq *this_rq);
 extern long calc_load_fold_active(struct rq *this_rq);
 
 #ifdef CONFIG_SMP
-extern void update_cpu_load_active(struct rq *this_rq);
+extern void cpu_load_update_active(struct rq *this_rq);
 #else
-static inline void update_cpu_load_active(struct rq *this_rq) { }
+static inline void cpu_load_update_active(struct rq *this_rq) { }
 #endif
 
 /*
kernel/time/tick-sched.c +1 -1
index 58e3310..66bdc9a 100644
@@ -806,7 +806,7 @@ static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now, int
 {
 	/* Update jiffies first */
 	tick_do_update_jiffies64(now);
-	update_cpu_load_nohz(active);
+	cpu_load_update_nohz(active);
 
 	calc_load_exit_idle();
 	touch_softlockup_watchdog_sched();
Signature · 1 line
2.7.0
maintainer
#3 lore raw

Re: [PATCH 2/3] sched: Correctly handle nohz ticks cpu load accounting

In reply to Frederic Weisbecker

kernel/sched/fair.c static void cpu_load_update_idle(struct rq *this_rq) · line 4637

Quoted 11 lines · Frederic Weisbecker
kernel/sched/fair.c +9 -0
+void cpu_load_update_nohz_start(void)
 {
 	struct rq *this_rq = this_rq();
+
+	/*
+	 * This is all lockless but should be fine. If weighted_cpuload changes
+	 * concurrently we'll exit nohz. And cpu_load write can race with
+	 * cpu_load_update_idle() but both updater would be writing the same.
+	 */
+	this_rq->cpu_load[0] = weighted_cpuload(cpu_of(this_rq));
+}

There is more to this; this also updates ->cpu_load[0] at possibly much higher frequency than we've done before, while not updating the other ->cpu_load[] members.

Now, I'm not sure we care, but it is a bit odd.

Quoted 7 lines
kernel/sched/fair.c +6 -0
+/*
+ * Account the tickless load in the end of a nohz frame.
+ */
+void cpu_load_update_nohz_stop(void)
+{
 	unsigned long curr_jiffies = READ_ONCE(jiffies);
+	struct rq *this_rq = this_rq();

kernel/sched/fair.c static void cpu_load_update_idle(struct rq *this_rq) · line 4655

Quoted 10 lines
kernel/sched/fair.c +3 -0
+	unsigned long load;
 
 	if (curr_jiffies == this_rq->last_load_update_tick)
 		return;
 
+	load = weighted_cpuload(cpu_of(this_rq));
 	raw_spin_lock(&this_rq->lock);
+	cpu_load_update_nohz(this_rq, curr_jiffies, load);
 	raw_spin_unlock(&this_rq->lock);
 }

And this makes us take rq->lock when waking from nohz; a bit unfortunate. Do we really need this though? Will not a tick be forthcoming real-soon-now?

maintainer
#7 lore raw

Re: [PATCH 3/3] sched: Optimize !CONFIG_NO_HZ_COMMON cpu load updates

In reply to Frederic Weisbecker
Quoted 19 lines · Frederic Weisbecker

index 4c522a7..59a2821 100644

kernel/sched/core.c +2 -1
@@ -7327,8 +7327,9 @@ void __init sched_init(void)
 
 		for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
 			rq->cpu_load[j] = 0;
-
+#ifdef CONFIG_NO_HZ_COMMON
 		rq->last_load_update_tick = jiffies;
+#endif
 
 #ifdef CONFIG_SMP
 		rq->sd = NULL;
kernel/sched/fair.c +0 -0
index 1dd864d..4618e5b 100644

kernel/sched/fair.c static inline void cpu_load_update_nohz(struct rq *this_rq, · line 4689

Quoted 10 lines
kernel/sched/fair.c +2 -0
@@ -4661,8 +4680,10 @@ static inline void cpu_load_update_nohz(struct rq *this_rq,
 
 static void cpu_load_update_periodic(struct rq *this_rq, unsigned long load)
 {
+#ifdef CONFIG_NO_HZ_COMMON
 	/* See the mess around cpu_load_update_nohz(). */
 	this_rq->last_load_update_tick = READ_ONCE(jiffies);
+#endif
 	cpu_load_update(this_rq, load, 1);
 }
 

Here you do the simple #ifdef, while here you make a giant mess instead of the relatively straight forward:

kernel/sched/fair.c static void cpu_load_update(struct rq *this_rq, unsigned long this_load, · line 4543

Quoted 5 lines
kernel/sched/fair.c +0 -1
@@ -4540,17 +4568,8 @@ static void cpu_load_update(struct rq *this_rq, unsigned long this_load,
 
 		/* scale is effectively 1 << i now, and >> i divides by scale */
 
-		old_load = this_rq->cpu_load[i];

#ifdef CONFIG_NO_HZ_COMMON

kernel/sched/fair.c static void cpu_load_update(struct rq *this_rq, unsigned long this_load, · line 4553

Quoted 10 lines
kernel/sched/fair.c +0 -10
-		old_load = decay_load_missed(old_load, pending_updates - 1, i);
-		if (tickless_load) {
-			old_load -= decay_load_missed(tickless_load, pending_updates - 1, i);
-			/*
-			 * old_load can never be a negative value because a
-			 * decayed tickless_load cannot be greater than the
-			 * original tickless_load.
-			 */
-			old_load += tickless_load;
-		}

#endif

Quoted 3 lines
kernel/sched/fair.c +0 -0
 		new_load = this_load;
 		/*
 		 * Round up the averaging division if load is increasing. This
#4 lore raw

Re: [PATCH 2/3] sched: Correctly handle nohz ticks cpu load accounting

In reply to Peter Zijlstra
Quoted 17 lines · Peter Zijlstra

kernel/sched/fair.c static void cpu_load_update_idle(struct rq *this_rq) · line 4637

Quoted 11 lines · Frederic Weisbecker
kernel/sched/fair.c +9 -0
+void cpu_load_update_nohz_start(void)
 {
 	struct rq *this_rq = this_rq();
+
+	/*
+	 * This is all lockless but should be fine. If weighted_cpuload changes
+	 * concurrently we'll exit nohz. And cpu_load write can race with
+	 * cpu_load_update_idle() but both updater would be writing the same.
+	 */
+	this_rq->cpu_load[0] = weighted_cpuload(cpu_of(this_rq));
+}

There is more to this; this also updates ->cpu_load[0] at possibly much higher frequency than we've done before, while not updating the other ->cpu_load[] members.

Quoted 1 line

Now, I'm not sure we care, but it is a bit odd.

This is right. cpu_load[0] is aimed at showing the most recent load, without knowing when was the last update (the last tick/update could have been recent or not, the readers shouldn't care). Now we can indeed worry about it if this field is read altogether with the other indexes and those are put into some relation. But it seems that each of the rq->cpu_load[i] are read independently without relation or comparison. Now really I'm saying that without much assurance as I don't know the details of the load balancing code.

If in doubt I can create a field in struct rq to record the tickless load instead of storing it in cpu_load[0]. That was in fact the first direction I took in my drafts.

Quoted 13 lines
Quoted 13 lines
kernel/sched/fair.c +7 -0
+/*
+ * Account the tickless load in the end of a nohz frame.
+ */
+void cpu_load_update_nohz_stop(void)
+{
 	unsigned long curr_jiffies = READ_ONCE(jiffies);
+	struct rq *this_rq = this_rq();
+	unsigned long load;
 
 	if (curr_jiffies == this_rq->last_load_update_tick)
 		return;
Quoted 8 lines

kernel/sched/fair.c static void cpu_load_update_idle(struct rq *this_rq) · line 4655

Quoted 5 lines
kernel/sched/fair.c +2 -0
+	load = weighted_cpuload(cpu_of(this_rq));
 	raw_spin_lock(&this_rq->lock);
+	cpu_load_update_nohz(this_rq, curr_jiffies, load);
 	raw_spin_unlock(&this_rq->lock);
 }

And this makes us take rq->lock when waking from nohz; a bit unfortunate. Do we really need this though?

Note it was already the case before this patchset, the function was called cpu_load_update_nohz() instead.

I think we need it, at least due to remote updates performed by cpu_load_update_idle() (which only updates when load is 0 though).

Quoted 1 line

Will not a tick be forthcoming real-soon-now?

No guarantee about that. If the next task only runs for less than 10ms (in HZ=100), there might be no tick until the next idle period.

Besides in nohz_full, the next task may be running tickless as well, in which case we really need to update now.

#8 lore raw

Re: [PATCH 3/3] sched: Optimize !CONFIG_NO_HZ_COMMON cpu load updates

In reply to Peter Zijlstra
Quoted 40 lines · Peter Zijlstra
Quoted 19 lines · Frederic Weisbecker

index 4c522a7..59a2821 100644

kernel/sched/core.c +2 -1
@@ -7327,8 +7327,9 @@ void __init sched_init(void)
 
 		for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
 			rq->cpu_load[j] = 0;
-
+#ifdef CONFIG_NO_HZ_COMMON
 		rq->last_load_update_tick = jiffies;
+#endif
 
 #ifdef CONFIG_SMP
 		rq->sd = NULL;
kernel/sched/fair.c +0 -0
index 1dd864d..4618e5b 100644

kernel/sched/fair.c static inline void cpu_load_update_nohz(struct rq *this_rq, · line 4689

Quoted 10 lines
kernel/sched/fair.c +2 -0
@@ -4661,8 +4680,10 @@ static inline void cpu_load_update_nohz(struct rq *this_rq,
 
 static void cpu_load_update_periodic(struct rq *this_rq, unsigned long load)
 {
+#ifdef CONFIG_NO_HZ_COMMON
 	/* See the mess around cpu_load_update_nohz(). */
 	this_rq->last_load_update_tick = READ_ONCE(jiffies);
+#endif
 	cpu_load_update(this_rq, load, 1);
 }
 

Here you do the simple #ifdef, while here you make a giant mess instead of the relatively straight forward:

kernel/sched/fair.c static void cpu_load_update(struct rq *this_rq, unsigned long this_load, · line 4543

Quoted 5 lines
kernel/sched/fair.c +0 -1
@@ -4540,17 +4568,8 @@ static void cpu_load_update(struct rq *this_rq, unsigned long this_load,
 
 		/* scale is effectively 1 << i now, and >> i divides by scale */
 
-		old_load = this_rq->cpu_load[i];

#ifdef CONFIG_NO_HZ_COMMON

Quoted 11 lines

kernel/sched/fair.c static void cpu_load_update(struct rq *this_rq, unsigned long this_load, · line 4553

Quoted 10 lines
kernel/sched/fair.c +0 -10
-		old_load = decay_load_missed(old_load, pending_updates - 1, i);
-		if (tickless_load) {
-			old_load -= decay_load_missed(tickless_load, pending_updates - 1, i);
-			/*
-			 * old_load can never be a negative value because a
-			 * decayed tickless_load cannot be greater than the
-			 * original tickless_load.
-			 */
-			old_load += tickless_load;
-		}

#endif

Ah sure, if you prefer it that way, I can do that.

Thanks.

maintainer
#5 lore raw

Re: [PATCH 2/3] sched: Correctly handle nohz ticks cpu load accounting

In reply to Frederic Weisbecker
Quoted 29 lines · Frederic Weisbecker
Quoted 17 lines · Peter Zijlstra

kernel/sched/fair.c static void cpu_load_update_idle(struct rq *this_rq) · line 4637

Quoted 11 lines · Frederic Weisbecker
kernel/sched/fair.c +9 -0
+void cpu_load_update_nohz_start(void)
 {
 	struct rq *this_rq = this_rq();
+
+	/*
+	 * This is all lockless but should be fine. If weighted_cpuload changes
+	 * concurrently we'll exit nohz. And cpu_load write can race with
+	 * cpu_load_update_idle() but both updater would be writing the same.
+	 */
+	this_rq->cpu_load[0] = weighted_cpuload(cpu_of(this_rq));
+}

There is more to this; this also updates ->cpu_load[0] at possibly much higher frequency than we've done before, while not updating the other ->cpu_load[] members.

Quoted 1 line

Now, I'm not sure we care, but it is a bit odd.

This is right. cpu_load[0] is aimed at showing the most recent load, without knowing when was the last update (the last tick/update could have been recent or not, the readers shouldn't care). Now we can indeed worry about it if this field is read altogether with the other indexes and those are put into some relation. But it seems that each of the rq->cpu_load[i] are read independently without relation or comparison. Now really I'm saying that without much assurance as I don't know the details of the load balancing code.

Quoted 3 lines

If in doubt I can create a field in struct rq to record the tickless load instead of storing it in cpu_load[0]. That was in fact the first direction I took in my drafts.

Yeah, no I think this is fine, just maybe wants a comment.

Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
kernel/sched/fair.c +7 -0
+/*
+ * Account the tickless load in the end of a nohz frame.
+ */
+void cpu_load_update_nohz_stop(void)
+{
 	unsigned long curr_jiffies = READ_ONCE(jiffies);
+	struct rq *this_rq = this_rq();
+	unsigned long load;
 
 	if (curr_jiffies == this_rq->last_load_update_tick)
 		return;
Quoted 8 lines

kernel/sched/fair.c static void cpu_load_update_idle(struct rq *this_rq) · line 4655

Quoted 5 lines
kernel/sched/fair.c +2 -0
+	load = weighted_cpuload(cpu_of(this_rq));
 	raw_spin_lock(&this_rq->lock);
+	cpu_load_update_nohz(this_rq, curr_jiffies, load);
 	raw_spin_unlock(&this_rq->lock);
 }

And this makes us take rq->lock when waking from nohz; a bit unfortunate. Do we really need this though?

Quoted 2 lines

Note it was already the case before this patchset, the function was called cpu_load_update_nohz() instead.

Ah, ok. I got lost in the whole rename + nohz maze (again). OK no problem then.

maintainer
#9 lore raw

Re: [PATCH 3/3] sched: Optimize !CONFIG_NO_HZ_COMMON cpu load updates

In reply to Frederic Weisbecker
Quoted 18 lines · Frederic Weisbecker
Quoted 6 lines

kernel/sched/fair.c static void cpu_load_update(struct rq *this_rq, unsigned long this_load, · line 4543

Quoted 5 lines
kernel/sched/fair.c +0 -1
@@ -4540,17 +4568,8 @@ static void cpu_load_update(struct rq *this_rq, unsigned long this_load,
 
 		/* scale is effectively 1 << i now, and >> i divides by scale */
 
-		old_load = this_rq->cpu_load[i];

#ifdef CONFIG_NO_HZ_COMMON

Quoted 11 lines

kernel/sched/fair.c static void cpu_load_update(struct rq *this_rq, unsigned long this_load, · line 4553

Quoted 10 lines
kernel/sched/fair.c +0 -10
-		old_load = decay_load_missed(old_load, pending_updates - 1, i);
-		if (tickless_load) {
-			old_load -= decay_load_missed(tickless_load, pending_updates - 1, i);
-			/*
-			 * old_load can never be a negative value because a
-			 * decayed tickless_load cannot be greater than the
-			 * original tickless_load.
-			 */
-			old_load += tickless_load;
-		}

#endif

Quoted 1 line

Ah sure, if you prefer it that way, I can do that.

Yes please. I normally favour the thing you did, but here it makes tricky code that much harder to read.

#10 lore raw

Re: [PATCH 3/3] sched: Optimize !CONFIG_NO_HZ_COMMON cpu load updates

Quoted 19 lines · Peter Zijlstra
Quoted 18 lines · Frederic Weisbecker
Quoted 17 lines

kernel/sched/fair.c static void cpu_load_update(struct rq *this_rq, unsigned long this_load, · line 4543

Quoted 5 lines
kernel/sched/fair.c +0 -1
@@ -4540,17 +4568,8 @@ static void cpu_load_update(struct rq *this_rq, unsigned long this_load,
 
 		/* scale is effectively 1 << i now, and >> i divides by scale */
 
-		old_load = this_rq->cpu_load[i];

#ifdef CONFIG_NO_HZ_COMMON

kernel/sched/fair.c static void cpu_load_update(struct rq *this_rq, unsigned long this_load, · line 4553

Quoted 10 lines
kernel/sched/fair.c +0 -10
-		old_load = decay_load_missed(old_load, pending_updates - 1, i);
-		if (tickless_load) {
-			old_load -= decay_load_missed(tickless_load, pending_updates - 1, i);
-			/*
-			 * old_load can never be a negative value because a
-			 * decayed tickless_load cannot be greater than the
-			 * original tickless_load.
-			 */
-			old_load += tickless_load;
-		}

#endif

Quoted 4 lines
Quoted 1 line

Ah sure, if you prefer it that way, I can do that.

Yes please. I normally favour the thing you did, but here it makes tricky code that much harder to read.

So I tried and it warns about the unused variable tickless_load, so I would need two scattered ifdeffery in the function:

@@ -4528,7 +4529,9 @@ decay_load_missed(unsigned long load, unsigned long missed_updates, int idx)
static void cpu_load_update(struct rq *this_rq, unsigned long this_load,
unsigned long pending_updates)
{
+#ifdef CONFIG_NO_HZ_COMMON
unsigned long tickless_load = this_rq->cpu_load[0];
+#endif
int i, scale;

this_rq->nr_load_updates++;
@@ -4541,6 +4544,7 @@ static void cpu_load_update(struct rq *this_rq, unsigned long this_load,
/* scale is effectively 1 << i now, and >> i divides by scale */

old_load = this_rq->cpu_load[i];
+#ifdef CONFIG_NO_HZ_COMMON
old_load = decay_load_missed(old_load, pending_updates - 1, i); if (tickless_load) {
old_load -= decay_load_missed(tickless_load, pending_updates - 1, i);
@@ -4551,6 +4555,7 @@ static void cpu_load_update(struct rq *this_rq, unsigned long this_load,
*/
old_load += tickless_load;
}
+#endif
new_load = this_load;
/*
* Round up the averaging division if load is increasing. This

Are you still sure you don't want the ifdeffed inline function?

Thanks.

#11 lore raw

Re: [PATCH 3/3] sched: Optimize !CONFIG_NO_HZ_COMMON cpu load updates

kernel/sched/fair.c decay_load_missed(unsigned long load, unsigned long missed_updates, int idx) · line 4534

Quoted 10 lines · Frederic Weisbecker

So I tried and it warns about the unused variable tickless_load, so I would need two scattered ifdeffery in the function:

kernel/sched/fair.c +2 -0
@@ -4528,7 +4529,9 @@ decay_load_missed(unsigned long load, unsigned long missed_updates, int idx)
  static void cpu_load_update(struct rq *this_rq, unsigned long this_load,
  			    unsigned long pending_updates)
  {
+#ifdef CONFIG_NO_HZ_COMMON
  	unsigned long tickless_load = this_rq->cpu_load[0];
+#endif

Just move the initialization down to the first use, as a regular assignment, and add __maybe_unused to the declaration, and the compiler will then keep quiet (see Documentation/CodingStyle).

I have no comment on which of the approaches looks better overall, but I think using __maybe_unused definitely improves this approach.

Signature · 2 lines
Chris Metcalf, Mellanox Technologies
http://www.mellanox.com
#12 lore raw

Re: [PATCH 3/3] sched: Optimize !CONFIG_NO_HZ_COMMON cpu load updates

Quoted 16 lines · Chris Metcalf

kernel/sched/fair.c decay_load_missed(unsigned long load, unsigned long missed_updates, int idx) · line 4534

Quoted 10 lines · Frederic Weisbecker

So I tried and it warns about the unused variable tickless_load, so I would need two scattered ifdeffery in the function:

kernel/sched/fair.c +2 -0
@@ -4528,7 +4529,9 @@ decay_load_missed(unsigned long load, unsigned long missed_updates, int idx)
 static void cpu_load_update(struct rq *this_rq, unsigned long this_load,
 			    unsigned long pending_updates)
 {
+#ifdef CONFIG_NO_HZ_COMMON
 	unsigned long tickless_load = this_rq->cpu_load[0];
+#endif

Just move the initialization down to the first use, as a regular assignment, and add __maybe_unused to the declaration, and the compiler will then keep quiet (see Documentation/CodingStyle).

Quoted 2 lines

I have no comment on which of the approaches looks better overall, but I think using __maybe_unused definitely improves this approach.

I thought about it yeah. I usually avoid __maybe_unused because it's often a bad sign concerning the code layout.

Now in this precise case I wouldn't mind though. Peter what's your opinion?

Thanks.

maintainer
#13 lore raw

Re: [PATCH 3/3] sched: Optimize !CONFIG_NO_HZ_COMMON cpu load updates

Quoted 17 lines · Frederic Weisbecker
Quoted 16 lines · Chris Metcalf

Review decay_load_missed(unsigned long load, unsigned long missed_updates, int idx) · line 4534

Quoted 10 lines · Frederic Weisbecker

So I tried and it warns about the unused variable tickless_load, so I would need two scattered ifdeffery in the function:

(quoted diff) +2 -0
@@ -4528,7 +4529,9 @@ decay_load_missed(unsigned long load, unsigned long missed_updates, int idx)
 static void cpu_load_update(struct rq *this_rq, unsigned long this_load,
 			    unsigned long pending_updates)
 {
+#ifdef CONFIG_NO_HZ_COMMON
 	unsigned long tickless_load = this_rq->cpu_load[0];
+#endif

Just move the initialization down to the first use, as a regular assignment, and add __maybe_unused to the declaration, and the compiler will then keep quiet (see Documentation/CodingStyle).

Quoted 7 lines
Quoted 2 lines

I have no comment on which of the approaches looks better overall, but I think using __maybe_unused definitely improves this approach.

I thought about it yeah. I usually avoid __maybe_unused because it's often a bad sign concerning the code layout.

Now in this precise case I wouldn't mind though. Peter what's your opinion?

Sure, go with __maybe_unused.