1*b8851fccSafresh1package Time::HiRes; 2*b8851fccSafresh1 3*b8851fccSafresh1{ use 5.006; } 4*b8851fccSafresh1use strict; 5*b8851fccSafresh1 6*b8851fccSafresh1require Exporter; 7*b8851fccSafresh1require DynaLoader; 8*b8851fccSafresh1 9*b8851fccSafresh1our @ISA = qw(Exporter DynaLoader); 10*b8851fccSafresh1 11*b8851fccSafresh1our @EXPORT = qw( ); 12*b8851fccSafresh1our @EXPORT_OK = qw (usleep sleep ualarm alarm gettimeofday time tv_interval 13*b8851fccSafresh1 getitimer setitimer nanosleep clock_gettime clock_getres 14*b8851fccSafresh1 clock clock_nanosleep 15*b8851fccSafresh1 CLOCK_BOOTTIME CLOCK_HIGHRES 16*b8851fccSafresh1 CLOCK_MONOTONIC CLOCK_MONOTONIC_COARSE 17*b8851fccSafresh1 CLOCK_MONOTONIC_PRECISE CLOCK_MONOTONIC_RAW 18*b8851fccSafresh1 CLOCK_PROCESS_CPUTIME_ID 19*b8851fccSafresh1 CLOCK_REALTIME CLOCK_REALTIME_COARSE 20*b8851fccSafresh1 CLOCK_REALTIME_FAST CLOCK_REALTIME_PRECISE 21*b8851fccSafresh1 CLOCK_SECOND CLOCK_SOFTTIME CLOCK_THREAD_CPUTIME_ID 22*b8851fccSafresh1 CLOCK_TIMEOFDAY CLOCKS_PER_SEC 23*b8851fccSafresh1 ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF ITIMER_REALPROF 24*b8851fccSafresh1 TIMER_ABSTIME 25*b8851fccSafresh1 d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer 26*b8851fccSafresh1 d_nanosleep d_clock_gettime d_clock_getres 27*b8851fccSafresh1 d_clock d_clock_nanosleep 28*b8851fccSafresh1 stat lstat 29*b8851fccSafresh1 ); 30*b8851fccSafresh1 31*b8851fccSafresh1our $VERSION = '1.9733'; 32*b8851fccSafresh1our $XS_VERSION = $VERSION; 33*b8851fccSafresh1$VERSION = eval $VERSION; 34*b8851fccSafresh1 35*b8851fccSafresh1our $AUTOLOAD; 36*b8851fccSafresh1sub AUTOLOAD { 37*b8851fccSafresh1 my $constname; 38*b8851fccSafresh1 ($constname = $AUTOLOAD) =~ s/.*:://; 39*b8851fccSafresh1 # print "AUTOLOAD: constname = $constname ($AUTOLOAD)\n"; 40*b8851fccSafresh1 die "&Time::HiRes::constant not defined" if $constname eq 'constant'; 41*b8851fccSafresh1 my ($error, $val) = constant($constname); 42*b8851fccSafresh1 # print "AUTOLOAD: error = $error, val = $val\n"; 43*b8851fccSafresh1 if ($error) { 44*b8851fccSafresh1 my (undef,$file,$line) = caller; 45*b8851fccSafresh1 die "$error at $file line $line.\n"; 46*b8851fccSafresh1 } 47*b8851fccSafresh1 { 48*b8851fccSafresh1 no strict 'refs'; 49*b8851fccSafresh1 *$AUTOLOAD = sub { $val }; 50*b8851fccSafresh1 } 51*b8851fccSafresh1 goto &$AUTOLOAD; 52*b8851fccSafresh1} 53*b8851fccSafresh1 54*b8851fccSafresh1sub import { 55*b8851fccSafresh1 my $this = shift; 56*b8851fccSafresh1 for my $i (@_) { 57*b8851fccSafresh1 if (($i eq 'clock_getres' && !&d_clock_getres) || 58*b8851fccSafresh1 ($i eq 'clock_gettime' && !&d_clock_gettime) || 59*b8851fccSafresh1 ($i eq 'clock_nanosleep' && !&d_clock_nanosleep) || 60*b8851fccSafresh1 ($i eq 'clock' && !&d_clock) || 61*b8851fccSafresh1 ($i eq 'nanosleep' && !&d_nanosleep) || 62*b8851fccSafresh1 ($i eq 'usleep' && !&d_usleep) || 63*b8851fccSafresh1 ($i eq 'ualarm' && !&d_ualarm)) { 64*b8851fccSafresh1 require Carp; 65*b8851fccSafresh1 Carp::croak("Time::HiRes::$i(): unimplemented in this platform"); 66*b8851fccSafresh1 } 67*b8851fccSafresh1 } 68*b8851fccSafresh1 Time::HiRes->export_to_level(1, $this, @_); 69*b8851fccSafresh1} 70*b8851fccSafresh1 71*b8851fccSafresh1bootstrap Time::HiRes; 72*b8851fccSafresh1 73*b8851fccSafresh1# Preloaded methods go here. 74*b8851fccSafresh1 75*b8851fccSafresh1sub tv_interval { 76*b8851fccSafresh1 # probably could have been done in C 77*b8851fccSafresh1 my ($a, $b) = @_; 78*b8851fccSafresh1 $b = [gettimeofday()] unless defined($b); 79*b8851fccSafresh1 (${$b}[0] - ${$a}[0]) + ((${$b}[1] - ${$a}[1]) / 1_000_000); 80*b8851fccSafresh1} 81*b8851fccSafresh1 82*b8851fccSafresh1# Autoload methods go after =cut, and are processed by the autosplit program. 83*b8851fccSafresh1 84*b8851fccSafresh11; 85*b8851fccSafresh1__END__ 86*b8851fccSafresh1 87*b8851fccSafresh1=head1 NAME 88*b8851fccSafresh1 89*b8851fccSafresh1Time::HiRes - High resolution alarm, sleep, gettimeofday, interval timers 90*b8851fccSafresh1 91*b8851fccSafresh1=head1 SYNOPSIS 92*b8851fccSafresh1 93*b8851fccSafresh1 use Time::HiRes qw( usleep ualarm gettimeofday tv_interval nanosleep 94*b8851fccSafresh1 clock_gettime clock_getres clock_nanosleep clock 95*b8851fccSafresh1 stat lstat ); 96*b8851fccSafresh1 97*b8851fccSafresh1 usleep ($microseconds); 98*b8851fccSafresh1 nanosleep ($nanoseconds); 99*b8851fccSafresh1 100*b8851fccSafresh1 ualarm ($microseconds); 101*b8851fccSafresh1 ualarm ($microseconds, $interval_microseconds); 102*b8851fccSafresh1 103*b8851fccSafresh1 $t0 = [gettimeofday]; 104*b8851fccSafresh1 ($seconds, $microseconds) = gettimeofday; 105*b8851fccSafresh1 106*b8851fccSafresh1 $elapsed = tv_interval ( $t0, [$seconds, $microseconds]); 107*b8851fccSafresh1 $elapsed = tv_interval ( $t0, [gettimeofday]); 108*b8851fccSafresh1 $elapsed = tv_interval ( $t0 ); 109*b8851fccSafresh1 110*b8851fccSafresh1 use Time::HiRes qw ( time alarm sleep ); 111*b8851fccSafresh1 112*b8851fccSafresh1 $now_fractions = time; 113*b8851fccSafresh1 sleep ($floating_seconds); 114*b8851fccSafresh1 alarm ($floating_seconds); 115*b8851fccSafresh1 alarm ($floating_seconds, $floating_interval); 116*b8851fccSafresh1 117*b8851fccSafresh1 use Time::HiRes qw( setitimer getitimer ); 118*b8851fccSafresh1 119*b8851fccSafresh1 setitimer ($which, $floating_seconds, $floating_interval ); 120*b8851fccSafresh1 getitimer ($which); 121*b8851fccSafresh1 122*b8851fccSafresh1 use Time::HiRes qw( clock_gettime clock_getres clock_nanosleep 123*b8851fccSafresh1 ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF 124*b8851fccSafresh1 ITIMER_REALPROF ); 125*b8851fccSafresh1 126*b8851fccSafresh1 $realtime = clock_gettime(CLOCK_REALTIME); 127*b8851fccSafresh1 $resolution = clock_getres(CLOCK_REALTIME); 128*b8851fccSafresh1 129*b8851fccSafresh1 clock_nanosleep(CLOCK_REALTIME, 1.5e9); 130*b8851fccSafresh1 clock_nanosleep(CLOCK_REALTIME, time()*1e9 + 10e9, TIMER_ABSTIME); 131*b8851fccSafresh1 132*b8851fccSafresh1 my $ticktock = clock(); 133*b8851fccSafresh1 134*b8851fccSafresh1 use Time::HiRes qw( stat lstat ); 135*b8851fccSafresh1 136*b8851fccSafresh1 my @stat = stat("file"); 137*b8851fccSafresh1 my @stat = stat(FH); 138*b8851fccSafresh1 my @stat = lstat("file"); 139*b8851fccSafresh1 140*b8851fccSafresh1=head1 DESCRIPTION 141*b8851fccSafresh1 142*b8851fccSafresh1The C<Time::HiRes> module implements a Perl interface to the 143*b8851fccSafresh1C<usleep>, C<nanosleep>, C<ualarm>, C<gettimeofday>, and 144*b8851fccSafresh1C<setitimer>/C<getitimer> system calls, in other words, high 145*b8851fccSafresh1resolution time and timers. See the L</EXAMPLES> section below and the 146*b8851fccSafresh1test scripts for usage; see your system documentation for the 147*b8851fccSafresh1description of the underlying C<nanosleep> or C<usleep>, C<ualarm>, 148*b8851fccSafresh1C<gettimeofday>, and C<setitimer>/C<getitimer> calls. 149*b8851fccSafresh1 150*b8851fccSafresh1If your system lacks C<gettimeofday()> or an emulation of it you don't 151*b8851fccSafresh1get C<gettimeofday()> or the one-argument form of C<tv_interval()>. 152*b8851fccSafresh1If your system lacks all of C<nanosleep()>, C<usleep()>, 153*b8851fccSafresh1C<select()>, and C<poll>, you don't get C<Time::HiRes::usleep()>, 154*b8851fccSafresh1C<Time::HiRes::nanosleep()>, or C<Time::HiRes::sleep()>. 155*b8851fccSafresh1If your system lacks both C<ualarm()> and C<setitimer()> you don't get 156*b8851fccSafresh1C<Time::HiRes::ualarm()> or C<Time::HiRes::alarm()>. 157*b8851fccSafresh1 158*b8851fccSafresh1If you try to import an unimplemented function in the C<use> statement 159*b8851fccSafresh1it will fail at compile time. 160*b8851fccSafresh1 161*b8851fccSafresh1If your subsecond sleeping is implemented with C<nanosleep()> instead 162*b8851fccSafresh1of C<usleep()>, you can mix subsecond sleeping with signals since 163*b8851fccSafresh1C<nanosleep()> does not use signals. This, however, is not portable, 164*b8851fccSafresh1and you should first check for the truth value of 165*b8851fccSafresh1C<&Time::HiRes::d_nanosleep> to see whether you have nanosleep, and 166*b8851fccSafresh1then carefully read your C<nanosleep()> C API documentation for any 167*b8851fccSafresh1peculiarities. 168*b8851fccSafresh1 169*b8851fccSafresh1If you are using C<nanosleep> for something else than mixing sleeping 170*b8851fccSafresh1with signals, give some thought to whether Perl is the tool you should 171*b8851fccSafresh1be using for work requiring nanosecond accuracies. 172*b8851fccSafresh1 173*b8851fccSafresh1Remember that unless you are working on a I<hard realtime> system, 174*b8851fccSafresh1any clocks and timers will be imprecise, especially so if you are working 175*b8851fccSafresh1in a pre-emptive multiuser system. Understand the difference between 176*b8851fccSafresh1I<wallclock time> and process time (in UNIX-like systems the sum of 177*b8851fccSafresh1I<user> and I<system> times). Any attempt to sleep for X seconds will 178*b8851fccSafresh1most probably end up sleeping B<more> than that, but don't be surprised 179*b8851fccSafresh1if you end up sleeping slightly B<less>. 180*b8851fccSafresh1 181*b8851fccSafresh1The following functions can be imported from this module. 182*b8851fccSafresh1No functions are exported by default. 183*b8851fccSafresh1 184*b8851fccSafresh1=over 4 185*b8851fccSafresh1 186*b8851fccSafresh1=item gettimeofday () 187*b8851fccSafresh1 188*b8851fccSafresh1In array context returns a two-element array with the seconds and 189*b8851fccSafresh1microseconds since the epoch. In scalar context returns floating 190*b8851fccSafresh1seconds like C<Time::HiRes::time()> (see below). 191*b8851fccSafresh1 192*b8851fccSafresh1=item usleep ( $useconds ) 193*b8851fccSafresh1 194*b8851fccSafresh1Sleeps for the number of microseconds (millionths of a second) 195*b8851fccSafresh1specified. Returns the number of microseconds actually slept. 196*b8851fccSafresh1Can sleep for more than one second, unlike the C<usleep> system call. 197*b8851fccSafresh1Can also sleep for zero seconds, which often works like a I<thread yield>. 198*b8851fccSafresh1See also C<Time::HiRes::usleep()>, C<Time::HiRes::sleep()>, and 199*b8851fccSafresh1C<Time::HiRes::clock_nanosleep()>. 200*b8851fccSafresh1 201*b8851fccSafresh1Do not expect usleep() to be exact down to one microsecond. 202*b8851fccSafresh1 203*b8851fccSafresh1=item nanosleep ( $nanoseconds ) 204*b8851fccSafresh1 205*b8851fccSafresh1Sleeps for the number of nanoseconds (1e9ths of a second) specified. 206*b8851fccSafresh1Returns the number of nanoseconds actually slept (accurate only to 207*b8851fccSafresh1microseconds, the nearest thousand of them). Can sleep for more than 208*b8851fccSafresh1one second. Can also sleep for zero seconds, which often works like 209*b8851fccSafresh1a I<thread yield>. See also C<Time::HiRes::sleep()>, 210*b8851fccSafresh1C<Time::HiRes::usleep()>, and C<Time::HiRes::clock_nanosleep()>. 211*b8851fccSafresh1 212*b8851fccSafresh1Do not expect nanosleep() to be exact down to one nanosecond. 213*b8851fccSafresh1Getting even accuracy of one thousand nanoseconds is good. 214*b8851fccSafresh1 215*b8851fccSafresh1=item ualarm ( $useconds [, $interval_useconds ] ) 216*b8851fccSafresh1 217*b8851fccSafresh1Issues a C<ualarm> call; the C<$interval_useconds> is optional and 218*b8851fccSafresh1will be zero if unspecified, resulting in C<alarm>-like behaviour. 219*b8851fccSafresh1 220*b8851fccSafresh1Returns the remaining time in the alarm in microseconds, or C<undef> 221*b8851fccSafresh1if an error occurred. 222*b8851fccSafresh1 223*b8851fccSafresh1ualarm(0) will cancel an outstanding ualarm(). 224*b8851fccSafresh1 225*b8851fccSafresh1Note that the interaction between alarms and sleeps is unspecified. 226*b8851fccSafresh1 227*b8851fccSafresh1=item tv_interval 228*b8851fccSafresh1 229*b8851fccSafresh1tv_interval ( $ref_to_gettimeofday [, $ref_to_later_gettimeofday] ) 230*b8851fccSafresh1 231*b8851fccSafresh1Returns the floating seconds between the two times, which should have 232*b8851fccSafresh1been returned by C<gettimeofday()>. If the second argument is omitted, 233*b8851fccSafresh1then the current time is used. 234*b8851fccSafresh1 235*b8851fccSafresh1=item time () 236*b8851fccSafresh1 237*b8851fccSafresh1Returns a floating seconds since the epoch. This function can be 238*b8851fccSafresh1imported, resulting in a nice drop-in replacement for the C<time> 239*b8851fccSafresh1provided with core Perl; see the L</EXAMPLES> below. 240*b8851fccSafresh1 241*b8851fccSafresh1B<NOTE 1>: This higher resolution timer can return values either less 242*b8851fccSafresh1or more than the core C<time()>, depending on whether your platform 243*b8851fccSafresh1rounds the higher resolution timer values up, down, or to the nearest second 244*b8851fccSafresh1to get the core C<time()>, but naturally the difference should be never 245*b8851fccSafresh1more than half a second. See also L</clock_getres>, if available 246*b8851fccSafresh1in your system. 247*b8851fccSafresh1 248*b8851fccSafresh1B<NOTE 2>: Since Sunday, September 9th, 2001 at 01:46:40 AM GMT, when 249*b8851fccSafresh1the C<time()> seconds since epoch rolled over to 1_000_000_000, the 250*b8851fccSafresh1default floating point format of Perl and the seconds since epoch have 251*b8851fccSafresh1conspired to produce an apparent bug: if you print the value of 252*b8851fccSafresh1C<Time::HiRes::time()> you seem to be getting only five decimals, not 253*b8851fccSafresh1six as promised (microseconds). Not to worry, the microseconds are 254*b8851fccSafresh1there (assuming your platform supports such granularity in the first 255*b8851fccSafresh1place). What is going on is that the default floating point format of 256*b8851fccSafresh1Perl only outputs 15 digits. In this case that means ten digits 257*b8851fccSafresh1before the decimal separator and five after. To see the microseconds 258*b8851fccSafresh1you can use either C<printf>/C<sprintf> with C<"%.6f">, or the 259*b8851fccSafresh1C<gettimeofday()> function in list context, which will give you the 260*b8851fccSafresh1seconds and microseconds as two separate values. 261*b8851fccSafresh1 262*b8851fccSafresh1=item sleep ( $floating_seconds ) 263*b8851fccSafresh1 264*b8851fccSafresh1Sleeps for the specified amount of seconds. Returns the number of 265*b8851fccSafresh1seconds actually slept (a floating point value). This function can 266*b8851fccSafresh1be imported, resulting in a nice drop-in replacement for the C<sleep> 267*b8851fccSafresh1provided with perl, see the L</EXAMPLES> below. 268*b8851fccSafresh1 269*b8851fccSafresh1Note that the interaction between alarms and sleeps is unspecified. 270*b8851fccSafresh1 271*b8851fccSafresh1=item alarm ( $floating_seconds [, $interval_floating_seconds ] ) 272*b8851fccSafresh1 273*b8851fccSafresh1The C<SIGALRM> signal is sent after the specified number of seconds. 274*b8851fccSafresh1Implemented using C<setitimer()> if available, C<ualarm()> if not. 275*b8851fccSafresh1The C<$interval_floating_seconds> argument is optional and will be 276*b8851fccSafresh1zero if unspecified, resulting in C<alarm()>-like behaviour. This 277*b8851fccSafresh1function can be imported, resulting in a nice drop-in replacement for 278*b8851fccSafresh1the C<alarm> provided with perl, see the L</EXAMPLES> below. 279*b8851fccSafresh1 280*b8851fccSafresh1Returns the remaining time in the alarm in seconds, or C<undef> 281*b8851fccSafresh1if an error occurred. 282*b8851fccSafresh1 283*b8851fccSafresh1B<NOTE 1>: With some combinations of operating systems and Perl 284*b8851fccSafresh1releases C<SIGALRM> restarts C<select()>, instead of interrupting it. 285*b8851fccSafresh1This means that an C<alarm()> followed by a C<select()> may together 286*b8851fccSafresh1take the sum of the times specified for the C<alarm()> and the 287*b8851fccSafresh1C<select()>, not just the time of the C<alarm()>. 288*b8851fccSafresh1 289*b8851fccSafresh1Note that the interaction between alarms and sleeps is unspecified. 290*b8851fccSafresh1 291*b8851fccSafresh1=item setitimer ( $which, $floating_seconds [, $interval_floating_seconds ] ) 292*b8851fccSafresh1 293*b8851fccSafresh1Start up an interval timer: after a certain time, a signal ($which) arrives, 294*b8851fccSafresh1and more signals may keep arriving at certain intervals. To disable 295*b8851fccSafresh1an "itimer", use C<$floating_seconds> of zero. If the 296*b8851fccSafresh1C<$interval_floating_seconds> is set to zero (or unspecified), the 297*b8851fccSafresh1timer is disabled B<after> the next delivered signal. 298*b8851fccSafresh1 299*b8851fccSafresh1Use of interval timers may interfere with C<alarm()>, C<sleep()>, 300*b8851fccSafresh1and C<usleep()>. In standard-speak the "interaction is unspecified", 301*b8851fccSafresh1which means that I<anything> may happen: it may work, it may not. 302*b8851fccSafresh1 303*b8851fccSafresh1In scalar context, the remaining time in the timer is returned. 304*b8851fccSafresh1 305*b8851fccSafresh1In list context, both the remaining time and the interval are returned. 306*b8851fccSafresh1 307*b8851fccSafresh1There are usually three or four interval timers (signals) available: the 308*b8851fccSafresh1C<$which> can be C<ITIMER_REAL>, C<ITIMER_VIRTUAL>, C<ITIMER_PROF>, or 309*b8851fccSafresh1C<ITIMER_REALPROF>. Note that which ones are available depends: true 310*b8851fccSafresh1UNIX platforms usually have the first three, but only Solaris seems to 311*b8851fccSafresh1have C<ITIMER_REALPROF> (which is used to profile multithreaded programs). 312*b8851fccSafresh1Win32 unfortunately does not have interval timers. 313*b8851fccSafresh1 314*b8851fccSafresh1C<ITIMER_REAL> results in C<alarm()>-like behaviour. Time is counted in 315*b8851fccSafresh1I<real time>; that is, wallclock time. C<SIGALRM> is delivered when 316*b8851fccSafresh1the timer expires. 317*b8851fccSafresh1 318*b8851fccSafresh1C<ITIMER_VIRTUAL> counts time in (process) I<virtual time>; that is, 319*b8851fccSafresh1only when the process is running. In multiprocessor/user/CPU systems 320*b8851fccSafresh1this may be more or less than real or wallclock time. (This time is 321*b8851fccSafresh1also known as the I<user time>.) C<SIGVTALRM> is delivered when the 322*b8851fccSafresh1timer expires. 323*b8851fccSafresh1 324*b8851fccSafresh1C<ITIMER_PROF> counts time when either the process virtual time or when 325*b8851fccSafresh1the operating system is running on behalf of the process (such as I/O). 326*b8851fccSafresh1(This time is also known as the I<system time>.) (The sum of user 327*b8851fccSafresh1time and system time is known as the I<CPU time>.) C<SIGPROF> is 328*b8851fccSafresh1delivered when the timer expires. C<SIGPROF> can interrupt system calls. 329*b8851fccSafresh1 330*b8851fccSafresh1The semantics of interval timers for multithreaded programs are 331*b8851fccSafresh1system-specific, and some systems may support additional interval 332*b8851fccSafresh1timers. For example, it is unspecified which thread gets the signals. 333*b8851fccSafresh1See your C<setitimer()> documentation. 334*b8851fccSafresh1 335*b8851fccSafresh1=item getitimer ( $which ) 336*b8851fccSafresh1 337*b8851fccSafresh1Return the remaining time in the interval timer specified by C<$which>. 338*b8851fccSafresh1 339*b8851fccSafresh1In scalar context, the remaining time is returned. 340*b8851fccSafresh1 341*b8851fccSafresh1In list context, both the remaining time and the interval are returned. 342*b8851fccSafresh1The interval is always what you put in using C<setitimer()>. 343*b8851fccSafresh1 344*b8851fccSafresh1=item clock_gettime ( $which ) 345*b8851fccSafresh1 346*b8851fccSafresh1Return as seconds the current value of the POSIX high resolution timer 347*b8851fccSafresh1specified by C<$which>. All implementations that support POSIX high 348*b8851fccSafresh1resolution timers are supposed to support at least the C<$which> value 349*b8851fccSafresh1of C<CLOCK_REALTIME>, which is supposed to return results close to the 350*b8851fccSafresh1results of C<gettimeofday>, or the number of seconds since 00:00:00:00 351*b8851fccSafresh1January 1, 1970 Greenwich Mean Time (GMT). Do not assume that 352*b8851fccSafresh1CLOCK_REALTIME is zero, it might be one, or something else. 353*b8851fccSafresh1Another potentially useful (but not available everywhere) value is 354*b8851fccSafresh1C<CLOCK_MONOTONIC>, which guarantees a monotonically increasing time 355*b8851fccSafresh1value (unlike time() or gettimeofday(), which can be adjusted). 356*b8851fccSafresh1See your system documentation for other possibly supported values. 357*b8851fccSafresh1 358*b8851fccSafresh1=item clock_getres ( $which ) 359*b8851fccSafresh1 360*b8851fccSafresh1Return as seconds the resolution of the POSIX high resolution timer 361*b8851fccSafresh1specified by C<$which>. All implementations that support POSIX high 362*b8851fccSafresh1resolution timers are supposed to support at least the C<$which> value 363*b8851fccSafresh1of C<CLOCK_REALTIME>, see L</clock_gettime>. 364*b8851fccSafresh1 365*b8851fccSafresh1B<NOTE>: the resolution returned may be highly optimistic. Even if 366*b8851fccSafresh1the resolution is high (a small number), all it means is that you'll 367*b8851fccSafresh1be able to specify the arguments to clock_gettime() and clock_nanosleep() 368*b8851fccSafresh1with that resolution. The system might not actually be able to measure 369*b8851fccSafresh1events at that resolution, and the various overheads and the overall system 370*b8851fccSafresh1load are certain to affect any timings. 371*b8851fccSafresh1 372*b8851fccSafresh1=item clock_nanosleep ( $which, $nanoseconds, $flags = 0) 373*b8851fccSafresh1 374*b8851fccSafresh1Sleeps for the number of nanoseconds (1e9ths of a second) specified. 375*b8851fccSafresh1Returns the number of nanoseconds actually slept. The $which is the 376*b8851fccSafresh1"clock id", as with clock_gettime() and clock_getres(). The flags 377*b8851fccSafresh1default to zero but C<TIMER_ABSTIME> can specified (must be exported 378*b8851fccSafresh1explicitly) which means that C<$nanoseconds> is not a time interval 379*b8851fccSafresh1(as is the default) but instead an absolute time. Can sleep for more 380*b8851fccSafresh1than one second. Can also sleep for zero seconds, which often works 381*b8851fccSafresh1like a I<thread yield>. See also C<Time::HiRes::sleep()>, 382*b8851fccSafresh1C<Time::HiRes::usleep()>, and C<Time::HiRes::nanosleep()>. 383*b8851fccSafresh1 384*b8851fccSafresh1Do not expect clock_nanosleep() to be exact down to one nanosecond. 385*b8851fccSafresh1Getting even accuracy of one thousand nanoseconds is good. 386*b8851fccSafresh1 387*b8851fccSafresh1=item clock() 388*b8851fccSafresh1 389*b8851fccSafresh1Return as seconds the I<process time> (user + system time) spent by 390*b8851fccSafresh1the process since the first call to clock() (the definition is B<not> 391*b8851fccSafresh1"since the start of the process", though if you are lucky these times 392*b8851fccSafresh1may be quite close to each other, depending on the system). What this 393*b8851fccSafresh1means is that you probably need to store the result of your first call 394*b8851fccSafresh1to clock(), and subtract that value from the following results of clock(). 395*b8851fccSafresh1 396*b8851fccSafresh1The time returned also includes the process times of the terminated 397*b8851fccSafresh1child processes for which wait() has been executed. This value is 398*b8851fccSafresh1somewhat like the second value returned by the times() of core Perl, 399*b8851fccSafresh1but not necessarily identical. Note that due to backward 400*b8851fccSafresh1compatibility limitations the returned value may wrap around at about 401*b8851fccSafresh12147 seconds or at about 36 minutes. 402*b8851fccSafresh1 403*b8851fccSafresh1=item stat 404*b8851fccSafresh1 405*b8851fccSafresh1=item stat FH 406*b8851fccSafresh1 407*b8851fccSafresh1=item stat EXPR 408*b8851fccSafresh1 409*b8851fccSafresh1=item lstat 410*b8851fccSafresh1 411*b8851fccSafresh1=item lstat FH 412*b8851fccSafresh1 413*b8851fccSafresh1=item lstat EXPR 414*b8851fccSafresh1 415*b8851fccSafresh1As L<perlfunc/stat> or L<perlfunc/lstat> 416*b8851fccSafresh1but with the access/modify/change file timestamps 417*b8851fccSafresh1in subsecond resolution, if the operating system and the filesystem 418*b8851fccSafresh1both support such timestamps. To override the standard stat(): 419*b8851fccSafresh1 420*b8851fccSafresh1 use Time::HiRes qw(stat); 421*b8851fccSafresh1 422*b8851fccSafresh1Test for the value of &Time::HiRes::d_hires_stat to find out whether 423*b8851fccSafresh1the operating system supports subsecond file timestamps: a value 424*b8851fccSafresh1larger than zero means yes. There are unfortunately no easy 425*b8851fccSafresh1ways to find out whether the filesystem supports such timestamps. 426*b8851fccSafresh1UNIX filesystems often do; NTFS does; FAT doesn't (FAT timestamp 427*b8851fccSafresh1granularity is B<two> seconds). 428*b8851fccSafresh1 429*b8851fccSafresh1A zero return value of &Time::HiRes::d_hires_stat means that 430*b8851fccSafresh1Time::HiRes::stat is a no-op passthrough for CORE::stat() 431*b8851fccSafresh1(and likewise for lstat), 432*b8851fccSafresh1and therefore the timestamps will stay integers. The same 433*b8851fccSafresh1thing will happen if the filesystem does not do subsecond timestamps, 434*b8851fccSafresh1even if the &Time::HiRes::d_hires_stat is non-zero. 435*b8851fccSafresh1 436*b8851fccSafresh1In any case do not expect nanosecond resolution, or even a microsecond 437*b8851fccSafresh1resolution. Also note that the modify/access timestamps might have 438*b8851fccSafresh1different resolutions, and that they need not be synchronized, e.g. 439*b8851fccSafresh1if the operations are 440*b8851fccSafresh1 441*b8851fccSafresh1 write 442*b8851fccSafresh1 stat # t1 443*b8851fccSafresh1 read 444*b8851fccSafresh1 stat # t2 445*b8851fccSafresh1 446*b8851fccSafresh1the access time stamp from t2 need not be greater-than the modify 447*b8851fccSafresh1time stamp from t1: it may be equal or I<less>. 448*b8851fccSafresh1 449*b8851fccSafresh1=back 450*b8851fccSafresh1 451*b8851fccSafresh1=head1 EXAMPLES 452*b8851fccSafresh1 453*b8851fccSafresh1 use Time::HiRes qw(usleep ualarm gettimeofday tv_interval); 454*b8851fccSafresh1 455*b8851fccSafresh1 $microseconds = 750_000; 456*b8851fccSafresh1 usleep($microseconds); 457*b8851fccSafresh1 458*b8851fccSafresh1 # signal alarm in 2.5s & every .1s thereafter 459*b8851fccSafresh1 ualarm(2_500_000, 100_000); 460*b8851fccSafresh1 # cancel that ualarm 461*b8851fccSafresh1 ualarm(0); 462*b8851fccSafresh1 463*b8851fccSafresh1 # get seconds and microseconds since the epoch 464*b8851fccSafresh1 ($s, $usec) = gettimeofday(); 465*b8851fccSafresh1 466*b8851fccSafresh1 # measure elapsed time 467*b8851fccSafresh1 # (could also do by subtracting 2 gettimeofday return values) 468*b8851fccSafresh1 $t0 = [gettimeofday]; 469*b8851fccSafresh1 # do bunch of stuff here 470*b8851fccSafresh1 $t1 = [gettimeofday]; 471*b8851fccSafresh1 # do more stuff here 472*b8851fccSafresh1 $t0_t1 = tv_interval $t0, $t1; 473*b8851fccSafresh1 474*b8851fccSafresh1 $elapsed = tv_interval ($t0, [gettimeofday]); 475*b8851fccSafresh1 $elapsed = tv_interval ($t0); # equivalent code 476*b8851fccSafresh1 477*b8851fccSafresh1 # 478*b8851fccSafresh1 # replacements for time, alarm and sleep that know about 479*b8851fccSafresh1 # floating seconds 480*b8851fccSafresh1 # 481*b8851fccSafresh1 use Time::HiRes; 482*b8851fccSafresh1 $now_fractions = Time::HiRes::time; 483*b8851fccSafresh1 Time::HiRes::sleep (2.5); 484*b8851fccSafresh1 Time::HiRes::alarm (10.6666666); 485*b8851fccSafresh1 486*b8851fccSafresh1 use Time::HiRes qw ( time alarm sleep ); 487*b8851fccSafresh1 $now_fractions = time; 488*b8851fccSafresh1 sleep (2.5); 489*b8851fccSafresh1 alarm (10.6666666); 490*b8851fccSafresh1 491*b8851fccSafresh1 # Arm an interval timer to go off first at 10 seconds and 492*b8851fccSafresh1 # after that every 2.5 seconds, in process virtual time 493*b8851fccSafresh1 494*b8851fccSafresh1 use Time::HiRes qw ( setitimer ITIMER_VIRTUAL time ); 495*b8851fccSafresh1 496*b8851fccSafresh1 $SIG{VTALRM} = sub { print time, "\n" }; 497*b8851fccSafresh1 setitimer(ITIMER_VIRTUAL, 10, 2.5); 498*b8851fccSafresh1 499*b8851fccSafresh1 use Time::HiRes qw( clock_gettime clock_getres CLOCK_REALTIME ); 500*b8851fccSafresh1 # Read the POSIX high resolution timer. 501*b8851fccSafresh1 my $high = clock_gettime(CLOCK_REALTIME); 502*b8851fccSafresh1 # But how accurate we can be, really? 503*b8851fccSafresh1 my $reso = clock_getres(CLOCK_REALTIME); 504*b8851fccSafresh1 505*b8851fccSafresh1 use Time::HiRes qw( clock_nanosleep TIMER_ABSTIME ); 506*b8851fccSafresh1 clock_nanosleep(CLOCK_REALTIME, 1e6); 507*b8851fccSafresh1 clock_nanosleep(CLOCK_REALTIME, 2e9, TIMER_ABSTIME); 508*b8851fccSafresh1 509*b8851fccSafresh1 use Time::HiRes qw( clock ); 510*b8851fccSafresh1 my $clock0 = clock(); 511*b8851fccSafresh1 ... # Do something. 512*b8851fccSafresh1 my $clock1 = clock(); 513*b8851fccSafresh1 my $clockd = $clock1 - $clock0; 514*b8851fccSafresh1 515*b8851fccSafresh1 use Time::HiRes qw( stat ); 516*b8851fccSafresh1 my ($atime, $mtime, $ctime) = (stat("istics"))[8, 9, 10]; 517*b8851fccSafresh1 518*b8851fccSafresh1=head1 C API 519*b8851fccSafresh1 520*b8851fccSafresh1In addition to the perl API described above, a C API is available for 521*b8851fccSafresh1extension writers. The following C functions are available in the 522*b8851fccSafresh1modglobal hash: 523*b8851fccSafresh1 524*b8851fccSafresh1 name C prototype 525*b8851fccSafresh1 --------------- ---------------------- 526*b8851fccSafresh1 Time::NVtime NV (*)() 527*b8851fccSafresh1 Time::U2time void (*)(pTHX_ UV ret[2]) 528*b8851fccSafresh1 529*b8851fccSafresh1Both functions return equivalent information (like C<gettimeofday>) 530*b8851fccSafresh1but with different representations. The names C<NVtime> and C<U2time> 531*b8851fccSafresh1were selected mainly because they are operating system independent. 532*b8851fccSafresh1(C<gettimeofday> is Unix-centric, though some platforms like Win32 and 533*b8851fccSafresh1VMS have emulations for it.) 534*b8851fccSafresh1 535*b8851fccSafresh1Here is an example of using C<NVtime> from C: 536*b8851fccSafresh1 537*b8851fccSafresh1 NV (*myNVtime)(); /* Returns -1 on failure. */ 538*b8851fccSafresh1 SV **svp = hv_fetch(PL_modglobal, "Time::NVtime", 12, 0); 539*b8851fccSafresh1 if (!svp) croak("Time::HiRes is required"); 540*b8851fccSafresh1 if (!SvIOK(*svp)) croak("Time::NVtime isn't a function pointer"); 541*b8851fccSafresh1 myNVtime = INT2PTR(NV(*)(), SvIV(*svp)); 542*b8851fccSafresh1 printf("The current time is: %" NVff "\n", (*myNVtime)()); 543*b8851fccSafresh1 544*b8851fccSafresh1=head1 DIAGNOSTICS 545*b8851fccSafresh1 546*b8851fccSafresh1=head2 useconds or interval more than ... 547*b8851fccSafresh1 548*b8851fccSafresh1In ualarm() you tried to use number of microseconds or interval (also 549*b8851fccSafresh1in microseconds) more than 1_000_000 and setitimer() is not available 550*b8851fccSafresh1in your system to emulate that case. 551*b8851fccSafresh1 552*b8851fccSafresh1=head2 negative time not invented yet 553*b8851fccSafresh1 554*b8851fccSafresh1You tried to use a negative time argument. 555*b8851fccSafresh1 556*b8851fccSafresh1=head2 internal error: useconds < 0 (unsigned ... signed ...) 557*b8851fccSafresh1 558*b8851fccSafresh1Something went horribly wrong-- the number of microseconds that cannot 559*b8851fccSafresh1become negative just became negative. Maybe your compiler is broken? 560*b8851fccSafresh1 561*b8851fccSafresh1=head2 useconds or uinterval equal to or more than 1000000 562*b8851fccSafresh1 563*b8851fccSafresh1In some platforms it is not possible to get an alarm with subsecond 564*b8851fccSafresh1resolution and later than one second. 565*b8851fccSafresh1 566*b8851fccSafresh1=head2 unimplemented in this platform 567*b8851fccSafresh1 568*b8851fccSafresh1Some calls simply aren't available, real or emulated, on every platform. 569*b8851fccSafresh1 570*b8851fccSafresh1=head1 CAVEATS 571*b8851fccSafresh1 572*b8851fccSafresh1Notice that the core C<time()> maybe rounding rather than truncating. 573*b8851fccSafresh1What this means is that the core C<time()> may be reporting the time 574*b8851fccSafresh1as one second later than C<gettimeofday()> and C<Time::HiRes::time()>. 575*b8851fccSafresh1 576*b8851fccSafresh1Adjusting the system clock (either manually or by services like ntp) 577*b8851fccSafresh1may cause problems, especially for long running programs that assume 578*b8851fccSafresh1a monotonously increasing time (note that all platforms do not adjust 579*b8851fccSafresh1time as gracefully as UNIX ntp does). For example in Win32 (and derived 580*b8851fccSafresh1platforms like Cygwin and MinGW) the Time::HiRes::time() may temporarily 581*b8851fccSafresh1drift off from the system clock (and the original time()) by up to 0.5 582*b8851fccSafresh1seconds. Time::HiRes will notice this eventually and recalibrate. 583*b8851fccSafresh1Note that since Time::HiRes 1.77 the clock_gettime(CLOCK_MONOTONIC) 584*b8851fccSafresh1might help in this (in case your system supports CLOCK_MONOTONIC). 585*b8851fccSafresh1 586*b8851fccSafresh1Some systems have APIs but not implementations: for example QNX and Haiku 587*b8851fccSafresh1have the interval timer APIs but not the functionality. 588*b8851fccSafresh1 589*b8851fccSafresh1In OS X clock_getres(), clock_gettime() and clock_nanosleep() are 590*b8851fccSafresh1emulated using the Mach timers; as a side effect of being emulated 591*b8851fccSafresh1the CLOCK_REALTIME and CLOCK_MONOTONIC are the same timer. 592*b8851fccSafresh1 593*b8851fccSafresh1=head1 SEE ALSO 594*b8851fccSafresh1 595*b8851fccSafresh1Perl modules L<BSD::Resource>, L<Time::TAI64>. 596*b8851fccSafresh1 597*b8851fccSafresh1Your system documentation for C<clock>, C<clock_gettime>, 598*b8851fccSafresh1C<clock_getres>, C<clock_nanosleep>, C<clock_settime>, C<getitimer>, 599*b8851fccSafresh1C<gettimeofday>, C<setitimer>, C<sleep>, C<stat>, C<ualarm>. 600*b8851fccSafresh1 601*b8851fccSafresh1=head1 AUTHORS 602*b8851fccSafresh1 603*b8851fccSafresh1D. Wegscheid <wegscd@whirlpool.com> 604*b8851fccSafresh1R. Schertler <roderick@argon.org> 605*b8851fccSafresh1J. Hietaniemi <jhi@iki.fi> 606*b8851fccSafresh1G. Aas <gisle@aas.no> 607*b8851fccSafresh1 608*b8851fccSafresh1=head1 COPYRIGHT AND LICENSE 609*b8851fccSafresh1 610*b8851fccSafresh1Copyright (c) 1996-2002 Douglas E. Wegscheid. All rights reserved. 611*b8851fccSafresh1 612*b8851fccSafresh1Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Jarkko Hietaniemi. 613*b8851fccSafresh1All rights reserved. 614*b8851fccSafresh1 615*b8851fccSafresh1Copyright (C) 2011, 2012, 2013 Andrew Main (Zefram) <zefram@fysh.org> 616*b8851fccSafresh1 617*b8851fccSafresh1This program is free software; you can redistribute it and/or modify 618*b8851fccSafresh1it under the same terms as Perl itself. 619*b8851fccSafresh1 620*b8851fccSafresh1=cut 621