selftests/resctrl: fastcat for benchmarking counter reads
Subsystems (3)
PATCH 1/2 selftests/resctrl: fastcat for benchmarking counter reads
This patch is provided for reference and not intended for submission.
This test program is used to evaluate the cost of reading resctrl counters. It is essentially a drop-in replacement for cat, allowing presumably small files, like resctrl nodes, to be read repeatedly using pread(). In contrast, repeated invocations of cat would open and close the counter files, which is an order of magnitude more costly than the actual counter file read operations.
For example, the steps below were used to determine the cost of reading a large number of local or remote counters on a dual-socket AMD Zen2:
Bind to a CPU in L3 domain 6:
# taskset -c 25 bash
Record the setup cost for the groups /sys/fs/resctrl/mon_groups/*:
# cd /sys/fs/resctrl # FASTCAT_READ_COUNT=0 perf stat -r 1000 /tmp/fastcat mon_groups/*/mon_data/mon_L3_06/mbm_*
Measure local read cost:
# perf stat -r 1000 /tmp/fastcat mon_groups/*/mon_data/mon_L3_06/mbm_*
Measure remote read cost:
# perf stat -r 1000 /tmp/fastcat mon_groups/*/mon_data/mon_L3_07/mbm_*
--- tools/testing/selftests/resctrl/.gitignore | 1 + tools/testing/selftests/resctrl/Makefile | 6 ++- tools/testing/selftests/resctrl/fastcat.c | 63 ++++++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/resctrl/fastcat.c
tools/testing/selftests/resctrl/.gitignore +1 -0
@@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only resctrl_tests +fastcat
tools/testing/selftests/resctrl/Makefile +4 -2
@@ -3,10 +3,12 @@ CFLAGS = -g -Wall -O2 -D_FORTIFY_SOURCE=2 CFLAGS += $(KHDR_INCLUDES) -TEST_GEN_PROGS := resctrl_tests +TEST_GEN_PROGS := resctrl_tests fastcat LOCAL_HDRS += $(wildcard *.h) include ../lib.mk -$(OUTPUT)/resctrl_tests: $(wildcard *.c) +$(OUTPUT)/resctrl_tests: cache.c cat_test.c cmt_test.c fill_buf.c mba_test.c mbm_test.c resctrlfs.c resctrl_tests.c resctrl_val.c + +$(OUTPUT)/fastcat: fastcat.c
tools/testing/selftests/resctrl/fastcat.c new file +63 -0
63 lines not on this page · raw message
base-commit: 81983758430957d9a5cb3333fe324fd70cf63e7e
Signature · 1 line
2.47.0.199.ga7371fff76-goog
PATCH 2/2 x86/resctrl: Don't workqueue local event counter reads
Performance-conscious users may use threads bound to CPUs within a specific monitoring domain to ensure that all bandwidth counters can be read efficiently. The hardware counters are only accessible to CPUs within the domain, so requests from CPUs outside the domain are forwarded to a kernel worker or IPI handler, incurring a substantial performance penalty on each read. Recently, this penalty was observed to be paid by local reads as well.
To support blocking implementations of resctrl_arch_rmid_read(), mon_event_read() switched to smp_call_on_cpu() in most cases to read event counters using a kernel worker thread. Unlike smp_call_function_any(), which optimizes to a local function call when the calling CPU is in the target cpumask, smp_call_on_cpu() queues the work unconditionally.
Add a fast-path to ensure that requests bound to within the monitoring domain are read using a simple function call into mon_event_count() regardless of whether all CPUs in the target domain are using nohz_full.
This is significant when supporting configurations such as a dual-socket AMD Zen2, with 32 L3 monitoring domains and 256 RMIDs. To read both MBM counters for all groups on all domains requires 32768 (32*256*2) counter reads. The resolution of global, per-group MBM data which can be provided is therefore sensitive to the cost of each counter read. Furthermore, redirecting this much work to IPI handlers or worker threads at a regular interval is disruptive to the present workload.
The test program fastcat, which was introduced in an earlier path, was used to simulate the impact of this change on an optimized event counter-reading procedure. The goal is to maximize the frequency at which MBM counters can be dumped, so the benchmark determines the cost of an additional global MBM counter sample.
The total number of cycles needed to read all local and total MBM counters for a large number of monitoring groups was collected using the perf tool. The test was run bound to a single CPU: once targeting counters in the local domain and again for counters in a remote domain. The cost of a dry-run reading no counters was substracted from the total of each run to remove one-time setup costs.
AMD EPYC 7B12 64-Core Processor (250 mon groups)
Local Domain: 3.25M -> 1.22M (-62.5%) Remote Domain: 7.91M -> 8.05M (+2.9%)
Intel(R) Xeon(R) Gold 6268CL CPU @ 2.80GHz (190 mon groups)
Local Domain: 2.98M -> 2.21M (-25.8%) Remote Domain: 4.49M -> 4.62M (+3.1%)
Note that there is a small increase in overhead for remote domains, which results from the introduction of a put_cpu() call to reenable preemption after determining whether the fast path can be used. Users sensitive to this cost should consider avoiding the remote counter read penalty completely.
Also note that the Remote Domain results and the baseline Local Domain results only measure cycles in the test program. Because all counter reading work was carried out in kernel worker threads, the total system cost of the operation is greater.
--- arch/x86/kernel/cpu/resctrl/ctrlmondata.c | 28 +++++++++++++++++++++++ 1 file changed, 28 insertions(+)
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +28 -0
@@ -541,6 +541,31 @@ void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,return ; } +/* +* If a performance-conscious caller has gone to the trouble of binding +* their thread to the monitoring domain of the event counter, ensure +* that the counters are read directly. smp_call_on_cpu() +* unconditionally uses a work queue to read the counter, substantially +* increasing the cost of the read. +* +* Preemption must be disabled to prevent a migration out of the domain +* after the CPU is checked, which would result in reading the wrong +* counters. Note that this makes the (slow) remote path a little slower +* by requiring preemption to be reenabled when redirecting the request +* to another domain was in fact necessary. +* +* In the case where all eligible target CPUs are nohz_full and +* smp_call_function_any() is used, keep preemption disabled to avoid +* the cost of reenabling it twice in the same read. +*/ + cpu =get_cpu (); +if (cpumask_test_cpu (cpu, cpumask)) { +mon_event_count (rr); +resctrl_arch_mon_ctx_free (r, evtid, rr->arch_mon_ctx); +put_cpu (); +return ; + } + cpu =cpumask_any_housekeeping (cpumask,RESCTRL_PICK_ANY_CPU );/*
@@ -554,6 +579,9 @@ void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,else smp_call_on_cpu (cpu, smp_mon_event_count, rr, false); +/* If smp_call_function_any() was used, preemption is reenabled here. */ +put_cpu (); +resctrl_arch_mon_ctx_free (r, evtid, rr->arch_mon_ctx); }
Signature · 1 line
2.47.0.199.ga7371fff76-goog
RE: [PATCH 2/2] x86/resctrl: Don't workqueue local event counter reads
Quoted 3 lines
Add a fast-path to ensure that requests bound to within the monitoring
domain are read using a simple function call intomon_event_count()regardless of whether all CPUs in the target domain are usingnohz_full.
...
Quoted 3 lines
Fixes 09909e098113 ("x86/resctrl: Queue mon_event_read() instead of sending an IPI")Signed-off-by Peter Newman <[email protected]>
-Tony
Re: [PATCH 2/2] x86/resctrl: Don't workqueue local event counter reads
In reply to Peter NewmanHi, Peter,
Quoted 90 lines · Peter Newman
Performance-conscious users may use threads bound to CPUs within a specific monitoring domain to ensure that all bandwidth counters can be read efficiently. The hardware counters are only accessible to CPUs within the domain, so requests from CPUs outside the domain are forwarded to a kernel worker or IPI handler, incurring a substantial performance penalty on each read. Recently, this penalty was observed to be paid by local reads as well.
To support blocking implementations of
resctrl_arch_rmid_read(),mon_event_read()switched tosmp_call_on_cpu()in most cases to read event counters using a kernel worker thread. Unlikesmp_call_function_any(), which optimizes to a local function call when the calling CPU is in the target cpumask,smp_call_on_cpu()queues the work unconditionally.Add a fast-path to ensure that requests bound to within the monitoring domain are read using a simple function call into
mon_event_count()regardless of whether all CPUs in the target domain are usingnohz_full.This is significant when supporting configurations such as a dual-socket AMD Zen2, with 32 L3 monitoring domains and 256 RMIDs. To read both MBM counters for all groups on all domains requires 32768 (32*256*2) counter reads. The resolution of global, per-group MBM data which can be provided is therefore sensitive to the cost of each counter read. Furthermore, redirecting this much work to IPI handlers or worker threads at a regular interval is disruptive to the present workload.
The test program fastcat, which was introduced in an earlier path, was used to simulate the impact of this change on an optimized event counter-reading procedure. The goal is to maximize the frequency at which MBM counters can be dumped, so the benchmark determines the cost of an additional global MBM counter sample.
The total number of cycles needed to read all local and total MBM counters for a large number of monitoring groups was collected using the perf tool. The test was run bound to a single CPU: once targeting counters in the local domain and again for counters in a remote domain. The cost of a dry-run reading no counters was substracted from the total of each run to remove one-time setup costs.
AMD EPYC 7B12 64-Core Processor (250 mon groups)
Local Domain: 3.25M -> 1.22M (-62.5%) Remote Domain: 7.91M -> 8.05M (+2.9%)Intel(R) Xeon(R) Gold 6268CL CPU @ 2.80GHz (190 mon groups)
Local Domain: 2.98M -> 2.21M (-25.8%) Remote Domain: 4.49M -> 4.62M (+3.1%)Note that there is a small increase in overhead for remote domains, which results from the introduction of a
put_cpu()call to reenable preemption after determining whether the fast path can be used. Users sensitive to this cost should consider avoiding the remote counter read penalty completely.Also note that the Remote Domain results and the baseline Local Domain results only measure cycles in the test program. Because all counter reading work was carried out in kernel worker threads, the total system cost of the operation is greater.
Fixes 09909e098113 ("x86/resctrl: Queue mon_event_read() instead of sending an IPI")Signed-off-by Peter Newman <[email protected]>--- arch/x86/kernel/cpu/resctrl/ctrlmondata.c | 28 +++++++++++++++++++++++ 1 file changed, 28 insertions(+)arch/x86/kernel/cpu/resctrl/ctrlmondata.c +14
-0@@ -541,6 +541,31 @@ void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,return ; } +/* +* If a performance-conscious caller has gone to the trouble of binding +* their thread to the monitoring domain of the event counter, ensure +* that the counters are read directly. smp_call_on_cpu() +* unconditionally uses a work queue to read the counter, substantially +* increasing the cost of the read. +* +* Preemption must be disabled to prevent a migration out of the domain +* after the CPU is checked, which would result in reading the wrong +* counters. Note that this makes the (slow) remote path a little slower +* by requiring preemption to be reenabled when redirecting the request +* to another domain was in fact necessary. +* +* In the case where all eligible target CPUs are nohz_full and
arch/x86/kernel/cpu/resctrl/ctrlmondata.c void mon_event_read(struct rmid_read *rr, struct rdt_resource *r, · line 567
Quoted 10 lines
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +10
-0+* smp_call_function_any() is used, keep preemption disabled to avoid +* the cost of reenabling it twice in the same read. +*/ + cpu =get_cpu (); +if (cpumask_test_cpu (cpu, cpumask)) { +mon_event_count (rr); +resctrl_arch_mon_ctx_free (r, evtid, rr->arch_mon_ctx); +put_cpu (); +return ; + }
This fast path code is a duplicate part of smp_call_funcion_any().
In nohz_full() case, the fast path doesn't gain much and even hurts remote domain performance:
1. On local domain, it may gain a little bit because it has a few lines less than directly calling smp_call_function_any(). But the gain is minor due to a lines less code, not due to heavy weight queued work.
2. On remote domain, it degrades performance because get_cpu() and put_cpu() are both called twice: one in the fast path code and one in smp_call_function_any(). As you mentioned earlier, put_cpu() impacts performance. I think get_cpu() has same impact too.
The fast path only gains in none nohz_full() case.
So maybe it's better to move the fast path code into the non nohz_full() case? With this change, you may have the following benefits:
1. No performance impact on nohz_full() case (either local or remote domain).
2. Improve performance on non nohz_full() case as you intended in this patch.
3. The fast path focuses on fixing the right performance bottleneck.
Quoted 5 lines
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +1
-0+ cpu =cpumask_any_housekeeping (cpumask,RESCTRL_PICK_ANY_CPU );/*
arch/x86/kernel/cpu/resctrl/ctrlmondata.c void mon_event_read(struct rmid_read *rr, struct rdt_resource *r, · line 587
Quoted 9 lines
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +3
-0@@ -554,6 +579,9 @@ void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,else smp_call_on_cpu (cpu, smp_mon_event_count, rr, false); +/* If smp_call_function_any() was used, preemption is reenabled here. */ +put_cpu (); +resctrl_arch_mon_ctx_free (r, evtid, rr->arch_mon_ctx); }
Thanks.
-Fenghua
Luck, Tony
maintainerRE: [PATCH 2/2] x86/resctrl: Don't workqueue local event counter reads
Quoted 1 line
cpu = cpumask_any_housekeeping(cpumask, RESCTRL_PICK_ANY_CPU);
To a large degree Peter's is working around inefficiency in this housekeeping call.
Code may be running on a suitable CPU from the domain cpumask, but this call will very likely pick the first CPU in that mask, rather than the current one.
From that point it's all downhill unless you are lucky enough that the first CPU is a tick_nohz_full_cpu() one and you take the
smp_call_function_any(cpumask, mon_event_count, rr, 1);
path. It seems that on many systems you'll take the
smp_call_on_cpu(cpu, smp_mon_event_count, rr, false);
path and make a pointless IPI to get the data.
-Tony
Re: [PATCH 2/2] x86/resctrl: Don't workqueue local event counter reads
Hi, Tony,
Quoted 7 lines · Tony
Quoted 1 line
cpu = cpumask_any_housekeeping(cpumask, RESCTRL_PICK_ANY_CPU);To a large degree Peter's is working around inefficiency in this housekeeping call.
Code may be running on a suitable CPU from the domain cpumask, but this call will very likely pick the first CPU in that mask, rather than the current one.
Agree.
Quoted 5 lines
From that point it's all downhill unless you are lucky enough that the first
CPU is atick_nohz_full_cpu()one and you take thesmp_call_function_any(cpumask, mon_event_count, rr, 1);
Whenever this function is called, the performance is degraded rather than improved because extra get_cpu()/put_cpu() are called in the fast path in the current patch.
On platforms that have less housekeeping CPUs (e.g. a RT platform), there could be a higher chance that the first CPU is a nohz_full CPU and run smp_call_function_any().
Quoted 6 lines
path. It seems that on many systems you'll take the
smp_call_on_cpu(cpu, smp_mon_event_count, rr, false);path and make a pointless IPI to get the data.
Yes, that's right. But it's not conflicting with my suggested change.
What I suggested is to move the fast path code to this case only. So the fast path is always checked/called in both cases if condition is met:
1. It's already checked/called inside smp_call_function_any() in nohz_full case. No need to call out the fast path separately.
- No extra get_cpu() and put_cpu() are called.
- The performance is better than the current patch.
2. It's called out in non nohz_full case. No performance difference from the current patch.
Thanks.
-Fenghua
Luck, Tony
maintainerRE: [PATCH 2/2] x86/resctrl: Don't workqueue local event counter reads
Quoted 3 lines
Whenever this function is called, the performance is degraded rather than improved because extra get_cpu()/put_cpu() are called in the fast path in the current patch.
But get_cpu()/put_cpu() aren't high overhead. Maybe costs less that the cpumask_any_housekeeping() call that is avoided by Peter's patch.
Note that if Peter's patch doesn't take its fast path because the calling CPU was on the wrong domain, then the subsequent code is going to do an IPI whichever of the if/else path is taken.
-Tony
Re: [PATCH 2/2] x86/resctrl: Don't workqueue local event counter reads
Hi, Tony,
Quoted 6 lines · Tony
Quoted 3 lines
Whenever this function is called, the performance is degraded rather than improved because extra get_cpu()/put_cpu() are called in the fast path in the current patch.
But get_cpu()/put_cpu() aren't high overhead. Maybe costs less that the
cpumask_any_housekeeping()call that is avoided by Peter's patch.
Quote from Peter:
"AMD EPYC 7B12 64-Core Processor (250 mon groups)
Local Domain: 3.25M -> 1.22M (-62.5%) Remote Domain: 7.91M -> 8.05M (+2.9%)
Intel(R) Xeon(R) Gold 6268CL CPU @ 2.80GHz (190 mon groups)
Local Domain: 2.98M -> 2.21M (-25.8%) Remote Domain: 4.49M -> 4.62M (+3.1%)
Note that there is a small increase in overhead for remote domains, which results from the introduction of a put_cpu() call to reenable preemption after determining whether the fast path can be used."
As his data shows, if the fast path is not taken, the extra put_cpu() itself costs +2.9% extra time on AMD machine and +3.1% extra time on Intel machine.
And this ~3% overhead is on top of queued work, which is more expensive than cpumask_any_housekeeping() IIUC.
Quoted 4 lines
Note that if Peter's patch doesn't take its fast path because the calling CPU was on the wrong domain, then the subsequent code is going to do an IPI whichever of the if/else path is taken.
In this case, actually IPI is only taken in smp_call_function_any() and smp_call_on_cpu() invokes a queued work instead of IPI.
My proposed change logically doesn't change Peter's fast path and performance for nohz_full/smp_call_on_cpu() case. It just utilizes the "built-in fast path already" inside smp_call_function_any() to save extra get_cpu() and put_cpu(). Hopefully the saved extra get_cpu() and put_cpu() can offset cost of cpumask_any_housekeeping().
From Peter's commit message, seems nohz_full case is not called/measured a lot if any. If only one or a very few housekeeping CPUs on a large system, the nohz_full case will be called frequently and fast path will fail most of time and the extra get_cpu()/put_cpu() around the fast path might impact more on both local and total domain.
Thanks.
-Fenghua
Re: [PATCH 2/2] x86/resctrl: Don't workqueue local event counter reads
In reply to Fenghua YuHi Fenghua,
Quoted 46 lines · Fenghua Yu
Hi, Peter,
Quoted 22 lines · Peter Newman
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +14
-0@@ -541,6 +541,31 @@ void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,return ; } +/* +* If a performance-conscious caller has gone to the trouble of binding +* their thread to the monitoring domain of the event counter, ensure +* that the counters are read directly. smp_call_on_cpu() +* unconditionally uses a work queue to read the counter, substantially +* increasing the cost of the read. +* +* Preemption must be disabled to prevent a migration out of the domain +* after the CPU is checked, which would result in reading the wrong +* counters. Note that this makes the (slow) remote path a little slower +* by requiring preemption to be reenabled when redirecting the request +* to another domain was in fact necessary. +* +* In the case where all eligible target CPUs are nohz_full and arch/x86/kernel/cpu/resctrl/ctrlmondata.c void mon_event_read(struct rmid_read *rr, struct rdt_resource *r, · line 567
Quoted 10 lines
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +10
-0+* smp_call_function_any() is used, keep preemption disabled to avoid +* the cost of reenabling it twice in the same read. +*/ + cpu =get_cpu (); +if (cpumask_test_cpu (cpu, cpumask)) { +mon_event_count (rr); +resctrl_arch_mon_ctx_free (r, evtid, rr->arch_mon_ctx); +put_cpu (); +return ; + }This fast path code is a duplicate part of
smp_call_funcion_any().In
nohz_full()case, the fast path doesn't gain much and even hurts remote domain performance:
1. On local domain, it may gain a little bit because it has a few lines less than directly callingsmp_call_function_any(). But the gain is minor due to a lines less code, not due to heavy weight queued work.
Quoted 4 lines
2. On remote domain, it degrades performance because
get_cpu()andput_cpu()are both called twice: one in the fast path code and one insmp_call_function_any(). As you mentioned earlier,put_cpu()impacts performance. I thinkget_cpu()has same impact too.
get_cpu() and put_cpu() nest, so only the put_cpu() that reduces the preempt count to 0 will call into the scheduler. See the source comment I had added below.
But... note that below smp_call_on_cpu() is now called with preemption disabled. (Looks like I only benchmarked and never ran a debug build...) I will have to change the patch to make sure put_cpu() is called before smp_call_on_cpu().
Quoted 11 lines
The fast path only gains in none
nohz_full()case.So maybe it's better to move the fast path code into the non
nohz_full()case? With this change, you may have the following benefits:1. No performance impact on
nohz_full()case (either local or remote domain).
2. Improve performance on nonnohz_full()case as you intended in this patch.
3. The fast path focuses on fixing the right performance bottleneck.
The consequence of reusing the current-cpu-in-mask check in smp_call_function_any() is that if the check fails, it could cause resctrl_arch_rmid_read() to fail by invoking it in an IPI handler when it would have succeeded if invoked on a kernel worker, undoing James's original work.
-Peter
Quoted 19 lines
Quoted 6 lines
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +1
-0+ cpu =cpumask_any_housekeeping (cpumask,RESCTRL_PICK_ANY_CPU );/* arch/x86/kernel/cpu/resctrl/ctrlmondata.c void mon_event_read(struct rmid_read *rr, struct rdt_resource *r, · line 587
Quoted 9 lines
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +3
-0@@ -554,6 +579,9 @@ void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,else smp_call_on_cpu (cpu, smp_mon_event_count, rr, false); +/* If smp_call_function_any() was used, preemption is reenabled here. */ +put_cpu (); +resctrl_arch_mon_ctx_free (r, evtid, rr->arch_mon_ctx); }Thanks.
-Fenghua
Reinette Chatre
maintainerRe: [PATCH 2/2] x86/resctrl: Don't workqueue local event counter reads
Hi Peter,
Quoted 76 lines · Peter Newman
Hi Fenghua,
Quoted 46 lines · Fenghua Yu
Hi, Peter,
Quoted 22 lines · Peter Newman
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +14
-0@@ -541,6 +541,31 @@ void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,return ; } +/* +* If a performance-conscious caller has gone to the trouble of binding +* their thread to the monitoring domain of the event counter, ensure +* that the counters are read directly. smp_call_on_cpu() +* unconditionally uses a work queue to read the counter, substantially +* increasing the cost of the read. +* +* Preemption must be disabled to prevent a migration out of the domain +* after the CPU is checked, which would result in reading the wrong +* counters. Note that this makes the (slow) remote path a little slower +* by requiring preemption to be reenabled when redirecting the request +* to another domain was in fact necessary. +* +* In the case where all eligible target CPUs are nohz_full and arch/x86/kernel/cpu/resctrl/ctrlmondata.c void mon_event_read(struct rmid_read *rr, struct rdt_resource *r, · line 567
Quoted 10 lines
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +10
-0+* smp_call_function_any() is used, keep preemption disabled to avoid +* the cost of reenabling it twice in the same read. +*/ + cpu =get_cpu (); +if (cpumask_test_cpu (cpu, cpumask)) { +mon_event_count (rr); +resctrl_arch_mon_ctx_free (r, evtid, rr->arch_mon_ctx); +put_cpu (); +return ; + }This fast path code is a duplicate part of
smp_call_funcion_any().In
nohz_full()case, the fast path doesn't gain much and even hurts remote domain performance:
1. On local domain, it may gain a little bit because it has a few lines less than directly callingsmp_call_function_any(). But the gain is minor due to a lines less code, not due to heavy weight queued work.Quoted 4 lines
2. On remote domain, it degrades performance because
get_cpu()andput_cpu()are both called twice: one in the fast path code and one insmp_call_function_any(). As you mentioned earlier,put_cpu()impacts performance. I thinkget_cpu()has same impact too.
get_cpu()andput_cpu()nest, so only theput_cpu()that reduces the preempt count to 0 will call into the scheduler. See the source comment I had added below.But... note that below
smp_call_on_cpu()is now called with preemption disabled. (Looks like I only benchmarked and never ran a debug build...) I will have to change the patch to make sureput_cpu()is called beforesmp_call_on_cpu().Quoted 11 lines
The fast path only gains in none
nohz_full()case.So maybe it's better to move the fast path code into the non
nohz_full()case? With this change, you may have the following benefits:1. No performance impact on
nohz_full()case (either local or remote domain).
2. Improve performance on nonnohz_full()case as you intended in this patch.
3. The fast path focuses on fixing the right performance bottleneck.
Quoted 5 lines
The consequence of reusing the current-cpu-in-mask check in
smp_call_function_any()is that if the check fails, it could causeresctrl_arch_rmid_read()to fail by invoking it in an IPI handler when it would have succeeded if invoked on a kernel worker, undoing James's original work.
I think this change already undoes the motivation for 09909e098113 ("x86/resctrl: Queue mon_event_read() instead of sending an IPI")? As you mention in changelog the goal of that work was to enable resctrl_arch_rmid_read() to sleep. This change will call resctrl_arch_rmid_read() with preemption disabled if it happens to be called on CPU in monitoring domain. Would that not cause MPAM monitor count reads from CPU in domain to be a bug?
Could you please try out this patch with CONFIG_DEBUG_ATOMIC_SLEEP=y?
Reinette
Luck, Tony
maintainerRE: [PATCH 2/2] x86/resctrl: Don't workqueue local event counter reads
Quoted 8 lines
I think this change already undoes the motivation for
09909e098113("x86/resctrl: Queuemon_event_read()instead of sending an IPI")? As you mention in changelog the goal of that work was to enableresctrl_arch_rmid_read()to sleep. This change will callresctrl_arch_rmid_read()with preemption disabled if it happens to be called on CPU in monitoring domain. Would that not cause MPAM monitor count reads from CPU in domain to be a bug?Could you please try out this patch with
CONFIG_DEBUG_ATOMIC_SLEEP=y?
How is this all going to look after the split into fs/resctrl and arch/* ?
Is the file system code going to have implementation choices that prevent performance sensitive users like Peter from optimizing monitor event reads by binding the monitor process to a CPU in the right domain to avoid IPI?
-Tony
Reinette Chatre
maintainerRe: [PATCH 2/2] x86/resctrl: Don't workqueue local event counter reads
Hi Tony,
Quoted 10 lines · Tony
Quoted 8 lines
I think this change already undoes the motivation for
09909e098113("x86/resctrl: Queuemon_event_read()instead of sending an IPI")? As you mention in changelog the goal of that work was to enableresctrl_arch_rmid_read()to sleep. This change will callresctrl_arch_rmid_read()with preemption disabled if it happens to be called on CPU in monitoring domain. Would that not cause MPAM monitor count reads from CPU in domain to be a bug?Could you please try out this patch with
CONFIG_DEBUG_ATOMIC_SLEEP=y?How is this all going to look after the split into fs/resctrl and
arch/*?
Unclear to me at this point. Peter exposed an issue with current implementation and this needs to be fixed. Since this involves preparatory work that impacts systems currently supported we could also consider reverting to original behavior and go back to drawing board with the preparatory work.
Quoted 4 lines
Is the file system code going to have implementation choices that prevent performance sensitive users like Peter from optimizing monitor event reads by binding the monitor process to a CPU in the right domain to avoid IPI?
Apologies for not clearly stating it but I do agree that there is an issue that needs to be fixed.
My response was not intended to be interpreted as a NACK but instead an attempt to engage in discussion by pointing out that the proposed fix may not be ideal.
I tried out my own suggestion and indeed when just trying to mount resctrl on x86 with this patch applied results in:
BUG: scheduling while atomic
I do not object to optimizing monitor event reads but the proposed fix is not appropriate in its current form.
Reinette
Re: [PATCH 2/2] x86/resctrl: Don't workqueue local event counter reads
Hi Reinette,
Quoted 31 lines · Reinette Chatre
Hi Tony,
Quoted 10 lines · Tony
Quoted 8 lines
I think this change already undoes the motivation for
09909e098113("x86/resctrl: Queuemon_event_read()instead of sending an IPI")? As you mention in changelog the goal of that work was to enableresctrl_arch_rmid_read()to sleep. This change will callresctrl_arch_rmid_read()with preemption disabled if it happens to be called on CPU in monitoring domain. Would that not cause MPAM monitor count reads from CPU in domain to be a bug?Could you please try out this patch with
CONFIG_DEBUG_ATOMIC_SLEEP=y?How is this all going to look after the split into fs/resctrl and
arch/*?Unclear to me at this point. Peter exposed an issue with current implementation and this needs to be fixed. Since this involves preparatory work that impacts systems currently supported we could also consider reverting to original behavior and go back to drawing board with the preparatory work.
Quoted 4 lines
Is the file system code going to have implementation choices that prevent performance sensitive users like Peter from optimizing monitor event reads by binding the monitor process to a CPU in the right domain to avoid IPI?
Apologies for not clearly stating it but I do agree that there is an issue that needs to be fixed.
My response was not intended to be interpreted as a NACK but instead an attempt to engage in discussion by pointing out that the proposed fix may not be ideal.
Quoted 6 lines
I tried out my own suggestion and indeed when just trying to mount resctrl on x86 with this patch applied results in:
BUG: scheduling while atomicI do not object to optimizing monitor event reads but the proposed fix is not appropriate in its current form.
Yes, I mentioned the atomic sleep issues in my reply to Fenghua. I expect even the new case I added will have problems on resctrl_arch_rmid_read() implementations which block, since it would need to be called with preemption disabled to ensure invocation by a direct function call stays in the right domain. The last proposed MPAM resctrl_arch_rmid_read() implementation I saw can return an error when called in an atomic context, so that means my change would cause slow counters (i.e., MPAM CSU) to be unreadable when read from the local domain. (Or totally unreadable on a single-domain machine.)
As a refresher, the original issue that led to this situation was how an MPAM CSU (cache occupancy) monitor can be installed in response to a read request. The number of monitors is usually small (or just 1), so they need to be frequently installed, there can be access issues depending on what CPU wants to read which domain, and installing a monitor is a slow operation that requires waiting.
https://lore.kernel.org/lkml/[email protected]/
Thanks,
-Peter
Luck, Tony
maintainerRE: [PATCH 2/2] x86/resctrl: Don't workqueue local event counter reads
Quoted 6 lines
As a refresher, the original issue that led to this situation was how an MPAM CSU (cache occupancy) monitor can be installed in response to a read request. The number of monitors is usually small (or just 1), so they need to be frequently installed, there can be access issues depending on what CPU wants to read which domain, and installing a monitor is a slow operation that requires waiting.
Maybe the ARM implementation should adopt a similar approach to Babu's ABMC patches and provide an explicit mechanism to bind a h/w counter to an instance of a monitor event. Binding on demand when a user reads an event file seems awkward for users. Especially of the number of counters is >1, but unknown.
-Tony
Re: [PATCH 2/2] x86/resctrl: Don't workqueue local event counter reads
In reply to Peter NewmanHi Fenghua,
Quoted 77 lines · Peter Newman
Hi Fenghua,
Quoted 46 lines · Fenghua Yu
Hi, Peter,
Quoted 22 lines · Peter Newman
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +14
-0@@ -541,6 +541,31 @@ void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,return ; } +/* +* If a performance-conscious caller has gone to the trouble of binding +* their thread to the monitoring domain of the event counter, ensure +* that the counters are read directly. smp_call_on_cpu() +* unconditionally uses a work queue to read the counter, substantially +* increasing the cost of the read. +* +* Preemption must be disabled to prevent a migration out of the domain +* after the CPU is checked, which would result in reading the wrong +* counters. Note that this makes the (slow) remote path a little slower +* by requiring preemption to be reenabled when redirecting the request +* to another domain was in fact necessary. +* +* In the case where all eligible target CPUs are nohz_full and arch/x86/kernel/cpu/resctrl/ctrlmondata.c void mon_event_read(struct rmid_read *rr, struct rdt_resource *r, · line 567
Quoted 10 lines
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +10
-0+* smp_call_function_any() is used, keep preemption disabled to avoid +* the cost of reenabling it twice in the same read. +*/ + cpu =get_cpu (); +if (cpumask_test_cpu (cpu, cpumask)) { +mon_event_count (rr); +resctrl_arch_mon_ctx_free (r, evtid, rr->arch_mon_ctx); +put_cpu (); +return ; + }This fast path code is a duplicate part of
smp_call_funcion_any().In
nohz_full()case, the fast path doesn't gain much and even hurts remote domain performance:
1. On local domain, it may gain a little bit because it has a few lines less than directly callingsmp_call_function_any(). But the gain is minor due to a lines less code, not due to heavy weight queued work.Quoted 4 lines
2. On remote domain, it degrades performance because
get_cpu()andput_cpu()are both called twice: one in the fast path code and one insmp_call_function_any(). As you mentioned earlier,put_cpu()impacts performance. I thinkget_cpu()has same impact too.
get_cpu()andput_cpu()nest, so only theput_cpu()that reduces the preempt count to 0 will call into the scheduler. See the source comment I had added below.But... note that below
smp_call_on_cpu()is now called with preemption disabled. (Looks like I only benchmarked and never ran a debug build...) I will have to change the patch to make sureput_cpu()is called beforesmp_call_on_cpu().Quoted 11 lines
The fast path only gains in none
nohz_full()case.So maybe it's better to move the fast path code into the non
nohz_full()case? With this change, you may have the following benefits:1. No performance impact on
nohz_full()case (either local or remote domain).
2. Improve performance on nonnohz_full()case as you intended in this patch.
3. The fast path focuses on fixing the right performance bottleneck.
Quoted 5 lines
The consequence of reusing the current-cpu-in-mask check in
smp_call_function_any()is that if the check fails, it could causeresctrl_arch_rmid_read()to fail by invoking it in an IPI handler when it would have succeeded if invoked on a kernel worker, undoing James's original work.
I noted in my earlier email to Reinette that this is already a problem in the fast path case I added.
I ended up creating a resctrl_arch_event_read_blocks() hook for asking the implementation whether a particular event actually blocks so that the workqueue is only used when it's really necessary:
https://lore.kernel.org/lkml/[email protected]/
With this, I was able to follow your suggestion of using smp_call_function_any() for the local counter read.
However, it's worth noting that this ended up making remote reads faster on AMD but slower on Intel for some reason. I assume it's a flaw in my benchmark where the IPIs were cross-socket on Intel while they were only cross-chiplet on AMD.
Thanks!
-Peter