Skip to content

Commit a6aadc8

Browse files
committed
sched/fair: Prefer the previous cpu for wakeup
EEVDF limited CPU saturation can be lack-luster with CPUs having many cores or CCDs. The provided patch improves limited saturation by 2%~21% with an AMD Threadripper 3970X CPU, depending on workload. I'm unqualified in the EEVDF internals. So, the patch was made by trial and error, plus much testing. It benefits CPU scheduling with the Threadripper machine. This is more an awareness for the folks in the CPU scheduler domain. From Andrea Righi The "is_idle_core" function Select target in select_idle_sibling(), if an idle core From Mario Roy Prefer the previous cpu for wakeup, if an idle core Move the recent_used_cpu heuristic to the top, and check if an idle core Results Tested with an AMD Ryzen Threadripper 3970X CPU (32/64) EEVDF, EEVDF with patch, scx_cosmos, and BMQ schedulers Pinned is affined to the primary CPUs, excluding siblings [6] cgterm_cpus 0-31 Cosmos is the sched-ext scheduler https://github.com/sched-ext/scx sudo scx_cosmos -m all -s 1250 -l 50000 -c 0 -p 0 -d :------------------------------------------------------------------- : pogocache million ops/sec [7][8] :------------------------------------------------------------------- FILE1=1.2.0.tar.gz; FILE2=pogocache-1.2.0.tar.gz wget https://github.com/tidwall/pogocache/archive/refs/tags/$FILE1 mv $FILE1 $FILE2 && tar xf $FILE2 && cd pogocache-1.2.0 make -j4; cd .. FILE1=2.2.0.tar.gz; FILE2=memtier_benchmark-2.2.0.tar.gz wget https://github.com/RedisLabs/memtier_benchmark/archive/refs/tags/$FILE1 mv $FILE1 $FILE2 && tar xf $FILE2 && cd memtier_benchmark-2.2.0 autoreconf -ivf; ./configure; make -j4; cd .. bash -c ' NUM_CPU_CORES=32 cd pogocache-1.2.0 ./pogocache -t $NUM_CPU_CORES & POGOCACHE_PID=$! sleep 3 cd ../memtier_benchmark-2.2.0 ./memtier_benchmark --hide-histogram -t $NUM_CPU_CORES \ -P memcache_text -c 1 -p 9401 --pipeline=16 --test-time=15 \ --ratio=1:1 sleep 1 kill $POGOCACHE_PID sleep 1 ' eevdf patch pinned cosmos bmq ------ ------ ------ ------ ------ 9.67 10.42 10.42 7.53 1.79 9.65 10.32 10.43 7.52 1.54 9.72 10.45 10.45 7.55 1.64 9.61 10.46 10.48 7.59 1.59 9.46 10.30 10.45 7.58 1.93 ------ ------ ------ ------ ------ 9.62 10.39 10.45 7.55 1.70 average million ops/sec ------ ------ ------ ------ ------ 100.0% 108.0% 108.6% 78.5% 17.7% higher is better :------------------------------------------------------------------- : x265 :------------------------------------------------------------------- FILE1=Bosphorus_1920x1080_120fps_420_8bit_YUV_Y4M.7z FILE2=Bosphorus_1920x1080_120fps_420_8bit_YUV.y4m wget --show-progress -Nq http://ultravideo.cs.tut.fi/video/$FILE1 7z e $FILE1 -o./ time x265 -p slow -b 6 -o /dev/null --no-progress \ --log-level none --input $FILE2 eevdf patch pinned cosmos bmq ------ ------ ------ ------ ------ 19.15 17.70 17.43 17.44 17.20 19.21 17.70 17.46 17.46 17.18 19.45 17.75 17.47 17.44 17.22 19.28 17.73 17.46 17.53 17.23 19.25 17.74 17.47 17.51 17.23 ------ ------ ------ ------ ------ 96.34 88.62 87.29 87.38 86.06 total time ------ ------ ------ ------ ------ 100.0% 92.0% 90.6% 90.7% 89.3% lower is better :------------------------------------------------------------------- : Encode 1920x1080 video with AOM [1] :------------------------------------------------------------------- Same input file as above time bash -c ' FILE=Bosphorus_1920x1080_120fps_420_8bit_YUV.y4m rm -f test.av1 time aomenc --threads=16 --cpu-used=6 -o test.av1 $FILE ' eevdf patch pinned cosmos bmq ------ ------ ------ ------ ------ 24.53 23.84 23.88 24.51 24.28 24.32 23.84 23.86 24.49 24.25 24.46 23.85 23.84 24.45 24.26 24.36 23.84 23.86 24.35 24.22 24.53 23.77 23.76 24.50 24.28 ------ ------ ------ ------ ------ 122.20 119.14 119.20 122.30 121.29 total time ------ ------ ------ ------ ------ 100.0% 97.5% 97.5% 100.1% 99.3% lower is better :------------------------------------------------------------------- : FFmpeg demux AV1 WebM to IVF that can be consumed by dav1d [1] :------------------------------------------------------------------- FILE=Stream2_AV1_4K_22.7mbps.webm wget http://www.phoronix-test-suite.com/benchmark-files/$FILE time bash -c ' FILE1=Stream2_AV1_4K_22.7mbps.webm FILE2=summer_nature_4k.ivf rm -f $FILE2 ffmpeg -hide_banner -i $FILE1 -vcodec copy -an -f ivf $FILE2 dav1d -i $FILE2 --muxer null --threads 16 --filmgrain 0 ' eevdf patch pinned cosmos bmq ------ ------ ------ ------ ------ 13.51 13.21 13.16 13.43 13.27 13.47 13.13 13.13 13.46 13.25 13.55 13.19 13.07 13.43 13.20 13.39 13.25 13.10 13.22 13.13 13.37 13.13 13.16 13.24 13.22 ------ ------ ------ ------ ------ 67.29 65.91 65.62 66.78 66.07 total time ------ ------ ------ ------ ------ 100.0% 97.9% 97.5% 99.2% 98.2% lower is better :------------------------------------------------------------------- : Query one million rows with SQLite [2] :------------------------------------------------------------------- cp -a sampledb /tmp/. && cd /tmp/sampledb ./create.pl 1000000 ./query1.pl | tail eevdf patch pinned cosmos bmq ------ ------ ------ ------ ------ 9.63 8.68 8.65 10.50 9.92 9.82 8.75 8.68 10.07 9.89 9.89 8.61 8.64 9.80 9.51 9.62 8.61 8.63 10.42 9.77 9.63 8.72 8.60 10.59 9.52 ------ ------ ------ ------ ------ 48.59 43.37 43.20 51.38 48.61 total time ------ ------ ------ ------ ------ 100.0% 89.3% 88.9% 105.7% 100.0% lower is better :------------------------------------------------------------------- : Chameneos-redux [3] :------------------------------------------------------------------- python3 pipe2.py 600000 eevdf patch pinned cosmos bmq ------ ------ ------ ------ ------ 7.00 6.26 6.23 7.07 7.27 6.85 6.19 6.25 6.92 6.39 6.83 6.19 6.24 7.15 6.25 7.00 6.20 6.19 7.30 7.46 6.89 6.25 6.23 7.18 6.11 ------ ------ ------ ------ ------ 34.57 31.09 31.14 35.62 33.48 total time ------ ------ ------ ------ ------ 100.0% 89.9% 90.1% 103.0% 96.8% lower is better :------------------------------------------------------------------- : Algorithm3 50% CPU Saturation [4] :------------------------------------------------------------------- ./algorithm3.pl 1e12 --threads=50% eevdf patch pinned cosmos bmq ------ ------ ------ ------ ------ 20.01 15.81 15.82 15.83 17.25 19.94 15.90 15.79 15.94 17.30 20.43 15.97 15.84 15.86 17.27 20.80 16.12 15.81 15.97 17.42 20.41 15.99 15.81 15.96 17.53 ------ ------ ------ ------ ------ 101.59 79.79 79.07 79.56 86.77 total time ------ ------ ------ ------ ------ 100.0% 78.5% 77.8% 78.3% 85.4% lower is better :------------------------------------------------------------------- : Algorithm3 31.25% CPU Saturation [4] :------------------------------------------------------------------- ./algorithm3.pl 1e12 --threads=31.25% eevdf patch pinned cosmos bmq ------ ------ ------ ------ ------ 29.70 23.74 23.79 24.36 24.08 30.19 23.99 23.80 24.57 23.87 30.26 24.16 23.88 24.59 24.33 30.26 23.93 23.95 24.69 24.33 30.63 24.14 23.92 24.37 24.20 ------ ------ ------ ------ ------ 151.04 119.96 119.34 122.58 120.81 total time ------ ------ ------ ------ ------ 100.0% 79.4% 79.0% 81.2% 80.0% lower is better :------------------------------------------------------------------- : Primesieve 31.25% CPU Saturation [5] :------------------------------------------------------------------- primesieve 2e12 --threads=20 eevdf patch pinned cosmos bmq ------ ------ ------ ------ ------ 18.57 18.30 18.51 18.97 18.41 18.57 18.37 18.45 18.60 18.43 18.51 18.34 18.35 18.81 18.55 18.35 18.41 18.36 18.81 18.42 18.33 18.37 18.40 19.05 18.37 ------ ------ ------ ------ ------ 92.33 91.79 92.07 94.24 92.18 total time ------ ------ ------ ------ ------ 100.0% 99.4% 99.7% 102.1% 99.8% lower is better [1] https://openbenchmarking.org/suite/pts/encoding [2] https://github.com/marioroy/mce-examples/tree/main/sampledb [3] https://github.com/marioroy/mce-examples/tree/main/chameneos [4] https://github.com/marioroy/mce-sandbox [5] https://github.com/kimwalisch/primesieve [6] https://github.com/marioroy/linux-cgroup-always [7] https://openbenchmarking.org/test/pts/pogocache [8] https://openbenchmarking.org/innhold/969a89ed33ff0f43dd5dbfdaa956a028c9d9e5d0 Signed-off-by: Mario Roy <> Signed-off-by: Eric Naim <[email protected]>
1 parent 536cd5f commit a6aadc8

