public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Ingo Molnar <mingo@elte.hu>
To: "Stephan Bärwolf" <stephan.baerwolf@tu-ilmenau.de>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	Nikhil Rao <ncrao@google.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Mike Galbraith <efault@gmx.de>,
	"Nikunj A. Dadhania" <nikunj@linux.vnet.ibm.com>,
	Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] sched: fix/optimise calculation of weight-inverse
Date: Wed, 11 May 2011 18:20:30 +0200	[thread overview]
Message-ID: <20110511162030.GA2638@elte.hu> (raw)
In-Reply-To: <4DCAB351.4010204@tu-ilmenau.de>


* Stephan Bärwolf <stephan.baerwolf@tu-ilmenau.de> wrote:

> If the inverse loadweight should be zero, function "calc_delta_mine"
> calculates the inverse of "lw->weight" (in 32bit integer ops).
> 
> This calculation is actually a little bit impure (because it is
> inverting something around "lw-weight"+1), especially when
> "lw->weight" becomes smaller. (This could explain some aritmetical
> issues for small shares...)
> 
> The correct inverse would be 1/lw->weight multiplied by
> "WMULT_CONST" for fixcomma-scaling it into integers.
> (So WMULT_CONST/lw->weight ...)
> 
> For safety it is also important to check if division by zero
> could happen...
> 
> The old, impure algorithm took two divisions for inverting lw->weight,
> the new, more exact one only takes one and an additional unlikely-if.
> 
> Signed-off-by: Stephan Baerwolf <stephan.baerwolf@tu-ilmenau.de>
> ---
>  kernel/sched.c |   12 +++++++++---
>  1 files changed, 9 insertions(+), 3 deletions(-)
> 
> diff --git a/kernel/sched.c b/kernel/sched.c
> index 312f8b9..bb55996 100644
> --- a/kernel/sched.c
> +++ b/kernel/sched.c
> @@ -1307,15 +1307,21 @@ calc_delta_mine(unsigned long delta_exec,
> unsigned long weight,
>  {
>      u64 tmp;
>  
> +    tmp = (u64)delta_exec * weight;
> +
> +    // actually we would have to trap - division by zero - but we stay
> and pretend the limit of the operation...
> +    if (unlikely(lw->weight == 0)) {
> +        if (unlikely(tmp == ((u64)0))) return (unsigned long)0;
> +        else return (unsigned long)LONG_MAX;

Can lw->weight ever be zero here? I dont think so - and if it is then getting a 
kernel crash there is preferred to hiding it.

Once we do that your patch becomes a lot simpler.

> +    }
> +
>      if (!lw->inv_weight) {
>          if (BITS_PER_LONG > 32 && unlikely(lw->weight >= WMULT_CONST))
>              lw->inv_weight = 1;
>          else
> -            lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)
> -                / (lw->weight+1);
> +            lw->inv_weight = WMULT_CONST / lw->weight;

hm, i definitely think there was a rounding reason for that - but apparently 
i'm an idiot who does not add comments to non-obvious code! :-)

Peter, do you remember this?

>      }
>  
> -    tmp = (u64)delta_exec * weight;

I agree that moving this multiplication early in the sequence is better for 
micro-performance regardless of the lw->weight optimization you do: it can be 
executed in parallel.

Thanks,

	Ingo

  reply	other threads:[~2011-05-11 16:21 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-11 16:03 [PATCH] sched: fix/optimise calculation of weight-inverse Stephan Bärwolf
2011-05-11 16:20 ` Ingo Molnar [this message]
2011-05-11 16:43   ` Peter Zijlstra
2011-05-11 17:35     ` Stephan Bärwolf
2011-05-11 19:49       ` Peter Zijlstra

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=20110511162030.GA2638@elte.hu \
    --to=mingo@elte.hu \
    --cc=efault@gmx.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ncrao@google.com \
    --cc=nikunj@linux.vnet.ibm.com \
    --cc=peterz@infradead.org \
    --cc=stephan.baerwolf@tu-ilmenau.de \
    --cc=torvalds@linux-foundation.org \
    --cc=vatsa@linux.vnet.ibm.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