github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/monotime/nanotime.go (about) 1 // Copyright (c) 2016 Arista Networks, Inc. 2 // Use of this source code is governed by the Apache License 2.0 3 // that can be found in the COPYING file. 4 5 // Package monotime provides a fast monotonic clock source. 6 package monotime 7 8 import ( 9 "time" 10 _ "unsafe" // required to use //go:linkname 11 ) 12 13 //go:noescape 14 //go:linkname nanotime runtime.nanotime 15 func nanotime() int64 16 17 // Now returns the current time in nanoseconds from a monotonic clock. 18 // The time returned is based on some arbitrary platform-specific point in the 19 // past. The time returned is guaranteed to increase monotonically at a 20 // constant rate, unlike time.Now() from the Go standard library, which may 21 // slow down, speed up, jump forward or backward, due to NTP activity or leap 22 // seconds. 23 func Now() uint64 { 24 return uint64(nanotime()) 25 } 26 27 // Since returns the amount of time that has elapsed since t. t should be 28 // the result of a call to Now() on the same machine. 29 func Since(t uint64) time.Duration { 30 return time.Duration(Now() - t) 31 }