[RFC PATCH 09/11] sched: Add per CPU variable sd_socket_id to specify the CPU's socket

From: Yuyang Du
Date: Thu Jun 16 2016 - 05:46:50 EST


Add an unique ID per socket (we use the first CPU number in the
cpumask of the socket). This allows us to quickly tell if two CPUs are
in the same socket, see cpus_share_socket().

Signed-off-by: Yuyang Du <yuyang.du@xxxxxxxxx>
---
include/linux/sched.h | 5 +++++
kernel/sched/core.c | 16 ++++++++++++++--
kernel/sched/sched.h | 1 +
3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/include/linux/sched.h b/include/linux/sched.h
index 0803abd..4b2a0fa 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1158,6 +1158,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_socket(int this_cpu, int that_cpu);

typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
typedef int (*sched_domain_flags_f)(void);
@@ -1206,6 +1207,10 @@ static inline bool cpus_share_cache(int this_cpu, int that_cpu)
return true;
}

+static inline bool cpus_share_socket(int this_cpu, int that_cpu)
+{
+ return true;
+}
#endif /* !CONFIG_SMP */


diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 56ac8f1..0a332ed 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -1859,6 +1859,11 @@ 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);
}
+
+bool cpus_share_socket(int this_cpu, int that_cpu)
+{
+ return per_cpu(sd_socket_id, this_cpu) == per_cpu(sd_socket_id, that_cpu);
+}
#endif /* CONFIG_SMP */

static void ttwu_queue(struct task_struct *p, int cpu, int wake_flags)
@@ -5875,10 +5880,15 @@ static void destroy_sched_domains(struct sched_domain *sd)
* Also keep a unique ID per domain (we use the first cpu number in
* the cpumask of the domain), this allows us to quickly tell if
* two cpus are in the same cache domain, see cpus_share_cache().
+
+ * And also keep a unique ID per socket (we use the first cpu number
+ * in the cpumask of the socket), this allows us to quickly tell if
+ * two cpus are in the same socket, see cpus_share_socket().
*/
DEFINE_PER_CPU(struct sched_domain *, sd_llc);
DEFINE_PER_CPU(int, sd_llc_size);
DEFINE_PER_CPU(int, sd_llc_id);
+DEFINE_PER_CPU(int, sd_socket_id);
DEFINE_PER_CPU(struct sched_domain_shared *, sd_llc_shared);
DEFINE_PER_CPU(struct sched_domain *, sd_numa);
DEFINE_PER_CPU(struct sched_domain *, sd_asym);
@@ -5887,8 +5897,7 @@ static void update_top_cache_domain(int cpu)
{
struct sched_domain_shared *sds = NULL;
struct sched_domain *sd;
- int id = cpu;
- int size = 1;
+ int id = cpu, size = 1, socket_id = 0;

sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES);
if (sd) {
@@ -5904,6 +5913,9 @@ static void update_top_cache_domain(int cpu)

sd = lowest_flag_domain(cpu, SD_NUMA);
rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
+ if (sd)
+ socket_id = cpumask_first(sched_group_cpus(sd->groups));
+ per_cpu(sd_socket_id, cpu) = socket_id;

sd = highest_flag_domain(cpu, SD_ASYM_PACKING);
rcu_assign_pointer(per_cpu(sd_asym, cpu), sd);
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index df27200..654bc65 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -856,6 +856,7 @@ static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
DECLARE_PER_CPU(struct sched_domain *, sd_llc);
DECLARE_PER_CPU(int, sd_llc_size);
DECLARE_PER_CPU(int, sd_llc_id);
+DECLARE_PER_CPU(int, sd_socket_id);
DECLARE_PER_CPU(struct sched_domain_shared *, sd_llc_shared);
DECLARE_PER_CPU(struct sched_domain *, sd_numa);
DECLARE_PER_CPU(struct sched_domain *, sd_asym);
--
1.7.9.5