templexxx / Tsc
Programming Languages
tsc
Get unix time (nanoseconds) in blazing low latency. About 10x~100x faster than time.Now().UnixNano().
-
Time Stamp Counter (TSC)
Based on CPU's TSC register.
-
Low Latency
Under 10ns to get each timestamp.
-
Stable
Unlike time.Now(), the latency of tsc is stable.
Although time.Now() is using VDSO to get time, but it's unstable, sometimes it will take more than 1000ns.
-
High Precision
tsc's precision is nanosecond.
-
Calibrate
Could be calibrated according to wall clock periodically, no worry about NTP. More details:
func Calibrate()
in tsc_amd64.go
Performance
OS | CPU | benchmark | time.Now().UnixNano() ns/op | tsc.UnixNano() ns/op | delta |
---|---|---|---|---|---|
MacOS Catalina | Intel Core i7-7700HQ | BenchmarkUnixNano-8 | 72.8 | 7.65 | -89.49% |
Ubuntu 18.04 | Intel Core i5-8250U | BenchmarkUnixNano-8 | 47.7 | 8.41 | -82.36% |
Clock Offset
The offset between wall clock and tsc is extremely low (under dozens ns in avg, maximum is hundreds-1000 ns), see test codes for more details.
Warning
If you need really accurate clock, you should run gofreq first to get TSC frequency, then set the value to Frequency Table.
Limitation
Intel Only
Only tested on Intel platform.
Invariant TSC supports
Invariant TSC could make sure TSC got synced among multi CPUs. They will be reset at same time, and run in same frequency.
Reliable TSC frequency
The TSC frequency provided by Intel CPU must be accurate.
AVX supports
Some instructions need AVX, see tsc_amd64.s for details.
No support on virtual machine
Have tested on AWS EC2, because of CPUID.15H is disabled. And tsc may won't work as we expect on a virtual machine.
Handle Limitation
If tsc can't be enabled, it will use time.Now().UnixNano() automatically.