4

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 TurboBoost or power saving settings.

As rdtsc is a lot more suitable for performance measurements than QueryPerformanceCounter because of its much lower overhead, I would like to use it whenever possible.

How can I detect reliably if the rdtsc is a constant rate counter or not?

Gabe
  • 84,912
  • 12
  • 139
  • 238
Suma
  • 33,181
  • 16
  • 123
  • 191

4 Answers4

7

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.

Gabe
  • 84,912
  • 12
  • 139
  • 238
  • Great. Seems to work for Intel as well: http://stackoverflow.com/questions/3388134/rdtsc-accuracy-across-cpu-cores/4145156#4145156 – Suma Dec 22 '10 at 14:16
  • The issue here is that with dynamic clock enabled, tasks would take different amount of time to execute. So even if the rdtsc register increment is not affected by the clock itself, the amount of time needed to execute tasks is affected by the clock speed. – Peter Dec 21 '14 at 19:51
0

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.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
-1

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

Necrolis
  • 25,836
  • 3
  • 63
  • 101
-1

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

Peter
  • 1,753
  • 18
  • 19