Re: nanosleep

Markus Kuhn (
Fri, 5 Apr 1996 12:28:52 +0200 (MET DST)

Mark Hahn wrote:

> > Suggestion: We could use the 128 Hz CMOS interrupt for periodic tasks
> > like process preemption. This would make the 8253 timer 1 free for
> > interrupt-on-demand timer implementation with around one microsecond
> > precision. For high precision time measurement with gettimeofday()
> > or the POSIX equivalent clock_gettime(), the Pentium timer could be
> > used.

> this sounds like an excellent idea. I believe that the 8353's
> current counter value can also be read, which would permit us
> to figure out the real time to high resolution.

Yes, gettimeofday() has done this since before Linux 1.0 in order to
get circa microsecond resolution.

> it would be
> lovely to use the P5/s cycle counter to avoid this (and then
> we can brag about our ~10 ns resolution clock. hmm, gettimeofday
> can't even represent that!)

POSIX.1b does not define "struct timeval", but "struct timespec"
instead, which provides nanoseconds instead of microseconds. Thus,
POSIX.1b clock_gettime() could return nanosecond resolution, and this
would allow us to use the Pentium time stamp counter (TSC) for highest
resolution performance evaluation.

> note that there are mutterings of doing something like this in the
> FreeBSD world. personally, I don't see any great reason to work
> hard at preserving the 100 Hz behavior, as long as the NTP gurus
> can still build a PLL...

In fact, the NTP PLL implementation would prefer a 128 Hz interrupt
for driving the phase locked-loop (PLL) which disciplines the kernel
clock phase and frequency, because they already use an approximation
100 ~ 2^7 in order to replace an ugly multiplication by an efficient
shift operation. In addition, Linux/Alpha uses already HZ=1000 and the
clock PLL still works.

Look at /usr/src/include/linux/timex.h:

* The following defines establish the engineering parameters of the PLL
* model. The HZ variable establishes the timer interrupt frequency, 100 Hz
* for the SunOS kernel, 256 Hz for the Ultrix kernel and 1024 Hz for the
* OSF/1 kernel. The SHIFT_HZ define expresses the same value as the
* nearest power of two in order to avoid hardware multiply operations.
#ifdef __alpha__
# define SHIFT_HZ 10 /* log2(HZ) */
# define SHIFT_HZ 7 /* log2(HZ) */


Markus Kuhn, Computer Science student -- University of Erlangen,
Internet Mail: <> - Germany
WWW Home: <>