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

4

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?

Utas answered 22/12, 2010 at 13:41 Comment(0)
M
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.

Menstruation answered 22/12, 2010 at 14:13 Comment(2)
Great. Seems to work for Intel as well: #3388634Utas
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.Sumach
C
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.

Cappella answered 20/12, 2012 at 14:57 Comment(0)
G
-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

Greyhen answered 22/12, 2010 at 14:10 Comment(0)
S
-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

Sumach answered 11/3, 2012 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.