?

Log in

No account? Create an account
Things I never cared about until recently: - brad's life — LiveJournal [entries|archive|friends|userinfo]
Brad Fitzpatrick

[ website | bradfitz.com ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Things I never cared about until recently: [Nov. 28th, 2003|04:17 pm]
Brad Fitzpatrick
nanosleep(2):

...

BUGS
The current implementation of nanosleep is based on the normal kernel timer mechanism, which has a resolution of 1/HZ s (i.e, 10 ms on Linux/i386 and 1 ms on Linux/Alpha). Therefore, nanosleep pauses always for at least the specified time, however it can take up to 10 ms longer than specified until the process becomes runnable again. For the same reason, the value returned in case of a delivered signal in *rem is usually rounded to the next larger multiple of 1/HZ s.

As some applications require much more precise pauses (e.g., in order to control some time-critical hardware), nanosleep is also capable of short high-precision pauses. If the process is scheduled under a real-time policy like SCHED_FIFO or SCHED_RR, then pauses of up to 2 ms will be performed as busy waits with microsecond precision.

Cool!
LinkReply

Comments:
From: jeffr
2003-11-28 09:06 pm (UTC)
You can also change your hz. Or on a single processor system, if you require very precise timing, you can use the tsc.
(Reply) (Thread)
[User Picture]From: brad
2003-11-28 11:36 pm (UTC)
Yeah, I can 10x the HZ with newer Linux versions, but I just thought it was cool the kernel would busy wait for me.... I was all ready to do it myself, but was dreading having to calculate the "bogomips value" myself, purely because it seemed ugly/wrong.

The tsc?
(Reply) (Parent) (Thread)
From: jeffr
2003-11-28 11:43 pm (UTC)
time stamp counter. Increments once per clock tick. You can read this from userspace.

uint64_t
rdtsc()
{
__asm("rdtsc;");
}

That should do it.
(Reply) (Parent) (Thread)
[User Picture]From: jwz
2003-11-29 11:17 pm (UTC)
I always found it annoying when writing screen savers that usleep(0) and usleep(10000) are equivalent. Sometimes I ended up doing a sleep every N iterations to get the timing right. Then five years would go by and machines would be 200x faster and the timing would be all wonky anyway.
(Reply) (Thread)
[User Picture]From: taral
2003-12-01 01:47 pm (UTC)
Linux 2.6 has HZ=1000
(Reply) (Thread)