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