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