All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] sched/fair: Wake task within the cluster when possible
@ 2021-12-15  4:11 ` Yicong Yang
  0 siblings, 0 replies; 20+ messages in thread
From: Yicong Yang @ 2021-12-15  4:11 UTC (permalink / raw)
  To: peterz, mingo, juri.lelli, vincent.guittot, tim.c.chen,
	linux-kernel, linux-arm-kernel
  Cc: dietmar.eggemann, rostedt, bsegall, bristot, prime.zeng,
	yangyicong, jonathan.cameron, ego, srikar, linuxarm, 21cnbao,
	song.bao.hua, guodong.xu

This is the follow-up work to support cluster scheduler. Previously
we have added cluster level in the scheduler for both ARM64[1] and
X86[2] to support load balance between clusters to bring more memory
bandwidth and decrease cache contention. This patchset, on the other
hand, takes care of wake-up path by giving CPUs within the same cluster
a try before scanning the whole LLC to benefit those tasks communicating
with each other.

[1] 778c558f49a2 ("sched: Add cluster scheduler level in core and related Kconfig for ARM64")
[2] 66558b730f25 ("sched: Add cluster scheduler level for x86")

Barry Song (2):
  sched: Add per_cpu cluster domain info and cpus_share_cluster API
  sched/fair: Scan cluster before scanning LLC in wake-up path

 include/linux/sched/sd_flags.h |  5 ++++
 include/linux/sched/topology.h |  8 +++++-
 kernel/sched/core.c            | 12 +++++++++
 kernel/sched/fair.c            | 46 +++++++++++++++++++++++++++++++---
 kernel/sched/sched.h           |  2 ++
 kernel/sched/topology.c        |  9 +++++++
 6 files changed, 77 insertions(+), 5 deletions(-)

-- 
2.33.0


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH 0/2] sched/fair: Wake task within the cluster when possible
@ 2021-12-15  4:11 ` Yicong Yang
  0 siblings, 0 replies; 20+ messages in thread
From: Yicong Yang @ 2021-12-15  4:11 UTC (permalink / raw)
  To: peterz, mingo, juri.lelli, vincent.guittot, tim.c.chen,
	linux-kernel, linux-arm-kernel
  Cc: dietmar.eggemann, rostedt, bsegall, bristot, prime.zeng,
	yangyicong, jonathan.cameron, ego, srikar, linuxarm, 21cnbao,
	song.bao.hua, guodong.xu

This is the follow-up work to support cluster scheduler. Previously
we have added cluster level in the scheduler for both ARM64[1] and
X86[2] to support load balance between clusters to bring more memory
bandwidth and decrease cache contention. This patchset, on the other
hand, takes care of wake-up path by giving CPUs within the same cluster
a try before scanning the whole LLC to benefit those tasks communicating
with each other.

[1] 778c558f49a2 ("sched: Add cluster scheduler level in core and related Kconfig for ARM64")
[2] 66558b730f25 ("sched: Add cluster scheduler level for x86")

Barry Song (2):
  sched: Add per_cpu cluster domain info and cpus_share_cluster API
  sched/fair: Scan cluster before scanning LLC in wake-up path

 include/linux/sched/sd_flags.h |  5 ++++
 include/linux/sched/topology.h |  8 +++++-
 kernel/sched/core.c            | 12 +++++++++
 kernel/sched/fair.c            | 46 +++++++++++++++++++++++++++++++---
 kernel/sched/sched.h           |  2 ++
 kernel/sched/topology.c        |  9 +++++++
 6 files changed, 77 insertions(+), 5 deletions(-)

-- 
2.33.0


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH 1/2] sched: Add per_cpu cluster domain info and cpus_share_cluster API
  2021-12-15  4:11 ` Yicong Yang
@ 2021-12-15  4:11   ` Yicong Yang
  -1 siblings, 0 replies; 20+ messages in thread
From: Yicong Yang @ 2021-12-15  4:11 UTC (permalink / raw)
  To: peterz, mingo, juri.lelli, vincent.guittot, tim.c.chen,
	linux-kernel, linux-arm-kernel
  Cc: dietmar.eggemann, rostedt, bsegall, bristot, prime.zeng,
	yangyicong, jonathan.cameron, ego, srikar, linuxarm, 21cnbao,
	song.bao.hua, guodong.xu

From: Barry Song <song.bao.hua@hisilicon.com>

Add per-cpu cluster domain info and cpus_share_cluster() API.
This is the preparation for the optimization of select_idle_cpu()
on platforms with cluster scheduler level.

Signed-off-by: Barry Song <song.bao.hua@hisilicon.com>
Signed-off-by: Yicong Yang <yangyicong@hisilicon.com>
---
 include/linux/sched/sd_flags.h |  5 +++++
 include/linux/sched/topology.h |  8 +++++++-
 kernel/sched/core.c            | 12 ++++++++++++
 kernel/sched/sched.h           |  2 ++
 kernel/sched/topology.c        |  9 +++++++++
 5 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/include/linux/sched/sd_flags.h b/include/linux/sched/sd_flags.h
index 57bde66d95f7..0f732bcfbb2c 100644
--- a/include/linux/sched/sd_flags.h
+++ b/include/linux/sched/sd_flags.h
@@ -109,6 +109,11 @@ SD_FLAG(SD_ASYM_CPUCAPACITY_FULL, SDF_SHARED_PARENT | SDF_NEEDS_GROUPS)
  */
 SD_FLAG(SD_SHARE_CPUCAPACITY, SDF_SHARED_CHILD | SDF_NEEDS_GROUPS)
 
+/*
+ * Set up for cluster domains sharing resources such as llc tags or l2
+ */
+SD_FLAG(SD_CLUSTER, SDF_NEEDS_GROUPS)
+
 /*
  * Domain members share CPU package resources (i.e. caches)
  *
diff --git a/include/linux/sched/topology.h b/include/linux/sched/topology.h
index c07bfa2d80f2..78c3a94fae66 100644
--- a/include/linux/sched/topology.h
+++ b/include/linux/sched/topology.h
@@ -45,7 +45,7 @@ static inline int cpu_smt_flags(void)
 #ifdef CONFIG_SCHED_CLUSTER
 static inline int cpu_cluster_flags(void)
 {
-	return SD_SHARE_PKG_RESOURCES;
+	return SD_CLUSTER | SD_SHARE_PKG_RESOURCES;
 }
 #endif
 
@@ -177,6 +177,7 @@ cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
 void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
 
 bool cpus_share_cache(int this_cpu, int that_cpu);
+bool cpus_share_cluster(int this_cpu, int that_cpu);
 
 typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
 typedef int (*sched_domain_flags_f)(void);
@@ -230,6 +231,11 @@ static inline bool cpus_share_cache(int this_cpu, int that_cpu)
 	return true;
 }
 
+static inline bool cpus_share_cluster(int this_cpu, int that_cpu)
+{
+	return true;
+}
+
 #endif	/* !CONFIG_SMP */
 
 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 3c9b0fda64ac..11f9b25c3068 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -3732,6 +3732,18 @@ bool cpus_share_cache(int this_cpu, int that_cpu)
 	return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
 }
 
+/*
+ * On non-Cluster machine this function works same with cpus_share_cache()
+ * as sd_cluster_id equals to sd_llc_id.
+ */
+bool cpus_share_cluster(int this_cpu, int that_cpu)
+{
+	if (this_cpu == that_cpu)
+		return true;
+
+	return per_cpu(sd_cluster_id, this_cpu) == per_cpu(sd_cluster_id, that_cpu);
+}
+
 static inline bool ttwu_queue_cond(int cpu, int wake_flags)
 {
 	/*
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 0e66749486e7..ddd29879ad40 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -1763,7 +1763,9 @@ static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc);
 DECLARE_PER_CPU(int, sd_llc_size);
 DECLARE_PER_CPU(int, sd_llc_id);
+DECLARE_PER_CPU(int, sd_cluster_id);
 DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
+DECLARE_PER_CPU(struct sched_domain __rcu *, sd_cluster);
 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa);
 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
index d201a7052a29..5642df384904 100644
--- a/kernel/sched/topology.c
+++ b/kernel/sched/topology.c
@@ -644,6 +644,8 @@ static void destroy_sched_domains(struct sched_domain *sd)
 DEFINE_PER_CPU(struct sched_domain __rcu *, sd_llc);
 DEFINE_PER_CPU(int, sd_llc_size);
 DEFINE_PER_CPU(int, sd_llc_id);
+DEFINE_PER_CPU(int, sd_cluster_id);
+DEFINE_PER_CPU(struct sched_domain __rcu *, sd_cluster);
 DEFINE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
 DEFINE_PER_CPU(struct sched_domain __rcu *, sd_numa);
 DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
@@ -669,6 +671,12 @@ static void update_top_cache_domain(int cpu)
 	per_cpu(sd_llc_id, cpu) = id;
 	rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds);
 
+	sd = lowest_flag_domain(cpu, SD_CLUSTER);
+	if (sd)
+		id = cpumask_first(sched_domain_span(sd));
+	rcu_assign_pointer(per_cpu(sd_cluster, cpu), sd);
+	per_cpu(sd_cluster_id, cpu) = id;
+
 	sd = lowest_flag_domain(cpu, SD_NUMA);
 	rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
 
@@ -1514,6 +1522,7 @@ static unsigned long __read_mostly *sched_numa_onlined_nodes;
  */
 #define TOPOLOGY_SD_FLAGS		\
 	(SD_SHARE_CPUCAPACITY	|	\
+	 SD_CLUSTER		|	\
 	 SD_SHARE_PKG_RESOURCES |	\
 	 SD_NUMA		|	\
 	 SD_ASYM_PACKING)
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 1/2] sched: Add per_cpu cluster domain info and cpus_share_cluster API
@ 2021-12-15  4:11   ` Yicong Yang
  0 siblings, 0 replies; 20+ messages in thread
From: Yicong Yang @ 2021-12-15  4:11 UTC (permalink / raw)
  To: peterz, mingo, juri.lelli, vincent.guittot, tim.c.chen,
	linux-kernel, linux-arm-kernel
  Cc: dietmar.eggemann, rostedt, bsegall, bristot, prime.zeng,
	yangyicong, jonathan.cameron, ego, srikar, linuxarm, 21cnbao,
	song.bao.hua, guodong.xu

From: Barry Song <song.bao.hua@hisilicon.com>

Add per-cpu cluster domain info and cpus_share_cluster() API.
This is the preparation for the optimization of select_idle_cpu()
on platforms with cluster scheduler level.

Signed-off-by: Barry Song <song.bao.hua@hisilicon.com>
Signed-off-by: Yicong Yang <yangyicong@hisilicon.com>
---
 include/linux/sched/sd_flags.h |  5 +++++
 include/linux/sched/topology.h |  8 +++++++-
 kernel/sched/core.c            | 12 ++++++++++++
 kernel/sched/sched.h           |  2 ++
 kernel/sched/topology.c        |  9 +++++++++
 5 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/include/linux/sched/sd_flags.h b/include/linux/sched/sd_flags.h
index 57bde66d95f7..0f732bcfbb2c 100644
--- a/include/linux/sched/sd_flags.h
+++ b/include/linux/sched/sd_flags.h
@@ -109,6 +109,11 @@ SD_FLAG(SD_ASYM_CPUCAPACITY_FULL, SDF_SHARED_PARENT | SDF_NEEDS_GROUPS)
  */
 SD_FLAG(SD_SHARE_CPUCAPACITY, SDF_SHARED_CHILD | SDF_NEEDS_GROUPS)
 
+/*
+ * Set up for cluster domains sharing resources such as llc tags or l2
+ */
+SD_FLAG(SD_CLUSTER, SDF_NEEDS_GROUPS)
+
 /*
  * Domain members share CPU package resources (i.e. caches)
  *
diff --git a/include/linux/sched/topology.h b/include/linux/sched/topology.h
index c07bfa2d80f2..78c3a94fae66 100644
--- a/include/linux/sched/topology.h
+++ b/include/linux/sched/topology.h
@@ -45,7 +45,7 @@ static inline int cpu_smt_flags(void)
 #ifdef CONFIG_SCHED_CLUSTER
 static inline int cpu_cluster_flags(void)
 {
-	return SD_SHARE_PKG_RESOURCES;
+	return SD_CLUSTER | SD_SHARE_PKG_RESOURCES;
 }
 #endif
 
@@ -177,6 +177,7 @@ cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
 void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
 
 bool cpus_share_cache(int this_cpu, int that_cpu);
+bool cpus_share_cluster(int this_cpu, int that_cpu);
 
 typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
 typedef int (*sched_domain_flags_f)(void);
@@ -230,6 +231,11 @@ static inline bool cpus_share_cache(int this_cpu, int that_cpu)
 	return true;
 }
 
+static inline bool cpus_share_cluster(int this_cpu, int that_cpu)
+{
+	return true;
+}
+
 #endif	/* !CONFIG_SMP */
 
 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 3c9b0fda64ac..11f9b25c3068 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -3732,6 +3732,18 @@ bool cpus_share_cache(int this_cpu, int that_cpu)
 	return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
 }
 
+/*
+ * On non-Cluster machine this function works same with cpus_share_cache()
+ * as sd_cluster_id equals to sd_llc_id.
+ */
+bool cpus_share_cluster(int this_cpu, int that_cpu)
+{
+	if (this_cpu == that_cpu)
+		return true;
+
+	return per_cpu(sd_cluster_id, this_cpu) == per_cpu(sd_cluster_id, that_cpu);
+}
+
 static inline bool ttwu_queue_cond(int cpu, int wake_flags)
 {
 	/*
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 0e66749486e7..ddd29879ad40 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -1763,7 +1763,9 @@ static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc);
 DECLARE_PER_CPU(int, sd_llc_size);
 DECLARE_PER_CPU(int, sd_llc_id);
+DECLARE_PER_CPU(int, sd_cluster_id);
 DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
+DECLARE_PER_CPU(struct sched_domain __rcu *, sd_cluster);
 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa);
 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
index d201a7052a29..5642df384904 100644
--- a/kernel/sched/topology.c
+++ b/kernel/sched/topology.c
@@ -644,6 +644,8 @@ static void destroy_sched_domains(struct sched_domain *sd)
 DEFINE_PER_CPU(struct sched_domain __rcu *, sd_llc);
 DEFINE_PER_CPU(int, sd_llc_size);
 DEFINE_PER_CPU(int, sd_llc_id);
+DEFINE_PER_CPU(int, sd_cluster_id);
+DEFINE_PER_CPU(struct sched_domain __rcu *, sd_cluster);
 DEFINE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
 DEFINE_PER_CPU(struct sched_domain __rcu *, sd_numa);
 DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
@@ -669,6 +671,12 @@ static void update_top_cache_domain(int cpu)
 	per_cpu(sd_llc_id, cpu) = id;
 	rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds);
 
+	sd = lowest_flag_domain(cpu, SD_CLUSTER);
+	if (sd)
+		id = cpumask_first(sched_domain_span(sd));
+	rcu_assign_pointer(per_cpu(sd_cluster, cpu), sd);
+	per_cpu(sd_cluster_id, cpu) = id;
+
 	sd = lowest_flag_domain(cpu, SD_NUMA);
 	rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
 
@@ -1514,6 +1522,7 @@ static unsigned long __read_mostly *sched_numa_onlined_nodes;
  */
 #define TOPOLOGY_SD_FLAGS		\
 	(SD_SHARE_CPUCAPACITY	|	\
+	 SD_CLUSTER		|	\
 	 SD_SHARE_PKG_RESOURCES |	\
 	 SD_NUMA		|	\
 	 SD_ASYM_PACKING)
-- 
2.33.0


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 2/2] sched/fair: Scan cluster before scanning LLC in wake-up path
  2021-12-15  4:11 ` Yicong Yang
