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