From a444b5eccba68812725a9125bbbc44f23d9538c5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 9 Apr 2024 08:21:20 +0530 Subject: [PATCH] Only use raw monotonic time on Linux and macOS --- tools/utils/clock_with_raw.go | 18 ++++++++++++++++++ tools/utils/clock_without_raw.go | 18 ++++++++++++++++++ tools/utils/misc.go | 15 --------------- 3 files changed, 36 insertions(+), 15 deletions(-) create mode 100644 tools/utils/clock_with_raw.go create mode 100644 tools/utils/clock_without_raw.go diff --git a/tools/utils/clock_with_raw.go b/tools/utils/clock_with_raw.go new file mode 100644 index 000000000..934647abd --- /dev/null +++ b/tools/utils/clock_with_raw.go @@ -0,0 +1,18 @@ +//go:build linux || darwin + +package utils + +import ( + "time" + + "golang.org/x/sys/unix" +) + +func MonotonicRaw() (time.Time, error) { + ts := unix.Timespec{} + if err := unix.ClockGettime(unix.CLOCK_MONOTONIC_RAW, &ts); err != nil { + return time.Time{}, err + } + s, ns := ts.Unix() + return time.Unix(s, ns), nil +} diff --git a/tools/utils/clock_without_raw.go b/tools/utils/clock_without_raw.go new file mode 100644 index 000000000..2cb7444d5 --- /dev/null +++ b/tools/utils/clock_without_raw.go @@ -0,0 +1,18 @@ +//go:build !linux && !darwin + +package utils + +import ( + "time" + + "golang.org/x/sys/unix" +) + +func MonotonicRaw() (time.Time, error) { + ts := unix.Timespec{} + if err := unix.ClockGettime(unix.CLOCK_MONOTONIC, &ts); err != nil { + return time.Time{}, err + } + s, ns := ts.Unix() + return time.Unix(s, ns), nil +} diff --git a/tools/utils/misc.go b/tools/utils/misc.go index 579efe397..60c75935c 100644 --- a/tools/utils/misc.go +++ b/tools/utils/misc.go @@ -10,11 +10,9 @@ import ( "reflect" "runtime" "strconv" - "time" "golang.org/x/exp/constraints" "golang.org/x/exp/slices" - "golang.org/x/sys/unix" ) var _ = fmt.Print @@ -312,16 +310,3 @@ func FunctionName(a any) string { } return "" } - -func MonotonicRaw() (time.Time, error) { - ts := unix.Timespec{} - var clock_id int32 = unix.CLOCK_MONOTONIC - if runtime.GOOS == "linux" { - clock_id = unix.CLOCK_MONOTONIC_RAW - } - if err := unix.ClockGettime(clock_id, &ts); err != nil { - return time.Time{}, err - } - s, ns := ts.Unix() - return time.Unix(s, ns), nil -}