Fix a warning bug in Perl_report_evil_fh (didn't report
[p5sagit/p5-mst-13.2.git] / ext / Time / HiRes / HiRes.pm
CommitLineData
dcf686c9 1package Time::HiRes;
2
3use strict;
3c72ec00 4use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD);
dcf686c9 5
6require Exporter;
3c72ec00 7use XSLoader;
dcf686c9 8
3c72ec00 9@ISA = qw(Exporter);
dcf686c9 10
11@EXPORT = qw( );
3c72ec00 12@EXPORT_OK = qw (usleep sleep ualarm alarm gettimeofday time tv_interval
13 getitimer setitimer ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF);
14
76fbd8c4 15$VERSION = '1.20_00';
3c72ec00 16
17sub AUTOLOAD {
18 my $constname;
19 ($constname= $AUTOLOAD) =~ s/.*:://;
20 my $val = constant($constname, @_ ? $_[0] : 0);
21 if ($!) {
22 my ($pack,$file,$line) = caller;
23 die "Your vendor has not defined Time::HiRes macro $constname, used at $file line $line.\n";
24 }
25 {
26 no strict 'refs';
27 *$AUTOLOAD = sub { $val };
28 }
29 goto &$AUTOLOAD;
30}
dcf686c9 31
3c72ec00 32XSLoader::load 'Time::HiRes', $VERSION;
dcf686c9 33
34# Preloaded methods go here.
35
36sub tv_interval {
37 # probably could have been done in C
38 my ($a, $b) = @_;
39 $b = [gettimeofday()] unless defined($b);
40 (${$b}[0] - ${$a}[0]) + ((${$b}[1] - ${$a}[1]) / 1_000_000);
41}
42
dcf686c9 43# Autoload methods go after =cut, and are processed by the autosplit program.
44
451;
46__END__
47
48=head1 NAME
49
f7916ddb 50Time::HiRes - High resolution alarm, sleep, gettimeofday, interval timers
dcf686c9 51
52=head1 SYNOPSIS
53
54 use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );
55
56 usleep ($microseconds);
57
58 ualarm ($microseconds);
59 ualarm ($microseconds, $interval_microseconds);
60
61 $t0 = [gettimeofday];
62 ($seconds, $microseconds) = gettimeofday;
63
64 $elapsed = tv_interval ( $t0, [$seconds, $microseconds]);
65 $elapsed = tv_interval ( $t0, [gettimeofday]);
66 $elapsed = tv_interval ( $t0 );
67
68 use Time::HiRes qw ( time alarm sleep );
3c72ec00 69
dcf686c9 70 $now_fractions = time;
71 sleep ($floating_seconds);
72 alarm ($floating_seconds);
73 alarm ($floating_seconds, $floating_interval);
74
3c72ec00 75 use Time::HiRes qw( setitimer getitimer
76 ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF );
77
78 setitimer ($which, $floating_seconds, $floating_interval );
79 getitimer ($which);
80
dcf686c9 81=head1 DESCRIPTION
82
f7916ddb 83The C<Time::HiRes> module implements a Perl interface to the usleep,
84ualarm, gettimeofday, and setitimer/getitimer system calls. See the
85EXAMPLES section below and the test scripts for usage; see your system
86documentation for the description of the underlying usleep, ualarm,
87gettimeofday, and setitimer/getitimer calls.
dcf686c9 88
f7916ddb 89If your system lacks gettimeofday(2) or an emulation of it you don't
90get gettimeofday() or the one-arg form of tv_interval().
91If you don't have usleep(3) or select(2) you don't get usleep()
92or sleep(). If your system don't have ualarm(3) or setitimer(2) you
93don't get ualarm() or alarm(). If you try to import an unimplemented
94function in the C<use> statement it will fail at compile time.
dcf686c9 95
3c72ec00 96The following functions can be imported from this module.
97No functions are exported by default.
dcf686c9 98
99=over 4
100
101=item gettimeofday ()
102
f7916ddb 103In array context returns a 2 element array with the seconds and
104microseconds since the epoch. In scalar context returns floating
dcf686c9 105seconds like Time::HiRes::time() (see below).
106
107=item usleep ( $useconds )
108
f7916ddb 109Sleeps for the number of microseconds specified. Returns the number
110of microseconds actually slept. Can sleep for more than one second
111unlike the usleep system call. See also Time::HiRes::sleep() below.
dcf686c9 112
113=item ualarm ( $useconds [, $interval_useconds ] )
114
115Issues a ualarm call; interval_useconds is optional and will be 0 if
116unspecified, resulting in alarm-like behaviour.
117
443572f5 118=item tv_interval
119
5cb3728c 120C<tv_interval ( $ref_to_gettimeofday [, $ref_to_later_gettimeofday] )>
dcf686c9 121
f7916ddb 122Returns the floating seconds between the two times, which should have
123been returned by gettimeofday(). If the second argument is omitted,
124then the current time is used.
dcf686c9 125
126=item time ()
127
f7916ddb 128Returns a floating seconds since the epoch. This function can be
129imported, resulting in a nice drop-in replacement for the C<time>
130provided with core Perl, see the EXAMPLES below.
dcf686c9 131
f7916ddb 132B<NOTE 1>: this higher resolution timer can return values either less or
133more than the core time(), depending on whether your platforms rounds
134the higher resolution timer values up, down, or to the nearest to get
135the core time(), but naturally the difference should be never more than
136half a second.
137
138B<NOTE 2>: Since Sunday, September 9th, 2001 at 01:46:40 AM GMT
b8ec5d27 139(when the time() seconds since epoch rolled over to 1_000_000_000),
140the default floating point format of Perl and the seconds since epoch
141have conspired to produce an apparent bug: if you print the value of
142Time::HiRes::time() you seem to be getting only five decimals, not six
143as promised (microseconds). Not to worry, the microseconds are there
144(assuming your platform supports such granularity). What is going on
145is that the default floating point format of Perl only outputs 15
146digits. In this case that means ten digits before the decimal
147separator and five after. To see the microseconds you can use either
148printf/sprintf with C<%.6f>, or the gettimeofday() function in list
149context, which will give you the seconds and microseconds as two
389199d8 150separate values.
151
dcf686c9 152=item sleep ( $floating_seconds )
153
f7916ddb 154Sleeps for the specified amount of seconds. Returns the number of
155seconds actually slept (a floating point value). This function can be
156imported, resulting in a nice drop-in replacement for the C<sleep>
157provided with perl, see the EXAMPLES below.
dcf686c9 158
159=item alarm ( $floating_seconds [, $interval_floating_seconds ] )
160
f7916ddb 161The SIGALRM signal is sent after the specfified number of seconds.
162Implemented using ualarm(). The $interval_floating_seconds argument
163is optional and will be 0 if unspecified, resulting in alarm()-like
dcf686c9 164behaviour. This function can be imported, resulting in a nice drop-in
165replacement for the C<alarm> provided with perl, see the EXAMPLES below.
166
443572f5 167=item setitimer
168
5cb3728c 169C<setitimer ( $which, $floating_seconds [, $interval_floating_seconds ] )>
3c72ec00 170
09fa32a4 171Start up an interval timer: after a certain time, a signal arrives,
172and more signals may keep arriving at certain intervals. To disable
173a timer, use time of zero. If interval is set to zero (or unspecified),
174the timer is disabled B<after> the next delivered signal.
3c72ec00 175
176Use of interval timers may interfere with alarm(), sleep(), and usleep().
177In standard-speak the "interaction is unspecified", which means that
178I<anything> may happen: it may work, it may not.
179
180In scalar context, the remaining time in the timer is returned.
181
182In list context, both the remaining time and the interval are returned.
183
184There are three interval timers: the $which can be ITIMER_REAL,
185ITIMER_VIRTUAL, or ITIMER_PROF.
186
187ITIMER_REAL results in alarm()-like behavior. Time is counted in
188I<real time>, that is, wallclock time. SIGALRM is delivered when
189the timer expires.
190
191ITIMER_VIRTUAL counts time in (process) I<virtual time>, that is, only
09fa32a4 192when the process is running. In multiprocessor/user/CPU systems this
193may be more or less than real or wallclock time. (This time is also
194known as the I<user time>.) SIGVTALRM is delivered when the timer expires.
3c72ec00 195
196ITIMER_PROF counts time when either the process virtual time or when
197the operating system is running on behalf of the process (such as
198I/O). (This time is also known as the I<system time>.) (Collectively
199these times are also known as the I<CPU time>.) SIGPROF is delivered
200when the timer expires. SIGPROF can interrupt system calls.
201
202The semantics of interval timers for multithreaded programs are
203system-specific, and some systems may support additional interval
204timers. See your setitimer() documentation.
205
206=item getitimer ( $which )
207
208Return the remaining time in the interval timer specified by $which.
209
210In scalar context, the remaining time is returned.
211
212In list context, both the remaining time and the interval are returned.
213The interval is always what you put in using setitimer().
214
dcf686c9 215=back
216
217=head1 EXAMPLES
218
219 use Time::HiRes qw(usleep ualarm gettimeofday tv_interval);
220
221 $microseconds = 750_000;
222 usleep $microseconds;
223
224 # signal alarm in 2.5s & every .1s thereafter
225 ualarm 2_500_000, 100_000;
226
227 # get seconds and microseconds since the epoch
228 ($s, $usec) = gettimeofday;
229
230 # measure elapsed time
231 # (could also do by subtracting 2 gettimeofday return values)
232 $t0 = [gettimeofday];
233 # do bunch of stuff here
234 $t1 = [gettimeofday];
235 # do more stuff here
236 $t0_t1 = tv_interval $t0, $t1;
237
238 $elapsed = tv_interval ($t0, [gettimeofday]);
239 $elapsed = tv_interval ($t0); # equivalent code
240
241 #
242 # replacements for time, alarm and sleep that know about
243 # floating seconds
244 #
245 use Time::HiRes;
246 $now_fractions = Time::HiRes::time;
247 Time::HiRes::sleep (2.5);
248 Time::HiRes::alarm (10.6666666);
249
250 use Time::HiRes qw ( time alarm sleep );
251 $now_fractions = time;
252 sleep (2.5);
253 alarm (10.6666666);
254
3c72ec00 255 # Arm an interval timer to go off first at 10 seconds and
256 # after that every 2.5 seconds, in process virtual time
257
258 use Time::HiRes qw ( setitimer ITIMER_VIRTUAL time );
259
260 $SIG{VTLARM} = sub { print time, "\n" };
261 setitimer(ITIMER_VIRTUAL, 10, 2.5);
262
dcf686c9 263=head1 C API
264
265In addition to the perl API described above, a C API is available for
266extension writers. The following C functions are available in the
267modglobal hash:
268
269 name C prototype
270 --------------- ----------------------
271 Time::NVtime double (*)()
272 Time::U2time void (*)(UV ret[2])
273
274Both functions return equivalent information (like C<gettimeofday>)
275but with different representations. The names C<NVtime> and C<U2time>
276were selected mainly because they are operating system independent.
277(C<gettimeofday> is Un*x-centric.)
278
279Here is an example of using NVtime from C:
280
281 double (*myNVtime)();
282 SV **svp = hv_fetch(PL_modglobal, "Time::NVtime", 12, 0);
283 if (!svp) croak("Time::HiRes is required");
284 if (!SvIOK(*svp)) croak("Time::NVtime isn't a function pointer");
356234a5 285 myNVtime = INT2PTR(double(*)(), SvIV(*svp));
dcf686c9 286 printf("The current time is: %f\n", (*myNVtime)());
287
f03b998d 288=head1 CAVEATS
289
290Notice that the core time() maybe rounding rather than truncating.
291What this means that the core time() may be giving time one second
292later than gettimeofday(), also known as Time::HiRes::time().
293
dcf686c9 294=head1 AUTHORS
295
296D. Wegscheid <wegscd@whirlpool.com>
297R. Schertler <roderick@argon.org>
298J. Hietaniemi <jhi@iki.fi>
299G. Aas <gisle@aas.no>
300
301=head1 REVISION
302
303$Id: HiRes.pm,v 1.20 1999/03/16 02:26:13 wegscd Exp $
304
305$Log: HiRes.pm,v $
306Revision 1.20 1999/03/16 02:26:13 wegscd
307Add documentation for NVTime and U2Time.
308
309Revision 1.19 1998/09/30 02:34:42 wegscd
310No changes, bump version.
311
312Revision 1.18 1998/07/07 02:41:35 wegscd
313No changes, bump version.
314
315Revision 1.17 1998/07/02 01:45:13 wegscd
316Bump version to 1.17
317
318Revision 1.16 1997/11/13 02:06:36 wegscd
319version bump to accomodate HiRes.xs fix.
320
321Revision 1.15 1997/11/11 02:17:59 wegscd
322POD editing, courtesy of Gisle Aas.
323
324Revision 1.14 1997/11/06 03:14:35 wegscd
325Update version # for Makefile.PL and HiRes.xs changes.
326
327Revision 1.13 1997/11/05 05:36:25 wegscd
328change version # for Makefile.pl and HiRes.xs changes.
329
330Revision 1.12 1997/10/13 20:55:33 wegscd
331Force a new version for Makefile.PL changes.
332
333Revision 1.11 1997/09/05 19:59:33 wegscd
334New version to bump version for README and Makefile.PL fixes.
335Fix bad RCS log.
336
337Revision 1.10 1997/05/23 01:11:38 wegscd
338Conditional compilation; EXPORT_FAIL fixes.
339
340Revision 1.2 1996/12/30 13:28:40 wegscd
341Update documentation for what to do when missing ualarm() and friends.
342
343Revision 1.1 1996/10/17 20:53:31 wegscd
344Fix =head1 being next to __END__ so pod2man works
345
346Revision 1.0 1996/09/03 18:25:15 wegscd
347Initial revision
348
349=head1 COPYRIGHT
350
351Copyright (c) 1996-1997 Douglas E. Wegscheid.
352All rights reserved. This program is free software; you can
353redistribute it and/or modify it under the same terms as Perl itself.
354
355=cut