2024-12-10 13:15:05 +09:00
|
|
|
package hodu
|
|
|
|
|
|
|
|
import "syscall"
|
|
|
|
import "unsafe"
|
|
|
|
import "golang.org/x/sys/unix"
|
|
|
|
|
|
|
|
|
|
|
|
// utilize the builtin runtime.nanotime()
|
|
|
|
//go:noescape
|
|
|
|
//go:linkname nanotime runtime.nanotime
|
|
|
|
func nanotime() int64
|
|
|
|
|
|
|
|
func monotonic_time() uint64 {
|
|
|
|
var n int64
|
|
|
|
var err error
|
|
|
|
var uts unix.Timespec
|
|
|
|
|
|
|
|
n = nanotime() // hopefully it's faster than a system call. say, vdso is utilized.
|
2024-12-27 14:43:44 +09:00
|
|
|
if n >= 0 { return uint64(n) }
|
2024-12-10 13:15:05 +09:00
|
|
|
|
|
|
|
err = unix.ClockGettime(unix.CLOCK_MONOTONIC, &uts)
|
|
|
|
if err != nil {
|
|
|
|
//var errno syscall.Errno
|
|
|
|
var r uintptr
|
|
|
|
var sts syscall.Timespec
|
|
|
|
r, _, _/*errno*/ = syscall.Syscall(syscall.SYS_CLOCK_GETTIME, unix.CLOCK_MONOTONIC, uintptr(unsafe.Pointer(&sts)), 0)
|
2024-12-26 00:20:44 +09:00
|
|
|
if r == ^uintptr(0) { return uint64(n) } // may be a negative number cast to unsigned. no other fall-back
|
2024-12-10 13:15:05 +09:00
|
|
|
return uint64(sts.Nano())
|
|
|
|
}
|
|
|
|
|
|
|
|
return uint64(uts.Nano())
|
|
|
|
}
|