Re: need help while implementing a timer.
From: Floyd Davidson (floyd_at_barrow.com)
Date: Mon, 17 Nov 2003 04:35:39 -0900
email@example.com (nikhil bhargav) wrote:
>Daniele Tamino <firstname.lastname@example.org> wrote in message news:<email@example.com>...
>> Daniele Tamino wrote:
>> I'll just answer myself, after a little test: setitimer has a higher
>> accuracy, not 20ms, but I was using nanosleep and such which *should*
>> have a high accuracy, because the interval is specified in nanoseconds,
>> but they round it up to 20ms for some reason...
>I am thankful to all of the programming experts who have read my
>problem and adviced me. I will try the solution and let you know.
>Further, Linux on my system doesnot have a timer granularity of 10 ms.
>I am working at preset on a i386 based host but all my applications
>will be porting to powerppc based target soon having any linux based
>RTOS. SO there timers of nano granularity is availbale but as of now i
>need a timer of 1 ms granularity for host testing.
All of these variations observed are certainly real! It all
On a i386 system running a 2.4 kernel, HZ is defined as 100,
which gives the mentioned 10ms granularity for the scheduler.
However, grepping the include files for a 2.6.0 kernel shows a
varietal plethora of values for HZ depending on the
asm-ia64/param.h: #define HZ 1024
asm-alpha/param.h: #define HZ 1024
asm-alpha/param.h: #define HZ 1200
asm-i386/param.h: #define HZ 1000
asm-m68knommu/param.h: #define HZ 1000
asm-mips/param.h: #define HZ 1000
asm-parisc/param.h: #define HZ 1000
asm-ppc64/param.h: #define HZ 1000
Several of the above also had other defines setting HZ to 100,
so there may be variations depending on configuration.
A few tests on an i386 box with a 2.6.0-test7 kernel verified
that it is no longer restrained by a 10 ms scheduler
The tests showed some interesting things that I'd never noticed
before. With a 2.4 kernel (on an old 400Mhz Intel PII) and on
my old laptop with a 2.2 kernel and 266Mhz PII, there is a big
difference in what happens when a process delays using usleep(),
nanosleep(), or select(). For reasons that I don't know, the
granularity with usleep() was 20ms, which seemed to be
relatively constant if there is light loading on the cpu.
Calling select() resulted in a 10ms delay Calling nanosleep()
gave results that varied wildly between 10 and 20ms on the
faster cpu, and between 15ms and 25ms on the slower one. (That
doesn't fit with what Daniele reported, which was that
nanosleep() gave the 20ms granularity, so we probably have
That was in a loop which gettimeofday() measured at 800-1100
microseconds, both systems, when no delay was added. The delay
was set for 100 microseconds, but different delays make no
difference as long as it is significantly less than the
A different box running a 2.6.0-test7 kernel does the loop in
roughly 200 microseconds with no delay. (A much faster cpu is
part of the reason for the speed difference, not just the
kernel; but the similarity in times between the other two
machines suggests the kernel has more effect on it than the cpu
speed). With the 2.6 kernel there doesn't seem to be any
significant difference with the different delay functions. The
typical range was 1 to 5 ms, but some times were less than 1ms,
and since this box is always doing things the high end was often
far more than any 10-20ms. (It is also a dual cpu SMP box, and
I had the loop wired to show which cpu it was running on, and
did not notice any tendancy for either faster or slower times if
a cpu switch took place while the process was delayed.)
One thing though, the clock() function itself has 10ms
granularity, and *never* reports any finer intervals.
Nikhil, you might want to look at using a 2.6.0 kernel for
development if you don't already have that.
-- Floyd L. Davidson <http://web.newsguy.com/floyd_davidson> Ukpeagvik (Barrow, Alaska) firstname.lastname@example.org