1 file changed

Lines changed: 49 additions & 25 deletions

File tree

‎kernel/sched/fair.c‎

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ static bool update_deadline(struct cfs_rq *cfs_rq, struct sched_entity *se)
12421242

12431243
#include "pelt.h"
12441244

1245-
static int select_idle_sibling(struct task_struct *p, int prev_cpu, int cpu);
1245+
static int select_idle_sibling(struct task_struct *p, int prev_cpu, int cpu, int sync);
12461246
static unsigned long task_h_load(struct task_struct *p);
12471247
static unsigned long capacity_of(int cpu);
12481248

@@ -7132,7 +7132,7 @@ static int sched_idle_rq(struct rq *rq)
71327132

71337133
static int choose_sched_idle_rq(struct rq *rq, struct task_struct *p)
71347134
{
7135-
return sched_idle_rq(rq) && !task_has_idle_policy(p);
7135+
return sched_idle_rq(rq) && (!p || !task_has_idle_policy(p));
71367136
}
71377137

71387138
static int choose_idle_cpu(int cpu, struct task_struct *p)
@@ -7795,6 +7795,24 @@ static inline int __select_idle_cpu(int cpu, struct task_struct *p)
77957795
DEFINE_STATIC_KEY_FALSE(sched_smt_present);
77967796
EXPORT_SYMBOL_GPL(sched_smt_present);
77977797

