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