Skip to content

Commit ad8a5cb

Browse files
committed
Read System Clock for Monotonic Time
This PR changes the `janet_gettime` implementation for OSX <10.12 to read the system clock for `(os/clock :monotonic)`. As far as I was able to find online this is _a_ monotonic clock, although it produces different values from `clock_gettime(CLOCK_MONOTONIC, ...)` on the same system. I can speculate that this is related to `SYSTEM_CLOCK` monotonic time being implemented with `mach_absolute_time` which is documented to _not advance during sleep_, and I suspect that `clock_gettime(CLOCK_MONOTONIC, ...)` does. **Resources**: - `clock_get_time` implementation for the `SYSTEM_CLOCK`: <https://github.com/apple-oss-distributions/xnu/blob/e3723e1f17661b24996789d8afc084c0c3303b26/osfmk/kern/clock_oldops.c#L284-L296> <https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/osfmk/arm/rtclock.c#L248-L260> - `mach_absolute_time` and `mach_continuous_time` definitions: <https://github.com/apple-oss-distributions/xnu/blob/e3723e1f17661b24996789d8afc084c0c3303b26/osfmk/mach/mach_time.h#L55-L68> - Stack overflow post for implementing `clock_gettime` on OS X before 10.12: <https://stackoverflow.com/questions/11680461/monotonic-clock-on-osx>
1 parent 99abada commit ad8a5cb

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/core/util.c

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -931,27 +931,24 @@ int janet_gettime(struct timespec *spec, enum JanetTimeSource source) {
931931
#include <mach/clock.h>
932932
#include <mach/mach.h>
933933
int janet_gettime(struct timespec *spec, enum JanetTimeSource source) {
934-
if (source == JANET_TIME_REALTIME) {
934+
if (source == JANET_TIME_CPUTIME) {
935+
clock_t tmp = clock();
936+
spec->tv_sec = tmp / CLOCKS_PER_SEC;
937+
spec->tv_nsec = ((tmp - (spec->tv_sec * CLOCKS_PER_SEC)) * 1000000000) / CLOCKS_PER_SEC;
938+
} else {
935939
clock_serv_t cclock;
936940
mach_timespec_t mts;
937-
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
941+
clock_id_t cid = CALENDAR_CLOCK;
942+
if (source == JANET_TIME_REALTIME) {
943+
cid = CALENDAR_CLOCK;
944+
} else if (source == JANET_TIME_MONOTONIC) {
945+
cid = SYSTEM_CLOCK;
946+
}
947+
host_get_clock_service(mach_host_self(), cid, &cclock);
938948
clock_get_time(cclock, &mts);
939949
mach_port_deallocate(mach_task_self(), cclock);
940950
spec->tv_sec = mts.tv_sec;
941951
spec->tv_nsec = mts.tv_nsec;
942-
} else if (source == JANET_TIME_MONOTONIC) {
943-
clock_serv_t cclock;
944-
int nsecs;
945-
mach_msg_type_number_t count;
946-
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
947-
clock_get_attributes(cclock, CLOCK_GET_TIME_RES, (clock_attr_t)&nsecs, &count);
948-
mach_port_deallocate(mach_task_self(), cclock);
949-
clock_getres(CLOCK_MONOTONIC, spec);
950-
}
951-
if (source == JANET_TIME_CPUTIME) {
952-
clock_t tmp = clock();
953-
spec->tv_sec = tmp / CLOCKS_PER_SEC;
954-
spec->tv_nsec = ((tmp - (spec->tv_sec * CLOCKS_PER_SEC)) * 1000000000) / CLOCKS_PER_SEC;
955952
}
956953
return 0;
957954
}

0 commit comments

Comments
 (0)