From 7e9e36b5680c08aedae61edc5fb9f40bd4ce4155 Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sat, 2 Mar 2019 20:51:18 +0100 Subject: [PATCH] [host][openbsd] Remove calls to sysctl binary in host/host_openbsd.go #639 --- host/host_openbsd.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/host/host_openbsd.go b/host/host_openbsd.go index bb16fca..90d1fd3 100644 --- a/host/host_openbsd.go +++ b/host/host_openbsd.go @@ -9,8 +9,8 @@ import ( "io/ioutil" "os" "runtime" - "strconv" "strings" + "sync/atomic" "time" "unsafe" @@ -66,20 +66,28 @@ func InfoWithContext(ctx context.Context) (*InfoStat, error) { return ret, nil } +// cachedBootTime must be accessed via atomic.Load/StoreUint64 +var cachedBootTime uint64 + func BootTime() (uint64, error) { return BootTimeWithContext(context.Background()) } func BootTimeWithContext(ctx context.Context) (uint64, error) { - val, err := common.DoSysctrl("kern.boottime") - if err != nil { - return 0, err + // https://github.com/AaronO/dashd/blob/222e32ef9f7a1f9bea4a8da2c3627c4cb992f860/probe/probe_darwin.go + t := atomic.LoadUint64(&cachedBootTime) + if t != 0 { + return t, nil } - - boottime, err := strconv.ParseUint(val[0], 10, 64) + value, err := unix.Sysctl("kern.boottime") if err != nil { return 0, err } + bytes := []byte(value[:]) + var boottime uint64 + boottime = uint64(bytes[0]) + uint64(bytes[1])*256 + uint64(bytes[2])*256*256 + uint64(bytes[3])*256*256*256 + + atomic.StoreUint64(&cachedBootTime, boottime) return boottime, nil }