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