@ 2021-12-15  4:11   ` Yicong Yang
  -1 siblings, 0 replies; 20+ messages in thread
From: Yicong Yang @ 2021-12-15  4:11 UTC (permalink / raw)
  To: peterz, mingo, juri.lelli, vincent.guittot, tim.c.chen,
	linux-kernel, linux-arm-kernel
  Cc: dietmar.eggemann, rostedt, bsegall, bristot, prime.zeng,
	yangyicong, jonathan.cameron, ego, srikar, linuxarm, 21cnbao,
	song.bao.hua, guodong.xu

From: Barry Song <song.bao.hua@hisilicon.com>

For platforms having clusters like Kunpeng920, CPUs within the same
cluster have lower latency when synchronizing and accessing shared
resources like cache. Thus, this patch tries to find an idle cpu
within the cluster of the target CPU before scanning the whole LLC
to gain lower latency.

Note neither Kunpeng920 nor x86 Jacobsville supports SMT, so this
patch doesn't consider SMT for this moment.

Testing has been done on Kunpeng920 by pinning tasks to one numa
and two numa. Each numa has 8 clusters and each cluster has 4 CPUs.

With this patch, We noticed enhancement on tbench within one
numa or cross two numa.

On numa 0:
                           5.16-rc1                patched
Hmean     1        329.17 (   0.00%)      384.84 *  16.91%*
Hmean     2        654.09 (   0.00%)      768.77 *  17.53%*
Hmean     4       1321.41 (   0.00%)     1538.10 *  16.40%*
Hmean     8       2650.43 (   0.00%)     3048.86 *  15.03%*
Hmean     16      5322.37 (   0.00%)     5975.20 *  12.27%*
Hmean     32     10002.11 (   0.00%)    10085.57 *   0.83%*
Hmean     64      7910.39 (   0.00%)     7936.37 *   0.33%*
Hmean     128     6745.25 (   0.00%)     6811.28 *   0.98%*

On numa 0-1:
                           5.16-rc1                patched
Hmean     1        326.61 (   0.00%)      385.36 *  17.99%*
Hmean     2        650.62 (   0.00%)      770.57 *  18.44%*
Hmean     4       1318.05 (   0.00%)     1534.83 *  16.45%*
Hmean     8       2621.50 (   0.00%)     3030.10 *  15.59%*
Hmean     16      5252.17 (   0.00%)     6023.08 *  14.68%*
Hmean     32      9829.30 (   0.00%)     9856.33 *   0.28%*
Hmean     64     12452.66 (   0.00%)    17338.48 *  39.24%*
Hmean     128    14181.24 (   0.00%)    15025.24 *   5.95%*
Hmean     256    12239.07 (   0.00%)    13080.16 *   6.87%*
Hmean     512    14297.00 (   0.00%)    15063.76 *   5.36%*

This will also help to improve the MySQL. With MySQL server
running on numa 0 and client running on numa 1, both QPS and
latency is imporved on read-write case:
                      5.16-rc1              patched
QPS-24threads        195327.48    202081.28(+3.46%)
QPS-32threads         242039.4     247059.5(+2.07%)
QPS-64threads        243024.52    254274.47(+4.63%)
avg-lat-24threads         2.46         2.37(+3.66%)
avg-lat-36threads         2.64         2.59(+1.89%)
avg-lat-64threads         5.27         5.03(+4.55%)

Tested-by: Yicong Yang <yangyicong@hisilicon.com>
Signed-off-by: Barry Song <song.bao.hua@hisilicon.com>
Signed-off-by: Yicong Yang <yangyicong@hisilicon.com>
---
 kernel/sched/fair.c | 46 +++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 42 insertions(+), 4 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 6e476f6d9435..8a5795c78af8 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -6230,12 +6230,46 @@ static inline int select_idle_smt(struct task_struct *p, struct sched_domain *sd
 
 #endif /* CONFIG_SCHED_SMT */
 
+#ifdef CONFIG_SCHED_CLUSTER
+/*
+ * Scan the cluster domain for idle CPUs and clear cluster cpumask after scanning
+ */
+static inline int scan_cluster(struct task_struct *p, int prev_cpu, int target)
+{
+	struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask);
+	struct sched_domain *sd = rcu_dereference(per_cpu(sd_cluster, target));
+	int cpu, idle_cpu;
+
+	/* TODO: Support SMT case while a machine with both cluster and SMT born */
+	if (!sched_smt_active() && sd) {
+		for_each_cpu_and(cpu, cpus, sched_domain_span(sd)) {
+			idle_cpu = __select_idle_cpu(cpu, p);
+			if ((unsigned int)idle_cpu < nr_cpumask_bits)
+				return idle_cpu;
+		}
+
+		/* Don't ping-pong tasks in and out cluster frequently */
+		if (cpus_share_cluster(target, prev_cpu))
+			return target;
+
+		cpumask_andnot(cpus, cpus, sched_domain_span(sd));
+	}
+
+	return -1;
+}
+#else
+static inline int scan_cluster(struct task_struct *p, int prev_cpu, int target)
+{
+	return -1;
+}
+#endif
+
 /*
  * Scan the LLC domain for idle CPUs; this is dynamically regulated by
  * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
  * average idle time for this rq (as found in rq->avg_idle).
  */
-static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool has_idle_core, int target)
+static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool has_idle_core, int prev_cpu, int target)
 {
 	struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask);
 	int i, cpu, idle_cpu = -1, nr = INT_MAX;
@@ -6250,6 +6284,10 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
 
 	cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr);
 
+	idle_cpu = scan_cluster(p, prev_cpu, target);
+	if ((unsigned int)idle_cpu < nr_cpumask_bits)
+		return idle_cpu;
+
 	if (sched_feat(SIS_PROP) && !has_idle_core) {
 		u64 avg_cost, avg_idle, span_avg;
 		unsigned long now = jiffies;
@@ -6384,7 +6422,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
 	/*
 	 * If the previous CPU is cache affine and idle, don't be stupid:
 	 */
-	if (prev != target && cpus_share_cache(prev, target) &&
+	if (prev != target && cpus_share_cluster(prev, target) &&
 	    (available_idle_cpu(prev) || sched_idle_cpu(prev)) &&
 	    asym_fits_capacity(task_util, prev))
 		return prev;
@@ -6408,7 +6446,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
 	p->recent_used_cpu = prev;
 	if (recent_used_cpu != prev &&
 	    recent_used_cpu != target &&
-	    cpus_share_cache(recent_used_cpu, target) &&
+	    cpus_share_cluster(recent_used_cpu, target) &&
 	    (available_idle_cpu(recent_used_cpu) || sched_idle_cpu(recent_used_cpu)) &&
 	    cpumask_test_cpu(p->recent_used_cpu, p->cpus_ptr) &&
 	    asym_fits_capacity(task_util, recent_used_cpu)) {
@@ -6449,7 +6487,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
 		}
 	}
 
-	i = select_idle_cpu(p, sd, has_idle_core, target);
+	i = select_idle_cpu(p, sd, has_idle_core, prev, target);
 	if ((unsigned)i < nr_cpumask_bits)
 		return i;
 
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 2/2] sched/fair: Scan cluster before scanning LLC in wake-up path
@ 2021-12-15  4:11   ` Yicong Yang
  0 siblings, 0 replies; 20+ messages in thread
From: Yicong Yang @ 2021-12-15  4:11 UTC (permalink / raw)
  To: peterz, mingo, juri.lelli, vincent.guittot, tim.c.chen,
	linux-kernel, linux-arm-kernel
  Cc: dietmar.eggemann, rostedt, bsegall, bristot, prime.zeng,
	yangyicong, jonathan.cameron, ego, srikar, linuxarm, 21cnbao,
	song.bao.hua, guodong.xu

From: Barry Song <song.bao.hua@hisilicon.com>

For platforms having clusters like Kunpeng920, CPUs within the same
cluster have lower latency when synchronizing and accessing shared
resources like cache. Thus, this patch tries to find an idle cpu
within the cluster of the target CPU before scanning the whole LLC
to gain lower latency.

Note neither Kunpeng920 nor x86 Jacobsville supports SMT, so this
patch doesn't consider SMT for this moment.

Testing has been done on Kunpeng920 by pinning tasks to one numa
and two numa. Each numa has 8 clusters and each cluster has 4 CPUs.

With this patch, We noticed enhancement on tbench within one
numa or cross two numa.

On numa 0:
                           5.16-rc1                patched
Hmean     1        329.17 (   0.00%)      384.84 *  16.91%*
Hmean     2        654.09 (   0.00%)      768.77 *  17.53%*
Hmean     4       1321.41 (   0.00%)     1538.10 *  16.40%*
Hmean     8       2650.43 (   0.00%)     3048.86 *  15.03%*
Hmean     16      5322.37 (   0.00%)     5975.20 *  12.27%*
Hmean     32     10002.11 (   0.00%)    10085.57 *   0.83%*
Hmean     64      7910.39 (   0.00%)     7936.37 *   0.33%*
Hmean     128     6745.25 (   0.00%)     6811.28 *   0.98%*

On numa 0-1:
                           5.16-rc1                patched
Hmean     1        326.61 (   0.00%)      385.36 *  17.99%*
Hmean     2        650.62 (   0.00%)      770.57 *  18.44%*
Hmean     4       1318.05 (   0.00%)     1534.83 *  16.45%*
Hmean     8       2621.50 (   0.00%)     3030.10 *  15.59%*
Hmean     16      5252.17 (   0.00%)     6023.08 *  14.68%*
Hmean     32      9829.30 (   0.00%)     9856.33 *   0.28%*
Hmean     64     12452.66 (   0.00%)    17338.48 *  39.24%*
Hmean     128    14181.24 (   0.00%)    15025.24 *   5.95%*
Hmean     256    12239.07 (   0.00%)    13080.16 *   6.87%*
Hmean     512    14297.00 (   0.00%)    15063.76 *   5.36%*

This will also help to improve the MySQL. With MySQL server
running on numa 0 and client running on numa 1, both QPS and
latency is imporved on read-write case:
                      5.16-rc1              patched
QPS-24threads        195327.48    202081.28(+3.46%)
QPS-32threads         242039.4     247059.5(+2.07%)
QPS-64threads        243024.52    254274.47(+4.63%)
avg-lat-24threads         2.46         2.37(+3.66%)
avg-lat-36threads         2.64         2.59(+1.89%)
avg-lat-64threads         5.27         5.03(+4.55%)

Tested-by: Yicong Yang <yangyicong@hisilicon.com>
Signed-off-by: Barry Song <song.bao.hua@hisilicon.com>
Signed-off-by: Yicong Yang <yangyicong@hisilicon.com>
---
 kernel/sched/fair.c | 46 +++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 42 insertions(+), 4 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 6e476f6d9435..8a5795c78af8 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -6230,12 +6230,46 @@ static inline int select_idle_smt(struct task_struct *p, struct sched_domain *sd
 
 #endif /* CONFIG_SCHED_SMT */
 
+#ifdef CONFIG_SCHED_CLUSTER
+/*
+ * Scan the cluster domain for idle CPUs and clear cluster cpumask after scanning
+ */
+static inline int scan_cluster(struct task_struct *p, int prev_cpu, int target)
+{
+	struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask);
+	struct sched_domain *sd = rcu_dereference(per_cpu(sd_cluster, target));
+	int cpu, idle_cpu;
+
+	/* TODO: Support SMT case while a machine with both cluster and SMT born */
+	if (!sched_smt_active() && sd) {
+		for_each_cpu_and(cpu, cpus, sched_domain_span(sd)) {
+			idle_cpu = __select_idle_cpu(cpu, p);
+			if ((unsigned int)idle_cpu < nr_cpumask_bits)
+				return idle_cpu;
+		}
+
+		/* Don't ping-pong tasks in and out cluster frequently */
+		if (cpus_share_cluster(target, prev_cpu))
+			return target;
+
+		cpumask_andnot(cpus, cpus, sched_domain_span(sd));
+	}
+
+	return -1;
+}
+#else
+static inline int scan_cluster(struct task_struct *p, int prev_cpu, int target)
+{
+	return -1;
+}
+#endif
+
 /*
  * Scan the LLC domain for idle CPUs; this is dynamically regulated by
  * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
  * average idle time for this rq (as found in rq->avg_idle).
  */
-static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool has_idle_core, int target)
+static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool has_idle_core, int prev_cpu, int target)
 {
 	struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask);
 	int i, cpu, idle_cpu = -1, nr = INT_MAX;
@@ -6250,6 +6284,10 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
 
 	cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr);
 
+	idle_cpu = scan_cluster(p, prev_cpu, target);
+	if ((unsigned int)idle_cpu < nr_cpumask_bits)
+		return idle_cpu;
+
 	if (sched_feat(SIS_PROP) && !has_idle_core) {
 		u64 avg_cost, avg_idle, span_avg;
 		unsigned long now = jiffies;
@@ -6384,7 +6422,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
 	/*
 	 * If the previous CPU is cache affine and idle, don't be stupid:
 	 */
-	if (prev != target && cpus_share_cache(prev, target) &&
+	if (prev != target && cpus_share_cluster(prev, target) &&
 	    (available_idle_cpu(prev) || sched_idle_cpu(prev)) &&
 	    asym_fits_capacity(task_util, prev))
 		return prev;
@@ -6408,7 +6446,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
 	p->recent_used_cpu = prev;
 	if (recent_used_cpu != prev &&
 	    recent_used_cpu != target &&
-	    cpus_share_cache(recent_used_cpu, target) &&
+	    cpus_share_cluster(recent_used_cpu, target) &&
 	    (available_idle_cpu(recent_used_cpu) || sched_idle_cpu(recent_used_cpu)) &&
 	    cpumask_test_cpu(p->recent_used_cpu, p->cpus_ptr) &&
 	    asym_fits_capacity(task_util, recent_used_cpu)) {
@@ -6449,7 +6487,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
 		}
 	}
 
-	i = select_idle_cpu(p, sd, has_idle_core, target);
+	i = select_idle_cpu(p, sd, has_idle_core, prev, target);
 	if ((unsigned)i < nr_cpumask_bits)
 		return i;
 
-- 
2.33.0


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/2] sched: Add per_cpu cluster domain info and cpus_share_cluster API
  2021-12-15  4:11   ` Yicong Yang
@ 2021-12-22 10:24     ` Gautham R. Shenoy
  -1 siblings, 0 replies; 20+ messages in thread
From: Gautham R. Shenoy @ 2021-12-22 10:24 UTC (permalink / raw)
  To: Yicong Yang
  Cc: peterz, mingo, juri.lelli, vincent.guittot, tim.c.chen,
	linux-kernel, linux-arm-kernel, dietmar.eggemann, rostedt,
	bsegall, bristot, prime.zeng, jonathan.cameron, ego, srikar,
	linuxarm, 21cnbao, song.bao.hua, guodong.xu

Hello Yicong, Barry,


On Wed, Dec 15, 2021 at 12:11:48PM +0800, Yicong Yang wrote:
> From: Barry Song <song.bao.hua@hisilicon.com>
> 
> Add per-cpu cluster domain info and cpus_share_cluster() API.
> This is the preparation for the optimization of select_idle_cpu()
> on platforms with cluster scheduler level.
> 
> Signed-off-by: Barry Song <song.bao.hua@hisilicon.com>
> Signed-off-by: Yicong Yang <yangyicong@hisilicon.com>
> ---
>  include/linux/sched/sd_flags.h |  5 +++++
>  include/linux/sched/topology.h |  8 +++++++-
>  kernel/sched/core.c            | 12 ++++++++++++
>  kernel/sched/sched.h           |  2 ++
>  kernel/sched/topology.c        |  9 +++++++++
>  5 files changed, 35 insertions(+), 1 deletion(-)
> 
> diff --git a/include/linux/sched/sd_flags.h b/include/linux/sched/sd_flags.h
> index 57bde66d95f7..0f732bcfbb2c 100644
> --- a/include/linux/sched/sd_flags.h
> +++ b/include/linux/sched/sd_flags.h
> @@ -109,6 +109,11 @@ SD_FLAG(SD_ASYM_CPUCAPACITY_FULL, SDF_SHARED_PARENT | SDF_NEEDS_GROUPS)
>   */
>  SD_FLAG(SD_SHARE_CPUCAPACITY, SDF_SHARED_CHILD | SDF_NEEDS_GROUPS)
>  
> +/*
> + * Set up for cluster domains sharing resources such as llc tags or l2
> + */
> +SD_FLAG(SD_CLUSTER, SDF_NEEDS_GROUPS)
> +
>  /*
>   * Domain members share CPU package resources (i.e. caches)
>   *
> diff --git a/include/linux/sched/topology.h b/include/linux/sched/topology.h
> index c07bfa2d80f2..78c3a94fae66 100644
> --- a/include/linux/sched/topology.h
> +++ b/include/linux/sched/topology.h
> @@ -45,7 +45,7 @@ static inline int cpu_smt_flags(void)
>  #ifdef CONFIG_SCHED_CLUSTER
>  static inline int cpu_cluster_flags(void)
>  {
> -	return SD_SHARE_PKG_RESOURCES;
> +	return SD_CLUSTER | SD_SHARE_PKG_RESOURCES;

On non-cluster systems, there would be only one group at the at the
CLS domain. Since SD_CLUSTER is also tagged with SDF_NEEDS_GROUP, it
would need the presence of two groups. Thus, on such non-cluster
systems, the CLS domain will continue to be degenerated in favour of
the SMT domain.

>  }
>  #endif
>  
> @@ -177,6 +177,7 @@ cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
>  void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
>  
>  bool cpus_share_cache(int this_cpu, int that_cpu);
> +bool cpus_share_cluster(int this_cpu, int that_cpu);
>  
>  typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
>  typedef int (*sched_domain_flags_f)(void);
> @@ -230,6 +231,11 @@ static inline bool cpus_share_cache(int this_cpu, int that_cpu)
>  	return true;
>  }
>  
> +static inline bool cpus_share_cluster(int this_cpu, int that_cpu)
> +{
> +	return true;
> +}
> +
>  #endif	/* !CONFIG_SMP */
>  
>  #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index 3c9b0fda64ac..11f9b25c3068 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -3732,6 +3732,18 @@ bool cpus_share_cache(int this_cpu, int that_cpu)
>  	return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
>  }
>  
> +/*
> + * On non-Cluster machine this function works same with cpus_share_cache()
> + * as sd_cluster_id equals to sd_llc_id.
> + */

Ok, so on a non-cluster machine, a cluster is trivially defined to be
a single core (this is reflected in what the cpu_cluster_mask() shows
up on such machines).

However, in cpus_share_cluster(), we are upgrading the definition of a
cluster to be group of CPUs that share the LLC. This is inconsistent
with the original definition. Is there a way to avoid this
inconsistency ?



