github.com/ScaleFT/monotime@v0.0.0-20180209174256-2da272f3dad0/clock_runtime_nanotime.go (about)

     1  // +build linux
     2  // +build !go1.9
     3  
     4  // Copyright (C) 2016  Arista Networks, Inc.
     5  // Use of this source code is governed by the Apache License 2.0
     6  // that can be found in the COPYING file.
     7  
     8  // Package monotime provides a fast monotonic clock source.
     9  package monotime
    10  
    11  import (
    12  	"time"
    13  	_ "unsafe" // required to use //go:linkname
    14  )
    15  
    16  //go:noescape
    17  //go:linkname nanotime runtime.nanotime
    18  func nanotime() int64
    19  
    20  // Now returns the current time in nanoseconds from a monotonic clock.
    21  // The time returned is based on some arbitrary platform-specific point in the
    22  // past.  The time returned is guaranteed to increase monotonically at a
    23  // constant rate, unlike time.Now() from the Go standard library, which may
    24  // slow down, speed up, jump forward or backward, due to NTP activity or leap
    25  // seconds.
    26  func monotime() Monotime {
    27  	return Monotime(nanotime())
    28  }
    29  
    30  func duration(start Monotime, end Monotime) time.Duration {
    31  	return time.Duration(uint64(end) - uint64(start))
    32  }