2018-06-28 16:40:04 +00:00
|
|
|
package tracer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/sys/windows"
|
2019-07-18 19:36:05 +00:00
|
|
|
|
|
|
|
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
|
2018-06-28 16:40:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// This method is more precise than the go1.8 time.Now on Windows
|
|
|
|
// See https://msdn.microsoft.com/en-us/library/windows/desktop/hh706895(v=vs.85).aspx
|
|
|
|
// It is however ~10x slower and requires Windows 8+.
|
|
|
|
func highPrecisionNow() int64 {
|
|
|
|
var ft windows.Filetime
|
|
|
|
windows.GetSystemTimePreciseAsFileTime(&ft)
|
|
|
|
return ft.Nanoseconds()
|
|
|
|
}
|
|
|
|
|
|
|
|
func lowPrecisionNow() int64 {
|
|
|
|
return time.Now().UTC().UnixNano()
|
|
|
|
}
|
|
|
|
|
|
|
|
var now func() int64
|
|
|
|
|
|
|
|
// If GetSystemTimePreciseAsFileTime is not available we default to the less
|
|
|
|
// precise implementation based on time.Now()
|
|
|
|
func init() {
|
|
|
|
if err := windows.LoadGetSystemTimePreciseAsFileTime(); err != nil {
|
2019-07-18 19:36:05 +00:00
|
|
|
log.Warn("Unable to load high precison timer, defaulting to time.Now()")
|
2018-06-28 16:40:04 +00:00
|
|
|
now = lowPrecisionNow
|
|
|
|
} else {
|
|
|
|
now = highPrecisionNow
|
|
|
|
}
|
|
|
|
}
|