Skip to contentSkip to conversation

sched: Improve scalability of select_idle_sibling using SMT balance

RFC PATCH Superseded subhra mazumdar 2 participants 3 messages Dec 8, 2017 – Dec 20, 2017 Scheduler

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 sched: Improve scalability of select_idle_sibling using SMT balance

Current select_idle_sibling first tries to find a fully idle core using select_idle_cores which can potentially search all cores and if it fails it finds any idle cpu using select_idle_cpu. select_idle_cpu can potentially search all cpus in the llc domain. These don't scale for large llc domains and will only get worse with more cores in future.

This patch uses SMT balancing instead which uses a randomized approach. It maintains SMT utilization per scheduling group which is the no. of busy cpus in the group. This is accounted at the time of context switch. In the fast path of wakeup it compares the target cpu group in select_idle_sibling with a random group in the LLC domain and chooses the group which has more SMT capacity (idle cpus) left using the SMT utilization. The balancing happens top down till the lowest level (SMT core) is reached. Finally it does a idle cpu search only in that core starting from a random cpu index. The random number generation needs to be fast and uses a per cpu pseudo random number generator.

Following are the numbers with various benchmarks on a x86 2 socket system with 22 cores per socket and 2 hyperthreads per core:

hackbench process:

groups  baseline-rc6(avg)  %stdev  patch(avg)      %stdev
1       0.4797             15.75   0.4324 (+9.86%) 2.23
2       0.4877             9.99    0.4535 (+7.01%) 3.36
4       0.8603             1.09    0.8376 (+2.64%) 0.95
8       1.496              0.60    1.4516 (+2.97%) 1.38
16      2.6642             0.37    2.5857 (+2.95%) 0.68
32      4.6715             0.40    4.5158 (+3.33%) 0.67

uperf pingpong throughput with loopback interface and message size = 8k:

threads baseline-rc6(avg)  %stdev  patch(avg)       %stdev
8       49.47              0.35    51.16 (+3.42%)   0.53
16      95.28              0.77    101.02 (+6.03%)  0.43
32      156.77             1.17    181.52 (+15.79%) 0.96
48      193.24             0.22    212.90 (+10.17%) 0.45
64      216.21             9.33    264.14 (+22.17%) 0.69
128     379.62             10.29   416.36 (+9.68%)  1.04

Oracle DB TPC-C throughput normalized to baseline:

users   baseline-rc6 norm(avg)  %stdev  patch norm(avg)   %stdev
20      1                       0.94    1.0071 (+0.71%)   1.03
40      1                       0.82    1.0126 (+1.26%)   0.65
60      1                       1.10    0.9928 (-0.72%)   0.67
80      1                       0.63    1.003 (+0.30%)    0.64
100     1                       0.82    0.9957 (-0.43%)   0.15
120     1                       0.46    1.0034 (+0.34%)   1.74
140     1                       1.44    1.0247 (+2.47%)   0.15
160     1                       0.85    1.0445 (+4.45%)   0.81
180     1                       0.19    1.0382 (+3.82%)   0.57
200     1                       1.40    1.0295 (+2.95%)   0.94
220     1                       1.02    1.0242 (+2.42%)   0.85

Following are the cost (in us) of context_switch() and select_idle_sibling() with hackbench 16 groups:

function baseline-rc6 %stdev patch %stdev

context_switch()         663.8799      4.46    687.4068 (+3.54%)   2.85
select_idle_sibling()    0.556         1.72    0.263 (-52.70%)     0.78
---
 include/linux/sched/topology.h |   4 +
 kernel/sched/core.c            |  60 ++++++++++
 kernel/sched/fair.c            | 249 +++++++++++++++++++++--------------------
 kernel/sched/idle_task.c       |   1 -
 kernel/sched/sched.h           |  37 +++---
 kernel/sched/topology.c        |  52 ++++++++-
 6 files changed, 257 insertions(+), 146 deletions(-)
