2016-10-13 22:51:29 -05:00
|
|
|
// Copyright (C) 2016 Arista Networks, Inc.
|
|
|
|
// Use of this source code is governed by the Apache License 2.0
|
|
|
|
// that can be found in the COPYING file.
|
|
|
|
|
|
|
|
// Package monotime provides a fast monotonic clock source.
|
|
|
|
package monotime
|
|
|
|
|
|
|
|
import (
|
2017-02-16 06:21:11 -06:00
|
|
|
"time"
|
2016-10-13 22:51:29 -05:00
|
|
|
_ "unsafe" // required to use //go:linkname
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:noescape
|
|
|
|
//go:linkname nanotime runtime.nanotime
|
|
|
|
func nanotime() int64
|
|
|
|
|
|
|
|
// Now returns the current time in nanoseconds from a monotonic clock.
|
|
|
|
// The time returned is based on some arbitrary platform-specific point in the
|
|
|
|
// past. The time returned is guaranteed to increase monotonically at a
|
|
|
|
// constant rate, unlike time.Now() from the Go standard library, which may
|
|
|
|
// slow down, speed up, jump forward or backward, due to NTP activity or leap
|
|
|
|
// seconds.
|
|
|
|
func Now() uint64 {
|
|
|
|
return uint64(nanotime())
|
|
|
|
}
|
2017-02-16 06:21:11 -06:00
|
|
|
|
|
|
|
// Since returns the amount of time that has elapsed since t. t should be
|
|
|
|
// the result of a call to Now() on the same machine.
|
|
|
|
func Since(t uint64) time.Duration {
|
|
|
|
return time.Duration(Now() - t)
|
|
|
|
}
|