1b8851fccSafresh1package Time::HiRes;
2b8851fccSafresh1
3b8851fccSafresh1{ use 5.006; }
4b8851fccSafresh1use strict;
5b8851fccSafresh1
6b8851fccSafresh1require Exporter;
7*9f11ffb7Safresh1use XSLoader ();
8b8851fccSafresh1
9*9f11ffb7Safresh1our @ISA = qw(Exporter);
10b8851fccSafresh1
11b8851fccSafresh1our @EXPORT = qw( );
12*9f11ffb7Safresh1# 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
16*9f11ffb7Safresh1		 CLOCKS_PER_SEC
17*9f11ffb7Safresh1		 CLOCK_BOOTTIME
18*9f11ffb7Safresh1		 CLOCK_HIGHRES
19*9f11ffb7Safresh1		 CLOCK_MONOTONIC
20*9f11ffb7Safresh1		 CLOCK_MONOTONIC_COARSE
21*9f11ffb7Safresh1		 CLOCK_MONOTONIC_FAST
22*9f11ffb7Safresh1		 CLOCK_MONOTONIC_PRECISE
23*9f11ffb7Safresh1		 CLOCK_MONOTONIC_RAW
24b8851fccSafresh1		 CLOCK_PROCESS_CPUTIME_ID
25*9f11ffb7Safresh1		 CLOCK_PROF
26*9f11ffb7Safresh1		 CLOCK_REALTIME
27*9f11ffb7Safresh1		 CLOCK_REALTIME_COARSE
28*9f11ffb7Safresh1		 CLOCK_REALTIME_FAST
29*9f11ffb7Safresh1		 CLOCK_REALTIME_PRECISE
30*9f11ffb7Safresh1		 CLOCK_REALTIME_RAW
31*9f11ffb7Safresh1		 CLOCK_SECOND
32*9f11ffb7Safresh1		 CLOCK_SOFTTIME
33*9f11ffb7Safresh1		 CLOCK_THREAD_CPUTIME_ID
34*9f11ffb7Safresh1		 CLOCK_TIMEOFDAY
35*9f11ffb7Safresh1		 CLOCK_UPTIME
36*9f11ffb7Safresh1		 CLOCK_UPTIME_COARSE
37*9f11ffb7Safresh1		 CLOCK_UPTIME_FAST
38*9f11ffb7Safresh1		 CLOCK_UPTIME_PRECISE
39*9f11ffb7Safresh1		 CLOCK_UPTIME_RAW
40*9f11ffb7Safresh1		 CLOCK_VIRTUAL
41*9f11ffb7Safresh1		 ITIMER_PROF
42*9f11ffb7Safresh1		 ITIMER_REAL
43*9f11ffb7Safresh1		 ITIMER_REALPROF
44*9f11ffb7Safresh1		 ITIMER_VIRTUAL
45b8851fccSafresh1		 TIMER_ABSTIME
46b8851fccSafresh1		 d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
47*9f11ffb7Safresh1		 d_nanosleep d_clock_gettime d_clock_getres
48*9f11ffb7Safresh1		 d_clock d_clock_nanosleep d_hires_stat
49*9f11ffb7Safresh1		 d_futimens d_utimensat d_hires_utime
50014083a1Safresh1		 stat lstat utime
51b8851fccSafresh1		);
52b8851fccSafresh1
53*9f11ffb7Safresh1our $VERSION = '1.9759';
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
94*9f11ffb7Safresh1XSLoader::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
146b8851fccSafresh1		      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>.
224b8851fccSafresh1See also C<Time::HiRes::usleep()>, C<Time::HiRes::sleep()>, and
225b8851fccSafresh1C<Time::HiRes::clock_nanosleep()>.
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
235b8851fccSafresh1a I<thread yield>.  See also C<Time::HiRes::sleep()>,
236b8851fccSafresh1C<Time::HiRes::usleep()>, and C<Time::HiRes::clock_nanosleep()>.
237b8851fccSafresh1
238b8851fccSafresh1Do not expect nanosleep() to be exact down to one nanosecond.
239b8851fccSafresh1Getting even accuracy of one thousand nanoseconds is good.
240b8851fccSafresh1
241b8851fccSafresh1=item ualarm ( $useconds [, $interval_useconds ] )
242b8851fccSafresh1
243b8851fccSafresh1Issues a C<ualarm> call; the C<$interval_useconds> is optional and
244b8851fccSafresh1will be zero if unspecified, resulting in C<alarm>-like behaviour.
245b8851fccSafresh1
246b8851fccSafresh1Returns the remaining time in the alarm in microseconds, or C<undef>
247b8851fccSafresh1if an error occurred.
248b8851fccSafresh1
249b8851fccSafresh1ualarm(0) will cancel an outstanding ualarm().
250b8851fccSafresh1
251b8851fccSafresh1Note that the interaction between alarms and sleeps is unspecified.
252b8851fccSafresh1
253b8851fccSafresh1=item tv_interval
254b8851fccSafresh1
255b8851fccSafresh1tv_interval ( $ref_to_gettimeofday [, $ref_to_later_gettimeofday] )
256b8851fccSafresh1
257b8851fccSafresh1Returns the floating seconds between the two times, which should have
258b8851fccSafresh1been returned by C<gettimeofday()>. If the second argument is omitted,
259b8851fccSafresh1then the current time is used.
260b8851fccSafresh1
261b8851fccSafresh1=item time ()
262b8851fccSafresh1
263b8851fccSafresh1Returns a floating seconds since the epoch. This function can be
264b8851fccSafresh1imported, resulting in a nice drop-in replacement for the C<time>
265b8851fccSafresh1provided with core Perl; see the L</EXAMPLES> below.
266b8851fccSafresh1
267b8851fccSafresh1B<NOTE 1>: This higher resolution timer can return values either less
268b8851fccSafresh1or more than the core C<time()>, depending on whether your platform
269b8851fccSafresh1rounds the higher resolution timer values up, down, or to the nearest second
270b8851fccSafresh1to get the core C<time()>, but naturally the difference should be never
271b8851fccSafresh1more than half a second.  See also L</clock_getres>, if available
272b8851fccSafresh1in your system.
273b8851fccSafresh1
274b8851fccSafresh1B<NOTE 2>: Since Sunday, September 9th, 2001 at 01:46:40 AM GMT, when
275b8851fccSafresh1the C<time()> seconds since epoch rolled over to 1_000_000_000, the
276b8851fccSafresh1default floating point format of Perl and the seconds since epoch have
277b8851fccSafresh1conspired to produce an apparent bug: if you print the value of
278b8851fccSafresh1C<Time::HiRes::time()> you seem to be getting only five decimals, not
279b8851fccSafresh1six as promised (microseconds).  Not to worry, the microseconds are
280b8851fccSafresh1there (assuming your platform supports such granularity in the first
281b8851fccSafresh1place).  What is going on is that the default floating point format of
282b8851fccSafresh1Perl only outputs 15 digits.  In this case that means ten digits
283b8851fccSafresh1before the decimal separator and five after.  To see the microseconds
284b8851fccSafresh1you can use either C<printf>/C<sprintf> with C<"%.6f">, or the
285b8851fccSafresh1C<gettimeofday()> function in list context, which will give you the
286b8851fccSafresh1seconds and microseconds as two separate values.
287b8851fccSafresh1
288b8851fccSafresh1=item sleep ( $floating_seconds )
289b8851fccSafresh1
290b8851fccSafresh1Sleeps for the specified amount of seconds.  Returns the number of
291b8851fccSafresh1seconds actually slept (a floating point value).  This function can
292b8851fccSafresh1be imported, resulting in a nice drop-in replacement for the C<sleep>
293b8851fccSafresh1provided with perl, see the L</EXAMPLES> below.
294b8851fccSafresh1
295b8851fccSafresh1Note that the interaction between alarms and sleeps is unspecified.
296b8851fccSafresh1
297b8851fccSafresh1=item alarm ( $floating_seconds [, $interval_floating_seconds ] )
298b8851fccSafresh1
299b8851fccSafresh1The C<SIGALRM> signal is sent after the specified number of seconds.
300b8851fccSafresh1Implemented using C<setitimer()> if available, C<ualarm()> if not.
301b8851fccSafresh1The C<$interval_floating_seconds> argument is optional and will be
302b8851fccSafresh1zero if unspecified, resulting in C<alarm()>-like behaviour.  This
303b8851fccSafresh1function can be imported, resulting in a nice drop-in replacement for
304b8851fccSafresh1the C<alarm> provided with perl, see the L</EXAMPLES> below.
305b8851fccSafresh1
306b8851fccSafresh1Returns the remaining time in the alarm in seconds, or C<undef>
307b8851fccSafresh1if an error occurred.
308b8851fccSafresh1
309b8851fccSafresh1B<NOTE 1>: With some combinations of operating systems and Perl
310b8851fccSafresh1releases C<SIGALRM> restarts C<select()>, instead of interrupting it.
311b8851fccSafresh1This means that an C<alarm()> followed by a C<select()> may together
312b8851fccSafresh1take the sum of the times specified for the C<alarm()> and the
313b8851fccSafresh1C<select()>, not just the time of the C<alarm()>.
314b8851fccSafresh1
315b8851fccSafresh1Note that the interaction between alarms and sleeps is unspecified.
316b8851fccSafresh1
317b8851fccSafresh1=item setitimer ( $which, $floating_seconds [, $interval_floating_seconds ] )
318b8851fccSafresh1
319b8851fccSafresh1Start up an interval timer: after a certain time, a signal ($which) arrives,
320b8851fccSafresh1and more signals may keep arriving at certain intervals.  To disable
321b8851fccSafresh1an "itimer", use C<$floating_seconds> of zero.  If the
322b8851fccSafresh1C<$interval_floating_seconds> is set to zero (or unspecified), the
323b8851fccSafresh1timer is disabled B<after> the next delivered signal.
324b8851fccSafresh1
325b8851fccSafresh1Use of interval timers may interfere with C<alarm()>, C<sleep()>,
326b8851fccSafresh1and C<usleep()>.  In standard-speak the "interaction is unspecified",
327b8851fccSafresh1which means that I<anything> may happen: it may work, it may not.
328b8851fccSafresh1
329b8851fccSafresh1In scalar context, the remaining time in the timer is returned.
330b8851fccSafresh1
331b8851fccSafresh1In list context, both the remaining time and the interval are returned.
332b8851fccSafresh1
333b8851fccSafresh1There are usually three or four interval timers (signals) available: the
334b8851fccSafresh1C<$which> can be C<ITIMER_REAL>, C<ITIMER_VIRTUAL>, C<ITIMER_PROF>, or
335b8851fccSafresh1C<ITIMER_REALPROF>.  Note that which ones are available depends: true
336b8851fccSafresh1UNIX platforms usually have the first three, but only Solaris seems to
337b8851fccSafresh1have C<ITIMER_REALPROF> (which is used to profile multithreaded programs).
338b8851fccSafresh1Win32 unfortunately does not have interval timers.
339b8851fccSafresh1
340b8851fccSafresh1C<ITIMER_REAL> results in C<alarm()>-like behaviour.  Time is counted in
341b8851fccSafresh1I<real time>; that is, wallclock time.  C<SIGALRM> is delivered when
342b8851fccSafresh1the timer expires.
343b8851fccSafresh1
344b8851fccSafresh1C<ITIMER_VIRTUAL> counts time in (process) I<virtual time>; that is,
345b8851fccSafresh1only when the process is running.  In multiprocessor/user/CPU systems
346b8851fccSafresh1this may be more or less than real or wallclock time.  (This time is
347b8851fccSafresh1also known as the I<user time>.)  C<SIGVTALRM> is delivered when the
348b8851fccSafresh1timer expires.
349b8851fccSafresh1
350b8851fccSafresh1C<ITIMER_PROF> counts time when either the process virtual time or when
351b8851fccSafresh1the operating system is running on behalf of the process (such as I/O).
352b8851fccSafresh1(This time is also known as the I<system time>.)  (The sum of user
353b8851fccSafresh1time and system time is known as the I<CPU time>.)  C<SIGPROF> is
354b8851fccSafresh1delivered when the timer expires.  C<SIGPROF> can interrupt system calls.
355b8851fccSafresh1
356b8851fccSafresh1The semantics of interval timers for multithreaded programs are
357b8851fccSafresh1system-specific, and some systems may support additional interval
358b8851fccSafresh1timers.  For example, it is unspecified which thread gets the signals.
359b8851fccSafresh1See your C<setitimer()> documentation.
360b8851fccSafresh1
361b8851fccSafresh1=item getitimer ( $which )
362b8851fccSafresh1
363b8851fccSafresh1Return the remaining time in the interval timer specified by C<$which>.
364b8851fccSafresh1
365b8851fccSafresh1In scalar context, the remaining time is returned.
366b8851fccSafresh1
367b8851fccSafresh1In list context, both the remaining time and the interval are returned.
368b8851fccSafresh1The interval is always what you put in using C<setitimer()>.
369b8851fccSafresh1
370b8851fccSafresh1=item clock_gettime ( $which )
371b8851fccSafresh1
372b8851fccSafresh1Return as seconds the current value of the POSIX high resolution timer
373b8851fccSafresh1specified by C<$which>.  All implementations that support POSIX high
374b8851fccSafresh1resolution timers are supposed to support at least the C<$which> value
375b8851fccSafresh1of C<CLOCK_REALTIME>, which is supposed to return results close to the
376b8851fccSafresh1results of C<gettimeofday>, or the number of seconds since 00:00:00:00
377b8851fccSafresh1January 1, 1970 Greenwich Mean Time (GMT).  Do not assume that
378b8851fccSafresh1CLOCK_REALTIME is zero, it might be one, or something else.
379b8851fccSafresh1Another potentially useful (but not available everywhere) value is
380b8851fccSafresh1C<CLOCK_MONOTONIC>, which guarantees a monotonically increasing time
381b8851fccSafresh1value (unlike time() or gettimeofday(), which can be adjusted).
382b8851fccSafresh1See your system documentation for other possibly supported values.
383b8851fccSafresh1
384b8851fccSafresh1=item clock_getres ( $which )
385b8851fccSafresh1
386b8851fccSafresh1Return as seconds the resolution of the POSIX high resolution timer
387b8851fccSafresh1specified by C<$which>.  All implementations that support POSIX high
388b8851fccSafresh1resolution timers are supposed to support at least the C<$which> value
389b8851fccSafresh1of C<CLOCK_REALTIME>, see L</clock_gettime>.
390b8851fccSafresh1
391b8851fccSafresh1B<NOTE>: the resolution returned may be highly optimistic.  Even if
392b8851fccSafresh1the resolution is high (a small number), all it means is that you'll
393b8851fccSafresh1be able to specify the arguments to clock_gettime() and clock_nanosleep()
394b8851fccSafresh1with that resolution.  The system might not actually be able to measure
395b8851fccSafresh1events at that resolution, and the various overheads and the overall system
396b8851fccSafresh1load are certain to affect any timings.
397b8851fccSafresh1
398b8851fccSafresh1=item clock_nanosleep ( $which, $nanoseconds, $flags = 0)
399b8851fccSafresh1
400b8851fccSafresh1Sleeps for the number of nanoseconds (1e9ths of a second) specified.
401b8851fccSafresh1Returns the number of nanoseconds actually slept.  The $which is the
402b8851fccSafresh1"clock id", as with clock_gettime() and clock_getres().  The flags
403b8851fccSafresh1default to zero but C<TIMER_ABSTIME> can specified (must be exported
404b8851fccSafresh1explicitly) which means that C<$nanoseconds> is not a time interval
405b8851fccSafresh1(as is the default) but instead an absolute time.  Can sleep for more
406b8851fccSafresh1than one second.  Can also sleep for zero seconds, which often works
407b8851fccSafresh1like a I<thread yield>.  See also C<Time::HiRes::sleep()>,
408b8851fccSafresh1C<Time::HiRes::usleep()>, and C<Time::HiRes::nanosleep()>.
409b8851fccSafresh1
410b8851fccSafresh1Do not expect clock_nanosleep() to be exact down to one nanosecond.
411b8851fccSafresh1Getting even accuracy of one thousand nanoseconds is good.
412b8851fccSafresh1
413b8851fccSafresh1=item clock()
414b8851fccSafresh1
415b8851fccSafresh1Return as seconds the I<process time> (user + system time) spent by
416b8851fccSafresh1the process since the first call to clock() (the definition is B<not>
417b8851fccSafresh1"since the start of the process", though if you are lucky these times
418b8851fccSafresh1may be quite close to each other, depending on the system).  What this
419b8851fccSafresh1means is that you probably need to store the result of your first call
420b8851fccSafresh1to clock(), and subtract that value from the following results of clock().
421b8851fccSafresh1
422b8851fccSafresh1The time returned also includes the process times of the terminated
423b8851fccSafresh1child processes for which wait() has been executed.  This value is
424b8851fccSafresh1somewhat like the second value returned by the times() of core Perl,
425b8851fccSafresh1but not necessarily identical.  Note that due to backward
426b8851fccSafresh1compatibility limitations the returned value may wrap around at about
427b8851fccSafresh12147 seconds or at about 36 minutes.
428b8851fccSafresh1
429b8851fccSafresh1=item stat
430b8851fccSafresh1
431b8851fccSafresh1=item stat FH
432b8851fccSafresh1
433b8851fccSafresh1=item stat EXPR
434b8851fccSafresh1
435b8851fccSafresh1=item lstat
436b8851fccSafresh1
437b8851fccSafresh1=item lstat FH
438b8851fccSafresh1
439b8851fccSafresh1=item lstat EXPR
440b8851fccSafresh1
441b8851fccSafresh1As L<perlfunc/stat> or L<perlfunc/lstat>
442b8851fccSafresh1but with the access/modify/change file timestamps
443b8851fccSafresh1in subsecond resolution, if the operating system and the filesystem
444b8851fccSafresh1both support such timestamps.  To override the standard stat():
445b8851fccSafresh1
446b8851fccSafresh1    use Time::HiRes qw(stat);
447b8851fccSafresh1
448b8851fccSafresh1Test for the value of &Time::HiRes::d_hires_stat to find out whether
449b8851fccSafresh1the operating system supports subsecond file timestamps: a value
450b8851fccSafresh1larger than zero means yes. There are unfortunately no easy
451b8851fccSafresh1ways to find out whether the filesystem supports such timestamps.
452b8851fccSafresh1UNIX filesystems often do; NTFS does; FAT doesn't (FAT timestamp
453b8851fccSafresh1granularity is B<two> seconds).
454b8851fccSafresh1
455b8851fccSafresh1A zero return value of &Time::HiRes::d_hires_stat means that
456b8851fccSafresh1Time::HiRes::stat is a no-op passthrough for CORE::stat()
457b8851fccSafresh1(and likewise for lstat),
458b8851fccSafresh1and therefore the timestamps will stay integers.  The same
459b8851fccSafresh1thing will happen if the filesystem does not do subsecond timestamps,
460b8851fccSafresh1even if the &Time::HiRes::d_hires_stat is non-zero.
461b8851fccSafresh1
462b8851fccSafresh1In any case do not expect nanosecond resolution, or even a microsecond
463b8851fccSafresh1resolution.  Also note that the modify/access timestamps might have
464b8851fccSafresh1different resolutions, and that they need not be synchronized, e.g.
465b8851fccSafresh1if the operations are
466b8851fccSafresh1
467b8851fccSafresh1    write
468b8851fccSafresh1    stat # t1
469b8851fccSafresh1    read
470b8851fccSafresh1    stat # t2
471b8851fccSafresh1
472b8851fccSafresh1the access time stamp from t2 need not be greater-than the modify
473b8851fccSafresh1time stamp from t1: it may be equal or I<less>.
474b8851fccSafresh1
475014083a1Safresh1=item utime LIST
476014083a1Safresh1
477014083a1Safresh1As L<perlfunc/utime>
478014083a1Safresh1but with the ability to set the access/modify file timestamps
479*9f11ffb7Safresh1in subsecond resolution, if the operating system and the filesystem,
480*9f11ffb7Safresh1and the mount options of the filesystem, all support such timestamps.
481*9f11ffb7Safresh1
482*9f11ffb7Safresh1To override the standard utime():
483014083a1Safresh1
484014083a1Safresh1    use Time::HiRes qw(utime);
485014083a1Safresh1
486014083a1Safresh1Test for the value of &Time::HiRes::d_hires_utime to find out whether
487014083a1Safresh1the operating system supports setting subsecond file timestamps.
488014083a1Safresh1
489014083a1Safresh1As with CORE::utime(), passing undef as both the atime and mtime will
490014083a1Safresh1call the syscall with a NULL argument.
491014083a1Safresh1
492014083a1Safresh1The actual achievable subsecond resolution depends on the combination
493014083a1Safresh1of the operating system and the filesystem.
494014083a1Safresh1
495*9f11ffb7Safresh1Modifying the timestamps may not be possible at all: for example, the
496*9f11ffb7Safresh1C<noatime> filesystem mount option may prohibit you from changing the
497*9f11ffb7Safresh1access time timestamp.
498*9f11ffb7Safresh1
499014083a1Safresh1Returns the number of files successfully changed.
500014083a1Safresh1
501b8851fccSafresh1=back
502b8851fccSafresh1
503b8851fccSafresh1=head1 EXAMPLES
504b8851fccSafresh1
505b8851fccSafresh1  use Time::HiRes qw(usleep ualarm gettimeofday tv_interval);
506b8851fccSafresh1
507b8851fccSafresh1  $microseconds = 750_000;
508b8851fccSafresh1  usleep($microseconds);
509b8851fccSafresh1
510b8851fccSafresh1  # signal alarm in 2.5s & every .1s thereafter
511b8851fccSafresh1  ualarm(2_500_000, 100_000);
512b8851fccSafresh1  # cancel that ualarm
513b8851fccSafresh1  ualarm(0);
514b8851fccSafresh1
515b8851fccSafresh1  # get seconds and microseconds since the epoch
516b8851fccSafresh1  ($s, $usec) = gettimeofday();
517b8851fccSafresh1
518b8851fccSafresh1  # measure elapsed time
519b8851fccSafresh1  # (could also do by subtracting 2 gettimeofday return values)
520b8851fccSafresh1  $t0 = [gettimeofday];
521b8851fccSafresh1  # do bunch of stuff here
522b8851fccSafresh1  $t1 = [gettimeofday];
523b8851fccSafresh1  # do more stuff here
524b8851fccSafresh1  $t0_t1 = tv_interval $t0, $t1;
525b8851fccSafresh1
526b8851fccSafresh1  $elapsed = tv_interval ($t0, [gettimeofday]);
527b8851fccSafresh1  $elapsed = tv_interval ($t0);	# equivalent code
528b8851fccSafresh1
529b8851fccSafresh1  #
530b8851fccSafresh1  # replacements for time, alarm and sleep that know about
531b8851fccSafresh1  # floating seconds
532b8851fccSafresh1  #
533b8851fccSafresh1  use Time::HiRes;
534b8851fccSafresh1  $now_fractions = Time::HiRes::time;
535b8851fccSafresh1  Time::HiRes::sleep (2.5);
536b8851fccSafresh1  Time::HiRes::alarm (10.6666666);
537b8851fccSafresh1
538b8851fccSafresh1  use Time::HiRes qw ( time alarm sleep );
539b8851fccSafresh1  $now_fractions = time;
540b8851fccSafresh1  sleep (2.5);
541b8851fccSafresh1  alarm (10.6666666);
542b8851fccSafresh1
543b8851fccSafresh1  # Arm an interval timer to go off first at 10 seconds and
544b8851fccSafresh1  # after that every 2.5 seconds, in process virtual time
545b8851fccSafresh1
546b8851fccSafresh1  use Time::HiRes qw ( setitimer ITIMER_VIRTUAL time );
547b8851fccSafresh1
548b8851fccSafresh1  $SIG{VTALRM} = sub { print time, "\n" };
549b8851fccSafresh1  setitimer(ITIMER_VIRTUAL, 10, 2.5);
550b8851fccSafresh1
551b8851fccSafresh1  use Time::HiRes qw( clock_gettime clock_getres CLOCK_REALTIME );
552b8851fccSafresh1  # Read the POSIX high resolution timer.
553b8851fccSafresh1  my $high = clock_gettime(CLOCK_REALTIME);
554b8851fccSafresh1  # But how accurate we can be, really?
555b8851fccSafresh1  my $reso = clock_getres(CLOCK_REALTIME);
556b8851fccSafresh1
557b8851fccSafresh1  use Time::HiRes qw( clock_nanosleep TIMER_ABSTIME );
558b8851fccSafresh1  clock_nanosleep(CLOCK_REALTIME, 1e6);
559b8851fccSafresh1  clock_nanosleep(CLOCK_REALTIME, 2e9, TIMER_ABSTIME);
560b8851fccSafresh1
561b8851fccSafresh1  use Time::HiRes qw( clock );
562b8851fccSafresh1  my $clock0 = clock();
563b8851fccSafresh1  ... # Do something.
564b8851fccSafresh1  my $clock1 = clock();
565b8851fccSafresh1  my $clockd = $clock1 - $clock0;
566b8851fccSafresh1
567b8851fccSafresh1  use Time::HiRes qw( stat );
568b8851fccSafresh1  my ($atime, $mtime, $ctime) = (stat("istics"))[8, 9, 10];
569b8851fccSafresh1
570b8851fccSafresh1=head1 C API
571b8851fccSafresh1
572b8851fccSafresh1In addition to the perl API described above, a C API is available for
573b8851fccSafresh1extension writers.  The following C functions are available in the
574b8851fccSafresh1modglobal hash:
575b8851fccSafresh1
576b8851fccSafresh1  name             C prototype
577b8851fccSafresh1  ---------------  ----------------------
578b8851fccSafresh1  Time::NVtime     NV (*)()
579b8851fccSafresh1  Time::U2time     void (*)(pTHX_ UV ret[2])
580b8851fccSafresh1
581b8851fccSafresh1Both functions return equivalent information (like C<gettimeofday>)
582b8851fccSafresh1but with different representations.  The names C<NVtime> and C<U2time>
583b8851fccSafresh1were selected mainly because they are operating system independent.
584b8851fccSafresh1(C<gettimeofday> is Unix-centric, though some platforms like Win32 and
585b8851fccSafresh1VMS have emulations for it.)
586b8851fccSafresh1
587b8851fccSafresh1Here is an example of using C<NVtime> from C:
588b8851fccSafresh1
589b8851fccSafresh1  NV (*myNVtime)(); /* Returns -1 on failure. */
590c0dd97bfSafresh1  SV **svp = hv_fetchs(PL_modglobal, "Time::NVtime", 0);
591b8851fccSafresh1  if (!svp)         croak("Time::HiRes is required");
592b8851fccSafresh1  if (!SvIOK(*svp)) croak("Time::NVtime isn't a function pointer");
593b8851fccSafresh1  myNVtime = INT2PTR(NV(*)(), SvIV(*svp));
594b8851fccSafresh1  printf("The current time is: %" NVff "\n", (*myNVtime)());
595b8851fccSafresh1
596b8851fccSafresh1=head1 DIAGNOSTICS
597b8851fccSafresh1
598b8851fccSafresh1=head2 useconds or interval more than ...
599b8851fccSafresh1
600b8851fccSafresh1In ualarm() you tried to use number of microseconds or interval (also
601b8851fccSafresh1in microseconds) more than 1_000_000 and setitimer() is not available
602b8851fccSafresh1in your system to emulate that case.
603b8851fccSafresh1
604b8851fccSafresh1=head2 negative time not invented yet
605b8851fccSafresh1
606b8851fccSafresh1You tried to use a negative time argument.
607b8851fccSafresh1
608b8851fccSafresh1=head2 internal error: useconds < 0 (unsigned ... signed ...)
609b8851fccSafresh1
610b8851fccSafresh1Something went horribly wrong-- the number of microseconds that cannot
611b8851fccSafresh1become negative just became negative.  Maybe your compiler is broken?
612b8851fccSafresh1
613b8851fccSafresh1=head2 useconds or uinterval equal to or more than 1000000
614b8851fccSafresh1
615b8851fccSafresh1In some platforms it is not possible to get an alarm with subsecond
616b8851fccSafresh1resolution and later than one second.
617b8851fccSafresh1
618b8851fccSafresh1=head2 unimplemented in this platform
619b8851fccSafresh1
620b8851fccSafresh1Some calls simply aren't available, real or emulated, on every platform.
621b8851fccSafresh1
622b8851fccSafresh1=head1 CAVEATS
623b8851fccSafresh1
624b8851fccSafresh1Notice that the core C<time()> maybe rounding rather than truncating.
625b8851fccSafresh1What this means is that the core C<time()> may be reporting the time
626b8851fccSafresh1as one second later than C<gettimeofday()> and C<Time::HiRes::time()>.
627b8851fccSafresh1
628b8851fccSafresh1Adjusting the system clock (either manually or by services like ntp)
629b8851fccSafresh1may cause problems, especially for long running programs that assume
630b8851fccSafresh1a monotonously increasing time (note that all platforms do not adjust
631b8851fccSafresh1time as gracefully as UNIX ntp does).  For example in Win32 (and derived
632b8851fccSafresh1platforms like Cygwin and MinGW) the Time::HiRes::time() may temporarily
633b8851fccSafresh1drift off from the system clock (and the original time())  by up to 0.5
634b8851fccSafresh1seconds. Time::HiRes will notice this eventually and recalibrate.
635b8851fccSafresh1Note that since Time::HiRes 1.77 the clock_gettime(CLOCK_MONOTONIC)
636b8851fccSafresh1might help in this (in case your system supports CLOCK_MONOTONIC).
637b8851fccSafresh1
638b8851fccSafresh1Some systems have APIs but not implementations: for example QNX and Haiku
639b8851fccSafresh1have the interval timer APIs but not the functionality.
640b8851fccSafresh1
641014083a1Safresh1In pre-Sierra macOS (pre-10.12, OS X) clock_getres(), clock_gettime()
642014083a1Safresh1and clock_nanosleep() are emulated using the Mach timers; as a side
643014083a1Safresh1effect of being emulated the CLOCK_REALTIME and CLOCK_MONOTONIC are
644014083a1Safresh1the same timer.
645014083a1Safresh1
646014083a1Safresh1gnukfreebsd seems to have non-functional futimens() and utimensat()
647014083a1Safresh1(at least as of 10.1): therefore the hires utime() does not work.
648b8851fccSafresh1
649b8851fccSafresh1=head1 SEE ALSO
650b8851fccSafresh1
651b8851fccSafresh1Perl modules L<BSD::Resource>, L<Time::TAI64>.
652b8851fccSafresh1
653b8851fccSafresh1Your system documentation for C<clock>, C<clock_gettime>,
654b8851fccSafresh1C<clock_getres>, C<clock_nanosleep>, C<clock_settime>, C<getitimer>,
655b8851fccSafresh1C<gettimeofday>, C<setitimer>, C<sleep>, C<stat>, C<ualarm>.
656b8851fccSafresh1
657b8851fccSafresh1=head1 AUTHORS
658b8851fccSafresh1
659b8851fccSafresh1D. Wegscheid <wegscd@whirlpool.com>
660b8851fccSafresh1R. Schertler <roderick@argon.org>
661b8851fccSafresh1J. Hietaniemi <jhi@iki.fi>
662b8851fccSafresh1G. Aas <gisle@aas.no>
663b8851fccSafresh1
664b8851fccSafresh1=head1 COPYRIGHT AND LICENSE
665b8851fccSafresh1
666b8851fccSafresh1Copyright (c) 1996-2002 Douglas E. Wegscheid.  All rights reserved.
667b8851fccSafresh1
668b8851fccSafresh1Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Jarkko Hietaniemi.
669b8851fccSafresh1All rights reserved.
670b8851fccSafresh1
671b8851fccSafresh1Copyright (C) 2011, 2012, 2013 Andrew Main (Zefram) <zefram@fysh.org>
672b8851fccSafresh1
673b8851fccSafresh1This program is free software; you can redistribute it and/or modify
674b8851fccSafresh1it under the same terms as Perl itself.
675b8851fccSafresh1
676b8851fccSafresh1=cut
677