include/linux/sched/topology.h +4 -0
index 7d065ab..da1f11f 100644
@@ -146,6 +146,10 @@ struct sched_domain {
 	struct sched_domain_shared *shared;
 
 	unsigned int span_weight;
+	struct sched_group **sg_array;
+	struct sched_group *sg_cpu;
+	int sg_nos;
+	int *cp_array;
 	/*
 	 * Span of all CPUs in this domain.
 	 *
kernel/sched/core.c +60 -0

72 lines not on this page · raw message

kernel/sched/fair.c +125 -124

300 lines not on this page · raw message

kernel/sched/idle_task.c +0 -1
index 0c00172..a3d5a7c 100644
@@ -27,7 +27,6 @@ static struct task_struct *
 pick_next_task_idle(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
 {
 	put_prev_task(rq, prev);
-	update_idle_core(rq);
 	schedstat_inc(rq->sched_goidle);
 	return rq->idle;
 }
kernel/sched/sched.h +20 -17
index 14db76c..0e681d4 100644
@@ -47,6 +47,22 @@
 struct rq;
 struct cpuidle_state;
 
+#define	CPU_PSEUDO_RANDOM(cpu)	(((cpu_rq((cpu)))->rotor)++)
+
+/* states of the rq */
+#define UTIL_UNINITIALIZED	0
+#define UTIL_IDLE		1
+#define UTIL_BUSY		2
+
+/* context switch conditions */
+#define	PREV_IDLE_NEXT_IDLE	0
+#define	PREV_IDLE_NEXT_NIDLE	1
+#define	PREV_NIDLE_NEXT_IDLE	2
+#define	PREV_NIDLE_NEXT_NIDLE	3
+
+/* smt utilization of a thread */
+#define	SMT_THREAD_UTIL	1
+
 /* task_struct::on_rq states: */
 #define TASK_ON_RQ_QUEUED	1
 #define TASK_ON_RQ_MIGRATING	2
@@ -737,6 +753,8 @@ struct rq {
 	/* cpu of this runqueue: */
 	int cpu;
 	int online;
+	unsigned int rotor;
+	int initial_util;
 
 	struct list_head cfs_tasks;
 
@@ -808,23 +826,6 @@ static inline int cpu_of(struct rq *rq)
 #endif
 }
 
-
-#ifdef CONFIG_SCHED_SMT
-
-extern struct static_key_false sched_smt_present;
-
-extern void __update_idle_core(struct rq *rq);
-
-static inline void update_idle_core(struct rq *rq)
-{
-	if (static_branch_unlikely(&sched_smt_present))
-		__update_idle_core(rq);
-}
-
-#else
-static inline void update_idle_core(struct rq *rq) { }
-#endif
-
 DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
 
 #define cpu_rq(cpu)		(&per_cpu(runqueues, (cpu)))
@@ -1080,6 +1081,8 @@ struct sched_group {
 	unsigned int group_weight;
 	struct sched_group_capacity *sgc;
 	int asym_prefer_cpu;		/* cpu of highest priority in group */
+	int utilization;
+	int *cp_array;
 
 	/*
 	 * The CPUs this group covers.
kernel/sched/topology.c +48 -4

89 lines not on this page · raw message

Signature · 1 line
2.9.3
maintainer
#1 lore raw

kernel/sched/core.c asmlinkage __visible void schedule_tail(struct task_struct *prev) · line 2758

Quoted 13 lines · subhra mazumdar
kernel/sched/core.c +13 -0
+static inline void
+sd_context_switch(struct sched_domain *sd, struct rq *rq, int util)
+{
+	struct sched_group *sg_cpu;
+
+       /* atomically add/subtract the util */
+	sg_cpu = sd->sg_cpu;
+	if (util > 0)
+		atomic_inc(
+		    (atomic_t *)(&(sg_cpu->utilization)));
+	else
+		atomic_dec(
+		    (atomic_t *)(&(sg_cpu->utilization)));

Whahah, lol, no!

Quoted 6 lines
kernel/sched/core.c +2 -0
+}
+
 /*
  * context_switch - switch to the new MM and the new thread's register state.
  */

kernel/sched/core.c context_switch(struct rq *rq, struct task_struct *prev, · line 2774

Quoted 9 lines
kernel/sched/core.c +6 -0
@@ -2751,6 +2766,51 @@ context_switch(struct rq *rq, struct task_struct *prev,
 	       struct task_struct *next, struct rq_flags *rf)
 {
 	struct mm_struct *mm, *oldmm;
+	int this_cpu = rq->cpu;
+	struct sched_domain *sd;
+	unsigned int cond;
+
+	cond = ((prev != rq->idle) << 1) | (next != rq->idle);
+	sd = rcu_dereference(per_cpu(sd_llc, this_cpu));

That one is RCU, not RCU-sched protected..

Quoted 28 lines
kernel/sched/core.c +28 -0
+	/*
+	 * From sd_llc downward update the SMT utilization.
+	 * Skip the lowest level 0.
+	 */
+	for_each_lower_domain(sd) {
+		if (sd->level == 0)
+			break;
+		if (rq->initial_util == UTIL_UNINITIALIZED) {
+			switch (cond) {
+			case PREV_IDLE_NEXT_NIDLE:
+			case PREV_NIDLE_NEXT_NIDLE:
+				sd_context_switch(sd, rq, SMT_THREAD_UTIL);
+				break;
+			case PREV_NIDLE_NEXT_IDLE:
+			case PREV_IDLE_NEXT_IDLE:
+				break;
+			}
+		} else {
+			switch (cond) {
+			case PREV_IDLE_NEXT_NIDLE:
+				sd_context_switch(sd, rq, SMT_THREAD_UTIL);
+				break;
+			case PREV_NIDLE_NEXT_IDLE:
+				sd_context_switch(sd, rq, -SMT_THREAD_UTIL);
+				break;
+			case PREV_IDLE_NEXT_IDLE:
+			case PREV_NIDLE_NEXT_NIDLE:
+				break;

kernel/sched/core.c context_switch(struct rq *rq, struct task_struct *prev, · line 2813

Quoted 10 lines
kernel/sched/core.c +10 -0
+			}
+		}
+	}
+
+	if (sd) {
+		if (next == rq->idle)
+			rq->initial_util = UTIL_IDLE;
+		else
+			rq->initial_util = UTIL_BUSY;
+	}

WTH do you even think this is reasonable?

kernel/sched/core.c context_switch(struct rq *rq, struct task_struct *prev, · line 2816

Quoted 2 lines
kernel/sched/core.c +0 -0
 	prepare_task_switch(rq, prev, next);
 

And I still have no idea what the patch does, but I can't be bothered to reverse engineer it just now.

#2 lore raw
Quoted 15 lines · Peter Zijlstra

kernel/sched/core.c asmlinkage __visible void schedule_tail(struct task_struct *prev) · line 2758

Quoted 13 lines · subhra mazumdar
kernel/sched/core.c +13 -0
+static inline void
+sd_context_switch(struct sched_domain *sd, struct rq *rq, int util)
+{
+	struct sched_group *sg_cpu;
+
+       /* atomically add/subtract the util */
+	sg_cpu = sd->sg_cpu;
+	if (util > 0)
+		atomic_inc(
+		    (atomic_t *)(&(sg_cpu->utilization)));
+	else
+		atomic_dec(
+		    (atomic_t *)(&(sg_cpu->utilization)));

Whahah, lol, no!

This patch solves the scalability problem of potentially searching all cores or cpus in current logic while scheduling a thread. It does so by using a randomized approach. We can determine a better core to schedule by comparing the SMT utilization (i.e. how many CPUs are busy in a core) between the 2 cores of a llc domain. The comparison is done between the core of the target cpu passed to select_idle_sibling and a random core. This comparison can be done in O(1) if we have an accurate accounting. The SMT utilization is accounted during context switch and for that the atomic operation is needed for correctness.

The cost of atomic increment/decrement is minimal as it happens only for levels down from llc, i.e only for cores. My data for context_switch() time (I had sent it in the patch) shows the extra cost is minimal and mostly within noise margin.

I am open to any suggestions on better way of doing it.

Quoted 17 lines
Quoted 7 lines
kernel/sched/core.c +2 -0
+}
+
  /*
   * context_switch - switch to the new MM and the new thread's register state.
   */

kernel/sched/core.c context_switch(struct rq *rq, struct task_struct *prev, · line 2774

Quoted 9 lines
kernel/sched/core.c +6 -0
@@ -2751,6 +2766,51 @@ context_switch(struct rq *rq, struct task_struct *prev,
  	       struct task_struct *next, struct rq_flags *rf)
  {
  	struct mm_struct *mm, *oldmm;
+	int this_cpu = rq->cpu;
+	struct sched_domain *sd;
+	unsigned int cond;
+
+	cond = ((prev != rq->idle) << 1) | (next != rq->idle);
+	sd = rcu_dereference(per_cpu(sd_llc, this_cpu));

That one is RCU, not RCU-sched protected..

Ok

Quoted 32 lines
Quoted 32 lines
kernel/sched/core.c +31 -0
+	/*
+	 * From sd_llc downward update the SMT utilization.
+	 * Skip the lowest level 0.
+	 */
+	for_each_lower_domain(sd) {
+		if (sd->level == 0)
+			break;
+		if (rq->initial_util == UTIL_UNINITIALIZED) {
+			switch (cond) {
+			case PREV_IDLE_NEXT_NIDLE:
+			case PREV_NIDLE_NEXT_NIDLE:
+				sd_context_switch(sd, rq, SMT_THREAD_UTIL);
+				break;
+			case PREV_NIDLE_NEXT_IDLE:
+			case PREV_IDLE_NEXT_IDLE:
+				break;
+			}
+		} else {
+			switch (cond) {
+			case PREV_IDLE_NEXT_NIDLE:
+				sd_context_switch(sd, rq, SMT_THREAD_UTIL);
+				break;
+			case PREV_NIDLE_NEXT_IDLE:
+				sd_context_switch(sd, rq, -SMT_THREAD_UTIL);
+				break;
+			case PREV_IDLE_NEXT_IDLE:
+			case PREV_NIDLE_NEXT_NIDLE:
+				break;
+			}
+		}
+	}
Quoted 8 lines

kernel/sched/core.c context_switch(struct rq *rq, struct task_struct *prev, · line 2813

Quoted 7 lines
kernel/sched/core.c +7 -0
+
+	if (sd) {
+		if (next == rq->idle)
+			rq->initial_util = UTIL_IDLE;
+		else
+			rq->initial_util = UTIL_BUSY;
+	}

WTH do you even think this is reasonable?

I will clean this part up.

Quoted 5 lines

kernel/sched/core.c context_switch(struct rq *rq, struct task_struct *prev, · line 2816

Quoted 3 lines
kernel/sched/core.c +0 -0
  	prepare_task_switch(rq, prev, next);
 

And I still have no idea what the patch does, but I can't be bothered to reverse engineer it just now.

I will improve the changelog to explain the logic better in v2.

Thanks,
Subhra