Re: [PATCH 4/4] sched/fair: Proportional newidle balance
From: Peter Zijlstra
Date: Tue Nov 11 2025 - 04:26:18 EST
On Tue, Nov 11, 2025 at 05:07:45PM +0800, Adam Li wrote:
> > @@ -12843,6 +12858,22 @@ static int sched_balance_newidle(struct
> > break;
> >
> > if (sd->flags & SD_BALANCE_NEWIDLE) {
> > + unsigned int weight = 1;
> > +
> > + if (sched_feat(NI_RANDOM)) {
> > + /*
> > + * Throw a 1k sided dice; and only run
> > + * newidle_balance according to the success
> > + * rate.
> > + */
> > + u32 d1k = sched_rng() % 1024;
> > + weight = 1 + sd->newidle_ratio;
> > + if (d1k > weight) {
> > + update_newidle_stats(sd, 0);
> > + continue;
> > + }
> > + weight = (1024 + weight/2) / weight;
> > + }
> >
> e.g: Why 'weight = (1024 + weight/2) / weight'
Not sure what you're asking, so two answers:
That's a rounding divide. We have a helper for that, but I never can
remember what its called.
The transformation as a whole here is from a ratio to a weight, suppose
our ratio is 256, this means that we do 1-in-4 or 25% of the balance
calls. However this also means that each success needs to be weighted as
4 (=1024/256), otherwise we under-account the successes and not even a
100% success rate can lift you out the hole.
Now, I made it a rounding divide to make it a little easier to climb out
of said hole (I even considered ceiling divide).