Closed rfyiamcool closed 3 years ago
Could you print tsc.Enabled ?
If could not get the tsc frequency, it will just wrap time.Now().UnixNano()
test file
func BenchmarkGenTimeStamp(b *testing.B) {
for i := 0; i < b.N; i++ {
time.Now().UnixNano()
}
}
func BenchmarkGenTsc(b *testing.B) {
b.Log("cur: ", tsc.Enabled)
tsc.Enabled = true
b.Log("modify: ", tsc.Enabled)
for i := 0; i < b.N; i++ {
tsc.UnixNano()
}
}
result
$ go test -bench .
goos: linux
goarch: amd64
pkg: s
BenchmarkGenTimeStamp-40 20257611 52.6 ns/op
BenchmarkGenTsc-40 20446849 56.4 ns/op
--- BENCH: BenchmarkGenTsc-40
g_test.go:19: cur: false
g_test.go:21: modify: true
g_test.go:19: cur: true
g_test.go:21: modify: true
g_test.go:19: cur: true
g_test.go:21: modify: true
g_test.go:19: cur: true
g_test.go:21: modify: true
g_test.go:19: cur: true
g_test.go:21: modify: true
... [output truncated]
PASS
ok s 4.527s
😁 tsc's latency is abort 10ns in mac os, but tsc is slow in linux, I tried to execute on a different host.
原来是中国人。。。。
我在机房物理机、阿里和腾讯云的主机都有尝试过,速度都不理想,不知道是否有跟配置有关系。
There are many limitation of using TSC as a stable clock source, in init() process, we'll check these conditions.
And in your testing, it showed that tsc wasn't enabled. Which means this CPU can't satisify the needs of TSC. That's a pity. :D
😅 thank u.
原来是中国人。。。。
我在机房物理机、阿里和腾讯云的主机都有尝试过,速度都不理想,不知道是否有跟配置有关系。
虚拟机的部分 CPUID 指令受限(原因不详,没有探究过),导致无法获取 frequency,我这里做的比较谨慎,直接就判定 tsc 无法按照预期稳定工作了。所以虚拟机上一般干不了这事。
之前试过 AWS 的 metal 好像也不行(可能记错了)
好的,谢谢。
uname
cpu
bench
test file