diff --git a/system-freebsd.go b/system-freebsd.go index 796dd65..d2e79b5 100644 --- a/system-freebsd.go +++ b/system-freebsd.go @@ -1,5 +1,4 @@ //go:build freebsd - package hodu import "syscall" @@ -13,10 +12,15 @@ import "unsafe" //const FREEBSD_CLOCK_MONOTONIC uintptr = C.CLOCK_MONOTONIC const CLOCK_MONOTONIC uintptr = 4 +// utilize the builtin runtime.nanotime() +//go:noescape +//go:linkname nanotime runtime.nanotime +func nanotime() int64 + func monotonic_time() uint64 { var ts syscall.Timespec var errno syscall.Errno _, _, errno = syscall.Syscall(syscall.SYS_CLOCK_GETTIME, CLOCK_MONOTONIC, uintptr(unsafe.Pointer(&ts)), 0) - if errno != 0 { return 0 } + if errno != 0 { return uint64(nanotime()) } return uint64(ts.Nano()) } diff --git a/system-linux.go b/system-linux.go index efb7866..9c3cb13 100644 --- a/system-linux.go +++ b/system-linux.go @@ -1,5 +1,4 @@ //go:build linux - package hodu import "syscall" @@ -13,10 +12,15 @@ import "unsafe" //const CLOCK_MONOTONIC uintptr = C.CLOCK_MONOTONIC const CLOCK_MONOTONIC uintptr = 1 +// utilize the builtin runtime.nanotime() +//go:noescape +//go:linkname nanotime runtime.nanotime +func nanotime() int64 + func monotonic_time() uint64 { var ts syscall.Timespec var errno syscall.Errno _, _, errno = syscall.Syscall(syscall.SYS_CLOCK_GETTIME, CLOCK_MONOTONIC, uintptr(unsafe.Pointer(&ts)), 0) - if errno != 0 { return 0 } + if errno != 0 { return uint64(nanotime()) } return uint64(ts.Nano()) }