public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* sched_clock() uses are broken
@ 2006-05-02 13:29 Russell King
  2006-05-02 14:21 ` Nick Piggin
                   ` (2 more replies)
  0 siblings, 3 replies; 41+ messages in thread
From: Russell King @ 2006-05-02 13:29 UTC (permalink / raw)
  To: Linux Kernel List

Hi,

Someone just asked a question about sched_clock() on the ARM list, asking
whether there was any way to find the range of values returned by this
function.

My initial thought was "why would you want that", but on considering the
problem a bit further, I could see why the question arises.  In looking
deeper, I discovered a rather sad state of affairs with sched_clock().

The kernel assumes that the following will work:

	unsigned long long t0, t1, time_passed_ns;

	t0 = sched_clock();
	/* do something */
	t1 = sched_clock();

	time_passed_ns = t1 - t0;

This is all very well if sched_clock() returns values filling the
entire range of an unsigned long long - two complement maths will
naturally return the time difference in the case where t1 < t0.

However, this is not the case.  On x86 with TSC, it returns a 54 bit
number.  This means that when t1 < t0, time_passed_ns becomes a very
large number which no longer represents the amount of time.

All uses in kernel/sched.c seem to be aflicted by this problem.

There are several solutions to this - the most obvious being that we
need a function which returns the nanosecond difference between two
sched_clock() return values, and this function needs to know how to
handle the case where sched_clock() has wrapped.

IOW:

	t0 = sched_clock();
	/* do something */
	t1 = sched_clock();

	time_passed = sched_clock_diff(t1, t0);

Comments?

-- 
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 Serial core

^ permalink raw reply	[flat|nested] 41+ messages in thread

end of thread, other threads:[~2006-05-08  5:30 UTC | newest]

Thread overview: 41+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-05-02 13:29 sched_clock() uses are broken Russell King
2006-05-02 14:21 ` Nick Piggin
2006-05-02 16:43 ` Andi Kleen
2006-05-02 16:50   ` Russell King
2006-05-02 17:01     ` Andi Kleen
2006-05-02 17:18       ` Nicolas Pitre
2006-05-02 18:55         ` Russell King
2006-05-02 19:05           ` Nicolas Pitre
2006-05-02 19:08             ` Russell King
2006-05-02 19:23               ` Nicolas Pitre
2006-05-02 21:35                 ` Russell King
2006-05-02 17:15     ` Nicolas Pitre
2006-05-04  3:50       ` George Anzinger
2006-05-04 14:18         ` Nicolas Pitre
2006-05-02 16:54   ` Christopher Friesen
2006-05-02 16:59     ` Andi Kleen
2006-05-02 17:07       ` Nick Piggin
2006-05-03  7:09         ` Mike Galbraith
2006-05-03  7:40           ` Andi Kleen
2006-05-03  9:11             ` Mike Galbraith
2006-05-03  9:16               ` Andi Kleen
2006-05-03  9:31                 ` Mike Galbraith
2006-05-07 12:33           ` Nick Piggin
2006-05-07 12:43             ` Russell King
2006-05-07 12:56               ` Nick Piggin
2006-05-07 13:00                 ` Nick Piggin
2006-05-07 13:18                   ` Russell King
2006-05-07 13:30                     ` Nick Piggin
2006-05-07 13:55                       ` Russell King
2006-05-07 14:04                         ` Nick Piggin
2006-05-07 16:03                           ` Andi Kleen
2006-05-07 16:53                       ` Russell King
2006-05-07 17:52                         ` Mike Galbraith
2006-05-07 17:37               ` Mike Galbraith
2006-05-07 17:32             ` Mike Galbraith
2006-05-08  4:14               ` Mike Galbraith
2006-05-08  4:37                 ` Mike Galbraith
2006-05-08  4:46                   ` Nick Piggin
2006-05-08  5:24                     ` Mike Galbraith
2006-05-08  5:30                       ` Nick Piggin
2006-05-04 20:02 ` Florian Paul Schmidt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox