[PATCH] sched/psi: Skip CPUs with zero non-idle jiffies in per-cpu aggregation

Zhan Xusheng posted 1 patch 4 days, 5 hours ago
kernel/sched/psi.c | 7 +++++++
1 file changed, 7 insertions(+)
[PATCH] sched/psi: Skip CPUs with zero non-idle jiffies in per-cpu aggregation
Posted by Zhan Xusheng 4 days, 5 hours ago
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 <zhanxusheng@xiaomi.com>
---
 kernel/sched/psi.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/kernel/sched/psi.c b/kernel/sched/psi.c
index 59fdb7ebbf22..ce2321793a67 100644
--- a/kernel/sched/psi.c
+++ b/kernel/sched/psi.c
@@ -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++)
-- 
2.43.0
Re: [PATCH] sched/psi: Skip CPUs with zero non-idle jiffies in per-cpu aggregation
Posted by Johannes Weiner 3 days, 22 hours ago
On Tue, Feb 03, 2026 at 06:00:07PM +0800, Zhan Xusheng wrote:
> 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 <zhanxusheng@xiaomi.com>

Makes sense.

> ---
>  kernel/sched/psi.c | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/kernel/sched/psi.c b/kernel/sched/psi.c
> index 59fdb7ebbf22..ce2321793a67 100644
> --- a/kernel/sched/psi.c
> +++ b/kernel/sched/psi.c
> @@ -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] sched/psi: Skip CPUs with zero non-idle jiffies in per-CPU aggregation
Posted by Zhan Xusheng 3 days, 13 hours ago
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.

Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>
Reviewed-by: Johannes Weiner <hannes@cmpxchg.org>
---
 kernel/sched/psi.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/kernel/sched/psi.c b/kernel/sched/psi.c
index 59fdb7ebbf22..f1bf5449d3f9 100644
--- a/kernel/sched/psi.c
+++ b/kernel/sched/psi.c
@@ -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]);
-- 
2.43.0