From: Qing Wang <wangqing7171@gmail.com>
To: peterz@infradead.org
Cc: bsegall@google.com, davichazbh@gmail.com,
dietmar.eggemann@arm.com, juri.lelli@redhat.com,
kprateek.nayak@amd.com, linux-kernel@vger.kernel.org,
mgorman@suse.de, mingo@redhat.com, rostedt@goodmis.org,
tim.c.chen@linux.intel.com, vincent.guittot@linaro.org,
vschneid@redhat.com, wangqing7171@gmail.com, yu.c.chen@intel.com
Subject: Re: [PATCH v3] sched/fair: Replace random newidle_balance with Bresenham accumulator
Date: Thu, 29 Oct 2026 16:41:10 +0800 [thread overview]
Message-ID: <20261029084110.2467458-1-wangqing7171@gmail.com> (raw)
In-Reply-To: <20261028090555.GF2668804@noisy.programming.kicks-ass.net>
Hi Peter,
Thanks for the review. The newidle balance throttling you added is a
really nice optimization. I only have a slightly different view on
how to implement it.
On Wed, 28 Oct 2026 at 17:05, Peter Zijlstra <peterz@infradead.org> wrote:
> On Wed, Oct 28, 2026 at 11:44:49AM +0800, Qing Wang wrote:
> > The current NI_RANDOM implementation uses a random dice roll to allow
> > newidle_balance attempts according to the success rate. There is a better
> > way to implement it.
>
> Well, sorta. So Bresenham is a linear algorithm; it doesn't (and cannot)
> properly deal with slope changes, and that is exactly what we do here.
>
> Your solution:
>
> > @@ -14198,6 +14198,7 @@ static inline void update_newidle_stats(struct sched_domain *sd, unsigned int su
> > ratio += sd->newidle_success;
> >
> > sd->newidle_ratio = min(1024, ratio);
> > + sd->newidle_window_pos = 0;
> > sd->newidle_call /= 2;
> > sd->newidle_success /= 2;
> > }
>
> is to simply delete the residual on slope change. If we were drawing
> actual lines (like Bresenham was), everybody would be yelling at you for
> creating jaggies.
>
> Now, we can rescale the residual, but realistically this probably
> doesn't matter.
Sorry, the "sd->newidle_window_pos = 0" in update_newidle_stats() is
a bug and I'll send a v4 with it removed. I wrongly assumed that
newidle_ratio is updated once every 1024 calls. Since newidle_call
is halved after each update, the update actually happens every 512
calls, and the reset discards the accumulated value before it can
reach 1024.
Right, Bresenham assumes a constant slope. But the only effect of a
slope change is that the first attempt after it may come a bit earlier
or later.
A simulation where the ratio jumps to a random value in [0, 1023]
every 512 calls (20000 windows, ~10M calls):
accumulator random dice
max per-window error 0.5 attempts 41 attempts (~16%)
> > Replace the random dice with a Bresenham accumulator that distributes the
> > allowed attempts uniformly across a 1024-step window:
> >
> > Each step does the following:
> > - Accumulate (1 + newidle_ratio) into newidle_window_pos.
> > - If the accumulator reaches 1024, allow the balance attempt and
> > subtract 1024.
> >
> > This guarantees exactly (1 + newidle_ratio) newidle_balance operations per
> > 1024 steps, uniformly spaced for any ratio in [0, 1023].
>
> Right, except you get the boundaries wrong. Also; as you mention, the
> new pattern is uniform, which might (but probably doesn't) matter.
I don't understand which boundaries you mean here. Could you spell
it out?
> But the thing is; you didn't call out any of this. Can you please update
> the Changelog to be explicit about these things and argue why it doesn't
> matter.
I'll add more explanation in the v4 changelog.
--
Best regards,
Qing
prev parent reply other threads:[~2026-10-29 8:41 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-28 3:44 Qing Wang
2026-10-28 9:01 ` Kayra Cizmeci
2026-10-29 7:30 ` Qing Wang
2026-10-29 8:41 ` Kayra Cizmeci
2026-10-29 9:31 ` Qing Wang
2026-10-28 9:05 ` Peter Zijlstra
2026-10-29 8:41 ` Qing Wang [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261029084110.2467458-1-wangqing7171@gmail.com \
--to=wangqing7171@gmail.com \
--cc=bsegall@google.com \
--cc=davichazbh@gmail.com \
--cc=dietmar.eggemann@arm.com \
--cc=juri.lelli@redhat.com \
--cc=kprateek.nayak@amd.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=tim.c.chen@linux.intel.com \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
--cc=yu.c.chen@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®