7798+
/*
7799+
* Return true if all the CPUs in the SMT core where @cpu belongs are idle,
7800+
* false otherwise.
7801+
*/
7802+
static bool is_idle_core(int cpu, struct task_struct *p)
7803+
{
7804+
int sibling;
7805+
7806+
if (!sched_smt_active())
7807+
return choose_idle_cpu(cpu, p);
7808+
7809+
for_each_cpu(sibling, cpu_smt_mask(cpu))
7810+
if (!choose_idle_cpu(sibling, p))
7811+
return false;
7812+
7813+
return true;
7814+
}
7815+
77987816
static inline void set_idle_cores(int cpu, int val)
77997817
{
78007818
struct sched_domain_shared *sds;
@@ -8069,13 +8087,29 @@ static inline bool asym_fits_cpu(unsigned long util,
80698087
/*
80708088
* Try and locate an idle core/thread in the LLC cache domain.
80718089
*/
8072-
static int select_idle_sibling(struct task_struct *p, int prev, int target)
8090+
static int select_idle_sibling(struct task_struct *p, int prev, int target, int sync)
80738091
{
80748092
bool has_idle_core = false;
80758093
struct sched_domain *sd;
80768094
unsigned long task_util, util_min, util_max;
80778095
int i, recent_used_cpu, prev_aff = -1;
80788096

8097+
/* Check a recently used CPU as a potential idle candidate: */
8098+
recent_used_cpu = p->recent_used_cpu;
8099+
p->recent_used_cpu = prev;
8100+
if (recent_used_cpu != prev &&
8101+
recent_used_cpu != target &&
8102+
cpus_share_cache(recent_used_cpu, target) &&
8103+
choose_idle_cpu(recent_used_cpu, p) &&
8104+
cpumask_test_cpu(recent_used_cpu, p->cpus_ptr)) {
8105+
8106+
if ((unsigned int)recent_used_cpu < nr_cpumask_bits &&
8107+
is_idle_core(recent_used_cpu, p))
8108+
return recent_used_cpu;
8109+
} else {
8110+
recent_used_cpu = -1;
8111+
}
8112+
80798113
/*
80808114
* On asymmetric system, update task utilization because we will check
80818115
* that the task fits with CPU's capacity.
@@ -8092,7 +8126,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
80928126
*/
80938127
lockdep_assert_irqs_disabled();
80948128

8095-
if (choose_idle_cpu(target, p) &&
8129+
if (sync && is_idle_core(target, p) &&
80968130
asym_fits_cpu(task_util, util_min, util_max, target))
80978131
return target;
80988132

@@ -8126,24 +8160,6 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
81268160
return prev;
81278161
}
81288162

8129-
/* Check a recently used CPU as a potential idle candidate: */
8130-
recent_used_cpu = p->recent_used_cpu;
8131-
p->recent_used_cpu = prev;
8132-
if (recent_used_cpu != prev &&
8133-
recent_used_cpu != target &&
8134-
cpus_share_cache(recent_used_cpu, target) &&
8135-
choose_idle_cpu(recent_used_cpu, p) &&
8136-
cpumask_test_cpu(recent_used_cpu, p->cpus_ptr) &&
8137-
asym_fits_cpu(task_util, util_min, util_max, recent_used_cpu)) {
8138-
8139-
if (!static_branch_unlikely(&sched_cluster_active) ||
8140-
cpus_share_resources(recent_used_cpu, target))
8141-
return recent_used_cpu;
8142-
8143-
} else {
8144-
recent_used_cpu = -1;
8145-
}
8146-
81478163
/*
81488164
* For asymmetric CPU capacity systems, our domain of interest is
81498165
* sd_asym_cpucapacity rather than sd_llc.
@@ -8867,8 +8883,16 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)
88678883
return sched_balance_find_dst_cpu(sd, p, cpu, prev_cpu, sd_flag);
88688884

88698885
/* Fast path */
8870-
if (wake_flags & WF_TTWU)
8871-
return select_idle_sibling(p, prev_cpu, new_cpu);
8886+
if (wake_flags & WF_TTWU) {
8887+
/*
8888+
* If the previous CPU is an idle core, retain the same for
8889+
* cache locality. Otherwise, search for an idle sibling.
8890+
*/
8891+
if (is_idle_core(prev_cpu, p))
8892+
return prev_cpu;
8893+
8894+
return select_idle_sibling(p, prev_cpu, new_cpu, sync);
8895+
}
88728896

88738897
return new_cpu;
88748898
}
@@ -10545,7 +10569,7 @@ static bool sched_use_asym_prio(struct sched_domain *sd, int cpu)
1054510569
if (!sched_smt_active())
1054610570
return true;
1054710571

10548-
return sd->flags & SD_SHARE_CPUCAPACITY || is_core_idle(cpu);
10572+
return sd->flags & SD_SHARE_CPUCAPACITY || is_idle_core(cpu, NULL);
1054910573
}
1055010574

1055110575
static inline bool sched_asym(struct sched_domain *sd, int dst_cpu, int src_cpu)

0 commit comments

Comments
 (0)