linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Paul Turner <pjt@google.com>
To: linux-kernel@vger.kernel.org
Cc: Salman Qazi <sqazi@google.com>, Ingo Molnar <mingo@elte.hu>,
	LKML <linux-kernel@vger.kernel.org>,
	Peter Zijlstra <a.p.zijlstra@chello.nl>
Subject: Re: [PATCH] sched: avoid unnecessary overflow in sched_clock
Date: Tue, 15 Nov 2011 16:07:38 -0800	[thread overview]
Message-ID: <4EC2FECA.4080502@google.com> (raw)
In-Reply-To: <1321398123.2352.29.camel@work-vm>

On 11/15/2011 03:02 PM, john stultz wrote:
> On Tue, 2011-11-15 at 14:12 -0800, Salman Qazi wrote:
>> (Added the missing signed-off-by line)
>>
>> In hundreds of days, the __cycles_2_ns calculation in sched_clock
>> has an overflow.  cyc * per_cpu(cyc2ns, cpu) exceeds 64 bits, causing
>> the final value to become zero.  We can solve this without losing
>> any precision.
>>
>> We can decompose TSC into quotient and remainder of division by the
>> scale factor, and then use this to convert TSC into nanoseconds.
>>
>> Signed-off-by: Salman Qazi<sqazi@google.com>
>
> Acked-by: John Stultz<johnstul@us.ibm.com>
>

Reviewed-by: Paul Turner <pjt@google.com>

>
>> ---
>>   arch/x86/include/asm/timer.h |   23 ++++++++++++++++++++++-
>>   1 files changed, 22 insertions(+), 1 deletions(-)
>>
>> diff --git a/arch/x86/include/asm/timer.h b/arch/x86/include/asm/timer.h
>> index fa7b917..431793e 100644
>> --- a/arch/x86/include/asm/timer.h
>> +++ b/arch/x86/include/asm/timer.h
>> @@ -32,6 +32,22 @@ extern int no_timer_check;
>>    *  (mathieu.desnoyers@polymtl.ca)
>>    *
>>    *			-johnstul@us.ibm.com "math is hard, lets go shopping!"
>> + *
>> + * In:
>> + *
>> + * ns = cycles * cyc2ns_scale / SC
>> + *
>> + * Although we may still have enough bits to store the value of ns,
>> + * in some cases, we may not have enough bits to store cycles * cyc2ns_scale,
>> + * leading to an incorrect result.
>> + *
>> + * To avoid this, we can decompose 'cycles' into quotient and remainder
>> + * of division by SC.  Then,
>> + *
>> + * ns = (quot * SC + rem) * cyc2ns_scale / SC
>> + *    = quot * cyc2ns_scale + (rem * cyc2ns_scale) / SC
>> + *
>> + *			- sqazi@google.com
>>    */
>>
>>   DECLARE_PER_CPU(unsigned long, cyc2ns);
>> @@ -41,9 +57,14 @@ DECLARE_PER_CPU(unsigned long long, cyc2ns_offset);
>>
>>   static inline unsigned long long __cycles_2_ns(unsigned long long cyc)
>>   {
>> +	unsigned long long quot;
>> +	unsigned long long rem;
>>   	int cpu = smp_processor_id();
>>   	unsigned long long ns = per_cpu(cyc2ns_offset, cpu);
>> -	ns += cyc * per_cpu(cyc2ns, cpu)>>  CYC2NS_SCALE_FACTOR;
>> +	quot = (cyc>>  CYC2NS_SCALE_FACTOR);
>> +	rem = cyc&  ((1ULL<<  CYC2NS_SCALE_FACTOR) - 1);
>> +	ns += quot * per_cpu(cyc2ns, cpu) +
>> +		((rem * per_cpu(cyc2ns, cpu))>>  CYC2NS_SCALE_FACTOR);
>>   	return ns;
>>   }
>>
>>
>
>



  reply	other threads:[~2011-11-16  0:07 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-15 21:59 [PATCH] sched: avoid unnecessary overflow in sched_clock Salman Qazi
2011-11-15 22:12 ` Salman Qazi
2011-11-15 23:02   ` john stultz
2011-11-16  0:07     ` Paul Turner [this message]
2011-11-16  6:41     ` Mike Galbraith
2011-11-16  7:08       ` Paul Turner
2011-11-16  8:56         ` Peter Zijlstra
2011-11-16 20:55           ` Salman Qazi
2011-11-18 23:45   ` [tip:sched/urgent] sched, x86: Avoid " tip-bot for Salman Qazi

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=4EC2FECA.4080502@google.com \
    --to=pjt@google.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=sqazi@google.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;
as well as URLs for NNTP newsgroup(s).