monotime.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  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 functions to access 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. //
  15. // The time returned is based on some arbitrary platform-specific point in the
  16. // past. The time returned is guaranteed to increase monotonically without
  17. // notable jumps, unlike time.Now() from the Go standard library, which may
  18. // jump forward or backward significantly due to system time changes or leap
  19. // seconds.
  20. //
  21. // It's implemented using runtime.nanotime(), which uses CLOCK_MONOTONIC on
  22. // Linux. Note that unlike CLOCK_MONOTONIC_RAW, CLOCK_MONOTONIC is affected
  23. // by time changes. However, time changes never cause clock jumps; instead,
  24. // clock frequency is adjusted slowly.
  25. func Now() time.Duration {
  26. return time.Duration(nanotime())
  27. }
  28. // Since returns the time elapsed since t, obtained previously using Now.
  29. func Since(t time.Duration) time.Duration {
  30. return Now() - t
  31. }