> +bool cpus_share_cluster(int this_cpu, int that_cpu)
> +{
> +	if (this_cpu == that_cpu)
> +		return true;
> +
> +	return per_cpu(sd_cluster_id, this_cpu) == per_cpu(sd_cluster_id, that_cpu);
> +}
> +
>  static inline bool ttwu_queue_cond(int cpu, int wake_flags)
>  {
>  	/*
> diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
> index 0e66749486e7..ddd29879ad40 100644
> --- a/kernel/sched/sched.h
> +++ b/kernel/sched/sched.h
> @@ -1763,7 +1763,9 @@ static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
>  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc);
>  DECLARE_PER_CPU(int, sd_llc_size);
>  DECLARE_PER_CPU(int, sd_llc_id);
> +DECLARE_PER_CPU(int, sd_cluster_id);
>  DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
> +DECLARE_PER_CPU(struct sched_domain __rcu *, sd_cluster);
>  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa);
>  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
>  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
> diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
> index d201a7052a29..5642df384904 100644
> --- a/kernel/sched/topology.c
> +++ b/kernel/sched/topology.c
> @@ -644,6 +644,8 @@ static void destroy_sched_domains(struct sched_domain *sd)
>  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_llc);
>  DEFINE_PER_CPU(int, sd_llc_size);
>  DEFINE_PER_CPU(int, sd_llc_id);
> +DEFINE_PER_CPU(int, sd_cluster_id);
> +DEFINE_PER_CPU(struct sched_domain __rcu *, sd_cluster);
>  DEFINE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
>  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_numa);
>  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
> @@ -669,6 +671,12 @@ static void update_top_cache_domain(int cpu)
>  	per_cpu(sd_llc_id, cpu) = id;
>  	rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds);
>  
> +	sd = lowest_flag_domain(cpu, SD_CLUSTER);
> +	if (sd)
> +		id = cpumask_first(sched_domain_span(sd));
> +	rcu_assign_pointer(per_cpu(sd_cluster, cpu), sd);
> +	per_cpu(sd_cluster_id, cpu) = id;
> +
>  	sd = lowest_flag_domain(cpu, SD_NUMA);
>  	rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
>  
> @@ -1514,6 +1522,7 @@ static unsigned long __read_mostly *sched_numa_onlined_nodes;
>   */
>  #define TOPOLOGY_SD_FLAGS		\
>  	(SD_SHARE_CPUCAPACITY	|	\
> +	 SD_CLUSTER		|	\
>  	 SD_SHARE_PKG_RESOURCES |	\
>  	 SD_NUMA		|	\
>  	 SD_ASYM_PACKING)
> -- 
> 2.33.0
> 


--
Thanks and Regards
gautham.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/2] sched: Add per_cpu cluster domain info and cpus_share_cluster API
@ 2021-12-22 10:24     ` Gautham R. Shenoy
  0 siblings, 0 replies; 20+ messages in thread
From: Gautham R. Shenoy @ 2021-12-22 10:24 UTC (permalink / raw)
  To: Yicong Yang
  Cc: peterz, mingo, juri.lelli, vincent.guittot, tim.c.chen,
	linux-kernel, linux-arm-kernel, dietmar.eggemann, rostedt,
	bsegall, bristot, prime.zeng, jonathan.cameron, ego, srikar,
	linuxarm, 21cnbao, song.bao.hua, guodong.xu

Hello Yicong, Barry,


On Wed, Dec 15, 2021 at 12:11:48PM +0800, Yicong Yang wrote:
> From: Barry Song <song.bao.hua@hisilicon.com>
> 
> Add per-cpu cluster domain info and cpus_share_cluster() API.
> This is the preparation for the optimization of select_idle_cpu()
> on platforms with cluster scheduler level.
> 
> Signed-off-by: Barry Song <song.bao.hua@hisilicon.com>
> Signed-off-by: Yicong Yang <yangyicong@hisilicon.com>
> ---
>  include/linux/sched/sd_flags.h |  5 +++++
>  include/linux/sched/topology.h |  8 +++++++-
>  kernel/sched/core.c            | 12 ++++++++++++
>  kernel/sched/sched.h           |  2 ++
>  kernel/sched/topology.c        |  9 +++++++++
>  5 files changed, 35 insertions(+), 1 deletion(-)
> 
> diff --git a/include/linux/sched/sd_flags.h b/include/linux/sched/sd_flags.h
> index 57bde66d95f7..0f732bcfbb2c 100644
> --- a/include/linux/sched/sd_flags.h
> +++ b/include/linux/sched/sd_flags.h
> @@ -109,6 +109,11 @@ SD_FLAG(SD_ASYM_CPUCAPACITY_FULL, SDF_SHARED_PARENT | SDF_NEEDS_GROUPS)
>   */
>  SD_FLAG(SD_SHARE_CPUCAPACITY, SDF_SHARED_CHILD | SDF_NEEDS_GROUPS)
>  
> +/*
> + * Set up for cluster domains sharing resources such as llc tags or l2
> + */
> +SD_FLAG(SD_CLUSTER, SDF_NEEDS_GROUPS)
> +
>  /*
>   * Domain members share CPU package resources (i.e. caches)
>   *
> diff --git a/include/linux/sched/topology.h b/include/linux/sched/topology.h
> index c07bfa2d80f2..78c3a94fae66 100644
> --- a/include/linux/sched/topology.h
> +++ b/include/linux/sched/topology.h
> @@ -45,7 +45,7 @@ static inline int cpu_smt_flags(void)
>  #ifdef CONFIG_SCHED_CLUSTER
>  static inline int cpu_cluster_flags(void)
>  {
> -	return SD_SHARE_PKG_RESOURCES;
> +	return SD_CLUSTER | SD_SHARE_PKG_RESOURCES;

On non-cluster systems, there would be only one group at the at the
CLS domain. Since SD_CLUSTER is also tagged with SDF_NEEDS_GROUP, it
would need the presence of two groups. Thus, on such non-cluster
systems, the CLS domain will continue to be degenerated in favour of
the SMT domain.

>  }
>  #endif
>  
> @@ -177,6 +177,7 @@ cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
>  void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
>  
>  bool cpus_share_cache(int this_cpu, int that_cpu);
> +bool cpus_share_cluster(int this_cpu, int that_cpu);
>  
>  typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
>  typedef int (*sched_domain_flags_f)(void);
> @@ -230,6 +231,11 @@ static inline bool cpus_share_cache(int this_cpu, int that_cpu)
>  	return true;
>  }
>  
> +static inline bool cpus_share_cluster(int this_cpu, int that_cpu)
> +{
> +	return true;
> +}
> +
>  #endif	/* !CONFIG_SMP */
>  
>  #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index 3c9b0fda64ac..11f9b25c3068 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -3732,6 +3732,18 @@ bool cpus_share_cache(int this_cpu, int that_cpu)
>  	return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
>  }
>  
> +/*
> + * On non-Cluster machine this function works same with cpus_share_cache()
> + * as sd_cluster_id equals to sd_llc_id.
> + */

Ok, so on a non-cluster machine, a cluster is trivially defined to be
a single core (this is reflected in what the cpu_cluster_mask() shows
up on such machines).

However, in cpus_share_cluster(), we are upgrading the definition of a
cluster to be group of CPUs that share the LLC. This is inconsistent
with the original definition. Is there a way to avoid this
inconsistency ?



> +bool cpus_share_cluster(int this_cpu, int that_cpu)
> +{
> +	if (this_cpu == that_cpu)
> +		return true;
> +
> +	return per_cpu(sd_cluster_id, this_cpu) == per_cpu(sd_cluster_id, that_cpu);
> +}
> +
>  static inline bool ttwu_queue_cond(int cpu, int wake_flags)
>  {
>  	/*
> diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
> index 0e66749486e7..ddd29879ad40 100644
> --- a/kernel/sched/sched.h
> +++ b/kernel/sched/sched.h
> @@ -1763,7 +1763,9 @@ static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
>  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc);
>  DECLARE_PER_CPU(int, sd_llc_size);
>  DECLARE_PER_CPU(int, sd_llc_id);
> +DECLARE_PER_CPU(int, sd_cluster_id);
>  DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
> +DECLARE_PER_CPU(struct sched_domain __rcu *, sd_cluster);
>  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa);
>  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
>  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
> diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
> index d201a7052a29..5642df384904 100644
> --- a/kernel/sched/topology.c
> +++ b/kernel/sched/topology.c
> @@ -644,6 +644,8 @@ static void destroy_sched_domains(struct sched_domain *sd)
>  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_llc);
>  DEFINE_PER_CPU(int, sd_llc_size);
>  DEFINE_PER_CPU(int, sd_llc_id);
> +DEFINE_PER_CPU(int, sd_cluster_id);
> +DEFINE_PER_CPU(struct sched_domain __rcu *, sd_cluster);
>  DEFINE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
>  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_numa);
>  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
> @@ -669,6 +671,12 @@ static void update_top_cache_domain(int cpu)
>  	per_cpu(sd_llc_id, cpu) = id;
>  	rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds);
>  
> +	sd = lowest_flag_domain(cpu, SD_CLUSTER);
> +	if (sd)
> +		id = cpumask_first(sched_domain_span(sd));
> +	rcu_assign_pointer(per_cpu(sd_cluster, cpu), sd);
> +	per_cpu(sd_cluster_id, cpu) = id;
> +
>  	sd = lowest_flag_domain(cpu, SD_NUMA);
>  	rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
>  
> @@ -1514,6 +1522,7 @@ static unsigned long __read_mostly *sched_numa_onlined_nodes;
>   */
>  #define TOPOLOGY_SD_FLAGS		\
>  	(SD_SHARE_CPUCAPACITY	|	\
> +	 SD_CLUSTER		|	\
>  	 SD_SHARE_PKG_RESOURCES |	\
>  	 SD_NUMA		|	\
>  	 SD_ASYM_PACKING)
> -- 
> 2.33.0
> 


--
Thanks and Regards
gautham.

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/2] sched: Add per_cpu cluster domain info and cpus_share_cluster API
  2021-12-22 10:24     ` Gautham R. Shenoy
@ 2021-12-22 11:06       ` Barry Song
  -1 siblings, 0 replies; 20+ messages in thread
From: Barry Song @ 2021-12-22 11:06 UTC (permalink / raw)
  To: Gautham R. Shenoy
  Cc: Yicong Yang, Peter Zijlstra, Ingo Molnar, Juri Lelli,
	Vincent Guittot, Tim Chen, LKML, LAK, Dietmar Eggemann,
	Steven Rostedt, Ben Segall, Daniel Bristot de Oliveira,
	prime.zeng, Jonathan Cameron, ego, Srikar Dronamraju, Linuxarm,
	Barry Song, Guodong Xu

On Wed, Dec 22, 2021 at 11:24 PM Gautham R. Shenoy
<gautham.shenoy@amd.com> wrote:
>
> Hello Yicong, Barry,
>
>
> On Wed, Dec 15, 2021 at 12:11:48PM +0800, Yicong Yang wrote:
> > From: Barry Song <song.bao.hua@hisilicon.com>
> >
> > Add per-cpu cluster domain info and cpus_share_cluster() API.
> > This is the preparation for the optimization of select_idle_cpu()
> > on platforms with cluster scheduler level.
> >
> > Signed-off-by: Barry Song <song.bao.hua@hisilicon.com>
> > Signed-off-by: Yicong Yang <yangyicong@hisilicon.com>
> > ---
> >  include/linux/sched/sd_flags.h |  5 +++++
> >  include/linux/sched/topology.h |  8 +++++++-
> >  kernel/sched/core.c            | 12 ++++++++++++
> >  kernel/sched/sched.h           |  2 ++
> >  kernel/sched/topology.c        |  9 +++++++++
> >  5 files changed, 35 insertions(+), 1 deletion(-)
> >
> > diff --git a/include/linux/sched/sd_flags.h b/include/linux/sched/sd_flags.h
> > index 57bde66d95f7..0f732bcfbb2c 100644
> > --- a/include/linux/sched/sd_flags.h
> > +++ b/include/linux/sched/sd_flags.h
> > @@ -109,6 +109,11 @@ SD_FLAG(SD_ASYM_CPUCAPACITY_FULL, SDF_SHARED_PARENT | SDF_NEEDS_GROUPS)
> >   */
> >  SD_FLAG(SD_SHARE_CPUCAPACITY, SDF_SHARED_CHILD | SDF_NEEDS_GROUPS)
> >
> > +/*
> > + * Set up for cluster domains sharing resources such as llc tags or l2
> > + */
> > +SD_FLAG(SD_CLUSTER, SDF_NEEDS_GROUPS)
> > +
> >  /*
> >   * Domain members share CPU package resources (i.e. caches)
> >   *
> > diff --git a/include/linux/sched/topology.h b/include/linux/sched/topology.h
> > index c07bfa2d80f2..78c3a94fae66 100644
> > --- a/include/linux/sched/topology.h
> > +++ b/include/linux/sched/topology.h
> > @@ -45,7 +45,7 @@ static inline int cpu_smt_flags(void)
> >  #ifdef CONFIG_SCHED_CLUSTER
> >  static inline int cpu_cluster_flags(void)
> >  {
> > -     return SD_SHARE_PKG_RESOURCES;
> > +     return SD_CLUSTER | SD_SHARE_PKG_RESOURCES;
>
> On non-cluster systems, there would be only one group at the at the
> CLS domain. Since SD_CLUSTER is also tagged with SDF_NEEDS_GROUP, it
> would need the presence of two groups. Thus, on such non-cluster
> systems, the CLS domain will continue to be degenerated in favour of
> the SMT domain.
>
> >  }
> >  #endif
> >
> > @@ -177,6 +177,7 @@ cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
> >  void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
> >
> >  bool cpus_share_cache(int this_cpu, int that_cpu);
> > +bool cpus_share_cluster(int this_cpu, int that_cpu);
> >
> >  typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
> >  typedef int (*sched_domain_flags_f)(void);
> > @@ -230,6 +231,11 @@ static inline bool cpus_share_cache(int this_cpu, int that_cpu)
> >       return true;
> >  }
> >
> > +static inline bool cpus_share_cluster(int this_cpu, int that_cpu)
> > +{
> > +     return true;
> > +}
> > +
> >  #endif       /* !CONFIG_SMP */
> >
> >  #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
> > diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> > index 3c9b0fda64ac..11f9b25c3068 100644
> > --- a/kernel/sched/core.c
> > +++ b/kernel/sched/core.c
> > @@ -3732,6 +3732,18 @@ bool cpus_share_cache(int this_cpu, int that_cpu)
> >       return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
> >  }
> >
> > +/*
> > + * On non-Cluster machine this function works same with cpus_share_cache()
> > + * as sd_cluster_id equals to sd_llc_id.
> > + */
>
> Ok, so on a non-cluster machine, a cluster is trivially defined to be
> a single core (this is reflected in what the cpu_cluster_mask() shows
> up on such machines).
>
> However, in cpus_share_cluster(), we are upgrading the definition of a
> cluster to be group of CPUs that share the LLC. This is inconsistent
> with the original definition. Is there a way to avoid this
> inconsistency ?

Hi gautham,
Yep but I wrote this trick to keep the change in patch2/2 compatible
with non-cluster machine:

@@ -6384,7 +6422,7 @@ static int select_idle_sibling(struct
task_struct *p, int prev, int target)
  /*
  * If the previous CPU is cache affine and idle, don't be stupid:
  */
- if (prev != target && cpus_share_cache(prev, target) &&
+ if (prev != target && cpus_share_cluster(prev, target) &&
     (available_idle_cpu(prev) || sched_idle_cpu(prev)) &&
     asym_fits_capacity(task_util, prev))
  return prev;
@@ -6408,7 +6446,7 @@ static int select_idle_sibling(struct
task_struct *p, int prev, int target)
  p->recent_used_cpu = prev;
  if (recent_used_cpu != prev &&
     recent_used_cpu != target &&
-    cpus_share_cache(recent_used_cpu, target) &&
+    cpus_share_cluster(recent_used_cpu, target) &&
     (available_idle_cpu(recent_used_cpu) || sched_idle_cpu(recent_used_cpu)) &&
     cpumask_test_cpu(p->recent_used_cpu, p->cpus_ptr) &&
     asym_fits_capacity(task_util, recent_used_cpu)) {
@@ -6449,7 +6487,7 @@ static int select_idle_sibling(struct
task_struct *p, int prev, int target)
  }
  }

I didn't find a better way to implement this with minimum overhead and
code modification.

>
>
>
> > +bool cpus_share_cluster(int this_cpu, int that_cpu)
> > +{
> > +     if (this_cpu == that_cpu)
> > +             return true;
> > +
> > +     return per_cpu(sd_cluster_id, this_cpu) == per_cpu(sd_cluster_id, that_cpu);
> > +}
> > +
> >  static inline bool ttwu_queue_cond(int cpu, int wake_flags)
> >  {
> >       /*
> > diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
> > index 0e66749486e7..ddd29879ad40 100644
> > --- a/kernel/sched/sched.h
> > +++ b/kernel/sched/sched.h
> > @@ -1763,7 +1763,9 @@ static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
> >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc);
> >  DECLARE_PER_CPU(int, sd_llc_size);
> >  DECLARE_PER_CPU(int, sd_llc_id);
> > +DECLARE_PER_CPU(int, sd_cluster_id);
> >  DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
> > +DECLARE_PER_CPU(struct sched_domain __rcu *, sd_cluster);
> >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa);
> >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
> >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
> > diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
> > index d201a7052a29..5642df384904 100644
> > --- a/kernel/sched/topology.c
> > +++ b/kernel/sched/topology.c
> > @@ -644,6 +644,8 @@ static void destroy_sched_domains(struct sched_domain *sd)
> >  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_llc);
> >  DEFINE_PER_CPU(int, sd_llc_size);
> >  DEFINE_PER_CPU(int, sd_llc_id);
> > +DEFINE_PER_CPU(int, sd_cluster_id);
> > +DEFINE_PER_CPU(struct sched_domain __rcu *, sd_cluster);
> >  DEFINE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
> >  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_numa);
> >  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
> > @@ -669,6 +671,12 @@ static void update_top_cache_domain(int cpu)
> >       per_cpu(sd_llc_id, cpu) = id;
> >       rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds);
> >
> > +     sd = lowest_flag_domain(cpu, SD_CLUSTER);
> > +     if (sd)
> > +             id = cpumask_first(sched_domain_span(sd));
> > +     rcu_assign_pointer(per_cpu(sd_cluster, cpu), sd);
> > +     per_cpu(sd_cluster_id, cpu) = id;
> > +
> >       sd = lowest_flag_domain(cpu, SD_NUMA);
> >       rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
> >
> > @@ -1514,6 +1522,7 @@ static unsigned long __read_mostly *sched_numa_onlined_nodes;
> >   */
> >  #define TOPOLOGY_SD_FLAGS            \
> >       (SD_SHARE_CPUCAPACITY   |       \
> > +      SD_CLUSTER             |       \
> >        SD_SHARE_PKG_RESOURCES |       \
> >        SD_NUMA                |       \
> >        SD_ASYM_PACKING)
> > --
> > 2.33.0
> >
>
>
> --
> Thanks and Regards
> gautham.

Thanks
Barry

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/2] sched: Add per_cpu cluster domain info and cpus_share_cluster API
@ 2021-12-22 11:06       ` Barry Song
  0 siblings, 0 replies; 20+ messages in thread
From: Barry Song @ 2021-12-22 11:06 UTC (permalink / raw)
  To: Gautham R. Shenoy
  Cc: Yicong Yang, Peter Zijlstra, Ingo Molnar, Juri Lelli,
	Vincent Guittot, Tim Chen, LKML, LAK, Dietmar Eggemann,
	Steven Rostedt, Ben Segall, Daniel Bristot de Oliveira,
	prime.zeng, Jonathan Cameron, ego, Srikar Dronamraju, Linuxarm,
	Barry Song, Guodong Xu

