nanotime.go 1011 B

1234567891011121314151617181920212223242526272829303132
  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. // Package monotime provides a fast monotonic clock source.
  5. package monotime
  6. import (
  7. "time"
  8. _ "unsafe" // required to use //go:linkname
  9. )
  10. //go:noescape
  11. //go:linkname nanotime runtime.nanotime
  12. func nanotime() int64
  13. // Now returns the current time in nanoseconds from a monotonic clock.
  14. // The time returned is based on some arbitrary platform-specific point in the
  15. // past. The time returned is guaranteed to increase monotonically at a
  16. // constant rate, unlike time.Now() from the Go standard library, which may
  17. // slow down, speed up, jump forward or backward, due to NTP activity or leap
  18. // seconds.
  19. func Now() uint64 {
  20. return uint64(nanotime())
  21. }
  22. // Since returns the amount of time that has elapsed since t. t should be
  23. // the result of a call to Now() on the same machine.
  24. func Since(t uint64) time.Duration {
  25. return time.Duration(Now() - t)
  26. }