sched: delayed thread migration
Subsystems (5)
cover sched: delayed thread migration
This patch series proposes a tweak in the thread placement strategy of CFS to make a better use of cores running at a high frequency. It is the result of a published work at ATC'20, available here:
https://www.usenix.org/conference/atc20/presentation/gouicern
We address the frequency inversion problem that stems from new DVFS capabilities of CPUs where frequency can be different among cores of the same chip. On applications with a large number of fork/wait patterns, we see that CFS tends to place new threads on idle cores running at a low frequency, while cores where the parent is running are at a high frequency and become idle immediately after. This means that a low frequency core is used while a high frequency core becomes idle. A more detailed analysis of this behavior is available in the paper but here is small example with 2 cores c0 and c1 and 2 threads A and B.
Legend: - means high frequency
. means low frequency
nothing means nothing is running.
c0: A------fork()-wait()
\
c1: B................--------
c0 becomes idle while at a high frequency and c1 becomes busy and executes B at a low frequency for a long time.
The fix we propose is to delay the migration of threads in order to see if the parent's core can be used quickly before using another core. When choosing a core to place a new or waking up thread (in select_task_rq_fair()), we check if the destination core is idle or busy frequency-wise. If it is busy, we keep the current behavior and move the thread to this new core. If it is idle, we place the thread locally (parent's core on fork, waker's core on wakeup). We also arm a high-resolution timer that will trigger a migration of this thread to the new core chosen by the original algorithm of CFS. If the thread is able to run quickly on the local core, we cancel the timer (e.g. the parent thread calls the wait() syscall). Else, the thread will be moved to the core decided by CFS and run quickly. When the parent thread waits, the previous example becomes:
c0: A------fork()-wait()-B---------------
\/
c1:
If the parent thread keeps running, the example becomes:
c0: A------fork()---------timer------------
\/ \
c1: B.............--------
In the first case, we use a high frequency core (c0) and let c1 be idle. In the second case, we loose a few us of execution time for B.
There are two configurable parameters in this patch:
- the frequency threshold above which we consider a core busy frequency-wise. By default, the threshold is set to 0, which disables the delayed migrations. On our test server (an Intel Xeon E7-8870 v4), an efficient value was the minimal frequency of the CPU. On another test machine (AMD Ryzen 5 3400G), it was a frequency a little bit higher than the minimal one.
- the delay of the high-resolution timer. We choose a default value of 50 us since it is the mean delay we observed between a fork and a wait during a kernel build.
Both parameters can be modified through files in /proc/sys/kernel/. Since this is kind of work in progress, we are still looking to find better ways to choose the best threshold and delay, as both values probably depend on the machine.
In terms of performance, we benchmarked 60 applications on a 5.4 kernel at the time of the paper's publication. The detailed results are available in the paper but overall, we improve the performance of 23 applications, with a best improvement of 56%. Only 3 applications suffer large performance degradations (more than 5%), but the degradation is "small" (at most 8%). We also think there might be energy savings since we try to avoid waking idle cores. We did see small improvements on our machines, but nothing as good as in terms of performance (at most 5% improvement).
On the current development kernel (on the sched/core branch, commit f166b111e049), we run a few of these benchmarks to confirm our results on an up-to-date kernel. We run the build of the kernel (complete and scheduler only), hackbench, 2 applications from the NAS benchmark suite and openssl. We select these applications because they had either very good or very bad results on the 5.4 kernel. We present the mean of 10 runs, with the powersave scaling governor and the intel_pstate driver in active mode.
Benchmark | unit | 5.9-f166b111e049 | patched
-------------+------+------------------+------------------
LOWER IS BETTER
-------------+------+------------------+------------------
hackbench | s | 5.661 | 5.612 (- 0.9%) kbuild-sched | s | 7.370 | 6.309 (-14.4%) kbuild | s | 34.798 | 34.788 (- 0.0%) nas_ft.C | s | 10.080 | 10.012 (- 0.7%) nas_sp.B | s | 6.653 | 7.033 (+ 5.7%)
-------------+------+------------------+------------------
HIGHER IS BETTER
-------------+------+------------------+------------------
openssl |sign/s| 15086.89 | 15071.08 (- 0.1%)
On this small subset, the only application negatively impacted application is an HPC workload, so it is less of a problem since HPC people usually bypass the scheduler altogether. We still have a large beneficial impact on the scheduler build that proeminently has the fork/wait pattern and frequency inversions.
The first patch of the series is not specific to scheduling. It allows us (or anyone else) to use the cpufreq infrastructure at a different sampling rate without compromising the cpufreq subsystem and applications that depend on it.
The main idea behind this patch series is to bring to light the frequency inversion problem that will become more and more prominent with new CPUs that feature per-core DVFS. The solution proposed is a first idea for solving this problem that still needs to be tested across more CPUs and with more applications.
Redha Gouicem (3):
cpufreq: x86: allow external frequency measures
sched: core: x86: query frequency at each tick sched/fair: delay thread migration on fork/wakeup/exec
arch/x86/kernel/cpu/aperfmperf.c | 31 ++++++++++++++++++++--- drivers/cpufreq/cpufreq.c | 5 ++++ include/linux/cpufreq.h | 1 + include/linux/sched.h | 4 +++ include/linux/sched/sysctl.h | 3 +++ kernel/sched/core.c | 43 ++++++++++++++++++++++++++++++++ kernel/sched/fair.c | 42 ++++++++++++++++++++++++++++++- kernel/sched/sched.h | 7 ++++++ kernel/sysctl.c | 14 +++++++++++ 9 files changed, 145 insertions(+), 5 deletions(-)
Signature · 1 line
2.28.0
PATCH 1/3 cpufreq: x86: allow external frequency measures
Allow other subsystems to query the current frequency from the CPU without affecting the cached value of cpufreq. The subsystems doing this will need to maintain their own version of struct aperfmperf_sample.
This is useful if you need to query frequency more frequently than APERFMPERF_CACHE_THRESHOLD_MS but don't want to mess with the current caching behavior.
Even though querying too frequently may render the measures inaccurate, it is still useful if your subsystem tolerates this inaccuracy.
--- arch/x86/kernel/cpu/aperfmperf.c | 31 +++++++++++++++++++++++++++---- drivers/cpufreq/cpufreq.c | 5 +++++ include/linux/cpufreq.h | 1 + 3 files changed, 33 insertions(+), 4 deletions(-)
arch/x86/kernel/cpu/aperfmperf.c +27 -4
@@ -36,11 +36,11 @@ static DEFINE_PER_CPU(struct aperfmperf_sample, samples);* unless we already did it within 10ms * calculate kHz, save snapshot */ -static void aperfmperf_snapshot_khz (void *dummy) +static void aperfmperf_snapshot_khz (void *prev_sample) {u64 aperf, aperf_delta;u64 mperf, mperf_delta; -struct aperfmperf_sample *s =this_cpu_ptr (&samples); +struct aperfmperf_sample *s = prev_sample;unsigned long flags;local_irq_save (flags);
@@ -72,7 +72,8 @@ static bool aperfmperf_snapshot_cpu(int cpu, ktime_t now, bool wait)if (time_delta <APERFMPERF_CACHE_THRESHOLD_MS )return true; -smp_call_function_single (cpu, aperfmperf_snapshot_khz,NULL , wait); +smp_call_function_single (cpu, aperfmperf_snapshot_khz, +per_cpu_ptr (&samples, cpu), wait);/* Return false if the previous iteration was too long ago. */ return time_delta <=APERFMPERF_STALE_THRESHOLD_MS ;
@@ -131,7 +132,29 @@ unsigned int arch_freq_get_on_cpu(int cpu)return per_cpu (samples.khz, cpu);msleep (APERFMPERF_REFRESH_DELAY_MS ); -smp_call_function_single (cpu, aperfmperf_snapshot_khz,NULL ,1 ); +smp_call_function_single (cpu, aperfmperf_snapshot_khz, +per_cpu_ptr (&samples, cpu),1 );return per_cpu (samples.khz, cpu); } + +unsigned int arch_freq_get_on_cpu_from_sample (int cpu,void *sample) +{ +struct aperfmperf_sample *s = sample; + +if (!sample) +return 0 ; + +if (!cpu_khz) +return 0 ; + +if (!boot_cpu_has (X86_FEATURE_APERFMPERF )) +return 0 ; + +if (!housekeeping_cpu (cpu,HK_FLAG_MISC )) +return 0 ; + +smp_call_function_single (cpu, aperfmperf_snapshot_khz, s,1 ); + +return s->khz; +}
drivers/cpufreq/cpufreq.c +5 -0
@@ -695,6 +695,11 @@ __weak unsigned int arch_freq_get_on_cpu(int cpu)return 0 ; } +__weak unsigned int arch_freq_get_on_cpu_from_sample (int cpu,void *sample) +{ +return 0 ; +} +static ssize_t show_scaling_cur_freq (struct cpufreq_policy *policy,char *buf) {ssize_t ret;
include/linux/cpufreq.h +1 -0
@@ -1005,6 +1005,7 @@ static inline void sched_cpufreq_governor_change(struct cpufreq_policy *policy,extern void arch_freq_prepare_all (void );extern unsigned int arch_freq_get_on_cpu (int cpu); +extern unsigned int arch_freq_get_on_cpu_from_sample (int cpu,void *sample);extern void arch_set_freq_scale (struct cpumask *cpus,unsigned long cur_freq,unsigned long max_freq);
Signature · 1 line
2.28.0
PATCH 2/3 sched: core: x86: query frequency at each tick
In reply to Redha GouicemQuery the current frequency of the core during the scheduler tick. The scheduler subsystem maintains its own copies of the aperf/mperf structure because it will query the frequency more frequently than what the cpufreq subsystem does. This can lead to inaccurate measurements, but it is not problematic here.
--- kernel/sched/core.c | 11 +++++++++++ kernel/sched/sched.h | 4 ++++ 2 files changed, 15 insertions(+)
kernel/sched/core.c +11 -0
@@ -3976,6 +3976,15 @@ unsigned long long task_sched_runtime(struct task_struct *p)return ns; } +struct freq_sample { +unsigned int khz; +ktime_t time; +u64 aperf; +u64 mperf; +}; + +DEFINE_PER_CPU(struct freq_sample, freq_sample); +/* * This function gets called by the timer code, with HZ frequency. * We call it with interrupts disabled.
@@ -3996,6 +4005,8 @@ void scheduler_tick(void)update_rq_clock (rq); thermal_pressure =arch_scale_thermal_pressure (cpu_of (rq));update_thermal_load_avg (rq_clock_thermal (rq), rq, thermal_pressure); + rq->freq =arch_freq_get_on_cpu_from_sample (cpu, +this_cpu_ptr (&freq_sample)); curr->sched_class->task_tick (rq, curr,0 );calc_global_load_tick (rq);psi_task_tick (rq);
kernel/sched/sched.h +4 -0
@@ -1048,6 +1048,10 @@ struct rq {/* Must be inspected within a rcu lock section */ struct cpuidle_state *idle_state;#endif + +/* Frequency measured at the last tick */ +unsigned int freq; + };#ifdef CONFIG_FAIR_GROUP_SCHED
Signature · 1 line
2.28.0
kernel test robot
build botRe: [PATCH 2/3] sched: core: x86: query frequency at each tick
Thank you for the patch! Yet something to improve:
Hi Redha,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on tip/sched/core]
[also build test ERROR on pm/linux-next tip/x86/core linus/master v5.9 next-20201016] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Redha-Gouicem/cpufreq-x86-allow-external-frequency-measures/20201021-004445 base: https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git feff2e65efd8d84cf831668e182b2ce73c604bbb config: riscv-nommu_k210_defconfig (attached as .config) compiler: riscv64-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/188307dbf909fd1f2ed97b8db21a793b7057d794
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Redha-Gouicem/cpufreq-x86-allow-external-frequency-measures/20201021-004445 git checkout 188307dbf909fd1f2ed97b8db21a793b7057d794
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=riscv
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
riscv64-linux-ld: kernel/sched/core.o: in function `.L667':
>> core.c:(.text+0x2344): undefined reference to `arch_freq_get_on_cpu_from_sample'
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botRe: [PATCH 2/3] sched: core: x86: query frequency at each tick
In reply to Redha GouicemThank you for the patch! Yet something to improve:
Hi Redha,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on tip/sched/core]
[also build test ERROR on pm/linux-next tip/x86/core linus/master v5.9 next-20201016] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Redha-Gouicem/cpufreq-x86-allow-external-frequency-measures/20201021-004445 base: https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git feff2e65efd8d84cf831668e182b2ce73c604bbb config: parisc-defconfig (attached as .config) compiler: hppa-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/188307dbf909fd1f2ed97b8db21a793b7057d794
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Redha-Gouicem/cpufreq-x86-allow-external-frequency-measures/20201021-004445 git checkout 188307dbf909fd1f2ed97b8db21a793b7057d794
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=parisc
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
hppa-linux-ld: kernel/sched/core.o: in function `scheduler_tick':
>> (.text+0x4d8c): undefined reference to `arch_freq_get_on_cpu_from_sample'
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
PATCH 3/3 sched/fair: delay thread migration on fork/wakeup/exec
In reply to Redha GouicemOn CPUs that implement per-core frequency scaling (and not per-socket), it is beneficial to try to use cores running at a higher frequency. One way to do this on fork/exec/wakeup is to keep the targeted thread on its parent/previous/waker core, since it should already be running at a high frequency.
This is how this works:
- choose the new_cpu as usual in select_task_rq_fair()
- if we are in a fork/exec/wakeup and new_cpu runs at a low frequency, start a timer in 50us and place the
thread on its parent/previous/waker core
- if the thread is scheduled, cancel the timer
- if the timer expires, migrate the thread to new_cpu
This way, if the previous cpu is too busy to be used, the thread will use another cpu. This is particularly useful in fork/wait patterns where the child thread is placed on an idle core (low frequency) and the parent thread waits and makes its core idle (high frequency). This patch avoids using a low frequency core if a higher frquency one is available.
There are two configuration parameters for this feature:
- the frequency threshold under which we consider the core to be running at a low frequency, in kHz (/proc/sys/kernel/sched_lowfreq). By default, this is set to 0, which disables the delayed thread migration feature.
- the delay of the timer, in ns (/proc/sys/kernel/sched_delayed_placement)
--- include/linux/sched.h | 4 ++++ include/linux/sched/sysctl.h | 3 +++ kernel/sched/core.c | 32 +++++++++++++++++++++++++++ kernel/sched/fair.c | 42 +++++++++++++++++++++++++++++++++++- kernel/sched/sched.h | 3 +++ kernel/sysctl.c | 14 ++++++++++++ 6 files changed, 97 insertions(+), 1 deletion(-)
include/linux/sched.h +4 -0
@@ -662,6 +662,10 @@ struct task_struct {unsigned long wakee_flip_decay_ts;struct task_struct *last_wakee; +/* Delayed placement */ +struct hrtimer delay_placement_timer; +int delay_placement_cpu; +/* * recent_used_cpu is initially set as the last CPU used by a task * that wakes affine another task. Waker/wakee relationships can
include/linux/sched/sysctl.h +3 -0
@@ -52,6 +52,9 @@ int sched_proc_update_handler(struct ctl_table *table, int write, void *buffer, size_t *length, loff_t *ppos);#endif +extern __read_mostly unsigned int sysctl_sched_delayed_placement; +extern __read_mostly unsigned int sysctl_sched_lowfreq; +/* * control realtime throttling: *
kernel/sched/core.c +32 -0
@@ -3217,6 +3217,33 @@ int sysctl_schedstats(struct ctl_table *table, int write, void *buffer,static inline void init_schedstats (void ) {}#endif /* CONFIG_SCHEDSTATS */ +static enum hrtimer_restart delayed_placement_fn (struct hrtimer *data) +{ + struct task_struct *p = container_of(data, struct task_struct, + delay_placement_timer); +struct rq *rq; +struct rq_flags rf; +bool queued, running; + +/* +* If, by chance, p was already migrated to this cpu, no need to do +* anything. This can happen because of load balancing for example. +*/ +if (task_cpu (p) == p->delay_placement_cpu) +return HRTIMER_NORESTART ; + + rq =task_rq_lock (p, &rf); + + queued =task_on_rq_queued (p); + running =task_current (rq, p); +if (queued && !running) + rq =__migrate_task (rq, &rf, p, p->delay_placement_cpu); + +task_rq_unlock (rq, p, &rf); + +return HRTIMER_NORESTART ; +} +/* * fork()/clone()-time setup: */
@@ -3299,6 +3326,11 @@ int sched_fork(unsigned long clone_flags, struct task_struct *p)plist_node_init (&p->pushable_tasks,MAX_PRIO );RB_CLEAR_NODE (&p->pushable_dl_tasks);#endif + +hrtimer_init (&p->delay_placement_timer,CLOCK_MONOTONIC , +HRTIMER_MODE_REL ); + p->delay_placement_timer.function = delayed_placement_fn; +return 0 ; }
kernel/sched/fair.c +41 -1
72 lines not on this page · raw message
kernel/sched/sched.h +3 -0
@@ -2018,6 +2018,9 @@ extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags); extern const_debug unsigned int sysctl_sched_nr_migrate; extern const_debug unsigned int sysctl_sched_migration_cost; +extern unsigned int sysctl_sched_delayed_placement; +extern unsigned int sysctl_sched_lowfreq; +#ifdef CONFIG_SCHED_HRTICK /*
kernel/sysctl.c +14 -0
@@ -1712,6 +1712,20 @@ static struct ctl_table kern_table[] = { .mode = 0644, .proc_handler = proc_dointvec, }, + { + .procname = "sched_delayed_placement", + .data = &sysctl_sched_delayed_placement, + .maxlen = sizeof(unsigned int), + .mode = 0644, + .proc_handler = proc_dointvec, + }, + { + .procname = "sched_lowfreq", + .data = &sysctl_sched_lowfreq, + .maxlen = sizeof(unsigned int), + .mode = 0644, + .proc_handler = proc_dointvec, + }, #ifdef CONFIG_SCHEDSTATS { .procname = "sched_schedstats",
Signature · 1 line
2.28.0
Peter Zijlstra
maintainerQuoted 5 lines · Redha Gouicem
The first patch of the series is not specific to scheduling. It allows us (or anyone else) to use the cpufreq infrastructure at a different sampling rate without compromising the cpufreq subsystem and applications that depend on it.
It's also completely redudant as the scheduler already reads aperf/mperf on every tick. Clearly you didn't do your homework ;-)
Quoted 5 lines
The main idea behind this patch series is to bring to light the frequency inversion problem that will become more and more prominent with new CPUs that feature per-core DVFS. The solution proposed is a first idea for solving this problem that still needs to be tested across more CPUs and with more applications.
Which is why schedutil (the only cpufreq gov anybody should be using) is integrated with the scheduler and closes the loop and tells the CPU about the expected load.
Quoted 7 lines · Peter Zijlstra
Quoted 4 lines · Redha Gouicem
The first patch of the series is not specific to scheduling. It allows us (or anyone else) to use the cpufreq infrastructure at a different sampling rate without compromising the cpufreq subsystem and applications that depend on it.
It's also completely redudant as the scheduler already reads aperf/mperf on every tick. Clearly you didn't do your homework ;-)
My bad. I worked on this a year ago, just never got time to submit to the lkml and I should have re-done my homework more thoroughly before submitting. The paper was submitted approximately at the same time as the patch introducing support frequency invariance and frequency reading at every tick (1 week apart!)
Again, my bad.
Quoted 10 lines
Quoted 6 lines
The main idea behind this patch series is to bring to light the frequency inversion problem that will become more and more prominent with new CPUs that feature per-core DVFS. The solution proposed is a first idea for solving this problem that still needs to be tested across more CPUs and with more applications.
Which is why schedutil (the only cpufreq gov anybody should be using) is integrated with the scheduler and closes the loop and tells the CPU about the expected load.
While I agree that schedutil is probably a good option, I'm not sure we treat exactly the same problem. schedutil aims at mapping the frequency of the CPU to the actual load. What I'm saying is that since it takes some time for the frequency to match the load, why not account for the frequency when making placement/migration decisions. I know that with the frequency invariance code, capacity accounts for frequency, which means that thread placement decisions do account for frequency indirectly. However, we still have performance improvements with our patch for the workloads with fork/wait patterns. I really believe that we can still gain performance if we make decisions while accounting for the frequency more directly.
Peter Zijlstra
maintainerQuoted 9 lines · Redha
Quoted 9 lines
Quoted 5 lines
The main idea behind this patch series is to bring to light the frequency inversion problem that will become more and more prominent with new CPUs that feature per-core DVFS. The solution proposed is a first idea for solving this problem that still needs to be tested across more CPUs and with more applications.
Which is why schedutil (the only cpufreq gov anybody should be using) is integrated with the scheduler and closes the loop and tells the CPU about the expected load.
Quoted 5 lines
While I agree that schedutil is probably a good option, I'm not sure we treat exactly the same problem. schedutil aims at mapping the frequency of the CPU to the actual load. What I'm saying is that since it takes some time for the frequency to match the load, why not account for the frequency when making placement/migration decisions.
Because overhead, mostly :/ EAS does some of that. Mostly wakeup CPU selection is already a bottle-neck for some applications (see the fight over select_idle_sibling()).
Programming a timer is out of budget for high rate wakeup workloads. Worse, you also don't prime the CPU to ramp up during the enforced delay.
Also, two new config knobs :-(
Quoted 6 lines
I know that with the frequency invariance code, capacity accounts for frequency, which means that thread placement decisions do account for frequency indirectly. However, we still have performance improvements with our patch for the workloads with fork/wait patterns. I really believe that we can still gain performance if we make decisions while accounting for the frequency more directly.
So I don't think that's fundamentally a DVFS problem though, just something that's exacerbated by it. There's a long history with trying to detect this pattern, see for example WF_SYNC and wake_affine().
(we even had some code in the early CFS days that measured overlap between tasks, to account for the period between waking up the recipient and blocking on the answer, but that never worked reliably either, so that went the way of the dodo)
The classical micro-benchmark is pipe-bench, which ping-pongs a single byte between two tasks over a pipe. If you run that on a single CPU it is _much_ faster then when the tasks get split up. DVFS is just like caches here, yet another reason.
schedutil does solve the problem where, when we migrate a task, the CPU would have to individually re-learn the DVFS state. By using the scheduler statistics we can program the DVFS state up-front, on migration. Instead of waiting for it.
So from that PoV, schedutil fundamentally solves the individual DVFS problem as best is possible. It closes the control loop; we no longer have individually operating control loops that are unaware of one another.