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