Upgrade to Time::HiRes 1.42.
[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
98b50af3 18$VERSION = '1.42';
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
f7916ddb 86The C<Time::HiRes> module implements a Perl interface to the usleep,
87ualarm, gettimeofday, and setitimer/getitimer system calls. See the
88EXAMPLES section below and the test scripts for usage; see your system
3f2ee006 89documentation for the description of the underlying nanosleep or usleep,
90ualarm, gettimeofday, and setitimer/getitimer calls.
dcf686c9 91
f7916ddb 92If your system lacks gettimeofday(2) or an emulation of it you don't
3f2ee006 93get gettimeofday() or the one-arg form of tv_interval(). If you don't
94have nanosleep() or usleep(3) or select(2) you don't get Time::HiRes::usleep()
f7916ddb 95or sleep(). If your system don't have ualarm(3) or setitimer(2) you
3f2ee006 96don't get Time::HiRes::ualarm() or alarm().
97
98If you try to import an unimplemented function in the C<use> statement
99it will fail at compile time.
100
101If your subsecond sleeping is implemented with nanosleep() instead of
102usleep(), you can mix subsecond sleeping with signals since
103nanosleep() does not use signals. This, however, is unportable
104behavior, and you should first check for the truth value of
105C<&Time::HiRes::d_nanosleep> to see whether you have nanosleep,
106and then read carefully your nanosleep() C API documentation for
107any peculiarities. (There is no separate interface to call nanosleep();
108just use Time::HiRes::sleep() or usleep() with small enough values. Also,
109think twice whether using nanosecond accuracies in a Perl program is what
110you should be doing.)
dcf686c9 111
3c72ec00 112The following functions can be imported from this module.
113No functions are exported by default.
dcf686c9 114
115=over 4
116
117=item gettimeofday ()
118
f7916ddb 119In array context returns a 2 element array with the seconds and
120microseconds since the epoch. In scalar context returns floating
dcf686c9 121seconds like Time::HiRes::time() (see below).
122
123=item usleep ( $useconds )
124
f7916ddb 125Sleeps for the number of microseconds specified. Returns the number
126of microseconds actually slept. Can sleep for more than one second
127unlike the usleep system call. See also Time::HiRes::sleep() below.
dcf686c9 128
129=item ualarm ( $useconds [, $interval_useconds ] )
130
131Issues a ualarm call; interval_useconds is optional and will be 0 if
132unspecified, resulting in alarm-like behaviour.
133
443572f5 134=item tv_interval
135
5cb3728c 136C<tv_interval ( $ref_to_gettimeofday [, $ref_to_later_gettimeofday] )>
dcf686c9 137
f7916ddb 138Returns the floating seconds between the two times, which should have
139been returned by gettimeofday(). If the second argument is omitted,
140then the current time is used.
dcf686c9 141
142=item time ()
143
f7916ddb 144Returns a floating seconds since the epoch. This function can be
145imported, resulting in a nice drop-in replacement for the C<time>
146provided with core Perl, see the EXAMPLES below.
dcf686c9 147
f7916ddb 148B<NOTE 1>: this higher resolution timer can return values either less or
149more than the core time(), depending on whether your platforms rounds
150the higher resolution timer values up, down, or to the nearest to get
151the core time(), but naturally the difference should be never more than
152half a second.
153
154B<NOTE 2>: Since Sunday, September 9th, 2001 at 01:46:40 AM GMT
b8ec5d27 155(when the time() seconds since epoch rolled over to 1_000_000_000),
156the default floating point format of Perl and the seconds since epoch
157have conspired to produce an apparent bug: if you print the value of
158Time::HiRes::time() you seem to be getting only five decimals, not six
159as promised (microseconds). Not to worry, the microseconds are there
160(assuming your platform supports such granularity). What is going on
161is that the default floating point format of Perl only outputs 15
162digits. In this case that means ten digits before the decimal
163separator and five after. To see the microseconds you can use either
164printf/sprintf with C<%.6f>, or the gettimeofday() function in list
165context, which will give you the seconds and microseconds as two
389199d8 166separate values.
167
dcf686c9 168=item sleep ( $floating_seconds )
169
f7916ddb 170Sleeps for the specified amount of seconds. Returns the number of
171seconds actually slept (a floating point value). This function can be
172imported, resulting in a nice drop-in replacement for the C<sleep>
173provided with perl, see the EXAMPLES below.
dcf686c9 174
175=item alarm ( $floating_seconds [, $interval_floating_seconds ] )
176
3f2ee006 177The SIGALRM signal is sent after the specified number of seconds.
f7916ddb 178Implemented using ualarm(). The $interval_floating_seconds argument
179is optional and will be 0 if unspecified, resulting in alarm()-like
dcf686c9 180behaviour. This function can be imported, resulting in a nice drop-in
181replacement for the C<alarm> provided with perl, see the EXAMPLES below.
182
3f2ee006 183B<NOTE 1>: With some platform - Perl release combinations select()
184gets restarted by SIGALRM, instead of dropping out of select().
185This means that an alarm() followed by a select() may together take
186the sum of the times specified for the the alarm() and the select(),
187not just the time of the alarm().
188
443572f5 189=item setitimer
190
5cb3728c 191C<setitimer ( $which, $floating_seconds [, $interval_floating_seconds ] )>
3c72ec00 192
09fa32a4 193Start up an interval timer: after a certain time, a signal arrives,
194and more signals may keep arriving at certain intervals. To disable
195a timer, use time of zero. If interval is set to zero (or unspecified),
196the timer is disabled B<after> the next delivered signal.
3c72ec00 197
198Use of interval timers may interfere with alarm(), sleep(), and usleep().
199In standard-speak the "interaction is unspecified", which means that
200I<anything> may happen: it may work, it may not.
201
202In scalar context, the remaining time in the timer is returned.
203
204In list context, both the remaining time and the interval are returned.
205
3f2ee006 206There are usually three or four interval timers available: the $which
207can be ITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF, or ITIMER_REALPROF.
208Note that which ones are available depends: true UNIX platforms have
209usually all first three, but for example Win32 and Cygwin only have
210ITIMER_REAL, and only Solaris seems to have ITIMER_REALPROF (which is
211used to profile multithreaded programs).
3c72ec00 212
213ITIMER_REAL results in alarm()-like behavior. Time is counted in
214I<real time>, that is, wallclock time. SIGALRM is delivered when
215the timer expires.
216
217ITIMER_VIRTUAL counts time in (process) I<virtual time>, that is, only
09fa32a4 218when the process is running. In multiprocessor/user/CPU systems this
219may be more or less than real or wallclock time. (This time is also
220known as the I<user time>.) SIGVTALRM is delivered when the timer expires.
3c72ec00 221
222ITIMER_PROF counts time when either the process virtual time or when
223the operating system is running on behalf of the process (such as
224I/O). (This time is also known as the I<system time>.) (Collectively
225these times are also known as the I<CPU time>.) SIGPROF is delivered
226when the timer expires. SIGPROF can interrupt system calls.
227
228The semantics of interval timers for multithreaded programs are
229system-specific, and some systems may support additional interval
230timers. See your setitimer() documentation.
231
232=item getitimer ( $which )
233
234Return the remaining time in the interval timer specified by $which.
235
236In scalar context, the remaining time is returned.
237
238In list context, both the remaining time and the interval are returned.
239The interval is always what you put in using setitimer().
240
dcf686c9 241=back
242
243=head1 EXAMPLES
244
245 use Time::HiRes qw(usleep ualarm gettimeofday tv_interval);
246
247 $microseconds = 750_000;
248 usleep $microseconds;
249
250 # signal alarm in 2.5s & every .1s thereafter
251 ualarm 2_500_000, 100_000;
252
253 # get seconds and microseconds since the epoch
254 ($s, $usec) = gettimeofday;
255
256 # measure elapsed time
257 # (could also do by subtracting 2 gettimeofday return values)
258 $t0 = [gettimeofday];
259 # do bunch of stuff here
260 $t1 = [gettimeofday];
261 # do more stuff here
262 $t0_t1 = tv_interval $t0, $t1;
263
264 $elapsed = tv_interval ($t0, [gettimeofday]);
265 $elapsed = tv_interval ($t0); # equivalent code
266
267 #
268 # replacements for time, alarm and sleep that know about
269 # floating seconds
270 #
271 use Time::HiRes;
272 $now_fractions = Time::HiRes::time;
273 Time::HiRes::sleep (2.5);
274 Time::HiRes::alarm (10.6666666);
275
276 use Time::HiRes qw ( time alarm sleep );
277 $now_fractions = time;
278 sleep (2.5);
279 alarm (10.6666666);
280
3c72ec00 281 # Arm an interval timer to go off first at 10 seconds and
282 # after that every 2.5 seconds, in process virtual time
283
284 use Time::HiRes qw ( setitimer ITIMER_VIRTUAL time );
285
286 $SIG{VTLARM} = sub { print time, "\n" };
287 setitimer(ITIMER_VIRTUAL, 10, 2.5);
288
dcf686c9 289=head1 C API
290
291In addition to the perl API described above, a C API is available for
292extension writers. The following C functions are available in the
293modglobal hash:
294
295 name C prototype
296 --------------- ----------------------
297 Time::NVtime double (*)()
298 Time::U2time void (*)(UV ret[2])
299
300Both functions return equivalent information (like C<gettimeofday>)
301but with different representations. The names C<NVtime> and C<U2time>
302were selected mainly because they are operating system independent.
303(C<gettimeofday> is Un*x-centric.)
304
305Here is an example of using NVtime from C:
306
307 double (*myNVtime)();
308 SV **svp = hv_fetch(PL_modglobal, "Time::NVtime", 12, 0);
309 if (!svp) croak("Time::HiRes is required");
310 if (!SvIOK(*svp)) croak("Time::NVtime isn't a function pointer");
356234a5 311 myNVtime = INT2PTR(double(*)(), SvIV(*svp));
dcf686c9 312 printf("The current time is: %f\n", (*myNVtime)());
313
f03b998d 314=head1 CAVEATS
315
316Notice that the core time() maybe rounding rather than truncating.
317What this means that the core time() may be giving time one second
318later than gettimeofday(), also known as Time::HiRes::time().
319
dcf686c9 320=head1 AUTHORS
321
322D. Wegscheid <wegscd@whirlpool.com>
323R. Schertler <roderick@argon.org>
324J. Hietaniemi <jhi@iki.fi>
325G. Aas <gisle@aas.no>
326
3f2ee006 327=head1 COPYRIGHT AND LICENSE
dcf686c9 328
3f2ee006 329Copyright (c) 1996-2002 Douglas E. Wegscheid. All rights reserved.
dcf686c9 330
3f2ee006 331Copyright (c) 2002 Jarkko Hietaniemi. All rights reserved.
dcf686c9 332
3f2ee006 333This program is free software; you can redistribute it and/or modify
334it under the same terms as Perl itself.
dcf686c9 335
336=cut