How to detect if RDTSC returns a constant rate counter value?

后端 未结 4 1069
时光说笑
时光说笑 2021-01-19 15:17

It seems most newer CPUs from both AMD and Intel implement rdtsc as a constant rate counter, avoiding the issues caused by frequency changing as a result of things like Turb

相关标签:
4条回答
  • 2021-01-19 15:24

    I know it's a long time since the original question was asked, but can I just point out that checking the generation/model of the processor is absolutely the WRONG thing to do. First of all, it's very easy to get the code wrong so that it doesn't work on future generation processors (because the family/model numbers aren't always "linear"), and secondly, just because a processor is "a later family/model" than the ones you knew this works on, it's not a guarantee that the feature is there. It's LIKELY, but I've seen plenty of code that does this badly, and thus "new processor comes out, and the code gets things wrong".

    Use the CPUID bit to check if the processor has the correct bit or not.

    0 讨论(0)
  • 2021-01-19 15:34

    I do count the number o ticks in a second and then compare to the informed clock at /proc/cpuinfo. It only works with dynamic clock disabled. See the source: https://github.com/petersenna/rdtscbench

    0 讨论(0)
  • 2021-01-19 15:40

    You can use CPUID to tell you. From the docs on CPUID Fn8000_0007_EDX bit 8:

    TscInvariant: TSC invariant. The TSC rate is ensured to be invariant across all P-States, C-States, and stop grant transitions (such as STPCLK Throttling); therefore the TSC is suitable for use as a source of time. 0 = No such guarantee is made and software should avoid attempting to use the TSC as a source of time.

    0 讨论(0)
  • 2021-01-19 15:41

    just use CPUID to detect the generation of the CPU, see if its using constant counters. I'd however, suggest using a profiling API instead though, something like AMD's codeanalyst sdk would do well

    0 讨论(0)
提交回复
热议问题