tsc icon indicating copy to clipboard operation
tsc copied to clipboard

Cannot get TSC frequency by CPU feature detection

Open simonhf opened this issue 3 years ago • 4 comments

This code [1] is failing for me when I try to run this on a Google Cloud Platform box:

	// Cannot get TSC frequency by CPU feature detection, may caused by:
	// 1. New CPU which I haven't updated the its crystal frequency. Please raise a issue, thank you.
	// 2. Virtual environment
	if cpu.X86.TSCFrequency == 0 {
		return false
	}

cpuinfo says:

$ cat /proc/cpuinfo | grep name | tail -1
model name      : Intel(R) Xeon(R) CPU

$ cat /proc/cpuinfo | grep tsc | tail -1
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves arat avx512_vnni md_clear arch_capabilities

Is this a (1) issue or a (2) issue? Thanks!

[1] https://github.com/templexxx/tsc/blob/master/tsc_amd64.go#L69

simonhf avatar Jun 13 '21 01:06 simonhf

The baseline of tsc frequency is from: https://github.com/templexxx/cpu/blob/master/cpu_x86.go

And the logic in these codes couldn't handle your platform (see func getNativeTSCFrequency for details)

Could you print some useful infos between the key steps in that function? That would help a lot. @simonhf

Thank you!

templexxx avatar Jun 14 '21 02:06 templexxx

@templexxx Thank for the quick response!

I did the following to see what happens in getNativeTSCFrequency():

$ git clone https://github.com/templexxx/cpu.git
$ cd cpu/
$ GO111MODULE="off" go get github.com/stretchr/testify/require
$ cat cpu_test.go
package cpu

import (
        "fmt"
        "testing"
        "github.com/stretchr/testify/require"
)

func TestDummy(t *testing.T) {
        fmt.Printf("- TestDummy()\n")
        expected := 1
        actual := 1
        require.Equal(t, expected, actual)
}

$ # hack cpu_x86.go
$ git diff
diff --git a/cpu_x86.go b/cpu_x86.go
index 7297fe2..30e93f5 100644
--- a/cpu_x86.go
+++ b/cpu_x86.go
@@ -171,12 +171,15 @@ func getName() string {
 // only supports Intel (Skylake or later microarchitecture) & key information is from Intel manual & kernel codes
 // (especially this commit: https://github.com/torvalds/linux/commit/604dc9170f2435d27da5039a3efd757dceadc684).
 func getNativeTSCFrequency(name, sign string, steppingID uint32) uint64 {
+       fmt.Printf("- getNativeTSCFrequency(name=%s sign=%s steppingID=%d) // enter\n", name, sign, steppingID)
 
        if vendorID() != Intel {
+               fmt.Printf("- vendorID() != Intel // %d\n", vendorID())
                return 0
        }
 
        if maxFunctionID() < 0x15 {
+               fmt.Printf("- maxFunctionID() < 0x15 // 0x%x\n", maxFunctionID())
                return 0
        }
$ go test -v ./...
- getNativeTSCFrequency(name=Intel(R) Xeon(R) CPU sign=06_55H steppingID=7) // enter
- maxFunctionID() < 0x15 // 0xd
=== RUN   TestDummy
- TestDummy()
--- PASS: TestDummy (0.00s)
PASS
ok      _/home/simon_hardy_francis_dapperlabs_c/20210614-go-cpu/cpu     0.005s

Looks like this line returns [1]. Any ideas why?

[1] https://github.com/templexxx/cpu/blob/master/cpu_x86.go#L180

simonhf avatar Jun 14 '21 21:06 simonhf

@simonhf

That helps a lot. Thank you!

I think the issue is caused by the max "Input Value for Basic CPUID Information" has been modified in the virutal machine.

And the next calling needs to pass value which is bigger than the accepetable value returned by the CPUID.

I'll do more research soon. Maybe I could bypass the limitation of the input value, and using a new way to get a baseline TSC frequency on virtual environment.

templexxx avatar Jun 15 '21 01:06 templexxx

@simonhf I wonder tsc clock could be used in google cloud?

AWS EC2 could set the tsc clock source, if google could do that too, this lib will work since v1.2.1

templexxx avatar Feb 15 '22 07:02 templexxx