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