Fine grained time

M

Mark Seger

I have some code I had written awhile back (on perl 5.6 and redhat 9)
that used ualarm to wake me up every second and it worked really nicely.
However I've more recently dug into just how accurate that timer is
and was disappointed, especially on a cominaction of rhel4/u4 and perl
5.8.5. In fact, I see my code waking up almost 0.001 seconds later on
each subsequent interval.

Going back and rereading the manpage for Time:HiRes I spotted a comment
that the relationship between sigalarm and sleep are unspecified so I
changed my code to use setitimer() and it behaves the same way.

So my question is, is this a perl thing or a system thing? Or am I just
doing something silly. Here's what my code looks like:

use Time::HiRes qw(ITIMER_REAL);

$SIG{"ALRM"}= sub {};
Time::HiRes::setitimer(ITIMER_REAL, 1, 1);
while(1)
{
($intSeconds, $intUsecs)=Time::HiRes::gettimeofday();
printf("%d.%06d\n", $intSeconds, $intUsecs);
sleep 2;
}

and here's what it produces:

[root@cag-dl145-02 collectl]# ./alarm2-test.pl
1168452960.360707
1168452961.361269
1168452962.362297
1168452963.363291

I also tried using "select(undef, undef, undef, 2)" instead of the sleep
but that didn't make any difference.

-mark
 
X

xhoster

Mark Seger said:
I have some code I had written awhile back (on perl 5.6 and redhat 9)
that used ualarm to wake me up every second and it worked really nicely.
However I've more recently dug into just how accurate that timer is
and was disappointed, especially on a cominaction of rhel4/u4 and perl
5.8.5. In fact, I see my code waking up almost 0.001 seconds later on
each subsequent interval.

Going back and rereading the manpage for Time:HiRes I spotted a comment
that the relationship between sigalarm and sleep are unspecified so I
changed my code to use setitimer() and it behaves the same way.

So my question is, is this a perl thing or a system thing? Or am I just
doing something silly. Here's what my code looks like:

I "strace"d your program, and it looks to me like a system thing. Perl
just passes the setitimer directly on to the system, and from then on it is
up to the system to deliver the signals.

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top