Files
tailscale/tstime/monocoarse_std.go
Josh Bleecher Snyder a43f939828 tstime: add MonotonicCoarse
MonotonicCoarse provides a coarse monotonic time.
On some platforms, it is implemented in assembly,
which lets us do much less work than time.Now,
which gets a high precision monotonic time and
a high precision wall time.

The assembly code is tied to a particular Go release
because it reaches into the Go internals
in order to switch to the system stack for the vdso call.

On my darwin/arm64 machine, there is no perf difference.
On my linux/amd64 machine, MonotonicCoarse is 5x faster (50ns -> 10ns).
On my linux/arm64 VM, MonotonicCoarse is 16x faster (64ns -> 4ns).

We could also use this in the rate limiter and magicsock,
which are two other uses of time.Now that show up in the CPU pprof
when doing throughput benchmarking.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
2021-07-19 15:33:51 -07:00

17 lines
478 B
Go

// +build !go1.16 go1.17 !linux !amd64,!arm64
package tstime
import "time"
var referenceTime = time.Now()
// MonotonicCoarse returns the number of monotonic seconds elapsed
// since an unspecified starting point, at low precision.
// It is only meaningful when compared to the
// result of previous MonotonicCoarse calls.
// On some platforms, MonotonicCoarse is much faster than time.Now.
func MonotonicCoarse() int64 {
return int64(time.Since(referenceTime).Seconds())
}