On Wed, Dec 22, 2021 at 11:24 PM Gautham R. Shenoy
<gautham.shenoy@amd.com> wrote:
>
> Hello Yicong, Barry,
>
>
> On Wed, Dec 15, 2021 at 12:11:48PM +0800, Yicong Yang wrote:
> > From: Barry Song <song.bao.hua@hisilicon.com>
> >
> > Add per-cpu cluster domain info and cpus_share_cluster() API.
> > This is the preparation for the optimization of select_idle_cpu()
> > on platforms with cluster scheduler level.
> >
> > Signed-off-by: Barry Song <song.bao.hua@hisilicon.com>
> > Signed-off-by: Yicong Yang <yangyicong@hisilicon.com>
> > ---
> >  include/linux/sched/sd_flags.h |  5 +++++
> >  include/linux/sched/topology.h |  8 +++++++-
> >  kernel/sched/core.c            | 12 ++++++++++++
> >  kernel/sched/sched.h           |  2 ++
> >  kernel/sched/topology.c        |  9 +++++++++
> >  5 files changed, 35 insertions(+), 1 deletion(-)
> >
> > diff --git a/include/linux/sched/sd_flags.h b/include/linux/sched/sd_flags.h
> > index 57bde66d95f7..0f732bcfbb2c 100644
> > --- a/include/linux/sched/sd_flags.h
> > +++ b/include/linux/sched/sd_flags.h
> > @@ -109,6 +109,11 @@ SD_FLAG(SD_ASYM_CPUCAPACITY_FULL, SDF_SHARED_PARENT | SDF_NEEDS_GROUPS)
> >   */
> >  SD_FLAG(SD_SHARE_CPUCAPACITY, SDF_SHARED_CHILD | SDF_NEEDS_GROUPS)
> >
> > +/*
> > + * Set up for cluster domains sharing resources such as llc tags or l2
> > + */
> > +SD_FLAG(SD_CLUSTER, SDF_NEEDS_GROUPS)
> > +
> >  /*
> >   * Domain members share CPU package resources (i.e. caches)
> >   *
> > diff --git a/include/linux/sched/topology.h b/include/linux/sched/topology.h
> > index c07bfa2d80f2..78c3a94fae66 100644
> > --- a/include/linux/sched/topology.h
> > +++ b/include/linux/sched/topology.h
> > @@ -45,7 +45,7 @@ static inline int cpu_smt_flags(void)
> >  #ifdef CONFIG_SCHED_CLUSTER
> >  static inline int cpu_cluster_flags(void)
> >  {
> > -     return SD_SHARE_PKG_RESOURCES;
> > +     return SD_CLUSTER | SD_SHARE_PKG_RESOURCES;
>
> On non-cluster systems, there would be only one group at the at the
> CLS domain. Since SD_CLUSTER is also tagged with SDF_NEEDS_GROUP, it
> would need the presence of two groups. Thus, on such non-cluster
> systems, the CLS domain will continue to be degenerated in favour of
> the SMT domain.
>
> >  }
> >  #endif
> >
> > @@ -177,6 +177,7 @@ cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
> >  void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
> >
> >  bool cpus_share_cache(int this_cpu, int that_cpu);
> > +bool cpus_share_cluster(int this_cpu, int that_cpu);
> >
> >  typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
> >  typedef int (*sched_domain_flags_f)(void);
> > @@ -230,6 +231,11 @@ static inline bool cpus_share_cache(int this_cpu, int that_cpu)
> >       return true;
> >  }
> >
> > +static inline bool cpus_share_cluster(int this_cpu, int that_cpu)
> > +{
> > +     return true;
> > +}
> > +
> >  #endif       /* !CONFIG_SMP */
> >
> >  #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
> > diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> > index 3c9b0fda64ac..11f9b25c3068 100644
> > --- a/kernel/sched/core.c
> > +++ b/kernel/sched/core.c
> > @@ -3732,6 +3732,18 @@ bool cpus_share_cache(int this_cpu, int that_cpu)
> >       return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
> >  }
> >
> > +/*
> > + * On non-Cluster machine this function works same with cpus_share_cache()
> > + * as sd_cluster_id equals to sd_llc_id.
> > + */
>
> Ok, so on a non-cluster machine, a cluster is trivially defined to be
> a single core (this is reflected in what the cpu_cluster_mask() shows
> up on such machines).
>
> However, in cpus_share_cluster(), we are upgrading the definition of a
> cluster to be group of CPUs that share the LLC. This is inconsistent
> with the original definition. Is there a way to avoid this
> inconsistency ?

Hi gautham,
Yep but I wrote this trick to keep the change in patch2/2 compatible
with non-cluster machine:

@@ -6384,7 +6422,7 @@ static int select_idle_sibling(struct
task_struct *p, int prev, int target)
  /*
  * If the previous CPU is cache affine and idle, don't be stupid:
  */
- if (prev != target && cpus_share_cache(prev, target) &&
+ if (prev != target && cpus_share_cluster(prev, target) &&
     (available_idle_cpu(prev) || sched_idle_cpu(prev)) &&
     asym_fits_capacity(task_util, prev))
  return prev;
@@ -6408,7 +6446,7 @@ static int select_idle_sibling(struct
task_struct *p, int prev, int target)
  p->recent_used_cpu = prev;
  if (recent_used_cpu != prev &&
     recent_used_cpu != target &&
-    cpus_share_cache(recent_used_cpu, target) &&
+    cpus_share_cluster(recent_used_cpu, target) &&
     (available_idle_cpu(recent_used_cpu) || sched_idle_cpu(recent_used_cpu)) &&
     cpumask_test_cpu(p->recent_used_cpu, p->cpus_ptr) &&
     asym_fits_capacity(task_util, recent_used_cpu)) {
@@ -6449,7 +6487,7 @@ static int select_idle_sibling(struct
task_struct *p, int prev, int target)
  }
  }

I didn't find a better way to implement this with minimum overhead and
code modification.

>
>
>
> > +bool cpus_share_cluster(int this_cpu, int that_cpu)
> > +{
> > +     if (this_cpu == that_cpu)
> > +             return true;
> > +
> > +     return per_cpu(sd_cluster_id, this_cpu) == per_cpu(sd_cluster_id, that_cpu);
> > +}
> > +
> >  static inline bool ttwu_queue_cond(int cpu, int wake_flags)
> >  {
> >       /*
> > diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
> > index 0e66749486e7..ddd29879ad40 100644
> > --- a/kernel/sched/sched.h
> > +++ b/kernel/sched/sched.h
> > @@ -1763,7 +1763,9 @@ static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
> >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc);
> >  DECLARE_PER_CPU(int, sd_llc_size);
> >  DECLARE_PER_CPU(int, sd_llc_id);
> > +DECLARE_PER_CPU(int, sd_cluster_id);
> >  DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
> > +DECLARE_PER_CPU(struct sched_domain __rcu *, sd_cluster);
> >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa);
> >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
> >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
> > diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
> > index d201a7052a29..5642df384904 100644
> > --- a/kernel/sched/topology.c
> > +++ b/kernel/sched/topology.c
> > @@ -644,6 +644,8 @@ static void destroy_sched_domains(struct sched_domain *sd)
> >  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_llc);
> >  DEFINE_PER_CPU(int, sd_llc_size);
> >  DEFINE_PER_CPU(int, sd_llc_id);
> > +DEFINE_PER_CPU(int, sd_cluster_id);
> > +DEFINE_PER_CPU(struct sched_domain __rcu *, sd_cluster);
> >  DEFINE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
> >  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_numa);
> >  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
> > @@ -669,6 +671,12 @@ static void update_top_cache_domain(int cpu)
> >       per_cpu(sd_llc_id, cpu) = id;
> >       rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds);
> >
> > +     sd = lowest_flag_domain(cpu, SD_CLUSTER);
> > +     if (sd)
> > +             id = cpumask_first(sched_domain_span(sd));
> > +     rcu_assign_pointer(per_cpu(sd_cluster, cpu), sd);
> > +     per_cpu(sd_cluster_id, cpu) = id;
> > +
> >       sd = lowest_flag_domain(cpu, SD_NUMA);
> >       rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
> >
> > @@ -1514,6 +1522,7 @@ static unsigned long __read_mostly *sched_numa_onlined_nodes;
> >   */
> >  #define TOPOLOGY_SD_FLAGS            \
> >       (SD_SHARE_CPUCAPACITY   |       \
> > +      SD_CLUSTER             |       \
> >        SD_SHARE_PKG_RESOURCES |       \
> >        SD_NUMA                |       \
> >        SD_ASYM_PACKING)
> > --
> > 2.33.0
> >
>
>
> --
> Thanks and Regards
> gautham.

Thanks
Barry

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/2] sched: Add per_cpu cluster domain info and cpus_share_cluster API
  2021-12-22 11:06       ` Barry Song
@ 2021-12-22 13:14         ` Vincent Guittot
  -1 siblings, 0 replies; 20+ messages in thread
From: Vincent Guittot @ 2021-12-22 13:14 UTC (permalink / raw)
  To: Barry Song
  Cc: Gautham R. Shenoy, Yicong Yang, Peter Zijlstra, Ingo Molnar,
	Juri Lelli, Tim Chen, LKML, LAK, Dietmar Eggemann,
	Steven Rostedt, Ben Segall, Daniel Bristot de Oliveira,
	prime.zeng, Jonathan Cameron, ego, Srikar Dronamraju, Linuxarm,
	Barry Song, Guodong Xu

On Wed, 22 Dec 2021 at 12:06, Barry Song <21cnbao@gmail.com> wrote:
>
> On Wed, Dec 22, 2021 at 11:24 PM Gautham R. Shenoy
> <gautham.shenoy@amd.com> wrote:
> >
> > Hello Yicong, Barry,
> >
> >
> > On Wed, Dec 15, 2021 at 12:11:48PM +0800, Yicong Yang wrote:
> > > From: Barry Song <song.bao.hua@hisilicon.com>
> > >
> > > Add per-cpu cluster domain info and cpus_share_cluster() API.
> > > This is the preparation for the optimization of select_idle_cpu()
> > > on platforms with cluster scheduler level.
> > >
> > > Signed-off-by: Barry Song <song.bao.hua@hisilicon.com>
> > > Signed-off-by: Yicong Yang <yangyicong@hisilicon.com>
> > > ---
> > >  include/linux/sched/sd_flags.h |  5 +++++
> > >  include/linux/sched/topology.h |  8 +++++++-
> > >  kernel/sched/core.c            | 12 ++++++++++++
> > >  kernel/sched/sched.h           |  2 ++
> > >  kernel/sched/topology.c        |  9 +++++++++
> > >  5 files changed, 35 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/include/linux/sched/sd_flags.h b/include/linux/sched/sd_flags.h
> > > index 57bde66d95f7..0f732bcfbb2c 100644
> > > --- a/include/linux/sched/sd_flags.h
> > > +++ b/include/linux/sched/sd_flags.h
> > > @@ -109,6 +109,11 @@ SD_FLAG(SD_ASYM_CPUCAPACITY_FULL, SDF_SHARED_PARENT | SDF_NEEDS_GROUPS)
> > >   */
> > >  SD_FLAG(SD_SHARE_CPUCAPACITY, SDF_SHARED_CHILD | SDF_NEEDS_GROUPS)
> > >
> > > +/*
> > > + * Set up for cluster domains sharing resources such as llc tags or l2
> > > + */
> > > +SD_FLAG(SD_CLUSTER, SDF_NEEDS_GROUPS)
> > > +
> > >  /*
> > >   * Domain members share CPU package resources (i.e. caches)
> > >   *
> > > diff --git a/include/linux/sched/topology.h b/include/linux/sched/topology.h
> > > index c07bfa2d80f2..78c3a94fae66 100644
> > > --- a/include/linux/sched/topology.h
> > > +++ b/include/linux/sched/topology.h
> > > @@ -45,7 +45,7 @@ static inline int cpu_smt_flags(void)
> > >  #ifdef CONFIG_SCHED_CLUSTER
> > >  static inline int cpu_cluster_flags(void)
> > >  {
> > > -     return SD_SHARE_PKG_RESOURCES;
> > > +     return SD_CLUSTER | SD_SHARE_PKG_RESOURCES;
> >
> > On non-cluster systems, there would be only one group at the at the
> > CLS domain. Since SD_CLUSTER is also tagged with SDF_NEEDS_GROUP, it
> > would need the presence of two groups. Thus, on such non-cluster
> > systems, the CLS domain will continue to be degenerated in favour of
> > the SMT domain.
> >
> > >  }
> > >  #endif
> > >
> > > @@ -177,6 +177,7 @@ cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
> > >  void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
> > >
> > >  bool cpus_share_cache(int this_cpu, int that_cpu);
> > > +bool cpus_share_cluster(int this_cpu, int that_cpu);
> > >
> > >  typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
> > >  typedef int (*sched_domain_flags_f)(void);
> > > @@ -230,6 +231,11 @@ static inline bool cpus_share_cache(int this_cpu, int that_cpu)
> > >       return true;
> > >  }
> > >
> > > +static inline bool cpus_share_cluster(int this_cpu, int that_cpu)
> > > +{
> > > +     return true;
> > > +}
> > > +
> > >  #endif       /* !CONFIG_SMP */
> > >
> > >  #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
> > > diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> > > index 3c9b0fda64ac..11f9b25c3068 100644
> > > --- a/kernel/sched/core.c
> > > +++ b/kernel/sched/core.c
> > > @@ -3732,6 +3732,18 @@ bool cpus_share_cache(int this_cpu, int that_cpu)
> > >       return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
> > >  }
> > >
> > > +/*
> > > + * On non-Cluster machine this function works same with cpus_share_cache()
> > > + * as sd_cluster_id equals to sd_llc_id.
> > > + */
> >
> > Ok, so on a non-cluster machine, a cluster is trivially defined to be
> > a single core (this is reflected in what the cpu_cluster_mask() shows
> > up on such machines).
> >
> > However, in cpus_share_cluster(), we are upgrading the definition of a
> > cluster to be group of CPUs that share the LLC. This is inconsistent
> > with the original definition. Is there a way to avoid this
> > inconsistency ?
>
> Hi gautham,
> Yep but I wrote this trick to keep the change in patch2/2 compatible
> with non-cluster machine:
>
> @@ -6384,7 +6422,7 @@ static int select_idle_sibling(struct
> task_struct *p, int prev, int target)
>   /*
>   * If the previous CPU is cache affine and idle, don't be stupid:
>   */
> - if (prev != target && cpus_share_cache(prev, target) &&
> + if (prev != target && cpus_share_cluster(prev, target) &&
>      (available_idle_cpu(prev) || sched_idle_cpu(prev)) &&
>      asym_fits_capacity(task_util, prev))
>   return prev;
> @@ -6408,7 +6446,7 @@ static int select_idle_sibling(struct
> task_struct *p, int prev, int target)
>   p->recent_used_cpu = prev;
>   if (recent_used_cpu != prev &&
>      recent_used_cpu != target &&
> -    cpus_share_cache(recent_used_cpu, target) &&
> +    cpus_share_cluster(recent_used_cpu, target) &&
>      (available_idle_cpu(recent_used_cpu) || sched_idle_cpu(recent_used_cpu)) &&
>      cpumask_test_cpu(p->recent_used_cpu, p->cpus_ptr) &&
>      asym_fits_capacity(task_util, recent_used_cpu)) {
> @@ -6449,7 +6487,7 @@ static int select_idle_sibling(struct
> task_struct *p, int prev, int target)
>   }
>   }
>
> I didn't find a better way to implement this with minimum overhead and
> code modification.
>
> >
> >
> >
> > > +bool cpus_share_cluster(int this_cpu, int that_cpu)
> > > +{
> > > +     if (this_cpu == that_cpu)
> > > +             return true;
> > > +
> > > +     return per_cpu(sd_cluster_id, this_cpu) == per_cpu(sd_cluster_id, that_cpu);
> > > +}
> > > +
> > >  static inline bool ttwu_queue_cond(int cpu, int wake_flags)
> > >  {
> > >       /*
> > > diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
> > > index 0e66749486e7..ddd29879ad40 100644
> > > --- a/kernel/sched/sched.h
> > > +++ b/kernel/sched/sched.h
> > > @@ -1763,7 +1763,9 @@ static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
> > >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc);
> > >  DECLARE_PER_CPU(int, sd_llc_size);
> > >  DECLARE_PER_CPU(int, sd_llc_id);
> > > +DECLARE_PER_CPU(int, sd_cluster_id);
> > >  DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
> > > +DECLARE_PER_CPU(struct sched_domain __rcu *, sd_cluster);
> > >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa);
> > >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
> > >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
> > > diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
> > > index d201a7052a29..5642df384904 100644
> > > --- a/kernel/sched/topology.c
> > > +++ b/kernel/sched/topology.c
> > > @@ -644,6 +644,8 @@ static void destroy_sched_domains(struct sched_domain *sd)
> > >  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_llc);
> > >  DEFINE_PER_CPU(int, sd_llc_size);
> > >  DEFINE_PER_CPU(int, sd_llc_id);
> > > +DEFINE_PER_CPU(int, sd_cluster_id);
> > > +DEFINE_PER_CPU(struct sched_domain __rcu *, sd_cluster);
> > >  DEFINE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
> > >  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_numa);
> > >  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
> > > @@ -669,6 +671,12 @@ static void update_top_cache_domain(int cpu)
> > >       per_cpu(sd_llc_id, cpu) = id;
> > >       rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds);
> > >
> > > +     sd = lowest_flag_domain(cpu, SD_CLUSTER);
> > > +     if (sd)
> > > +             id = cpumask_first(sched_domain_span(sd));
> > > +     rcu_assign_pointer(per_cpu(sd_cluster, cpu), sd);
> > > +     per_cpu(sd_cluster_id, cpu) = id;
> > > +

This deserves a large comment to highlight that the new code above is
not self contained and  relies on the fact that it is done just after
looking for LLC and sd and that id and sd must not be changed in
between inorder to ensures that per_cpu(sd_cluster_id, cpu) equals
per_cpu(sd_llc_id, cpu) if there is no domain with SD_CLUSTER.

and per_cpu(sd_cluster_id, cpu) might not be cluster but llc

> > >       sd = lowest_flag_domain(cpu, SD_NUMA);
> > >       rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
> > >
> > > @@ -1514,6 +1522,7 @@ static unsigned long __read_mostly *sched_numa_onlined_nodes;
> > >   */
> > >  #define TOPOLOGY_SD_FLAGS            \
> > >       (SD_SHARE_CPUCAPACITY   |       \
> > > +      SD_CLUSTER             |       \
> > >        SD_SHARE_PKG_RESOURCES |       \
> > >        SD_NUMA                |       \
> > >        SD_ASYM_PACKING)
> > > --
> > > 2.33.0
> > >
> >
> >
> > --
> > Thanks and Regards
> > gautham.
>
> Thanks
> Barry

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/2] sched: Add per_cpu cluster domain info and cpus_share_cluster API
@ 2021-12-22 13:14         ` Vincent Guittot
  0 siblings, 0 replies; 20+ messages in thread
From: Vincent Guittot @ 2021-12-22 13:14 UTC (permalink / raw)
  To: Barry Song
  Cc: Gautham R. Shenoy, Yicong Yang, Peter Zijlstra, Ingo Molnar,
	Juri Lelli, Tim Chen, LKML, LAK, Dietmar Eggemann,
	Steven Rostedt, Ben Segall, Daniel Bristot de Oliveira,
	prime.zeng, Jonathan Cameron, ego, Srikar Dronamraju, Linuxarm,
	Barry Song, Guodong Xu

On Wed, 22 Dec 2021 at 12:06, Barry Song <21cnbao@gmail.com> wrote:
>
> On Wed, Dec 22, 2021 at 11:24 PM Gautham R. Shenoy
> <gautham.shenoy@amd.com> wrote:
> >
> > Hello Yicong, Barry,
> >
> >
> > On Wed, Dec 15, 2021 at 12:11:48PM +0800, Yicong Yang wrote:
> > > From: Barry Song <song.bao.hua@hisilicon.com>
> > >
> > > Add per-cpu cluster domain info and cpus_share_cluster() API.
> > > This is the preparation for the optimization of select_idle_cpu()
> > > on platforms with cluster scheduler level.
> > >
> > > Signed-off-by: Barry Song <song.bao.hua@hisilicon.com>
> > > Signed-off-by: Yicong Yang <yangyicong@hisilicon.com>
> > > ---
> > >  include/linux/sched/sd_flags.h |  5 +++++
> > >  include/linux/sched/topology.h |  8 +++++++-
> > >  kernel/sched/core.c            | 12 ++++++++++++
> > >  kernel/sched/sched.h           |  2 ++
> > >  kernel/sched/topology.c        |  9 +++++++++
> > >  5 files changed, 35 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/include/linux/sched/sd_flags.h b/include/linux/sched/sd_flags.h
> > > index 57bde66d95f7..0f732bcfbb2c 100644
> > > --- a/include/linux/sched/sd_flags.h
> > > +++ b/include/linux/sched/sd_flags.h
> > > @@ -109,6 +109,11 @@ SD_FLAG(SD_ASYM_CPUCAPACITY_FULL, SDF_SHARED_PARENT | SDF_NEEDS_GROUPS)
> > >   */
> > >  SD_FLAG(SD_SHARE_CPUCAPACITY, SDF_SHARED_CHILD | SDF_NEEDS_GROUPS)
> > >
> > > +/*
> > > + * Set up for cluster domains sharing resources such as llc tags or l2
> > > + */
> > > +SD_FLAG(SD_CLUSTER, SDF_NEEDS_GROUPS)
> > > +
> > >  /*
> > >   * Domain members share CPU package resources (i.e. caches)
> > >   *
> > > diff --git a/include/linux/sched/topology.h b/include/linux/sched/topology.h
> > > index c07bfa2d80f2..78c3a94fae66 100644
> > > --- a/include/linux/sched/topology.h
> > > +++ b/include/linux/sched/topology.h
> > > @@ -45,7 +45,7 @@ static inline int cpu_smt_flags(void)
> > >  #ifdef CONFIG_SCHED_CLUSTER
> > >  static inline int cpu_cluster_flags(void)
> > >  {
> > > -     return SD_SHARE_PKG_RESOURCES;
> > > +     return SD_CLUSTER | SD_SHARE_PKG_RESOURCES;
> >
> > On non-cluster systems, there would be only one group at the at the
> > CLS domain. Since SD_CLUSTER is also tagged with SDF_NEEDS_GROUP, it
> > would need the presence of two groups. Thus, on such non-cluster
> > systems, the CLS domain will continue to be degenerated in favour of
> > the SMT domain.
> >
> > >  }
> > >  #endif
> > >
> > > @@ -177,6 +177,7 @@ cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
> > >  void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
> > >
> > >  bool cpus_share_cache(int this_cpu, int that_cpu);
> > > +bool cpus_share_cluster(int this_cpu, int that_cpu);
> > >
> > >  typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
> > >  typedef int (*sched_domain_flags_f)(void);
> > > @@ -230,6 +231,11 @@ static inline bool cpus_share_cache(int this_cpu, int that_cpu)
> > >       return true;
> > >  }
> > >
> > > +static inline bool cpus_share_cluster(int this_cpu, int that_cpu)
> > > +{
> > > +     return true;
> > > +}
> > > +
> > >  #endif       /* !CONFIG_SMP */
> > >
> > >  #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
> > > diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> > > index 3c9b0fda64ac..11f9b25c3068 100644
> > > --- a/kernel/sched/core.c
> > > +++ b/kernel/sched/core.c
> > > @@ -3732,6 +3732,18 @@ bool cpus_share_cache(int this_cpu, int that_cpu)
> > >       return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
> > >  }
> > >
> > > +/*
> > > + * On non-Cluster machine this function works same with cpus_share_cache()
> > > + * as sd_cluster_id equals to sd_llc_id.
> > > + */
> >
> > Ok, so on a non-cluster machine, a cluster is trivially defined to be
> > a single core (this is reflected in what the cpu_cluster_mask() shows
> > up on such machines).
> >
> > However, in cpus_share_cluster(), we are upgrading the definition of a
> > cluster to be group of CPUs that share the LLC. This is inconsistent
> > with the original definition. Is there a way to avoid this
> > inconsistency ?
>
> Hi gautham,
> Yep but I wrote this trick to keep the change in patch2/2 compatible
> with non-cluster machine:
>
> @@ -6384,7 +6422,7 @@ static int select_idle_sibling(struct
> task_struct *p, int prev, int target)
>   /*
>   * If the previous CPU is cache affine and idle, don't be stupid:
>   */
> - if (prev != target && cpus_share_cache(prev, target) &&
> + if (prev != target && cpus_share_cluster(prev, target) &&
>      (available_idle_cpu(prev) || sched_idle_cpu(prev)) &&
>      asym_fits_capacity(task_util, prev))
>   return prev;
> @@ -6408,7 +6446,7 @@ static int select_idle_sibling(struct
> task_struct *p, int prev, int target)
>   p->recent_used_cpu = prev;
>   if (recent_used_cpu != prev &&
>      recent_used_cpu != target &&
> -    cpus_share_cache(recent_used_cpu, target) &&
> +    cpus_share_cluster(recent_used_cpu, target) &&
>      (available_idle_cpu(recent_used_cpu) || sched_idle_cpu(recent_used_cpu)) &&
>      cpumask_test_cpu(p->recent_used_cpu, p->cpus_ptr) &&
>      asym_fits_capacity(task_util, recent_used_cpu)) {
> @@ -6449,7 +6487,7 @@ static int select_idle_sibling(struct
> task_struct *p, int prev, int target)
>   }
>   }
>
> I didn't find a better way to implement this with minimum overhead and
> code modification.
>
> >
> >
> >
> > > +bool cpus_share_cluster(int this_cpu, int that_cpu)
> > > +{
> > > +     if (this_cpu == that_cpu)
> > > +             return true;
> > > +
> > > +     return per_cpu(sd_cluster_id, this_cpu) == per_cpu(sd_cluster_id, that_cpu);
> > > +}
> > > +
> > >  static inline bool ttwu_queue_cond(int cpu, int wake_flags)
> > >  {
> > >       /*
> > > diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
> > > index 0e66749486e7..ddd29879ad40 100644
> > > --- a/kernel/sched/sched.h
> > > +++ b/kernel/sched/sched.h
> > > @@ -1763,7 +1763,9 @@ static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
> > >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc);
> > >  DECLARE_PER_CPU(int, sd_llc_size);
> > >  DECLARE_PER_CPU(int, sd_llc_id);
> > > +DECLARE_PER_CPU(int, sd_cluster_id);
> > >  DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
> > > +DECLARE_PER_CPU(struct sched_domain __rcu *, sd_cluster);
> > >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa);
> > >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
> > >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
> > > diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
> > > index d201a7052a29..5642df384904 100644
> > > --- a/kernel/sched/topology.c
> > > +++ b/kernel/sched/topology.c
> > > @@ -644,6 +644,8 @@ static void destroy_sched_domains(struct sched_domain *sd)
> > >  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_llc);
> > >  DEFINE_PER_CPU(int, sd_llc_size);
> > >  DEFINE_PER_CPU(int, sd_llc_id);
> > > +DEFINE_PER_CPU(int, sd_cluster_id);
> > > +DEFINE_PER_CPU(struct sched_domain __rcu *, sd_cluster);
> > >  DEFINE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
> > >  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_numa);
> > >  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
> > > @@ -669,6 +671,12 @@ static void update_top_cache_domain(int cpu)
> > >       per_cpu(sd_llc_id, cpu) = id;
> > >       rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds);
> > >
> > > +     sd = lowest_flag_domain(cpu, SD_CLUSTER);
> > > +     if (sd)
> > > +             id = cpumask_first(sched_domain_span(sd));
> > > +     rcu_assign_pointer(per_cpu(sd_cluster, cpu), sd);
> > > +     per_cpu(sd_cluster_id, cpu) = id;
> > > +

This deserves a large comment to highlight that the new code above is
not self contained and  relies on the fact that it is done just after
looking for LLC and sd and that id and sd must not be changed in
between inorder to ensures that per_cpu(sd_cluster_id, cpu) equals
per_cpu(sd_llc_id, cpu) if there is no domain with SD_CLUSTER.

and per_cpu(sd_cluster_id, cpu) might not be cluster but llc

> > >       sd = lowest_flag_domain(cpu, SD_NUMA);
> > >       rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
> > >
> > > @@ -1514,6 +1522,7 @@ static unsigned long __read_mostly *sched_numa_onlined_nodes;
> > >   */
> > >  #define TOPOLOGY_SD_FLAGS            \
> > >       (SD_SHARE_CPUCAPACITY   |       \
> > > +      SD_CLUSTER             |       \
> > >        SD_SHARE_PKG_RESOURCES |       \
> > >        SD_NUMA                |       \
> > >        SD_ASYM_PACKING)
> > > --
> > > 2.33.0
> > >
> >
> >
> > --
> > Thanks and Regards
> > gautham.
>
> Thanks
> Barry

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 2/2] sched/fair: Scan cluster before scanning LLC in wake-up path
  2021-12-15  4:11   ` Yicong Yang
