sched/psi: Skip CPUs with zero non-idle jiffies in per-cpu aggregation
Subsystems (2)
PATCH 1/1 sched/psi: Skip CPUs with zero non-idle jiffies in per-cpu aggregation
PSI per-cpu aggregation weights each CPU's contribution by its non-idle time converted to jiffies. CPUs with zero non-idle jiffies do not contribute to the weighted result, but are still processed in the current implementation.
Skip CPUs with zero non-idle jiffies early to avoid unnecessary per-cpu arithmetic during aggregation.
No functional change intended.
--- kernel/sched/psi.c | 7 +++++++ 1 file changed, 7 insertions(+)
kernel/sched/psi.c +7 -0
@@ -387,6 +387,13 @@ static void collect_percpu_times(struct psi_group *group, changed_states |= cpu_changed_states; nonidle =nsecs_to_jiffies (times[PSI_NONIDLE ]); +/* +* A CPU with zero non-idle jiffies does not contribute to the +* weighted per-CPU aggregation. There is no need to include it +* in deltas or total accumulation. +*/ +if (!nonidle) +continue ; nonidle_total += nonidle;for (s =0 ; s <PSI_NONIDLE ; s++)
Signature · 1 line
2.43.0
Johannes Weiner
maintainerQuoted 11 lines · Zhan Xusheng
PSI per-cpu aggregation weights each CPU's contribution by its non-idle time converted to jiffies. CPUs with zero non-idle jiffies do not contribute to the weighted result, but are still processed in the current implementation.
Skip CPUs with zero non-idle jiffies early to avoid unnecessary per-cpu arithmetic during aggregation.
No functional change intended.
Signed-off-by Zhan Xusheng <[email protected]>
Makes sense.
Quoted 9 lines
--- kernel/sched/psi.c | 7 +++++++ 1 file changed, 7 insertions(+)kernel/sched/psi.c +0
-0
kernel/sched/psi.c static void collect_percpu_times(struct psi_group *group, · line 396
Quoted 10 lines
kernel/sched/psi.c +7
-0@@ -387,6 +387,13 @@ static void collect_percpu_times(struct psi_group *group, changed_states |= cpu_changed_states; nonidle =nsecs_to_jiffies (times[PSI_NONIDLE ]); +/* +* A CPU with zero non-idle jiffies does not contribute to the +* weighted per-CPU aggregation. There is no need to include it +* in deltas or total accumulation. +*/ +if (!nonidle) +continue ;
You could save the nsecs_to_jiffies() dance as well by doing:
if (!(cpu_changed_states & (1 << PSI_NONIDLE))) coninue;
PATCH v2 1/1 sched/psi: Skip CPUs with zero non-idle jiffies in per-CPU aggregation
To improve performance during per-CPU aggregation, skip CPUs that have zero non-idle jiffies early in the process. These CPUs do not contribute to the weighted result and can be excluded from the per-CPU calculations. The change directly checks the cpu_changed_states for PSI_NONIDLE instead of performing unnecessary arithmetic.
No functional change intended.
--- kernel/sched/psi.c | 7 +++++++ 1 file changed, 7 insertions(+)
kernel/sched/psi.c +7 -0
@@ -384,6 +384,13 @@ static void collect_percpu_times(struct psi_group *group,get_recent_times (group, cpu, aggregator, times, &cpu_changed_states); +/* +* Skip CPUs with no non-idle time. These CPUs do not contribute +* to the weighted per-CPU aggregation, so we can avoid unnecessary +* calculations for them by checking cpu_changed_states. +*/ +if (!(cpu_changed_states & (1 <<PSI_NONIDLE ))) +continue ; changed_states |= cpu_changed_states; nonidle =nsecs_to_jiffies (times[PSI_NONIDLE ]);
Signature · 1 line
2.43.0
PATCH v3 1/1 sched/psi: Skip CPUs with zero non-idle jiffies in per-CPU aggregation
To improve performance during per-CPU aggregation, skip CPUs that have zero non-idle jiffies early in the process. These CPUs do not contribute to the weighted result and can be excluded from the per-CPU calculations. The change directly checks the cpu_changed_states for PSI_NONIDLE instead of performing unnecessary arithmetic.
No functional change intended.
--- kernel/sched/psi.c | 7 +++++++ 1 file changed, 7 insertions(+)
kernel/sched/psi.c +7 -0
@@ -384,6 +384,13 @@ static void collect_percpu_times(struct psi_group *group,get_recent_times (group, cpu, aggregator, times, &cpu_changed_states); +/* +* Skip CPUs with no non-idle time. These CPUs do not contribute +* to the weighted per-CPU aggregation, so we can avoid unnecessary +* calculations for them by checking cpu_changed_states. +*/ +if (!(cpu_changed_states & (1 <<PSI_NONIDLE ))) +continue ; changed_states |= cpu_changed_states; nonidle =nsecs_to_jiffies (times[PSI_NONIDLE ]);
Signature · 1 line
2.43.0
PATCH v4 1/1 sched/psi: Skip CPUs with zero non-idle delta in per-CPU aggregation
In reply to Johannes Weinercollect_percpu_times() iterates over every possible CPU to build a non-idle-weighted average of the PSI state times. When a CPU has no PSI_NONIDLE delta for the current sampling interval:
nonidle = nsecs_to_jiffies(times[PSI_NONIDLE]) = 0
deltas[s] += times[s] * nonidle /* += 0 */
so the weighted accumulation contributes nothing.
get_recent_times() already sets the PSI_NONIDLE bit in cpu_changed_states iff the PSI_NONIDLE delta is non-zero. Use that bit to skip such CPUs early, as suggested by Johannes, avoiding the nsecs_to_jiffies() call.
No functional change intended.
---
v4:
- Drop the incorrect Reviewed-by added in v2/v3; replace with Suggested-by. Johannes' "Makes sense." on v1 was an acknowledgement and an implementation suggestion, not a review tag.
- Rebase commit message wording to describe "PSI_NONIDLE delta" rather than "non-idle jiffies", matching the actual check.
v3: https://lore.kernel.org/all/[email protected]/
- Resend of v2.
v2: https://lore.kernel.org/all/[email protected]/
- Use cpu_changed_states & (1 << PSI_NONIDLE) per Johannes' suggestion, saving the nsecs_to_jiffies() call.
v1: https://lore.kernel.org/all/[email protected]/
--- kernel/sched/psi.c | 7 +++++++ 1 file changed, 7 insertions(+)
kernel/sched/psi.c +7 -0
@@ -384,6 +384,13 @@ static void collect_percpu_times(struct psi_group *group,get_recent_times (group, cpu, aggregator, times, &cpu_changed_states); +/* +* If this CPU's PSI_NONIDLE delta is zero, it contributes +* nothing to nonidle_total or to any deltas[] entry below, +* so skip it early. +*/ +if (!(cpu_changed_states & (1 <<PSI_NONIDLE ))) +continue ; changed_states |= cpu_changed_states; nonidle =nsecs_to_jiffies (times[PSI_NONIDLE ]);
Signature · 1 line
2.43.0
Peter Zijlstra
maintainerRe: [PATCH v4] sched/psi: Skip CPUs with zero non-idle delta in per-CPU aggregation
Quoted 6 lines · Zhan Xusheng
collect_percpu_times()iterates over every possible CPU to build a non-idle-weighted average of the PSI state times. When a CPU has noPSI_NONIDLEdelta for the current sampling interval:
nonidle =nsecs_to_jiffies(times[PSI_NONIDLE])= 0
deltas[s] += times[s] * nonidle /* += 0 */
Quoted 8 lines
so the weighted accumulation contributes nothing.
get_recent_times()already sets thePSI_NONIDLEbit incpu_changed_statesiff thePSI_NONIDLEdelta is non-zero. Use that bit to skip such CPUs early, as suggested by Johannes, avoiding thensecs_to_jiffies()call.No functional change intended.
So presumably this is an optimization. Where is the data that justifies this?
PATCH v5 1/1 sched/psi: Skip CPUs with zero non-idle delta in per-CPU aggregation
collect_percpu_times() iterates over every possible CPU to build a non-idle-weighted average of the PSI state times. When a CPU has no PSI_NONIDLE delta for the current sampling interval:
nonidle = nsecs_to_jiffies(times[PSI_NONIDLE]) = 0
deltas[s] += times[s] * nonidle /* += 0 */
so the weighted accumulation contributes nothing.
get_recent_times() already sets the PSI_NONIDLE bit in cpu_changed_states iff the PSI_NONIDLE delta is non-zero. Use that bit to skip such CPUs early, as suggested by Johannes, avoiding the nsecs_to_jiffies() call and the PSI_NONIDLE * u64 mul-adds that follow.
No functional change: on the skipped path the old code adds zero to deltas[] and zero to nonidle_total, which is exactly the result of not iterating.
Measured on i7-8700 (6C/12T), same mainline base and same build flags for both kernels. Reader is a pinned userspace loop of open()+read()+close() on /proc/pressure/cpu, 100k iterations inside a KVM guest with -smp matching the host LCPU count (12):
baseline patched diff idle p50 2438 ns 2270 ns -6.9% idle p99 2598 ns 2449 ns -5.7% 1 busy / 12 p50 2479 ns 2281 ns -8.0% all 12 busy p50 3738 ns 3537 ns -5.4%
The all-busy improvement shows the skip also kicks in when the box is hot: between two samples, many CPUs record no PSI_NONIDLE state transition even if they've been 100% utilised.
--- kernel/sched/psi.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/sched/psi.c +3 -0
@@ -386,6 +386,9 @@ static void collect_percpu_times(struct psi_group *group, &cpu_changed_states); changed_states |= cpu_changed_states; +if (!(cpu_changed_states & (1 <<PSI_NONIDLE ))) +continue ; + nonidle =nsecs_to_jiffies (times[PSI_NONIDLE ]); nonidle_total += nonidle;
Signature · 1 line
2.43.0
PATCH v6 1/1 sched/psi: Skip CPUs with zero non-idle delta in per-CPU aggregation
collect_percpu_times() iterates over every possible CPU to build a non-idle-weighted average of the PSI state times. When a CPU has no PSI_NONIDLE delta for the current sampling interval:
nonidle = nsecs_to_jiffies(times[PSI_NONIDLE]) = 0
deltas[s] += times[s] * nonidle /* += 0 */
so the weighted accumulation contributes nothing.
get_recent_times() already sets the PSI_NONIDLE bit in cpu_changed_states iff the PSI_NONIDLE delta is non-zero. Use that bit to skip such CPUs early, as suggested by Johannes, avoiding the nsecs_to_jiffies() call and the PSI_NONIDLE * u64 mul-adds that follow.
No functional change: on the skipped path the old code adds zero to deltas[] and zero to nonidle_total, which is exactly the result of not iterating. The PSI_NONIDLE bit is folded into changed_states before the skip, so the aggregator's reschedule decision is unaffected.
The cost is worth trimming because collect_percpu_times() is O(nr_possible_cpus) per group and is not only called from the 2s averaging work: when a PSI trigger is armed, psi_rtpoll_work() calls it at the trigger cadence. On a 12-thread host with a "some 50000 500000" trigger armed, bpftrace measured collect_percpu_times() running ~160 times/s (vs ~0.5/s on the averaging path), and on a partially loaded box a large fraction of the per-CPU iterations per call had no PSI_NONIDLE delta and are skipped.
read(/proc/pressure/cpu) median latency, QEMU/KVM A/B on mainline v7.1-rc2+ (identical config, the patch the only difference), idle guest, 100k iterations, varying -smp:
-smp baseline patched delta
2 1834 ns 1799 ns -1.9%
4 1957 ns 1897 ns -3.1%
8 2204 ns 2103 ns -4.6%
12 2369 ns 2272 ns -4.1%
16 2596 ns 2363 ns -9.0%
24 3083 ns 2677 ns -13.2%
32 3740 ns 3050 ns -18.4%The saving scales with CPU count, toward the many-CPU systems that run pressure-monitoring agents. (The high -smp points use KVM oversubscription on a 12-thread host and carry scheduling noise; the trend is the signal. The all-busy case is within run-to-run noise.)
---
Changes in v6:
- Add the benchmark data requested on v4: the -smp scaling A/B table and the psi_rtpoll_work() call-frequency measurement. No code change vs v5.
v5: reword changelog (consistency rationale; "No functional change")
https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
kernel/sched/psi.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/sched/psi.c +3 -0
@@ -386,6 +386,9 @@ static void collect_percpu_times(struct psi_group *group, &cpu_changed_states); changed_states |= cpu_changed_states; +if (!(cpu_changed_states & (1 <<PSI_NONIDLE ))) +continue ; + nonidle =nsecs_to_jiffies (times[PSI_NONIDLE ]); nonidle_total += nonidle;
Signature · 1 line
2.43.0