public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Possible BUG in sys_nanosleep() ?
@ 2005-03-22 20:10 Tyson Sawyer
  0 siblings, 0 replies; only message in thread
From: Tyson Sawyer @ 2005-03-22 20:10 UTC (permalink / raw)
  To: linux-kernel; +Cc: Tyson Sawyer

I have searched archives of linux-kernel and not found any reference to 
this behavior.  This situation exists in both 2.4 and 2.6 kernels.  I'm 
not quite prepared to call it a bug because I have not yet consulted 
with anyone closer to the code and that is the purpose of this message:


 From kernel/timer.c - sys_nanosleep():
         expire = timespec_to_jiffies(&t) + (t.tv_sec || t.tv_nsec);


 From include/linux/jiffies.h - timespec_to_jiffies():
/*
  * The TICK_NSEC - 1 rounds up the value to the next resolution.  Note
  * that a remainder subtract here would not do the right thing as the
[...]


Observed problem:

Processes seem to sleep for at least 2 timer ticks even when asked to 
sleep for less than 1.  Specifically, on a 2.4 kernel with HZ=100, a 
sleep for 5ms becomes a sleep for 20ms when 10ms is the expected behavior.

Source code inspection:

sys_nanosleep() rounds up the value returned by timespec_to_jiffies() by 
adding 1 unless the requested sleep time is zero.

timespec_to_jiffies() also rounds the returned number of jiffies, except 
in the case of an even number of jiffies being requested.  Thus, 
nanoseconds returns zero jiffies, but 1 nanosecond returns 1 jiffy.

The effect of both functions rounding up is that it is possible to sleep 
for zero nanoseconds (no sleep), but otherwise 1 is added to the number 
of jiffies to sleep.  Thus, what should be a sleep for one jiffie (wake 
up on next timer tick) becomes two jiffies (and wakes up on the 2nd 
timer tick).

Conclusion:

sys_nanosleep() should never add 1 to the value of expire as 
timespec_to_jiffies() already rounds up.

I post this as a question because I don't know that 
timespec_to_jiffies() isn't where the behavior should be changed or 
perhaps there is a good reason that I can't think of for the current 
behavior.

I am not subscribed to linux-kernel.  Please CC me on all replies.

Thanks!
Ty

-- 
Tyson D Sawyer                     iRobot Corporation
Lead Systems Engineer              Government & Industrial Robotics
tsawyer@irobot.com                 Robots for the Real World
781-345-0200 ext 3329              http://www.irobot.com

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2005-03-22 20:12 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-03-22 20:10 Possible BUG in sys_nanosleep() ? Tyson Sawyer

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