@ 2021-12-22 13:26     ` Vincent Guittot
  -1 siblings, 0 replies; 20+ messages in thread
From: Vincent Guittot @ 2021-12-22 13:26 UTC (permalink / raw)
  To: Yicong Yang
  Cc: peterz, mingo, juri.lelli, tim.c.chen, linux-kernel,
	linux-arm-kernel, dietmar.eggemann, rostedt, bsegall, bristot,
	prime.zeng, jonathan.cameron, ego, srikar, linuxarm, 21cnbao,
	song.bao.hua, guodong.xu

On Wed, 15 Dec 2021 at 05:11, Yicong Yang <yangyicong@hisilicon.com> wrote:
>
> From: Barry Song <song.bao.hua@hisilicon.com>
>
> For platforms having clusters like Kunpeng920, CPUs within the same
> cluster have lower latency when synchronizing and accessing shared
> resources like cache. Thus, this patch tries to find an idle cpu
> within the cluster of the target CPU before scanning the whole LLC
> to gain lower latency.
>
> Note neither Kunpeng920 nor x86 Jacobsville supports SMT, so this
> patch doesn't consider SMT for this moment.
>
> Testing has been done on Kunpeng920 by pinning tasks to one numa
> and two numa. Each numa has 8 clusters and each cluster has 4 CPUs.
>
> With this patch, We noticed enhancement on tbench within one
> numa or cross two numa.
>
> On numa 0:
>                            5.16-rc1                patched
> Hmean     1        329.17 (   0.00%)      384.84 *  16.91%*
> Hmean     2        654.09 (   0.00%)      768.77 *  17.53%*
> Hmean     4       1321.41 (   0.00%)     1538.10 *  16.40%*
> Hmean     8       2650.43 (   0.00%)     3048.86 *  15.03%*
> Hmean     16      5322.37 (   0.00%)     5975.20 *  12.27%*
> Hmean     32     10002.11 (   0.00%)    10085.57 *   0.83%*
> Hmean     64      7910.39 (   0.00%)     7936.37 *   0.33%*
> Hmean     128     6745.25 (   0.00%)     6811.28 *   0.98%*
>
> On numa 0-1:
>                            5.16-rc1                patched
> Hmean     1        326.61 (   0.00%)      385.36 *  17.99%*
> Hmean     2        650.62 (   0.00%)      770.57 *  18.44%*
> Hmean     4       1318.05 (   0.00%)     1534.83 *  16.45%*
> Hmean     8       2621.50 (   0.00%)     3030.10 *  15.59%*
> Hmean     16      5252.17 (   0.00%)     6023.08 *  14.68%*
> Hmean     32      9829.30 (   0.00%)     9856.33 *   0.28%*
> Hmean     64     12452.66 (   0.00%)    17338.48 *  39.24%*
> Hmean     128    14181.24 (   0.00%)    15025.24 *   5.95%*
> Hmean     256    12239.07 (   0.00%)    13080.16 *   6.87%*
> Hmean     512    14297.00 (   0.00%)    15063.76 *   5.36%*
>
> This will also help to improve the MySQL. With MySQL server
> running on numa 0 and client running on numa 1, both QPS and
> latency is imporved on read-write case:
>                       5.16-rc1              patched
> QPS-24threads        195327.48    202081.28(+3.46%)
> QPS-32threads         242039.4     247059.5(+2.07%)
> QPS-64threads        243024.52    254274.47(+4.63%)
> avg-lat-24threads         2.46         2.37(+3.66%)
> avg-lat-36threads         2.64         2.59(+1.89%)
> avg-lat-64threads         5.27         5.03(+4.55%)
>
> Tested-by: Yicong Yang <yangyicong@hisilicon.com>
> Signed-off-by: Barry Song <song.bao.hua@hisilicon.com>
> Signed-off-by: Yicong Yang <yangyicong@hisilicon.com>
> ---
>  kernel/sched/fair.c | 46 +++++++++++++++++++++++++++++++++++++++++----
>  1 file changed, 42 insertions(+), 4 deletions(-)
>
> diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
> index 6e476f6d9435..8a5795c78af8 100644
> --- a/kernel/sched/fair.c
> +++ b/kernel/sched/fair.c
> @@ -6230,12 +6230,46 @@ static inline int select_idle_smt(struct task_struct *p, struct sched_domain *sd
>
>  #endif /* CONFIG_SCHED_SMT */
>
> +#ifdef CONFIG_SCHED_CLUSTER
> +/*
> + * Scan the cluster domain for idle CPUs and clear cluster cpumask after scanning
> + */
> +static inline int scan_cluster(struct task_struct *p, int prev_cpu, int target)
> +{
> +       struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask);
> +       struct sched_domain *sd = rcu_dereference(per_cpu(sd_cluster, target));
> +       int cpu, idle_cpu;
> +
> +       /* TODO: Support SMT case while a machine with both cluster and SMT born */
> +       if (!sched_smt_active() && sd) {
> +               for_each_cpu_and(cpu, cpus, sched_domain_span(sd)) {
> +                       idle_cpu = __select_idle_cpu(cpu, p);
> +                       if ((unsigned int)idle_cpu < nr_cpumask_bits)
> +                               return idle_cpu;
> +               }
> +
> +               /* Don't ping-pong tasks in and out cluster frequently */
> +               if (cpus_share_cluster(target, prev_cpu))
> +                       return target;
> +
> +               cpumask_andnot(cpus, cpus, sched_domain_span(sd));
> +       }
> +
> +       return -1;
> +}
> +#else
> +static inline int scan_cluster(struct task_struct *p, int prev_cpu, int target)
> +{
> +       return -1;
> +}
> +#endif
> +
>  /*
>   * Scan the LLC domain for idle CPUs; this is dynamically regulated by
>   * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
>   * average idle time for this rq (as found in rq->avg_idle).
>   */
> -static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool has_idle_core, int target)
> +static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool has_idle_core, int prev_cpu, int target)
>  {
>         struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask);
>         int i, cpu, idle_cpu = -1, nr = INT_MAX;
> @@ -6250,6 +6284,10 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
>
>         cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr);
>
> +       idle_cpu = scan_cluster(p, prev_cpu, target);

Why don't you do the above before calling select_idle_cpu() like for smt ?

> +       if ((unsigned int)idle_cpu < nr_cpumask_bits)
> +               return idle_cpu;
> +
>         if (sched_feat(SIS_PROP) && !has_idle_core) {
>                 u64 avg_cost, avg_idle, span_avg;
>                 unsigned long now = jiffies;
> @@ -6384,7 +6422,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
>         /*
>          * If the previous CPU is cache affine and idle, don't be stupid:
>          */
> -       if (prev != target && cpus_share_cache(prev, target) &&
> +       if (prev != target && cpus_share_cluster(prev, target) &&

This is misleading because cpus_share_cluster is meaningless for most of system

Then, you don't care at all of the llc if there is a cluster domain ?


>             (available_idle_cpu(prev) || sched_idle_cpu(prev)) &&
>             asym_fits_capacity(task_util, prev))
>                 return prev;
> @@ -6408,7 +6446,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
>         p->recent_used_cpu = prev;
>         if (recent_used_cpu != prev &&
>             recent_used_cpu != target &&
> -           cpus_share_cache(recent_used_cpu, target) &&
> +           cpus_share_cluster(recent_used_cpu, target) &&
>             (available_idle_cpu(recent_used_cpu) || sched_idle_cpu(recent_used_cpu)) &&
>             cpumask_test_cpu(p->recent_used_cpu, p->cpus_ptr) &&
>             asym_fits_capacity(task_util, recent_used_cpu)) {
> @@ -6449,7 +6487,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
>                 }
>         }
>
> -       i = select_idle_cpu(p, sd, has_idle_core, target);
> +       i = select_idle_cpu(p, sd, has_idle_core, prev, target);
>         if ((unsigned)i < nr_cpumask_bits)
>                 return i;
>
> --
> 2.33.0
>

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 2/2] sched/fair: Scan cluster before scanning LLC in wake-up path
@ 2021-12-22 13:26     ` Vincent Guittot
  0 siblings, 0 replies; 20+ messages in thread
From: Vincent Guittot @ 2021-12-22 13:26 UTC (permalink / raw)
  To: Yicong Yang
  Cc: peterz, mingo, juri.lelli, tim.c.chen, linux-kernel,
	linux-arm-kernel, dietmar.eggemann, rostedt, bsegall, bristot,
	prime.zeng, jonathan.cameron, ego, srikar, linuxarm, 21cnbao,
	song.bao.hua, guodong.xu

