package Time::HiRes; { use 5.006; } use strict; require Exporter; use XSLoader (); our @ISA = qw(Exporter); our @EXPORT = qw( ); # More or less this same list is in Makefile.PL. Should unify. our @EXPORT_OK = qw (usleep sleep ualarm alarm gettimeofday time tv_interval getitimer setitimer nanosleep clock_gettime clock_getres clock clock_nanosleep CLOCKS_PER_SEC CLOCK_BOOTTIME CLOCK_HIGHRES CLOCK_MONOTONIC CLOCK_MONOTONIC_COARSE CLOCK_MONOTONIC_FAST CLOCK_MONOTONIC_PRECISE CLOCK_MONOTONIC_RAW CLOCK_PROCESS_CPUTIME_ID CLOCK_PROF CLOCK_REALTIME CLOCK_REALTIME_COARSE CLOCK_REALTIME_FAST CLOCK_REALTIME_PRECISE CLOCK_REALTIME_RAW CLOCK_SECOND CLOCK_SOFTTIME CLOCK_THREAD_CPUTIME_ID CLOCK_TIMEOFDAY CLOCK_UPTIME CLOCK_UPTIME_COARSE CLOCK_UPTIME_FAST CLOCK_UPTIME_PRECISE CLOCK_UPTIME_RAW CLOCK_VIRTUAL ITIMER_PROF ITIMER_REAL ITIMER_REALPROF ITIMER_VIRTUAL TIMER_ABSTIME d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer d_nanosleep d_clock_gettime d_clock_getres d_clock d_clock_nanosleep d_hires_stat d_futimens d_utimensat d_hires_utime stat lstat utime ); our $VERSION = '1.9760'; our $XS_VERSION = $VERSION; $VERSION = eval $VERSION; our $AUTOLOAD; sub AUTOLOAD { my $constname; ($constname = $AUTOLOAD) =~ s/.*:://; # print "AUTOLOAD: constname = $constname ($AUTOLOAD)\n"; die "&Time::HiRes::constant not defined" if $constname eq 'constant'; my ($error, $val) = constant($constname); # print "AUTOLOAD: error = $error, val = $val\n"; if ($error) { my (undef,$file,$line) = caller; die "$error at $file line $line.\n"; } { no strict 'refs'; *$AUTOLOAD = sub { $val }; } goto &$AUTOLOAD; } sub import { my $this = shift; for my $i (@_) { if (($i eq 'clock_getres' && !&d_clock_getres) || ($i eq 'clock_gettime' && !&d_clock_gettime) || ($i eq 'clock_nanosleep' && !&d_clock_nanosleep) || ($i eq 'clock' && !&d_clock) || ($i eq 'nanosleep' && !&d_nanosleep) || ($i eq 'usleep' && !&d_usleep) || ($i eq 'utime' && !&d_hires_utime) || ($i eq 'ualarm' && !&d_ualarm)) { require Carp; Carp::croak("Time::HiRes::$i(): unimplemented in this platform"); } } Time::HiRes->export_to_level(1, $this, @_); } XSLoader::load( 'Time::HiRes', $XS_VERSION ); # Preloaded methods go here. sub tv_interval { # probably could have been done in C my ($a, $b) = @_; $b = [gettimeofday()] unless defined($b); (${$b}[0] - ${$a}[0]) + ((${$b}[1] - ${$a}[1]) / 1_000_000); } # Autoload methods go after =cut, and are processed by the autosplit program. 1; __END__ =head1 NAME Time::HiRes - High resolution alarm, sleep, gettimeofday, interval timers =head1 SYNOPSIS use Time::HiRes qw( usleep ualarm gettimeofday tv_interval nanosleep clock_gettime clock_getres clock_nanosleep clock stat lstat utime); usleep ($microseconds); nanosleep ($nanoseconds); ualarm ($microseconds); ualarm ($microseconds, $interval_microseconds); $t0 = [gettimeofday]; ($seconds, $microseconds) = gettimeofday; $elapsed = tv_interval ( $t0, [$seconds, $microseconds]); $elapsed = tv_interval ( $t0, [gettimeofday]); $elapsed = tv_interval ( $t0 ); use Time::HiRes qw ( time alarm sleep ); $now_fractions = time; sleep ($floating_seconds); alarm ($floating_seconds); alarm ($floating_seconds, $floating_interval); use Time::HiRes qw( setitimer getitimer ); setitimer ($which, $floating_seconds, $floating_interval ); getitimer ($which); use Time::HiRes qw( clock_gettime clock_getres clock_nanosleep ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF ITIMER_REALPROF ); $realtime = clock_gettime(CLOCK_REALTIME); $resolution = clock_getres(CLOCK_REALTIME); clock_nanosleep(CLOCK_REALTIME, 1.5e9); clock_nanosleep(CLOCK_REALTIME, time()*1e9 + 10e9, TIMER_ABSTIME); my $ticktock = clock(); use Time::HiRes qw( stat lstat ); my @stat = stat("file"); my @stat = stat(FH); my @stat = lstat("file"); use Time::HiRes qw( utime ); utime $floating_seconds, $floating_seconds, file...; =head1 DESCRIPTION The C module implements a Perl interface to the C, C, C, C, and C/C system calls, in other words, high resolution time and timers. See the L section below and the test scripts for usage; see your system documentation for the description of the underlying C or C, C, C, and C/C calls. If your system lacks C or an emulation of it you don't get C or the one-argument form of C. If your system lacks all of C, C, C, and C, you don't get C, C, or C. If your system lacks both C and C you don't get C or C. If you try to import an unimplemented function in the C statement it will fail at compile time. If your subsecond sleeping is implemented with C instead of C, you can mix subsecond sleeping with signals since C does not use signals. This, however, is not portable, and you should first check for the truth value of C<&Time::HiRes::d_nanosleep> to see whether you have nanosleep, and then carefully read your C C API documentation for any peculiarities. If you are using C for something else than mixing sleeping with signals, give some thought to whether Perl is the tool you should be using for work requiring nanosecond accuracies. Remember that unless you are working on a I system, any clocks and timers will be imprecise, especially so if you are working in a pre-emptive multiuser system. Understand the difference between I and process time (in UNIX-like systems the sum of I and I times). Any attempt to sleep for X seconds will most probably end up sleeping B than that, but don't be surprised if you end up sleeping slightly B. The following functions can be imported from this module. No functions are exported by default. =over 4 =item gettimeofday () In array context returns a two-element array with the seconds and microseconds since the epoch. In scalar context returns floating seconds like C (see below). =item usleep ( $useconds ) Sleeps for the number of microseconds (millionths of a second) specified. Returns the number of microseconds actually slept. Can sleep for more than one second, unlike the C system call. Can also sleep for zero seconds, which often works like a I. See also C, C, and C. Do not expect usleep() to be exact down to one microsecond. =item nanosleep ( $nanoseconds ) Sleeps for the number of nanoseconds (1e9ths of a second) specified. Returns the number of nanoseconds actually slept (accurate only to microseconds, the nearest thousand of them). Can sleep for more than one second. Can also sleep for zero seconds, which often works like a I. See also C, C, and C. Do not expect nanosleep() to be exact down to one nanosecond. Getting even accuracy of one thousand nanoseconds is good. =item ualarm ( $useconds [, $interval_useconds ] ) Issues a C call; the C<$interval_useconds> is optional and will be zero if unspecified, resulting in C-like behaviour. Returns the remaining time in the alarm in microseconds, or C if an error occurred. ualarm(0) will cancel an outstanding ualarm(). Note that the interaction between alarms and sleeps is unspecified. =item tv_interval tv_interval ( $ref_to_gettimeofday [, $ref_to_later_gettimeofday] ) Returns the floating seconds between the two times, which should have been returned by C. If the second argument is omitted, then the current time is used. =item time () Returns a floating seconds since the epoch. This function can be imported, resulting in a nice drop-in replacement for the C