source: for-distributions/trunk/bin/windows/perl/lib/Time/HiRes.pm@ 14489

Last change on this file since 14489 was 14489, checked in by oranfry, 17 years ago

upgrading to perl 5.8

File size: 18.6 KB
Line 
1package Time::HiRes;
2
3use strict;
4use vars qw($VERSION $XS_VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD);
5
6require Exporter;
7require DynaLoader;
8
9@ISA = qw(Exporter DynaLoader);
10
11@EXPORT = qw( );
12@EXPORT_OK = qw (usleep sleep ualarm alarm gettimeofday time tv_interval
13 getitimer setitimer nanosleep clock_gettime clock_getres
14 clock clock_nanosleep
15 CLOCK_HIGHRES CLOCK_MONOTONIC CLOCK_PROCESS_CPUTIME_ID
16 CLOCK_REALTIME CLOCK_SOFTTIME CLOCK_THREAD_CPUTIME_ID
17 CLOCK_TIMEOFDAY CLOCKS_PER_SEC
18 ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF ITIMER_REALPROF
19 TIMER_ABSTIME
20 d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
21 d_nanosleep d_clock_gettime d_clock_getres
22 d_clock d_clock_nanosleep);
23
24$VERSION = '1.86';
25$XS_VERSION = $VERSION;
26$VERSION = eval $VERSION;
27
28sub AUTOLOAD {
29 my $constname;
30 ($constname = $AUTOLOAD) =~ s/.*:://;
31 # print "AUTOLOAD: constname = $constname ($AUTOLOAD)\n";
32 die "&Time::HiRes::constant not defined" if $constname eq 'constant';
33 my ($error, $val) = constant($constname);
34 # print "AUTOLOAD: error = $error, val = $val\n";
35 if ($error) {
36 my (undef,$file,$line) = caller;
37 die "$error at $file line $line.\n";
38 }
39 {
40 no strict 'refs';
41 *$AUTOLOAD = sub { $val };
42 }
43 goto &$AUTOLOAD;
44}
45
46sub import {
47 my $this = shift;
48 for my $i (@_) {
49 if (($i eq 'clock_getres' && !&d_clock_getres) ||
50 ($i eq 'clock_gettime' && !&d_clock_gettime) ||
51 ($i eq 'clock_nanosleep' && !&d_clock_nanosleep) ||
52 ($i eq 'clock' && !&d_clock) ||
53 ($i eq 'nanosleep' && !&d_nanosleep) ||
54 ($i eq 'usleep' && !&d_usleep) ||
55 ($i eq 'ualarm' && !&d_ualarm)) {
56 require Carp;
57 Carp::croak("Time::HiRes::$i(): unimplemented in this platform");
58 }
59 }
60 Time::HiRes->export_to_level(1, $this, @_);
61}
62
63bootstrap Time::HiRes;
64
65# Preloaded methods go here.
66
67sub tv_interval {
68 # probably could have been done in C
69 my ($a, $b) = @_;
70 $b = [gettimeofday()] unless defined($b);
71 (${$b}[0] - ${$a}[0]) + ((${$b}[1] - ${$a}[1]) / 1_000_000);
72}
73
74# Autoload methods go after =cut, and are processed by the autosplit program.
75
761;
77__END__
78
79=head1 NAME
80
81Time::HiRes - High resolution alarm, sleep, gettimeofday, interval timers
82
83=head1 SYNOPSIS
84
85 use Time::HiRes qw( usleep ualarm gettimeofday tv_interval nanosleep
86 clock_gettime clock_getres clock_nanosleep clock );
87
88 usleep ($microseconds);
89 nanosleep ($nanoseconds);
90
91 ualarm ($microseconds);
92 ualarm ($microseconds, $interval_microseconds);
93
94 $t0 = [gettimeofday];
95 ($seconds, $microseconds) = gettimeofday;
96
97 $elapsed = tv_interval ( $t0, [$seconds, $microseconds]);
98 $elapsed = tv_interval ( $t0, [gettimeofday]);
99 $elapsed = tv_interval ( $t0 );
100
101 use Time::HiRes qw ( time alarm sleep );
102
103 $now_fractions = time;
104 sleep ($floating_seconds);
105 alarm ($floating_seconds);
106 alarm ($floating_seconds, $floating_interval);
107
108 use Time::HiRes qw( setitimer getitimer
109 ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF ITIMER_REALPROF );
110
111 setitimer ($which, $floating_seconds, $floating_interval );
112 getitimer ($which);
113
114 $realtime = clock_gettime(CLOCK_REALTIME);
115 $resolution = clock_getres(CLOCK_REALTIME);
116
117 clock_nanosleep(CLOCK_REALTIME, 1.5);
118 clock_nanosleep(CLOCK_REALTIME, time() + 10, TIMER_ABSTIME);
119
120 my $ticktock = clock();
121
122=head1 DESCRIPTION
123
124The C<Time::HiRes> module implements a Perl interface to the
125C<usleep>, C<nanosleep>, C<ualarm>, C<gettimeofday>, and
126C<setitimer>/C<getitimer> system calls, in other words, high
127resolution time and timers. See the L</EXAMPLES> section below and the
128test scripts for usage; see your system documentation for the
129description of the underlying C<nanosleep> or C<usleep>, C<ualarm>,
130C<gettimeofday>, and C<setitimer>/C<getitimer> calls.
131
132If your system lacks C<gettimeofday()> or an emulation of it you don't
133get C<gettimeofday()> or the one-argument form of C<tv_interval()>.
134If your system lacks all of C<nanosleep()>, C<usleep()>,
135C<select()>, and C<poll>, you don't get C<Time::HiRes::usleep()>,
136C<Time::HiRes::nanosleep()>, or C<Time::HiRes::sleep()>.
137If your system lacks both C<ualarm()> and C<setitimer()> you don't get
138C<Time::HiRes::ualarm()> or C<Time::HiRes::alarm()>.
139
140If you try to import an unimplemented function in the C<use> statement
141it will fail at compile time.
142
143If your subsecond sleeping is implemented with C<nanosleep()> instead
144of C<usleep()>, you can mix subsecond sleeping with signals since
145C<nanosleep()> does not use signals. This, however, is not portable,
146and you should first check for the truth value of
147C<&Time::HiRes::d_nanosleep> to see whether you have nanosleep, and
148then carefully read your C<nanosleep()> C API documentation for any
149peculiarities.
150
151If you are using C<nanosleep> for something else than mixing sleeping
152with signals, give some thought to whether Perl is the tool you should
153be using for work requiring nanosecond accuracies.
154
155The following functions can be imported from this module.
156No functions are exported by default.
157
158=over 4
159
160=item gettimeofday ()
161
162In array context returns a two-element array with the seconds and
163microseconds since the epoch. In scalar context returns floating
164seconds like C<Time::HiRes::time()> (see below).
165
166=item usleep ( $useconds )
167
168Sleeps for the number of microseconds (millionths of a second)
169specified. Returns the number of microseconds actually slept. Can
170sleep for more than one second, unlike the C<usleep> system call. Can
171also sleep for zero seconds, which often works like a I<thread yield>.
172See also C<Time::HiRes::usleep()>, C<Time::HiRes::sleep()>, and
173C<Time::HiRes::clock_nanosleep()>.
174
175Do not expect usleep() to be exact down to one microsecond.
176
177=item nanosleep ( $nanoseconds )
178
179Sleeps for the number of nanoseconds (1e9ths of a second) specified.
180Returns the number of nanoseconds actually slept (accurate only to
181microseconds, the nearest thousand of them). Can sleep for more than
182one second. Can also sleep for zero seconds, which often works like a
183I<thread yield>. See also C<Time::HiRes::sleep()>,
184C<Time::HiRes::usleep()>, and C<Time::HiRes::clock_nanosleep()>.
185
186Do not expect nanosleep() to be exact down to one nanosecond.
187Getting even accuracy of one thousand nanoseconds is good.
188
189=item ualarm ( $useconds [, $interval_useconds ] )
190
191Issues a C<ualarm> call; the C<$interval_useconds> is optional and
192will be zero if unspecified, resulting in C<alarm>-like behaviour.
193
194Note that the interaction between alarms and sleeps is unspecified.
195
196=item tv_interval
197
198tv_interval ( $ref_to_gettimeofday [, $ref_to_later_gettimeofday] )
199
200Returns the floating seconds between the two times, which should have
201been returned by C<gettimeofday()>. If the second argument is omitted,
202then the current time is used.
203
204=item time ()
205
206Returns a floating seconds since the epoch. This function can be
207imported, resulting in a nice drop-in replacement for the C<time>
208provided with core Perl; see the L</EXAMPLES> below.
209
210B<NOTE 1>: This higher resolution timer can return values either less
211or more than the core C<time()>, depending on whether your platform
212rounds the higher resolution timer values up, down, or to the nearest second
213to get the core C<time()>, but naturally the difference should be never
214more than half a second. See also L</clock_getres>, if available
215in your system.
216
217B<NOTE 2>: Since Sunday, September 9th, 2001 at 01:46:40 AM GMT, when
218the C<time()> seconds since epoch rolled over to 1_000_000_000, the
219default floating point format of Perl and the seconds since epoch have
220conspired to produce an apparent bug: if you print the value of
221C<Time::HiRes::time()> you seem to be getting only five decimals, not
222six as promised (microseconds). Not to worry, the microseconds are
223there (assuming your platform supports such granularity in the first
224place). What is going on is that the default floating point format of
225Perl only outputs 15 digits. In this case that means ten digits
226before the decimal separator and five after. To see the microseconds
227you can use either C<printf>/C<sprintf> with C<"%.6f">, or the
228C<gettimeofday()> function in list context, which will give you the
229seconds and microseconds as two separate values.
230
231=item sleep ( $floating_seconds )
232
233Sleeps for the specified amount of seconds. Returns the number of
234seconds actually slept (a floating point value). This function can
235be imported, resulting in a nice drop-in replacement for the C<sleep>
236provided with perl, see the L</EXAMPLES> below.
237
238Note that the interaction between alarms and sleeps is unspecified.
239
240=item alarm ( $floating_seconds [, $interval_floating_seconds ] )
241
242The C<SIGALRM> signal is sent after the specified number of seconds.
243Implemented using C<ualarm()>. The C<$interval_floating_seconds> argument
244is optional and will be zero if unspecified, resulting in C<alarm()>-like
245behaviour. This function can be imported, resulting in a nice drop-in
246replacement for the C<alarm> provided with perl, see the L</EXAMPLES> below.
247
248B<NOTE 1>: With some combinations of operating systems and Perl
249releases C<SIGALRM> restarts C<select()>, instead of interrupting it.
250This means that an C<alarm()> followed by a C<select()> may together
251take the sum of the times specified for the the C<alarm()> and the
252C<select()>, not just the time of the C<alarm()>.
253
254Note that the interaction between alarms and sleeps is unspecified.
255
256=item setitimer ( $which, $floating_seconds [, $interval_floating_seconds ] )
257
258Start up an interval timer: after a certain time, a signal arrives,
259and more signals may keep arriving at certain intervals. To disable
260an "itimer", use C<$floating_seconds> of zero. If the
261C<$interval_floating_seconds> is set to zero (or unspecified), the
262timer is disabled B<after> the next delivered signal.
263
264Use of interval timers may interfere with C<alarm()>, C<sleep()>,
265and C<usleep()>. In standard-speak the "interaction is unspecified",
266which means that I<anything> may happen: it may work, it may not.
267
268In scalar context, the remaining time in the timer is returned.
269
270In list context, both the remaining time and the interval are returned.
271
272There are usually three or four interval timers available: the
273C<$which> can be C<ITIMER_REAL>, C<ITIMER_VIRTUAL>, C<ITIMER_PROF>, or
274C<ITIMER_REALPROF>. Note that which ones are available depends: true
275UNIX platforms usually have the first three, but (for example) Win32
276and Cygwin have only C<ITIMER_REAL>, and only Solaris seems to have
277C<ITIMER_REALPROF> (which is used to profile multithreaded programs).
278
279C<ITIMER_REAL> results in C<alarm()>-like behaviour. Time is counted in
280I<real time>; that is, wallclock time. C<SIGALRM> is delivered when
281the timer expires.
282
283C<ITIMER_VIRTUAL> counts time in (process) I<virtual time>; that is,
284only when the process is running. In multiprocessor/user/CPU systems
285this may be more or less than real or wallclock time. (This time is
286also known as the I<user time>.) C<SIGVTALRM> is delivered when the
287timer expires.
288
289C<ITIMER_PROF> counts time when either the process virtual time or when
290the operating system is running on behalf of the process (such as I/O).
291(This time is also known as the I<system time>.) (The sum of user
292time and system time is known as the I<CPU time>.) C<SIGPROF> is
293delivered when the timer expires. C<SIGPROF> can interrupt system calls.
294
295The semantics of interval timers for multithreaded programs are
296system-specific, and some systems may support additional interval
297timers. See your C<setitimer()> documentation.
298
299=item getitimer ( $which )
300
301Return the remaining time in the interval timer specified by C<$which>.
302
303In scalar context, the remaining time is returned.
304
305In list context, both the remaining time and the interval are returned.
306The interval is always what you put in using C<setitimer()>.
307
308=item clock_gettime ( $which )
309
310Return as seconds the current value of the POSIX high resolution timer
311specified by C<$which>. All implementations that support POSIX high
312resolution timers are supposed to support at least the C<$which> value
313of C<CLOCK_REALTIME>, which is supposed to return results close to the
314results of C<gettimeofday>, or the number of seconds since 00:00:00:00
315January 1, 1970 Greenwich Mean Time (GMT). Do not assume that
316CLOCK_REALTIME is zero, it might be one, or something else.
317Another potentially useful (but not available everywhere) value is
318C<CLOCK_MONOTONIC>, which guarantees a monotonically increasing time
319value (unlike time(), which can be adjusted). See your system
320documentation for other possibly supported values.
321
322=item clock_getres ( $which )
323
324Return as seconds the resolution of the POSIX high resolution timer
325specified by C<$which>. All implementations that support POSIX high
326resolution timers are supposed to support at least the C<$which> value
327of C<CLOCK_REALTIME>, see L</clock_gettime>.
328
329=item clock_nanosleep ( $which, $seconds, $flags = 0)
330
331Sleeps for the number of seconds (1e9ths of a second) specified.
332Returns the number of seconds actually slept. The $which is the
333"clock id", as with clock_gettime() and clock_getres(). The flags
334default to zero but C<TIMER_ABSTIME> can specified (must be exported
335explicitly) which means that C<$nanoseconds> is not a time interval
336(as is the default) but instead an absolute time. Can sleep for more
337than one second. Can also sleep for zero seconds, which often works
338like a I<thread yield>. See also C<Time::HiRes::sleep()>,
339C<Time::HiRes::usleep()>, and C<Time::HiRes::nanosleep()>.
340
341Do not expect clock_nanosleep() to be exact down to one nanosecond.
342Getting even accuracy of one thousand nanoseconds is good.
343
344=item clock()
345
346Return as seconds the I<process time> (user + system time) spent by
347the process since the first call to clock() (the definition is B<not>
348"since the start of the process", though if you are lucky these times
349may be quite close to each other, depending on the system). What this
350means is that you probably need to store the result of your first call
351to clock(), and subtract that value from the following results of clock().
352
353The time returned also includes the process times of the terminated
354child processes for which wait() has been executed. This value is
355somewhat like the second value returned by the times() of core Perl,
356but not necessarily identical. Note that due to backward
357compatibility limitations the returned value may wrap around at about
3582147 seconds or at about 36 minutes.
359
360=back
361
362=head1 EXAMPLES
363
364 use Time::HiRes qw(usleep ualarm gettimeofday tv_interval);
365
366 $microseconds = 750_000;
367 usleep($microseconds);
368
369 # signal alarm in 2.5s & every .1s thereafter
370 ualarm(2_500_000, 100_000);
371
372 # get seconds and microseconds since the epoch
373 ($s, $usec) = gettimeofday();
374
375 # measure elapsed time
376 # (could also do by subtracting 2 gettimeofday return values)
377 $t0 = [gettimeofday];
378 # do bunch of stuff here
379 $t1 = [gettimeofday];
380 # do more stuff here
381 $t0_t1 = tv_interval $t0, $t1;
382
383 $elapsed = tv_interval ($t0, [gettimeofday]);
384 $elapsed = tv_interval ($t0); # equivalent code
385
386 #
387 # replacements for time, alarm and sleep that know about
388 # floating seconds
389 #
390 use Time::HiRes;
391 $now_fractions = Time::HiRes::time;
392 Time::HiRes::sleep (2.5);
393 Time::HiRes::alarm (10.6666666);
394
395 use Time::HiRes qw ( time alarm sleep );
396 $now_fractions = time;
397 sleep (2.5);
398 alarm (10.6666666);
399
400 # Arm an interval timer to go off first at 10 seconds and
401 # after that every 2.5 seconds, in process virtual time
402
403 use Time::HiRes qw ( setitimer ITIMER_VIRTUAL time );
404
405 $SIG{VTALRM} = sub { print time, "\n" };
406 setitimer(ITIMER_VIRTUAL, 10, 2.5);
407
408 use Time::HiRes qw( clock_gettime clock_getres CLOCK_REALTIME );
409 # Read the POSIX high resolution timer.
410 my $high = clock_getres(CLOCK_REALTIME);
411 # But how accurate we can be, really?
412 my $reso = clock_getres(CLOCK_REALTIME);
413
414 use Time::HiRes qw( clock_nanosleep TIMER_ABSTIME );
415 clock_nanosleep(CLOCK_REALTIME, 1e6);
416 clock_nanosleep(CLOCK_REALTIME, 2e9, TIMER_ABSTIME);
417
418 use Time::HiRes qw( clock );
419 my $clock0 = clock();
420 ... # Do something.
421 my $clock1 = clock();
422 my $clockd = $clock1 - $clock0;
423
424=head1 C API
425
426In addition to the perl API described above, a C API is available for
427extension writers. The following C functions are available in the
428modglobal hash:
429
430 name C prototype
431 --------------- ----------------------
432 Time::NVtime double (*)()
433 Time::U2time void (*)(pTHX_ UV ret[2])
434
435Both functions return equivalent information (like C<gettimeofday>)
436but with different representations. The names C<NVtime> and C<U2time>
437were selected mainly because they are operating system independent.
438(C<gettimeofday> is Unix-centric, though some platforms like Win32 and
439VMS have emulations for it.)
440
441Here is an example of using C<NVtime> from C:
442
443 double (*myNVtime)(); /* Returns -1 on failure. */
444 SV **svp = hv_fetch(PL_modglobal, "Time::NVtime", 12, 0);
445 if (!svp) croak("Time::HiRes is required");
446 if (!SvIOK(*svp)) croak("Time::NVtime isn't a function pointer");
447 myNVtime = INT2PTR(double(*)(), SvIV(*svp));
448 printf("The current time is: %f\n", (*myNVtime)());
449
450=head1 DIAGNOSTICS
451
452=head2 negative time not invented yet
453
454You tried to use a negative time argument.
455
456=head2 internal error: useconds < 0 (unsigned ... signed ...)
457
458Something went horribly wrong-- the number of microseconds that cannot
459become negative just became negative. Maybe your compiler is broken?
460
461=head1 CAVEATS
462
463Notice that the core C<time()> maybe rounding rather than truncating.
464What this means is that the core C<time()> may be reporting the time
465as one second later than C<gettimeofday()> and C<Time::HiRes::time()>.
466
467Adjusting the system clock (either manually or by services like ntp)
468may cause problems, especially for long running programs that assume
469a monotonously increasing time (note that all platforms do not adjust
470time as gracefully as UNIX ntp does). For example in Win32 (and derived
471platforms like Cygwin and MinGW) the Time::HiRes::time() may temporarily
472drift off from the system clock (and the original time()) by up to 0.5
473seconds. Time::HiRes will notice this eventually and recalibrate.
474Note that since Time::HiRes 1.77 the clock_gettime(CLOCK_MONOTONIC)
475might help in this (in case your system supports CLOCK_MONOTONIC).
476
477=head1 SEE ALSO
478
479Perl modules L<BSD::Resource>, L<Time::TAI64>.
480
481Your system documentation for C<clock_gettime>, C<clock_settime>,
482C<gettimeofday>, C<getitimer>, C<setitimer>, C<ualarm>.
483
484=head1 AUTHORS
485
486D. Wegscheid <[email protected]>
487R. Schertler <[email protected]>
488J. Hietaniemi <[email protected]>
489G. Aas <[email protected]>
490
491=head1 COPYRIGHT AND LICENSE
492
493Copyright (c) 1996-2002 Douglas E. Wegscheid. All rights reserved.
494
495Copyright (c) 2002, 2003, 2004, 2005 Jarkko Hietaniemi. All rights reserved.
496
497This program is free software; you can redistribute it and/or modify
498it under the same terms as Perl itself.
499
500=cut
Note: See TracBrowser for help on using the repository browser.