On Wed, 15 Dec 2021 at 05:11, Yicong Yang <yangyicong@hisilicon.com> wrote:
>
> From: Barry Song <song.bao.hua@hisilicon.com>
>
> For platforms having clusters like Kunpeng920, CPUs within the same
> cluster have lower latency when synchronizing and accessing shared
> resources like cache. Thus, this patch tries to find an idle cpu
> within the cluster of the target CPU before scanning the whole LLC
> to gain lower latency.
>
> Note neither Kunpeng920 nor x86 Jacobsville supports SMT, so this
> patch doesn't consider SMT for this moment.
>
> Testing has been done on Kunpeng920 by pinning tasks to one numa
> and two numa. Each numa has 8 clusters and each cluster has 4 CPUs.
>
> With this patch, We noticed enhancement on tbench within one
> numa or cross two numa.
>
> On numa 0:
>                            5.16-rc1                patched
> Hmean     1        329.17 (   0.00%)      384.84 *  16.91%*
> Hmean     2        654.09 (   0.00%)      768.77 *  17.53%*
> Hmean     4       1321.41 (   0.00%)     1538.10 *  16.40%*
> Hmean     8       2650.43 (   0.00%)     3048.86 *  15.03%*
> Hmean     16      5322.37 (   0.00%)     5975.20 *  12.27%*
> Hmean     32     10002.11 (   0.00%)    10085.57 *   0.83%*
> Hmean     64      7910.39 (   0.00%)     7936.37 *   0.33%*
> Hmean     128     6745.25 (   0.00%)     6811.28 *   0.98%*
>
> On numa 0-1:
>                            5.16-rc1                patched
> Hmean     1        326.61 (   0.00%)      385.36 *  17.99%*
> Hmean     2        650.62 (   0.00%)      770.57 *  18.44%*
> Hmean     4       1318.05 (   0.00%)     1534.83 *  16.45%*
> Hmean     8       2621.50 (   0.00%)     3030.10 *  15.59%*
> Hmean     16      5252.17 (   0.00%)     6023.08 *  14.68%*
> Hmean     32      9829.30 (   0.00%)     9856.33 *   0.28%*
> Hmean     64     12452.66 (   0.00%)    17338.48 *  39.24%*
> Hmean     128    14181.24 (   0.00%)    15025.24 *   5.95%*
> Hmean     256    12239.07 (   0.00%)    13080.16 *   6.87%*
> Hmean     512    14297.00 (   0.00%)    15063.76 *   5.36%*
>
> This will also help to improve the MySQL. With MySQL server
> running on numa 0 and client running on numa 1, both QPS and
> latency is imporved on read-write case:
>                       5.16-rc1              patched
> QPS-24threads        195327.48    202081.28(+3.46%)
> QPS-32threads         242039.4     247059.5(+2.07%)
> QPS-64threads        243024.52    254274.47(+4.63%)
> avg-lat-24threads         2.46         2.37(+3.66%)
> avg-lat-36threads         2.64         2.59(+1.89%)
> avg-lat-64threads         5.27         5.03(+4.55%)
>
> Tested-by: Yicong Yang <yangyicong@hisilicon.com>
> Signed-off-by: Barry Song <song.bao.hua@hisilicon.com>
> Signed-off-by: Yicong Yang <yangyicong@hisilicon.com>
> ---
>  kernel/sched/fair.c | 46 +++++++++++++++++++++++++++++++++++++++++----
>  1 file changed, 42 insertions(+), 4 deletions(-)
>
> diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
> index 6e476f6d9435..8a5795c78af8 100644
> --- a/kernel/sched/fair.c
> +++ b/kernel/sched/fair.c
> @@ -6230,12 +6230,46 @@ static inline int select_idle_smt(struct task_struct *p, struct sched_domain *sd
>
>  #endif /* CONFIG_SCHED_SMT */
>
> +#ifdef CONFIG_SCHED_CLUSTER
> +/*
> + * Scan the cluster domain for idle CPUs and clear cluster cpumask after scanning
> + */
> +static inline int scan_cluster(struct task_struct *p, int prev_cpu, int target)
> +{
> +       struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask);
> +       struct sched_domain *sd = rcu_dereference(per_cpu(sd_cluster, target));
> +       int cpu, idle_cpu;
> +
> +       /* TODO: Support SMT case while a machine with both cluster and SMT born */
> +       if (!sched_smt_active() && sd) {
> +               for_each_cpu_and(cpu, cpus, sched_domain_span(sd)) {
> +                       idle_cpu = __select_idle_cpu(cpu, p);
> +                       if ((unsigned int)idle_cpu < nr_cpumask_bits)
> +                               return idle_cpu;
> +               }
> +
> +               /* Don't ping-pong tasks in and out cluster frequently */
> +               if (cpus_share_cluster(target, prev_cpu))
> +                       return target;
> +
> +               cpumask_andnot(cpus, cpus, sched_domain_span(sd));
> +       }
> +
> +       return -1;
> +}
> +#else
> +static inline int scan_cluster(struct task_struct *p, int prev_cpu, int target)
> +{
> +       return -1;
> +}
> +#endif
> +
>  /*
>   * Scan the LLC domain for idle CPUs; this is dynamically regulated by
>   * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
>   * average idle time for this rq (as found in rq->avg_idle).
>   */
> -static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool has_idle_core, int target)
> +static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool has_idle_core, int prev_cpu, int target)
>  {
>         struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask);
>         int i, cpu, idle_cpu = -1, nr = INT_MAX;
> @@ -6250,6 +6284,10 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
>
>         cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr);
>
> +       idle_cpu = scan_cluster(p, prev_cpu, target);

Why don't you do the above before calling select_idle_cpu() like for smt ?

> +       if ((unsigned int)idle_cpu < nr_cpumask_bits)
> +               return idle_cpu;
> +
>         if (sched_feat(SIS_PROP) && !has_idle_core) {
>                 u64 avg_cost, avg_idle, span_avg;
>                 unsigned long now = jiffies;
> @@ -6384,7 +6422,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
>         /*
>          * If the previous CPU is cache affine and idle, don't be stupid:
>          */
> -       if (prev != target && cpus_share_cache(prev, target) &&
> +       if (prev != target && cpus_share_cluster(prev, target) &&

This is misleading because cpus_share_cluster is meaningless for most of system

Then, you don't care at all of the llc if there is a cluster domain ?


>             (available_idle_cpu(prev) || sched_idle_cpu(prev)) &&
>             asym_fits_capacity(task_util, prev))
>                 return prev;
> @@ -6408,7 +6446,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
>         p->recent_used_cpu = prev;
>         if (recent_used_cpu != prev &&
>             recent_used_cpu != target &&
> -           cpus_share_cache(recent_used_cpu, target) &&
> +           cpus_share_cluster(recent_used_cpu, target) &&
>             (available_idle_cpu(recent_used_cpu) || sched_idle_cpu(recent_used_cpu)) &&
>             cpumask_test_cpu(p->recent_used_cpu, p->cpus_ptr) &&
>             asym_fits_capacity(task_util, recent_used_cpu)) {
> @@ -6449,7 +6487,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
>                 }
>         }
>
> -       i = select_idle_cpu(p, sd, has_idle_core, target);
> +       i = select_idle_cpu(p, sd, has_idle_core, prev, target);
>         if ((unsigned)i < nr_cpumask_bits)
>                 return i;
>
> --
> 2.33.0
>

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/2] sched: Add per_cpu cluster domain info and cpus_share_cluster API
  2021-12-22 13:14         ` Vincent Guittot
@ 2021-12-22 13:33           ` Vincent Guittot
  -1 siblings, 0 replies; 20+ messages in thread
From: Vincent Guittot @ 2021-12-22 13:33 UTC (permalink / raw)
  To: Barry Song
  Cc: Gautham R. Shenoy, Yicong Yang, Peter Zijlstra, Ingo Molnar,
	Juri Lelli, Tim Chen, LKML, LAK, Dietmar Eggemann,
	Steven Rostedt, Ben Segall, Daniel Bristot de Oliveira,
	prime.zeng, Jonathan Cameron, ego, Srikar Dronamraju, Linuxarm,
	Barry Song, Guodong Xu

On Wed, 22 Dec 2021 at 14:14, Vincent Guittot
<vincent.guittot@linaro.org> wrote:
>
> On Wed, 22 Dec 2021 at 12:06, Barry Song <21cnbao@gmail.com> wrote:
> >
> > On Wed, Dec 22, 2021 at 11:24 PM Gautham R. Shenoy
> > <gautham.shenoy@amd.com> wrote:
> > >
> > > Hello Yicong, Barry,
> > >
> > >
> > > On Wed, Dec 15, 2021 at 12:11:48PM +0800, Yicong Yang wrote:
> > > > From: Barry Song <song.bao.hua@hisilicon.com>
> > > >
> > > > Add per-cpu cluster domain info and cpus_share_cluster() API.
> > > > This is the preparation for the optimization of select_idle_cpu()
> > > > on platforms with cluster scheduler level.
> > > >
> > > > Signed-off-by: Barry Song <song.bao.hua@hisilicon.com>
> > > > Signed-off-by: Yicong Yang <yangyicong@hisilicon.com>
> > > > ---
> > > >  include/linux/sched/sd_flags.h |  5 +++++
> > > >  include/linux/sched/topology.h |  8 +++++++-
> > > >  kernel/sched/core.c            | 12 ++++++++++++
> > > >  kernel/sched/sched.h           |  2 ++
> > > >  kernel/sched/topology.c        |  9 +++++++++
> > > >  5 files changed, 35 insertions(+), 1 deletion(-)
> > > >
> > > > diff --git a/include/linux/sched/sd_flags.h b/include/linux/sched/sd_flags.h
> > > > index 57bde66d95f7..0f732bcfbb2c 100644
> > > > --- a/include/linux/sched/sd_flags.h
> > > > +++ b/include/linux/sched/sd_flags.h
> > > > @@ -109,6 +109,11 @@ SD_FLAG(SD_ASYM_CPUCAPACITY_FULL, SDF_SHARED_PARENT | SDF_NEEDS_GROUPS)
> > > >   */
> > > >  SD_FLAG(SD_SHARE_CPUCAPACITY, SDF_SHARED_CHILD | SDF_NEEDS_GROUPS)
> > > >
> > > > +/*
> > > > + * Set up for cluster domains sharing resources such as llc tags or l2
> > > > + */
> > > > +SD_FLAG(SD_CLUSTER, SDF_NEEDS_GROUPS)
> > > > +
> > > >  /*
> > > >   * Domain members share CPU package resources (i.e. caches)
> > > >   *
> > > > diff --git a/include/linux/sched/topology.h b/include/linux/sched/topology.h
> > > > index c07bfa2d80f2..78c3a94fae66 100644
> > > > --- a/include/linux/sched/topology.h
> > > > +++ b/include/linux/sched/topology.h
> > > > @@ -45,7 +45,7 @@ static inline int cpu_smt_flags(void)
> > > >  #ifdef CONFIG_SCHED_CLUSTER
> > > >  static inline int cpu_cluster_flags(void)
> > > >  {
> > > > -     return SD_SHARE_PKG_RESOURCES;
> > > > +     return SD_CLUSTER | SD_SHARE_PKG_RESOURCES;
> > >
> > > On non-cluster systems, there would be only one group at the at the
> > > CLS domain. Since SD_CLUSTER is also tagged with SDF_NEEDS_GROUP, it
> > > would need the presence of two groups. Thus, on such non-cluster
> > > systems, the CLS domain will continue to be degenerated in favour of
> > > the SMT domain.
> > >
> > > >  }
> > > >  #endif
> > > >
> > > > @@ -177,6 +177,7 @@ cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
> > > >  void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
> > > >
> > > >  bool cpus_share_cache(int this_cpu, int that_cpu);
> > > > +bool cpus_share_cluster(int this_cpu, int that_cpu);
> > > >
> > > >  typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
> > > >  typedef int (*sched_domain_flags_f)(void);
> > > > @@ -230,6 +231,11 @@ static inline bool cpus_share_cache(int this_cpu, int that_cpu)
> > > >       return true;
> > > >  }
> > > >
> > > > +static inline bool cpus_share_cluster(int this_cpu, int that_cpu)
> > > > +{
> > > > +     return true;
> > > > +}
> > > > +
> > > >  #endif       /* !CONFIG_SMP */
> > > >
> > > >  #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
> > > > diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> > > > index 3c9b0fda64ac..11f9b25c3068 100644
> > > > --- a/kernel/sched/core.c
> > > > +++ b/kernel/sched/core.c
> > > > @@ -3732,6 +3732,18 @@ bool cpus_share_cache(int this_cpu, int that_cpu)
> > > >       return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
> > > >  }
> > > >
> > > > +/*
> > > > + * On non-Cluster machine this function works same with cpus_share_cache()
> > > > + * as sd_cluster_id equals to sd_llc_id.
> > > > + */
> > >
> > > Ok, so on a non-cluster machine, a cluster is trivially defined to be
> > > a single core (this is reflected in what the cpu_cluster_mask() shows
> > > up on such machines).
> > >
> > > However, in cpus_share_cluster(), we are upgrading the definition of a
> > > cluster to be group of CPUs that share the LLC. This is inconsistent
> > > with the original definition. Is there a way to avoid this
> > > inconsistency ?
> >
> > Hi gautham,
> > Yep but I wrote this trick to keep the change in patch2/2 compatible
> > with non-cluster machine:
> >
> > @@ -6384,7 +6422,7 @@ static int select_idle_sibling(struct
> > task_struct *p, int prev, int target)
> >   /*
> >   * If the previous CPU is cache affine and idle, don't be stupid:
> >   */
> > - if (prev != target && cpus_share_cache(prev, target) &&
> > + if (prev != target && cpus_share_cluster(prev, target) &&
> >      (available_idle_cpu(prev) || sched_idle_cpu(prev)) &&
> >      asym_fits_capacity(task_util, prev))
> >   return prev;
> > @@ -6408,7 +6446,7 @@ static int select_idle_sibling(struct
> > task_struct *p, int prev, int target)
> >   p->recent_used_cpu = prev;
> >   if (recent_used_cpu != prev &&
> >      recent_used_cpu != target &&
> > -    cpus_share_cache(recent_used_cpu, target) &&
> > +    cpus_share_cluster(recent_used_cpu, target) &&
> >      (available_idle_cpu(recent_used_cpu) || sched_idle_cpu(recent_used_cpu)) &&
> >      cpumask_test_cpu(p->recent_used_cpu, p->cpus_ptr) &&
> >      asym_fits_capacity(task_util, recent_used_cpu)) {
> > @@ -6449,7 +6487,7 @@ static int select_idle_sibling(struct
> > task_struct *p, int prev, int target)
> >   }
> >   }
> >
> > I didn't find a better way to implement this with minimum overhead and
> > code modification.
> >
> > >
> > >
> > >
> > > > +bool cpus_share_cluster(int this_cpu, int that_cpu)
> > > > +{
> > > > +     if (this_cpu == that_cpu)
> > > > +             return true;
> > > > +
> > > > +     return per_cpu(sd_cluster_id, this_cpu) == per_cpu(sd_cluster_id, that_cpu);
> > > > +}
> > > > +
> > > >  static inline bool ttwu_queue_cond(int cpu, int wake_flags)
> > > >  {
> > > >       /*
> > > > diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
> > > > index 0e66749486e7..ddd29879ad40 100644
> > > > --- a/kernel/sched/sched.h
> > > > +++ b/kernel/sched/sched.h
> > > > @@ -1763,7 +1763,9 @@ static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
> > > >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc);
> > > >  DECLARE_PER_CPU(int, sd_llc_size);
> > > >  DECLARE_PER_CPU(int, sd_llc_id);
> > > > +DECLARE_PER_CPU(int, sd_cluster_id);
> > > >  DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
> > > > +DECLARE_PER_CPU(struct sched_domain __rcu *, sd_cluster);
> > > >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa);
> > > >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
> > > >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
> > > > diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
> > > > index d201a7052a29..5642df384904 100644
> > > > --- a/kernel/sched/topology.c
> > > > +++ b/kernel/sched/topology.c
> > > > @@ -644,6 +644,8 @@ static void destroy_sched_domains(struct sched_domain *sd)
> > > >  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_llc);
> > > >  DEFINE_PER_CPU(int, sd_llc_size);
> > > >  DEFINE_PER_CPU(int, sd_llc_id);
> > > > +DEFINE_PER_CPU(int, sd_cluster_id);
> > > > +DEFINE_PER_CPU(struct sched_domain __rcu *, sd_cluster);
> > > >  DEFINE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
> > > >  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_numa);
> > > >  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
> > > > @@ -669,6 +671,12 @@ static void update_top_cache_domain(int cpu)
> > > >       per_cpu(sd_llc_id, cpu) = id;
> > > >       rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds);
> > > >
> > > > +     sd = lowest_flag_domain(cpu, SD_CLUSTER);
> > > > +     if (sd)
> > > > +             id = cpumask_first(sched_domain_span(sd));
> > > > +     rcu_assign_pointer(per_cpu(sd_cluster, cpu), sd);
> > > > +     per_cpu(sd_cluster_id, cpu) = id;
> > > > +
>
> This deserves a large comment to highlight that the new code above is
> not self contained and  relies on the fact that it is done just after
> looking for LLC and sd and that id and sd must not be changed in
> between inorder to ensures that per_cpu(sd_cluster_id, cpu) equals
> per_cpu(sd_llc_id, cpu) if there is no domain with SD_CLUSTER.
>
> and per_cpu(sd_cluster_id, cpu) might not be cluster but llc

Maybe you should not name this sd_cluster_id at all but
per_cpu(sd_share_id, cpu) = id;

Then you have a function named

+bool cpus_share_resources(int this_cpu, int that_cpu)
+{
+     if (this_cpu == that_cpu)
+             return true;
+
+     return per_cpu(sd_share_id, this_cpu) == per_cpu(sd_share_id, that_cpu);
+}
+
which returns true when cpu shares resources which can be LLC or cluster

>
> > > >       sd = lowest_flag_domain(cpu, SD_NUMA);
> > > >       rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
> > > >
> > > > @@ -1514,6 +1522,7 @@ static unsigned long __read_mostly *sched_numa_onlined_nodes;
> > > >   */
> > > >  #define TOPOLOGY_SD_FLAGS            \
> > > >       (SD_SHARE_CPUCAPACITY   |       \
> > > > +      SD_CLUSTER             |       \
> > > >        SD_SHARE_PKG_RESOURCES |       \
> > > >        SD_NUMA                |       \
> > > >        SD_ASYM_PACKING)
> > > > --
> > > > 2.33.0
> > > >
> > >
> > >
> > > --
> > > Thanks and Regards
> > > gautham.
> >
> > Thanks
> > Barry

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/2] sched: Add per_cpu cluster domain info and cpus_share_cluster API
@ 2021-12-22 13:33           ` Vincent Guittot
  0 siblings, 0 replies; 20+ messages in thread
From: Vincent Guittot @ 2021-12-22 13:33 UTC (permalink / raw)
  To: Barry Song
  Cc: Gautham R. Shenoy, Yicong Yang, Peter Zijlstra, Ingo Molnar,
	Juri Lelli, Tim Chen, LKML, LAK, Dietmar Eggemann,
	Steven Rostedt, Ben Segall, Daniel Bristot de Oliveira,
	prime.zeng, Jonathan Cameron, ego, Srikar Dronamraju, Linuxarm,
	Barry Song, Guodong Xu

On Wed, 22 Dec 2021 at 14:14, Vincent Guittot
<vincent.guittot@linaro.org> wrote:
>
> On Wed, 22 Dec 2021 at 12:06, Barry Song <21cnbao@gmail.com> wrote:
> >
> > On Wed, Dec 22, 2021 at 11:24 PM Gautham R. Shenoy
> > <gautham.shenoy@amd.com> wrote:
> > >
> > > Hello Yicong, Barry,
> > >
> > >
> > > On Wed, Dec 15, 2021 at 12:11:48PM +0800, Yicong Yang wrote:
> > > > From: Barry Song <song.bao.hua@hisilicon.com>
> > > >
> > > > Add per-cpu cluster domain info and cpus_share_cluster() API.
> > > > This is the preparation for the optimization of select_idle_cpu()
> > > > on platforms with cluster scheduler level.
> > > >
> > > > Signed-off-by: Barry Song <song.bao.hua@hisilicon.com>
> > > > Signed-off-by: Yicong Yang <yangyicong@hisilicon.com>
> > > > ---
> > > >  include/linux/sched/sd_flags.h |  5 +++++
> > > >  include/linux/sched/topology.h |  8 +++++++-
> > > >  kernel/sched/core.c            | 12 ++++++++++++
> > > >  kernel/sched/sched.h           |  2 ++
> > > >  kernel/sched/topology.c        |  9 +++++++++
> > > >  5 files changed, 35 insertions(+), 1 deletion(-)
> > > >
> > > > diff --git a/include/linux/sched/sd_flags.h b/include/linux/sched/sd_flags.h
> > > > index 57bde66d95f7..0f732bcfbb2c 100644
> > > > --- a/include/linux/sched/sd_flags.h
> > > > +++ b/include/linux/sched/sd_flags.h
> > > > @@ -109,6 +109,11 @@ SD_FLAG(SD_ASYM_CPUCAPACITY_FULL, SDF_SHARED_PARENT | SDF_NEEDS_GROUPS)
> > > >   */
> > > >  SD_FLAG(SD_SHARE_CPUCAPACITY, SDF_SHARED_CHILD | SDF_NEEDS_GROUPS)
> > > >
> > > > +/*
> > > > + * Set up for cluster domains sharing resources such as llc tags or l2
> > > > + */
> > > > +SD_FLAG(SD_CLUSTER, SDF_NEEDS_GROUPS)
> > > > +
> > > >  /*
> > > >   * Domain members share CPU package resources (i.e. caches)
> > > >   *
> > > > diff --git a/include/linux/sched/topology.h b/include/linux/sched/topology.h
> > > > index c07bfa2d80f2..78c3a94fae66 100644
> > > > --- a/include/linux/sched/topology.h
> > > > +++ b/include/linux/sched/topology.h
> > > > @@ -45,7 +45,7 @@ static inline int cpu_smt_flags(void)
> > > >  #ifdef CONFIG_SCHED_CLUSTER
> > > >  static inline int cpu_cluster_flags(void)
> > > >  {
> > > > -     return SD_SHARE_PKG_RESOURCES;
> > > > +     return SD_CLUSTER | SD_SHARE_PKG_RESOURCES;
> > >
> > > On non-cluster systems, there would be only one group at the at the
> > > CLS domain. Since SD_CLUSTER is also tagged with SDF_NEEDS_GROUP, it
> > > would need the presence of two groups. Thus, on such non-cluster
> > > systems, the CLS domain will continue to be degenerated in favour of
> > > the SMT domain.
> > >
> > > >  }
> > > >  #endif
> > > >
> > > > @@ -177,6 +177,7 @@ cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
> > > >  void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
> > > >
> > > >  bool cpus_share_cache(int this_cpu, int that_cpu);
> > > > +bool cpus_share_cluster(int this_cpu, int that_cpu);
> > > >
> > > >  typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
> > > >  typedef int (*sched_domain_flags_f)(void);
> > > > @@ -230,6 +231,11 @@ static inline bool cpus_share_cache(int this_cpu, int that_cpu)
> > > >       return true;
> > > >  }
> > > >
> > > > +static inline bool cpus_share_cluster(int this_cpu, int that_cpu)
> > > > +{
> > > > +     return true;
> > > > +}
> > > > +
> > > >  #endif       /* !CONFIG_SMP */
> > > >
> > > >  #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
> > > > diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> > > > index 3c9b0fda64ac..11f9b25c3068 100644
> > > > --- a/kernel/sched/core.c
> > > > +++ b/kernel/sched/core.c
> > > > @@ -3732,6 +3732,18 @@ bool cpus_share_cache(int this_cpu, int that_cpu)
> > > >       return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
> > > >  }
> > > >
> > > > +/*
> > > > + * On non-Cluster machine this function works same with cpus_share_cache()
> > > > + * as sd_cluster_id equals to sd_llc_id.
> > > > + */
> > >
> > > Ok, so on a non-cluster machine, a cluster is trivially defined to be
> > > a single core (this is reflected in what the cpu_cluster_mask() shows
> > > up on such machines).
> > >
> > > However, in cpus_share_cluster(), we are upgrading the definition of a
> > > cluster to be group of CPUs that share the LLC. This is inconsistent
> > > with the original definition. Is there a way to avoid this
> > > inconsistency ?
> >
> > Hi gautham,
> > Yep but I wrote this trick to keep the change in patch2/2 compatible
> > with non-cluster machine:
> >
> > @@ -6384,7 +6422,7 @@ static int select_idle_sibling(struct
> > task_struct *p, int prev, int target)
> >   /*
> >   * If the previous CPU is cache affine and idle, don't be stupid:
> >   */
> > - if (prev != target && cpus_share_cache(prev, target) &&
> > + if (prev != target && cpus_share_cluster(prev, target) &&
> >      (available_idle_cpu(prev) || sched_idle_cpu(prev)) &&
> >      asym_fits_capacity(task_util, prev))
> >   return prev;
> > @@ -6408,7 +6446,7 @@ static int select_idle_sibling(struct
> > task_struct *p, int prev, int target)
> >   p->recent_used_cpu = prev;
> >   if (recent_used_cpu != prev &&
> >      recent_used_cpu != target &&
> > -    cpus_share_cache(recent_used_cpu, target) &&
> > +    cpus_share_cluster(recent_used_cpu, target) &&
> >      (available_idle_cpu(recent_used_cpu) || sched_idle_cpu(recent_used_cpu)) &&
> >      cpumask_test_cpu(p->recent_used_cpu, p->cpus_ptr) &&
> >      asym_fits_capacity(task_util, recent_used_cpu)) {
> > @@ -6449,7 +6487,7 @@ static int select_idle_sibling(struct
> > task_struct *p, int prev, int target)
> >   }
> >   }
> >
> > I didn't find a better way to implement this with minimum overhead and
> > code modification.
> >
> > >
> > >
> > >
> > > > +bool cpus_share_cluster(int this_cpu, int that_cpu)
> > > > +{
> > > > +     if (this_cpu == that_cpu)
> > > > +             return true;
> > > > +
> > > > +     return per_cpu(sd_cluster_id, this_cpu) == per_cpu(sd_cluster_id, that_cpu);
> > > > +}
> > > > +
> > > >  static inline bool ttwu_queue_cond(int cpu, int wake_flags)
> > > >  {
> > > >       /*
> > > > diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
> > > > index 0e66749486e7..ddd29879ad40 100644
> > > > --- a/kernel/sched/sched.h
> > > > +++ b/kernel/sched/sched.h
> > > > @@ -1763,7 +1763,9 @@ static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
> > > >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc);
> > > >  DECLARE_PER_CPU(int, sd_llc_size);
> > > >  DECLARE_PER_CPU(int, sd_llc_id);
> > > > +DECLARE_PER_CPU(int, sd_cluster_id);
> > > >  DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
> > > > +DECLARE_PER_CPU(struct sched_domain __rcu *, sd_cluster);
> > > >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa);
> > > >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
> > > >  DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
> > > > diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
> > > > index d201a7052a29..5642df384904 100644
> > > > --- a/kernel/sched/topology.c
> > > > +++ b/kernel/sched/topology.c
> > > > @@ -644,6 +644,8 @@ static void destroy_sched_domains(struct sched_domain *sd)
> > > >  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_llc);
> > > >  DEFINE_PER_CPU(int, sd_llc_size);
> > > >  DEFINE_PER_CPU(int, sd_llc_id);
> > > > +DEFINE_PER_CPU(int, sd_cluster_id);
> > > > +DEFINE_PER_CPU(struct sched_domain __rcu *, sd_cluster);
> > > >  DEFINE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
> > > >  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_numa);
> > > >  DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
> > > > @@ -669,6 +671,12 @@ static void update_top_cache_domain(int cpu)
> > > >       per_cpu(sd_llc_id, cpu) = id;
> > > >       rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds);
> > > >
> > > > +     sd = lowest_flag_domain(cpu, SD_CLUSTER);
> > > > +     if (sd)
> > > > +             id = cpumask_first(sched_domain_span(sd));
> > > > +     rcu_assign_pointer(per_cpu(sd_cluster, cpu), sd);
> > > > +     per_cpu(sd_cluster_id, cpu) = id;
> > > > +
>
> This deserves a large comment to highlight that the new code above is
> not self contained and  relies on the fact that it is done just after
> looking for LLC and sd and that id and sd must not be changed in
> between inorder to ensures that per_cpu(sd_cluster_id, cpu) equals
> per_cpu(sd_llc_id, cpu) if there is no domain with SD_CLUSTER.
>
> and per_cpu(sd_cluster_id, cpu) might not be cluster but llc

Maybe you should not name this sd_cluster_id at all but
per_cpu(sd_share_id, cpu) = id;

Then you have a function named

+bool cpus_share_resources(int this_cpu, int that_cpu)
+{
+     if (this_cpu == that_cpu)
+             return true;
+
+     return per_cpu(sd_share_id, this_cpu) == per_cpu(sd_share_id, that_cpu);
+}
+
which returns true when cpu shares resources which can be LLC or cluster

>
> > > >       sd = lowest_flag_domain(cpu, SD_NUMA);
> > > >       rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
> > > >
> > > > @@ -1514,6 +1522,7 @@ static unsigned long __read_mostly *sched_numa_onlined_nodes;
> > > >   */
> > > >  #define TOPOLOGY_SD_FLAGS            \
> > > >       (SD_SHARE_CPUCAPACITY   |       \
> > > > +      SD_CLUSTER             |       \
> > > >        SD_SHARE_PKG_RESOURCES |       \
> > > >        SD_NUMA                |       \
> > > >        SD_ASYM_PACKING)
> > > > --
> > > > 2.33.0
> > > >
> > >
> > >
> > > --
> > > Thanks and Regards
> > > gautham.
> >
> > Thanks
> > Barry

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 2/2] sched/fair: Scan cluster before scanning LLC in wake-up path
  2021-12-22 13:26     ` Vincent Guittot
