From: "Kenan.Liu" <Kenan.Liu@linux.alibaba.com>
To: mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com,
vincent.guittot@linaro.org, dietmar.eggemann@arm.com,
rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de,
bristot@redhat.com, vschneid@redhat.com
Cc: luoben@linux.alibaba.com, linux-kernel@vger.kernel.org
Subject: [RFC PATCH 1/2] sched/fair: Adjust CFS loadbalance for machine with qemu native CPU topology.
Date: Thu, 20 Jul 2023 16:34:12 +0800 [thread overview]
Message-ID: <1689842053-5291-2-git-send-email-Kenan.Liu@linux.alibaba.com> (raw)
In-Reply-To: <1689842053-5291-1-git-send-email-Kenan.Liu@linux.alibaba.com>
From: "Kenan.Liu" <Kenan.Liu@linux.alibaba.com>
Multithreading workloads in VM with Qemu may encounter an unexpected
phenomenon: one hyperthread of a physical core is busy while its sibling
is idle. The main reason is that hyperthread index is consecutive in qemu
native x86 CPU model which is different from the physical topology. As the
current kernel scheduler implementation, hyperthread with an even ID
number will be picked up in a much higher probability during load-balancing
and load-deploying. To solve the imbalance, when on a machine with multi
core and hyperthread index is consecutive per core, change the result of
select_idle_core() according to the hyperthread on which the task ran
before.
Signed-off-by: Kenan.Liu <Kenan.Liu@linux.alibaba.com>
Signed-off-by: Ben Luo <luoben@linux.alibaba.com>
---
kernel/sched/fair.c | 38 ++++++++++++++++++++++++++++++++++++--
1 file changed, 36 insertions(+), 2 deletions(-)
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index a80a739..ad7c93f 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -125,6 +125,9 @@
static unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
+static bool smt_neighbour_topo;
+static bool core_smt_topo_detect;
+static unsigned int smt_nr_cpu = 2;
int sched_thermal_decay_shift;
static int __init setup_sched_thermal_decay_shift(char *str)
@@ -140,6 +143,26 @@ static int __init setup_sched_thermal_decay_shift(char *str)
__setup("sched_thermal_decay_shift=", setup_sched_thermal_decay_shift);
#ifdef CONFIG_SMP
+static void explore_core_smp_topology(void)
+{
+ int cpu = smp_processor_id(), sibling;
+ const struct cpumask *smt_mask = cpu_smt_mask(cpu);
+
+ if (nr_cpu_ids <= 2)
+ return;
+
+ smt_nr_cpu = cpumask_weight(smt_mask);
+ if (smt_nr_cpu < 2)
+ return;
+
+ for_each_cpu(sibling, cpu_smt_mask(cpu)) {
+ if (cpu == sibling)
+ continue;
+ if (abs(cpu - sibling) == 1)
+ smt_neighbour_topo = true;
+ }
+}
+
/*
* For asym packing, by default the lower numbered CPU has higher priority.
*/
@@ -6887,9 +6910,16 @@ void __update_idle_core(struct rq *rq)
static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpus, int *idle_cpu)
{
bool idle = true;
- int cpu;
+ int cpu, sibling = core;
+
+ if (!core_smt_topo_detect) {
+ explore_core_smp_topology();
+ core_smt_topo_detect = true;
+ }
for_each_cpu(cpu, cpu_smt_mask(core)) {
+ if (cpu != core)
+ sibling = cpu;
if (!available_idle_cpu(cpu)) {
idle = false;
if (*idle_cpu == -1) {
@@ -6905,8 +6935,12 @@ static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpu
*idle_cpu = cpu;
}
- if (idle)
+ if (idle) {
+ if (!smt_neighbour_topo || unlikely(core % smt_nr_cpu))
+ return core;
+ core = task_cpu(p) % smt_nr_cpu ? core : sibling;
return core;
+ }
cpumask_andnot(cpus, cpus, cpu_smt_mask(core));
return -1;
--
1.8.3.1
next prev parent reply other threads:[~2023-07-20 9:00 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-20 8:34 [RFC PATCH 0/2] Adjust CFS loadbalance to adapt QEMU " Kenan.Liu
2023-07-20 8:34 ` Kenan.Liu [this message]
2023-07-20 8:34 ` [RFC PATCH 2/2] sched/fair: Export a param to control the traverse len when select idle cpu Kenan.Liu
2023-07-20 8:50 ` [RFC PATCH 0/2] Adjust CFS loadbalance to adapt QEMU CPU topology Peter Zijlstra
2023-07-21 2:58 ` Kenan.Liu
2023-07-21 8:33 ` Vincent Guittot
2023-07-21 9:13 ` Peter Zijlstra
2023-07-24 6:57 ` luoben
2023-07-24 14:07 ` Peter Zijlstra
2023-07-21 9:11 ` Peter Zijlstra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1689842053-5291-2-git-send-email-Kenan.Liu@linux.alibaba.com \
--to=kenan.liu@linux.alibaba.com \
--cc=bristot@redhat.com \
--cc=bsegall@google.com \
--cc=dietmar.eggemann@arm.com \
--cc=juri.lelli@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=luoben@linux.alibaba.com \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®