Subject: | Patch: OSX clock_getres() and clock_gettime() support |
Please find attached a patch that adds clock_getres() and clock_gettime() support for Mac OSX. CLOCK_MONOTONIC is properly
supported, and CLOCK_REALTIME is implemented in terms of gettimeofday().
The existing tests pass.
% prove -Ilib -Iblib/lib -Iblib/arch
t/alarm.t ......... ok
t/clock.t ......... ok
t/gettimeofday.t .. ok
t/itimer.t ........ ok
t/nanosleep.t ..... ok
t/sleep.t ......... ok
t/stat.t .......... ok
t/time.t .......... ok
t/tv_interval.t ... ok
t/ualarm.t ........ ok
t/usleep.t ........ ok
All tests successful.
Files=11, Tests=68, 26 wallclock secs ( 0.10 usr 0.05 sys + 14.34 cusr 0.21 csys = 14.70 CPU)
Result: PASS
1) macbook-wifki:~/projects/misc/3rd-party-projects/Time-HiRes-1.9725-patched% prove -Ilib -Iblib/lib -Iblib/arch -v
t/clock.tt/clock.t ..
1..5
# I am the main process 62659, starting the watchdog process...
# The watchdog process 62660 launched, continuing testing...
# I am the watchdog process 62660, sleeping for 360 seconds...
ok 1 - require Time::HiRes;
# have_clock_gettime = 1
# have_clock_getres = 1
# have_clock_nanosleep = 0
# have_clock = 1
# CLOCK_REALTIME: try = 1
# t1 = 1343628900.29546, t0 = 1343628898.29596
# dt = 1.9995002746582, rt = 0.333000183105469
ok 2
ok 3
ok 4 # skip no clock_nanosleep
# clock = 0.047162
# clock = 0.047162 0.120666
# clock = 0.047162 0.120666 0.192792
# clock = 0.047162 0.120666 0.192792 0.264973
ok 5
# I am the main process 62659, terminating the watchdog process 62660 before it terminates me in 358 seconds (testing took 2
seconds).
# kill KILL 62660 = 1
# All done.
ok
All tests successful.
Files=1, Tests=5, 2 wallclock secs ( 0.03 usr 0.01 sys + 0.25 cusr 0.01 csys = 0.30 CPU)
Result: PASS
Subject: | macos-clock-monotonic.diff |
diff -rN Time-HiRes-1.9725/HiRes.xs Time-HiRes-1.9725-patched/HiRes.xs
42a43,44
> #include "clock_emu.h"
>
diff -rN Time-HiRes-1.9725/Makefile.PL Time-HiRes-1.9725-patched/Makefile.PL
308a309
> #include "clock_emu.h"
536,538c537
< if (exists $Config{d_clock_gettime}) {
< $has_clock_gettime++ if $Config{d_clock_gettime}; # Unlikely...
< } elsif (has_clock_xxx('gettime')) {
---
> if (has_clock_xxx('gettime')) {
540a540,541
> } elsif (exists $Config{d_clock_gettime}) {
> $has_clock_gettime++ if $Config{d_clock_gettime}; # Unlikely...
diff -rN Time-HiRes-1.9725/clock_emu.h Time-HiRes-1.9725-patched/clock_emu.h
0a1,49
> #ifndef __CLOCK_EMU_H__
>
> // MacOS X has a monotonic clock, but it's not POSIX.
>
> # if defined(__APPLE__)
>
> # include <sys/time.h>
> # include <mach/mach_time.h>
>
> # define CLOCK_REALTIME 0x01
> # define CLOCK_MONOTONIC 0x02
>
> static uint64_t monotonic_timebase_factor = 0;
>
> int clock_gettime(int clock_id, struct timespec *ts) {
> switch (clock_id) {
> case CLOCK_REALTIME:
> // Cheezy hack? What would be better?
> return gettimeofday(ts, NULL);
>
> case CLOCK_MONOTONIC:
> if (monotonic_timebase_factor == 0) {
> mach_timebase_info_data_t timebase_info;
> (void)mach_timebase_info(&timebase_info);
> monotonic_timebase_factor = timebase_info.numer / timebase_info.denom;
> }
>
> uint64_t monotonic_nanoseconds = (
> mach_absolute_time() * monotonic_timebase_factor
> );
>
> ts->tv_sec = monotonic_nanoseconds / 1E9;
> ts->tv_nsec = monotonic_nanoseconds - ts->tv_sec;
> return 0;
>
> default:
> return -1;
> }
> }
>
> int clock_getres(int clock_id, struct timespec *ts) {
> ts->tv_sec = 0;
> ts->tv_nsec = 1;
> return 0;
> }
>
> # endif
>
> #endif