@ 2021-12-22 22:36       ` Barry Song
  -1 siblings, 0 replies; 20+ messages in thread
From: Barry Song @ 2021-12-22 22:36 UTC (permalink / raw)
  To: Vincent Guittot
  Cc: Yicong Yang, Peter Zijlstra, Ingo Molnar, Juri Lelli, Tim Chen,
	LKML, LAK, Dietmar Eggemann, Steven Rostedt, Ben Segall,
	Daniel Bristot de Oliveira, prime.zeng, Jonathan Cameron, ego,
	Srikar Dronamraju, Linuxarm, Barry Song, Guodong Xu

On Thu, Dec 23, 2021 at 2:27 AM Vincent Guittot
<vincent.guittot@linaro.org> wrote:
>
> On Wed, 15 Dec 2021 at 05:11, Yicong Yang <yangyicong@hisilicon.com> wrote:
> >
> > From: Barry Song <song.bao.hua@hisilicon.com>
> >
> > For platforms having clusters like Kunpeng920, CPUs within the same
> > cluster have lower latency when synchronizing and accessing shared
> > resources like cache. Thus, this patch tries to find an idle cpu
> > within the cluster of the target CPU before scanning the whole LLC
> > to gain lower latency.
> >
> > Note neither Kunpeng920 nor x86 Jacobsville supports SMT, so this
> > patch doesn't consider SMT for this moment.
> >
> > Testing has been done on Kunpeng920 by pinning tasks to one numa
> > and two numa. Each numa has 8 clusters and each cluster has 4 CPUs.
> >
> > With this patch, We noticed enhancement on tbench within one
> > numa or cross two numa.
> >
> > On numa 0:
> >                            5.16-rc1                patched
> > Hmean     1        329.17 (   0.00%)      384.84 *  16.91%*
> > Hmean     2        654.09 (   0.00%)      768.77 *  17.53%*
> > Hmean     4       1321.41 (   0.00%)     1538.10 *  16.40%*
> > Hmean     8       2650.43 (   0.00%)     3048.86 *  15.03%*
> > Hmean     16      5322.37 (   0.00%)     5975.20 *  12.27%*
> > Hmean     32     10002.11 (   0.00%)    10085.57 *   0.83%*
> > Hmean     64      7910.39 (   0.00%)     7936.37 *   0.33%*
> > Hmean     128     6745.25 (   0.00%)     6811.28 *   0.98%*
> >
> > On numa 0-1:
> >                            5.16-rc1                patched
> > Hmean     1        326.61 (   0.00%)      385.36 *  17.99%*
> > Hmean     2        650.62 (   0.00%)      770.57 *  18.44%*
> > Hmean     4       1318.05 (   0.00%)     1534.83 *  16.45%*
> > Hmean     8       2621.50 (   0.00%)     3030.10 *  15.59%*
> > Hmean     16      5252.17 (   0.00%)     6023.08 *  14.68%*
> > Hmean     32      9829.30 (   0.00%)     9856.33 *   0.28%*
> > Hmean     64     12452.66 (   0.00%)    17338.48 *  39.24%*
> > Hmean     128    14181.24 (   0.00%)    15025.24 *   5.95%*
> > Hmean     256    12239.07 (   0.00%)    13080.16 *   6.87%*
> > Hmean     512    14297.00 (   0.00%)    15063.76 *   5.36%*
> >
> > This will also help to improve the MySQL. With MySQL server
> > running on numa 0 and client running on numa 1, both QPS and
> > latency is imporved on read-write case:
> >                       5.16-rc1              patched
> > QPS-24threads        195327.48    202081.28(+3.46%)
> > QPS-32threads         242039.4     247059.5(+2.07%)
> > QPS-64threads        243024.52    254274.47(+4.63%)
> > avg-lat-24threads         2.46         2.37(+3.66%)
> > avg-lat-36threads         2.64         2.59(+1.89%)
> > avg-lat-64threads         5.27         5.03(+4.55%)
> >
> > Tested-by: Yicong Yang <yangyicong@hisilicon.com>
> > Signed-off-by: Barry Song <song.bao.hua@hisilicon.com>
> > Signed-off-by: Yicong Yang <yangyicong@hisilicon.com>
> > ---
> >  kernel/sched/fair.c | 46 +++++++++++++++++++++++++++++++++++++++++----
> >  1 file changed, 42 insertions(+), 4 deletions(-)
> >
> > diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
> > index 6e476f6d9435..8a5795c78af8 100644
> > --- a/kernel/sched/fair.c
> > +++ b/kernel/sched/fair.c
> > @@ -6230,12 +6230,46 @@ static inline int select_idle_smt(struct task_struct *p, struct sched_domain *sd
> >
> >  #endif /* CONFIG_SCHED_SMT */
> >
> > +#ifdef CONFIG_SCHED_CLUSTER
> > +/*
> > + * Scan the cluster domain for idle CPUs and clear cluster cpumask after scanning
> > + */
> > +static inline int scan_cluster(struct task_struct *p, int prev_cpu, int target)
> > +{
> > +       struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask);
> > +       struct sched_domain *sd = rcu_dereference(per_cpu(sd_cluster, target));
> > +       int cpu, idle_cpu;
> > +
> > +       /* TODO: Support SMT case while a machine with both cluster and SMT born */
> > +       if (!sched_smt_active() && sd) {
> > +               for_each_cpu_and(cpu, cpus, sched_domain_span(sd)) {
> > +                       idle_cpu = __select_idle_cpu(cpu, p);
> > +                       if ((unsigned int)idle_cpu < nr_cpumask_bits)
> > +                               return idle_cpu;
> > +               }
> > +
> > +               /* Don't ping-pong tasks in and out cluster frequently */
> > +               if (cpus_share_cluster(target, prev_cpu))
> > +                       return target;
> > +
> > +               cpumask_andnot(cpus, cpus, sched_domain_span(sd));
> > +       }
> > +
> > +       return -1;
> > +}
> > +#else
> > +static inline int scan_cluster(struct task_struct *p, int prev_cpu, int target)
> > +{
> > +       return -1;
> > +}
> > +#endif
> > +
> >  /*
> >   * Scan the LLC domain for idle CPUs; this is dynamically regulated by
> >   * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
> >   * average idle time for this rq (as found in rq->avg_idle).
> >   */
> > -static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool has_idle_core, int target)
> > +static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool has_idle_core, int prev_cpu, int target)
> >  {
> >         struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask);
> >         int i, cpu, idle_cpu = -1, nr = INT_MAX;
> > @@ -6250,6 +6284,10 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
> >
> >         cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr);
> >
> > +       idle_cpu = scan_cluster(p, prev_cpu, target);
>
> Why don't you do the above before calling select_idle_cpu() like for smt ?

yep. I did make two versions :
1. scan_cluster inside select_idle_cpu()
2. scan_cluster before select_idle_cpu()

Then I felt 1 looks a bit better than 2 as 2 requires changes in two
places. We still need to change
select_idle_cpu() to do cpumask_andnot() to exclude the cluster as we
have scanned the cluster
before select_idle_cpu(). In the first way, only one place is modified.

>
> > +       if ((unsigned int)idle_cpu < nr_cpumask_bits)
> > +               return idle_cpu;
> > +
> >         if (sched_feat(SIS_PROP) && !has_idle_core) {
> >                 u64 avg_cost, avg_idle, span_avg;
> >                 unsigned long now = jiffies;
> > @@ -6384,7 +6422,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
> >         /*
> >          * If the previous CPU is cache affine and idle, don't be stupid:
> >          */
> > -       if (prev != target && cpus_share_cache(prev, target) &&
> > +       if (prev != target && cpus_share_cluster(prev, target) &&
>
> This is misleading because cpus_share_cluster is meaningless for most of system

yep. The name is misleading though it hasn't side effects for
non-cluster systems as
cpus_share_cluster equals cpus_share_cache for them.
It seems this can be much better if we do the below as your comments
in patch1/2:

+bool cpus_share_resources(int this_cpu, int that_cpu)
+{
+     if (this_cpu == that_cpu)
+             return true;
+
+     return per_cpu(sd_share_id, this_cpu) == per_cpu(sd_share_id, that_cpu);
+}
+
which returns true when cpu shares resources which can be LLC or cluster

>
> Then, you don't care at all of the llc if there is a cluster domain ?

Basically, what I have seen is that in tbench, target is likely to be
idle especially when
the system load is not full. and waker and wakee are unlikely to run
in the same cluster
as fork has splitted them for LB. so we will almost always lose the
chance to pull them
together forever. This change made a big gain for tbench especially
for light and medium
load.

But Yicong did see some negative impact on benchmarks like pgbench. so
this change
is not a universal win. The difficulty might be how to make a decision
which can be
more aware of the hierarchy of two levels - cluster and llc, and aware
of the requirements
of the workload.

>
>
> >             (available_idle_cpu(prev) || sched_idle_cpu(prev)) &&
> >             asym_fits_capacity(task_util, prev))
> >                 return prev;
> > @@ -6408,7 +6446,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
> >         p->recent_used_cpu = prev;
> >         if (recent_used_cpu != prev &&
> >             recent_used_cpu != target &&
> > -           cpus_share_cache(recent_used_cpu, target) &&
> > +           cpus_share_cluster(recent_used_cpu, target) &&
> >             (available_idle_cpu(recent_used_cpu) || sched_idle_cpu(recent_used_cpu)) &&
> >             cpumask_test_cpu(p->recent_used_cpu, p->cpus_ptr) &&
> >             asym_fits_capacity(task_util, recent_used_cpu)) {
> > @@ -6449,7 +6487,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
> >                 }
> >         }
> >
> > -       i = select_idle_cpu(p, sd, has_idle_core, target);
> > +       i = select_idle_cpu(p, sd, has_idle_core, prev, target);
> >         if ((unsigned)i < nr_cpumask_bits)
> >                 return i;
> >
> > --
> > 2.33.0
> >

Thanks
Barry

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 2/2] sched/fair: Scan cluster before scanning LLC in wake-up path
@ 2021-12-22 22:36       ` Barry Song
  0 siblings, 0 replies; 20+ messages in thread
