Upgrade to Time::HiRes 1.63.
[p5sagit/p5-mst-13.2.git] / ext / Time / HiRes / HiRes.pm
CommitLineData
dcf686c9 1package Time::HiRes;
2
3use strict;
105cd853 4use vars qw($VERSION $XS_VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD);
dcf686c9 5
6require Exporter;
3f2ee006 7require DynaLoader;
dcf686c9 8
3f2ee006 9@ISA = qw(Exporter DynaLoader);
dcf686c9 10
11@EXPORT = qw( );
3c72ec00 12@EXPORT_OK = qw (usleep sleep ualarm alarm gettimeofday time tv_interval
3f2ee006 13 getitimer setitimer
14 ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF ITIMER_REALPROF
15 d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
16 d_nanosleep);
17
1fbb4de4 18$VERSION = '1.63';
105cd853 19$XS_VERSION = $VERSION;
20$VERSION = eval $VERSION;
3c72ec00 21
22sub AUTOLOAD {
23 my $constname;
98b50af3 24 ($constname = $AUTOLOAD) =~ s/.*:://;
25 die "&Time::HiRes::constant not defined" if $constname eq 'constant';
26 my ($error, $val) = constant($constname);
27 if ($error) { die $error; }
3c72ec00 28 {
29 no strict 'refs';
30 *$AUTOLOAD = sub { $val };
31 }
32 goto &$AUTOLOAD;
33}
dcf686c9 34
3f2ee006 35bootstrap Time::HiRes;
dcf686c9 36
37# Preloaded methods go here.
38
39sub tv_interval {
40 # probably could have been done in C
41 my ($a, $b) = @_;
42 $b = [gettimeofday()] unless defined($b);
43 (${$b}[0] - ${$a}[0]) + ((${$b}[1] - ${$a}[1]) / 1_000_000);
44}
45
dcf686c9 46# Autoload methods go after =cut, and are processed by the autosplit program.
47
481;
49__END__
50
51=head1 NAME
52
f7916ddb 53Time::HiRes - High resolution alarm, sleep, gettimeofday, interval timers
dcf686c9 54
55=head1 SYNOPSIS
56
57 use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );
58
59 usleep ($microseconds);
60
61 ualarm ($microseconds);
62 ualarm ($microseconds, $interval_microseconds);
63
64 $t0 = [gettimeofday];
65 ($seconds, $microseconds) = gettimeofday;
66
67 $elapsed = tv_interval ( $t0, [$seconds, $microseconds]);
68 $elapsed = tv_interval ( $t0, [gettimeofday]);
69 $elapsed = tv_interval ( $t0 );
70
71 use Time::HiRes qw ( time alarm sleep );
3c72ec00 72
dcf686c9 73 $now_fractions = time;
74 sleep ($floating_seconds);
75 alarm ($floating_seconds);
76 alarm ($floating_seconds, $floating_interval);
77
3c72ec00 78 use Time::HiRes qw( setitimer getitimer
3f2ee006 79 ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF ITIMER_REALPROF );
3c72ec00 80
81 setitimer ($which, $floating_seconds, $floating_interval );
82 getitimer ($which);
83
dcf686c9 84=head1 DESCRIPTION
85
4ed0e2d4 86The C<Time::HiRes> module implements a Perl interface to the
87C<usleep>, C<ualarm>, C<gettimeofday>, and C<setitimer>/C<getitimer>
88system calls, in other words, high resolution time and timers. See the
89L</EXAMPLES> section below and the test scripts for usage; see your
90system documentation for the description of the underlying
91C<nanosleep> or C<usleep>, C<ualarm>, C<gettimeofday>, and
92C<setitimer>/C<getitimer> calls.
dcf686c9 93
6937b144 94If your system lacks C<gettimeofday()> or an emulation of it you don't
4ed0e2d4 95get C<gettimeofday()> or the one-argument form of C<tv_interval()>.
96If your system lacks all of C<nanosleep()>, C<usleep()>, and
97C<select()>, you don't get C<Time::HiRes::usleep()> or
98C<Time::HiRes::sleep()>. If your system lacks both C<ualarm()> and
99C<setitimer()> you don't get C<Time::HiRes::ualarm()> or
100C<Time::HiRes::alarm()>.
3f2ee006 101
102If you try to import an unimplemented function in the C<use> statement
103it will fail at compile time.
104
4ed0e2d4 105If your subsecond sleeping is implemented with C<nanosleep()> instead
106of C<usleep()>, you can mix subsecond sleeping with signals since
107C<nanosleep()> does not use signals. This, however is unportable, and
108you should first check for the truth value of
109C<&Time::HiRes::d_nanosleep> to see whether you have nanosleep, and
110then carefully read your C<nanosleep()> C API documentation for any
111peculiarities. (There is no separate interface to call
112C<nanosleep()>; just use C<Time::HiRes::sleep()> or
113C<Time::HiRes::usleep()> with small enough values.)
0be47ac6 114
6937b144 115Unless using C<nanosleep> for mixing sleeping with signals, give
1fbb4de4 116some thought to whether Perl is the tool you should be using for
117work requiring nanosecond accuracies.
dcf686c9 118
3c72ec00 119The following functions can be imported from this module.
120No functions are exported by default.
dcf686c9 121
122=over 4
123
124=item gettimeofday ()
125
0be47ac6 126In array context returns a two-element array with the seconds and
f7916ddb 127microseconds since the epoch. In scalar context returns floating
6937b144 128seconds like C<Time::HiRes::time()> (see below).
dcf686c9 129
130=item usleep ( $useconds )
131
f7916ddb 132Sleeps for the number of microseconds specified. Returns the number
6937b144 133of microseconds actually slept. Can sleep for more than one second,
134unlike the C<usleep> system call. See also C<Time::HiRes::sleep()> below.
dcf686c9 135
136=item ualarm ( $useconds [, $interval_useconds ] )
137
6937b144 138Issues a C<ualarm> call; the C<$interval_useconds> is optional and
139will be zero if unspecified, resulting in C<alarm>-like behaviour.
dcf686c9 140
443572f5 141=item tv_interval
142
0be47ac6 143tv_interval ( $ref_to_gettimeofday [, $ref_to_later_gettimeofday] )
dcf686c9 144
f7916ddb 145Returns the floating seconds between the two times, which should have
6937b144 146been returned by C<gettimeofday()>. If the second argument is omitted,
f7916ddb 147then the current time is used.
dcf686c9 148
149=item time ()
150
f7916ddb 151Returns a floating seconds since the epoch. This function can be
6937b144 152imported, resulting in a nice drop-in replacement for the C<time>
153provided with core Perl; see the L</EXAMPLES> below.
dcf686c9 154
6937b144 155B<NOTE 1>: This higher resolution timer can return values either less
156or more than the core C<time()>, depending on whether your platform
157rounds the higher resolution timer values up, down, or to the nearest second
158to get the core C<time()>, but naturally the difference should be never
0be47ac6 159more than half a second.
f7916ddb 160
6937b144 161B<NOTE 2>: Since Sunday, September 9th, 2001 at 01:46:40 AM GMT, when
162the C<time()> seconds since epoch rolled over to 1_000_000_000, the
0be47ac6 163default floating point format of Perl and the seconds since epoch have
164conspired to produce an apparent bug: if you print the value of
4ed0e2d4 165C<Time::HiRes::time()> you seem to be getting only five decimals, not
166six as promised (microseconds). Not to worry, the microseconds are
167there (assuming your platform supports such granularity in first
168place). What is going on is that the default floating point format of
169Perl only outputs 15 digits. In this case that means ten digits
170before the decimal separator and five after. To see the microseconds
171you can use either C<printf>/C<sprintf> with C<"%.6f">, or the
172C<gettimeofday()> function in list context, which will give you the
173seconds and microseconds as two separate values.
389199d8 174
dcf686c9 175=item sleep ( $floating_seconds )
176
f7916ddb 177Sleeps for the specified amount of seconds. Returns the number of
178seconds actually slept (a floating point value). This function can be
6937b144 179imported, resulting in a nice drop-in replacement for the C<sleep>
180provided with perl, see the L</EXAMPLES> below.
dcf686c9 181
182=item alarm ( $floating_seconds [, $interval_floating_seconds ] )
183
6937b144 184The C<SIGALRM> signal is sent after the specified number of seconds.
185Implemented using C<ualarm()>. The C<$interval_floating_seconds> argument
186is optional and will be zero if unspecified, resulting in C<alarm()>-like
dcf686c9 187behaviour. This function can be imported, resulting in a nice drop-in
6937b144 188replacement for the C<alarm> provided with perl, see the L</EXAMPLES> below.
dcf686c9 189
0be47ac6 190B<NOTE 1>: With some operating system and Perl release combinations
6937b144 191C<SIGALRM> restarts C<select()>, instead of interuping it.
192This means that an C<alarm()> followed by a C<select()>
0be47ac6 193may together take the sum of the times specified for the the
6937b144 194C<alarm()> and the C<select()>, not just the time of the C<alarm()>.
3f2ee006 195
6937b144 196=item setitimer ( $which, $floating_seconds [, $interval_floating_seconds ] )
3c72ec00 197
09fa32a4 198Start up an interval timer: after a certain time, a signal arrives,
0be47ac6 199and more signals may keep arriving at certain intervals. To disable a
6937b144 200timer, use C<$floating_seconds> of zero. If the C<$interval_floating_seconds>
0be47ac6 201is set to zero (or unspecified), the timer is disabled B<after> the
202next delivered signal.
3c72ec00 203
6937b144 204Use of interval timers may interfere with C<alarm()>, C<sleep()>,
205and C<usleep()>. In standard-speak the "interaction is unspecified",
0be47ac6 206which means that I<anything> may happen: it may work, it may not.
3c72ec00 207
208In scalar context, the remaining time in the timer is returned.
209
210In list context, both the remaining time and the interval are returned.
211
4ed0e2d4 212There are usually three or four interval timers available: the
213C<$which> can be C<ITIMER_REAL>, C<ITIMER_VIRTUAL>, C<ITIMER_PROF>, or
214C<ITIMER_REALPROF>. Note that which ones are available depends: true
215UNIX platforms usually have the first three, but (for example) Win32
216and Cygwin have only C<ITIMER_REAL>, and only Solaris seems to have
217C<ITIMER_REALPROF> (which is used to profile multithreaded programs).
3c72ec00 218
6937b144 219C<ITIMER_REAL> results in C<alarm()>-like behavior. Time is counted in
220I<real time>; that is, wallclock time. C<SIGALRM> is delivered when
3c72ec00 221the timer expires.
222
4ed0e2d4 223C<ITIMER_VIRTUAL> counts time in (process) I<virtual time>; that is,
224only when the process is running. In multiprocessor/user/CPU systems
225this may be more or less than real or wallclock time. (This time is
226also known as the I<user time>.) C<SIGVTALRM> is delivered when the
227timer expires.
3c72ec00 228
6937b144 229C<ITIMER_PROF> counts time when either the process virtual time or when
0be47ac6 230the operating system is running on behalf of the process (such as I/O).
231(This time is also known as the I<system time>.) (The sum of user
6937b144 232time and system time is known as the I<CPU time>.) C<SIGPROF> is
233delivered when the timer expires. C<SIGPROF> can interrupt system calls.
3c72ec00 234
235The semantics of interval timers for multithreaded programs are
236system-specific, and some systems may support additional interval
6937b144 237timers. See your C<setitimer()> documentation.
3c72ec00 238
239=item getitimer ( $which )
240
6937b144 241Return the remaining time in the interval timer specified by C<$which>.
3c72ec00 242
243In scalar context, the remaining time is returned.
244
245In list context, both the remaining time and the interval are returned.
6937b144 246The interval is always what you put in using C<setitimer()>.
3c72ec00 247
dcf686c9 248=back
249
250=head1 EXAMPLES
251
252 use Time::HiRes qw(usleep ualarm gettimeofday tv_interval);
253
254 $microseconds = 750_000;
255 usleep $microseconds;
256
257 # signal alarm in 2.5s & every .1s thereafter
258 ualarm 2_500_000, 100_000;
259
260 # get seconds and microseconds since the epoch
261 ($s, $usec) = gettimeofday;
262
263 # measure elapsed time
264 # (could also do by subtracting 2 gettimeofday return values)
265 $t0 = [gettimeofday];
266 # do bunch of stuff here
267 $t1 = [gettimeofday];
268 # do more stuff here
269 $t0_t1 = tv_interval $t0, $t1;
0be47ac6 270
dcf686c9 271 $elapsed = tv_interval ($t0, [gettimeofday]);
272 $elapsed = tv_interval ($t0); # equivalent code
273
274 #
275 # replacements for time, alarm and sleep that know about
276 # floating seconds
277 #
278 use Time::HiRes;
279 $now_fractions = Time::HiRes::time;
280 Time::HiRes::sleep (2.5);
281 Time::HiRes::alarm (10.6666666);
0be47ac6 282
dcf686c9 283 use Time::HiRes qw ( time alarm sleep );
284 $now_fractions = time;
285 sleep (2.5);
286 alarm (10.6666666);
287
3c72ec00 288 # Arm an interval timer to go off first at 10 seconds and
289 # after that every 2.5 seconds, in process virtual time
290
291 use Time::HiRes qw ( setitimer ITIMER_VIRTUAL time );
292
36d6c396 293 $SIG{VTALRM} = sub { print time, "\n" };
3c72ec00 294 setitimer(ITIMER_VIRTUAL, 10, 2.5);
295
dcf686c9 296=head1 C API
297
298In addition to the perl API described above, a C API is available for
299extension writers. The following C functions are available in the
300modglobal hash:
301
302 name C prototype
303 --------------- ----------------------
304 Time::NVtime double (*)()
305 Time::U2time void (*)(UV ret[2])
306
6937b144 307Both functions return equivalent information (like C<gettimeofday>)
308but with different representations. The names C<NVtime> and C<U2time>
dcf686c9 309were selected mainly because they are operating system independent.
6937b144 310(C<gettimeofday> is Unix-centric, though some platforms like VMS have
0be47ac6 311emulations for it.)
dcf686c9 312
6937b144 313Here is an example of using C<NVtime> from C:
dcf686c9 314
315 double (*myNVtime)();
316 SV **svp = hv_fetch(PL_modglobal, "Time::NVtime", 12, 0);
317 if (!svp) croak("Time::HiRes is required");
318 if (!SvIOK(*svp)) croak("Time::NVtime isn't a function pointer");
356234a5 319 myNVtime = INT2PTR(double(*)(), SvIV(*svp));
dcf686c9 320 printf("The current time is: %f\n", (*myNVtime)());
321
db0b859f 322=head1 DIAGNOSTICS
323
324=head2 negative time not invented yet
325
326You tried to use a negative time argument.
327
328=head2 internal error: useconds < 0 (unsigned ... signed ...)
329
330Something went horribly wrong-- the number of microseconds that cannot
331become negative just became negative. Maybe your compiler is broken?
332
f03b998d 333=head1 CAVEATS
334
6937b144 335Notice that the core C<time()> maybe rounding rather than truncating.
d8cb5b61 336What this means is that the core C<time()> may be reporting the time
337as one second later than C<gettimeofday()> and C<Time::HiRes::time()>.
338
339Adjusting the system clock (either manually or by services like ntp)
340may cause problems, especially for long running programs that assume
341a monotonously increasing time (note that all platforms do not adjust
342time as gracefully as UNIX ntp does). For example in Win32 (and derived
343platforms like Cygwin and MinGW) the Time::HiRes::time() may temporarily
344drift off from the system clock (and the original time()) by up to 0.5
345seconds. Time::HiRes will notice this eventually and recalibrate.
f03b998d 346
dcf686c9 347=head1 AUTHORS
348
349D. Wegscheid <wegscd@whirlpool.com>
350R. Schertler <roderick@argon.org>
351J. Hietaniemi <jhi@iki.fi>
352G. Aas <gisle@aas.no>
353
3f2ee006 354=head1 COPYRIGHT AND LICENSE
dcf686c9 355
3f2ee006 356Copyright (c) 1996-2002 Douglas E. Wegscheid. All rights reserved.
dcf686c9 357
0225372c 358Copyright (c) 2002,2003,2004 Jarkko Hietaniemi. All rights reserved.
dcf686c9 359
3f2ee006 360This program is free software; you can redistribute it and/or modify
361it under the same terms as Perl itself.
dcf686c9 362
363=cut