From: Barry Song @ 2021-12-22 22:36 UTC (permalink / raw)
  To: Vincent Guittot
  Cc: Yicong Yang, Peter Zijlstra, Ingo Molnar, Juri Lelli, Tim Chen,
	LKML, LAK, Dietmar Eggemann, Steven Rostedt, Ben Segall,
	Daniel Bristot de Oliveira, prime.zeng, Jonathan Cameron, ego,
	Srikar Dronamraju, Linuxarm, Barry Song, Guodong Xu

On Thu, Dec 23, 2021 at 2:27 AM Vincent Guittot
<vincent.guittot@linaro.org> wrote:
>
> On Wed, 15 Dec 2021 at 05:11, Yicong Yang <yangyicong@hisilicon.com> wrote:
> >
> > From: Barry Song <song.bao.hua@hisilicon.com>
> >
> > For platforms having clusters like Kunpeng920, CPUs within the same
> > cluster have lower latency when synchronizing and accessing shared
> > resources like cache. Thus, this patch tries to find an idle cpu
> > within the cluster of the target CPU before scanning the whole LLC
> > to gain lower latency.
> >
> > Note neither Kunpeng920 nor x86 Jacobsville supports SMT, so this
> > patch doesn't consider SMT for this moment.
> >
> > Testing has been done on Kunpeng920 by pinning tasks to one numa
> > and two numa. Each numa has 8 clusters and each cluster has 4 CPUs.
> >
> > With this patch, We noticed enhancement on tbench within one
> > numa or cross two numa.
> >
> > On numa 0:
> >                            5.16-rc1                patched
> > Hmean     1        329.17 (   0.00%)      384.84 *  16.91%*
> > Hmean     2        654.09 (   0.00%)      768.77 *  17.53%*
> > Hmean     4       1321.41 (   0.00%)     1538.10 *  16.40%*
> > Hmean     8       2650.43 (   0.00%)     3048.86 *  15.03%*
> > Hmean     16      5322.37 (   0.00%)     5975.20 *  12.27%*
> > Hmean     32     10002.11 (   0.00%)    10085.57 *   0.83%*
> > Hmean     64      7910.39 (   0.00%)     7936.37 *   0.33%*
> > Hmean     128     6745.25 (   0.00%)     6811.28 *   0.98%*
> >
> > On numa 0-1:
> >                            5.16-rc1                patched
> > Hmean     1        326.61 (   0.00%)      385.36 *  17.99%*
> > Hmean     2        650.62 (   0.00%)      770.57 *  18.44%*
> > Hmean     4       1318.05 (   0.00%)     1534.83 *  16.45%*
> > Hmean     8       2621.50 (   0.00%)     3030.10 *  15.59%*
> > Hmean     16      5252.17 (   0.00%)     6023.08 *  14.68%*
> > Hmean     32      9829.30 (   0.00%)     9856.33 *   0.28%*
> > Hmean     64     12452.66 (   0.00%)    17338.48 *  39.24%*
> > Hmean     128    14181.24 (   0.00%)    15025.24 *   5.95%*
> > Hmean     256    12239.07 (   0.00%)    13080.16 *   6.87%*
> > Hmean     512    14297.00 (   0.00%)    15063.76 *   5.36%*
> >
> > This will also help to improve the MySQL. With MySQL server
> > running on numa 0 and client running on numa 1, both QPS and
> > latency is imporved on read-write case:
> >                       5.16-rc1              patched
> > QPS-24threads        195327.48    202081.28(+3.46%)
> > QPS-32threads         242039.4     247059.5(+2.07%)
> > QPS-64threads        243024.52    254274.47(+4.63%)
> > avg-lat-24threads         2.46         2.37(+3.66%)
> > avg-lat-36threads         2.64         2.59(+1.89%)
> > avg-lat-64threads         5.27         5.03(+4.55%)
> >
> > Tested-by: Yicong Yang <yangyicong@hisilicon.com>
> > Signed-off-by: Barry Song <song.bao.hua@hisilicon.com>
> > Signed-off-by: Yicong Yang <yangyicong@hisilicon.com>
> > ---
> >  kernel/sched/fair.c | 46 +++++++++++++++++++++++++++++++++++++++++----
> >  1 file changed, 42 insertions(+), 4 deletions(-)
> >
> > diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
> > index 6e476f6d9435..8a5795c78af8 100644
> > --- a/kernel/sched/fair.c
> > +++ b/kernel/sched/fair.c
> > @@ -6230,12 +6230,46 @@ static inline int select_idle_smt(struct task_struct *p, struct sched_domain *sd
> >
> >  #endif /* CONFIG_SCHED_SMT */
> >
> > +#ifdef CONFIG_SCHED_CLUSTER
> > +/*
> > + * Scan the cluster domain for idle CPUs and clear cluster cpumask after scanning
> > + */
> > +static inline int scan_cluster(struct task_struct *p, int prev_cpu, int target)
> > +{
> > +       struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask);
> > +       struct sched_domain *sd = rcu_dereference(per_cpu(sd_cluster, target));
> > +       int cpu, idle_cpu;
> > +
> > +       /* TODO: Support SMT case while a machine with both cluster and SMT born */
> > +       if (!sched_smt_active() && sd) {
> > +               for_each_cpu_and(cpu, cpus, sched_domain_span(sd)) {
> > +                       idle_cpu = __select_idle_cpu(cpu, p);
> > +                       if ((unsigned int)idle_cpu < nr_cpumask_bits)
> > +                               return idle_cpu;
> > +               }
> > +
> > +               /* Don't ping-pong tasks in and out cluster frequently */
> > +               if (cpus_share_cluster(target, prev_cpu))
> > +                       return target;
> > +
> > +               cpumask_andnot(cpus, cpus, sched_domain_span(sd));
> > +       }
> > +
> > +       return -1;
> > +}
> > +#else
> > +static inline int scan_cluster(struct task_struct *p, int prev_cpu, int target)
> > +{
> > +       return -1;
> > +}
> > +#endif
> > +
> >  /*
> >   * Scan the LLC domain for idle CPUs; this is dynamically regulated by
> >   * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
> >   * average idle time for this rq (as found in rq->avg_idle).
> >   */
> > -static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool has_idle_core, int target)
> > +static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool has_idle_core, int prev_cpu, int target)
> >  {
> >         struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask);
> >         int i, cpu, idle_cpu = -1, nr = INT_MAX;
> > @@ -6250,6 +6284,10 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
> >
> >         cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr);
> >
> > +       idle_cpu = scan_cluster(p, prev_cpu, target);
>
> Why don't you do the above before calling select_idle_cpu() like for smt ?

yep. I did make two versions :
1. scan_cluster inside select_idle_cpu()
2. scan_cluster before select_idle_cpu()

Then I felt 1 looks a bit better than 2 as 2 requires changes in two
places. We still need to change
select_idle_cpu() to do cpumask_andnot() to exclude the cluster as we
have scanned the cluster
before select_idle_cpu(). In the first way, only one place is modified.

>
> > +       if ((unsigned int)idle_cpu < nr_cpumask_bits)
> > +               return idle_cpu;
> > +
> >         if (sched_feat(SIS_PROP) && !has_idle_core) {
> >                 u64 avg_cost, avg_idle, span_avg;
> >                 unsigned long now = jiffies;
> > @@ -6384,7 +6422,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
> >         /*
> >          * If the previous CPU is cache affine and idle, don't be stupid:
> >          */
> > -       if (prev != target && cpus_share_cache(prev, target) &&
> > +       if (prev != target && cpus_share_cluster(prev, target) &&
>
> This is misleading because cpus_share_cluster is meaningless for most of system

yep. The name is misleading though it hasn't side effects for
non-cluster systems as
cpus_share_cluster equals cpus_share_cache for them.
It seems this can be much better if we do the below as your comments
in patch1/2:

+bool cpus_share_resources(int this_cpu, int that_cpu)
+{
+     if (this_cpu == that_cpu)
+             return true;
+
+     return per_cpu(sd_share_id, this_cpu) == per_cpu(sd_share_id, that_cpu);
+}
+
which returns true when cpu shares resources which can be LLC or cluster

>
> Then, you don't care at all of the llc if there is a cluster domain ?

Basically, what I have seen is that in tbench, target is likely to be
idle especially when
the system load is not full. and waker and wakee are unlikely to run
in the same cluster
as fork has splitted them for LB. so we will almost always lose the
chance to pull them
together forever. This change made a big gain for tbench especially
for light and medium
load.

But Yicong did see some negative impact on benchmarks like pgbench. so
this change
is not a universal win. The difficulty might be how to make a decision
which can be
more aware of the hierarchy of two levels - cluster and llc, and aware
of the requirements
of the workload.

>
>
> >             (available_idle_cpu(prev) || sched_idle_cpu(prev)) &&
> >             asym_fits_capacity(task_util, prev))
> >                 return prev;
> > @@ -6408,7 +6446,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
> >         p->recent_used_cpu = prev;
> >         if (recent_used_cpu != prev &&
> >             recent_used_cpu != target &&
> > -           cpus_share_cache(recent_used_cpu, target) &&
> > +           cpus_share_cluster(recent_used_cpu, target) &&
> >             (available_idle_cpu(recent_used_cpu) || sched_idle_cpu(recent_used_cpu)) &&
> >             cpumask_test_cpu(p->recent_used_cpu, p->cpus_ptr) &&
> >             asym_fits_capacity(task_util, recent_used_cpu)) {
> > @@ -6449,7 +6487,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
> >                 }
> >         }
> >
> > -       i = select_idle_cpu(p, sd, has_idle_core, target);
> > +       i = select_idle_cpu(p, sd, has_idle_core, prev, target);
> >         if ((unsigned)i < nr_cpumask_bits)
> >                 return i;
> >
> > --
> > 2.33.0
> >

Thanks
Barry

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/2] sched: Add per_cpu cluster domain info and cpus_share_cluster API
  2021-12-22 13:33           ` Vincent Guittot
@ 2021-12-23 14:39             ` Gautham R. Shenoy
  -1 siblings, 0 replies; 20+ messages in thread
From: Gautham R. Shenoy @ 2021-12-23 14:39 UTC (permalink / raw)
  To: Vincent Guittot
  Cc: Barry Song, Yicong Yang, Peter Zijlstra, Ingo Molnar, Juri Lelli,
	Tim Chen, LKML, LAK, Dietmar Eggemann, Steven Rostedt,
	Ben Segall, Daniel Bristot de Oliveira, prime.zeng,
	Jonathan Cameron, ego, Srikar Dronamraju, Linuxarm, Barry Song,
	Guodong Xu

Hello Vincent, Barry,

On Wed, Dec 22, 2021 at 02:33:37PM +0100, Vincent Guittot wrote:
> On Wed, 22 Dec 2021 at 14:14, Vincent Guittot
[..snip..]

> > > > > @@ -669,6 +671,12 @@ static void update_top_cache_domain(int cpu)
> > > > >       per_cpu(sd_llc_id, cpu) = id;
> > > > >       rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds);
> > > > >
> > > > > +     sd = lowest_flag_domain(cpu, SD_CLUSTER);
> > > > > +     if (sd)
> > > > > +             id = cpumask_first(sched_domain_span(sd));
> > > > > +     rcu_assign_pointer(per_cpu(sd_cluster, cpu), sd);
> > > > > +     per_cpu(sd_cluster_id, cpu) = id;
> > > > > +
> >
> > This deserves a large comment to highlight that the new code above is
> > not self contained and  relies on the fact that it is done just after
> > looking for LLC and sd and that id and sd must not be changed in
> > between inorder to ensures that per_cpu(sd_cluster_id, cpu) equals
> > per_cpu(sd_llc_id, cpu) if there is no domain with SD_CLUSTER.
> >
> > and per_cpu(sd_cluster_id, cpu) might not be cluster but llc
> 
> Maybe you should not name this sd_cluster_id at all but
> per_cpu(sd_share_id, cpu) = id;
> 
> Then you have a function named
> 
> +bool cpus_share_resources(int this_cpu, int that_cpu)
> +{
> +     if (this_cpu == that_cpu)
> +             return true;
> +
> +     return per_cpu(sd_share_id, this_cpu) == per_cpu(sd_share_id, that_cpu);
> +}
> +
> which returns true when cpu shares resources which can be LLC or cluster

+1. This would make it more readable than overloading the meaning of cluster itself.

--
Thanks and Regards
gautham.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/2] sched: Add per_cpu cluster domain info and cpus_share_cluster API
@ 2021-12-23 14:39             ` Gautham R. Shenoy
  0 siblings, 0 replies; 20+ messages in thread
From: Gautham R. Shenoy @ 2021-12-23 14:39 UTC (permalink / raw)
  To: Vincent Guittot
  Cc: Barry Song, Yicong Yang, Peter Zijlstra, Ingo Molnar, Juri Lelli,
	Tim Chen, LKML, LAK, Dietmar Eggemann, Steven Rostedt,
	Ben Segall, Daniel Bristot de Oliveira, prime.zeng,
	Jonathan Cameron, ego, Srikar Dronamraju, Linuxarm, Barry Song,
	Guodong Xu

Hello Vincent, Barry,

On Wed, Dec 22, 2021 at 02:33:37PM +0100, Vincent Guittot wrote:
> On Wed, 22 Dec 2021 at 14:14, Vincent Guittot
[..snip..]

> > > > > @@ -669,6 +671,12 @@ static void update_top_cache_domain(int cpu)
> > > > >       per_cpu(sd_llc_id, cpu) = id;
> > > > >       rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds);
> > > > >
> > > > > +     sd = lowest_flag_domain(cpu, SD_CLUSTER);
> > > > > +     if (sd)
> > > > > +             id = cpumask_first(sched_domain_span(sd));
> > > > > +     rcu_assign_pointer(per_cpu(sd_cluster, cpu), sd);
> > > > > +     per_cpu(sd_cluster_id, cpu) = id;
> > > > > +
> >
> > This deserves a large comment to highlight that the new code above is
> > not self contained and  relies on the fact that it is done just after
> > looking for LLC and sd and that id and sd must not be changed in
> > between inorder to ensures that per_cpu(sd_cluster_id, cpu) equals
> > per_cpu(sd_llc_id, cpu) if there is no domain with SD_CLUSTER.
> >
> > and per_cpu(sd_cluster_id, cpu) might not be cluster but llc
> 
> Maybe you should not name this sd_cluster_id at all but
> per_cpu(sd_share_id, cpu) = id;
> 
> Then you have a function named
> 
> +bool cpus_share_resources(int this_cpu, int that_cpu)
> +{
> +     if (this_cpu == that_cpu)
> +             return true;
> +
> +     return per_cpu(sd_share_id, this_cpu) == per_cpu(sd_share_id, that_cpu);
> +}
> +
> which returns true when cpu shares resources which can be LLC or cluster

+1. This would make it more readable than overloading the meaning of cluster itself.

--
Thanks and Regards
gautham.

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2021-12-23 14:41 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-15  4:11 [PATCH 0/2] sched/fair: Wake task within the cluster when possible Yicong Yang
2021-12-15  4:11 ` Yicong Yang
2021-12-15  4:11 ` [PATCH 1/2] sched: Add per_cpu cluster domain info and cpus_share_cluster API Yicong Yang
2021-12-15  4:11   ` Yicong Yang
2021-12-22 10:24   ` Gautham R. Shenoy
2021-12-22 10:24     ` Gautham R. Shenoy
2021-12-22 11:06     ` Barry Song
2021-12-22 11:06       ` Barry Song
2021-12-22 13:14       ` Vincent Guittot
2021-12-22 13:14         ` Vincent Guittot
2021-12-22 13:33         ` Vincent Guittot
2021-12-22 13:33           ` Vincent Guittot
2021-12-23 14:39           ` Gautham R. Shenoy
2021-12-23 14:39             ` Gautham R. Shenoy
2021-12-15  4:11 ` [PATCH 2/2] sched/fair: Scan cluster before scanning LLC in wake-up path Yicong Yang
2021-12-15  4:11   ` Yicong Yang
2021-12-22 13:26   ` Vincent Guittot
2021-12-22 13:26     ` Vincent Guittot
2021-12-22 22:36     ` Barry Song
2021-12-22 22:36       ` Barry Song

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.