# rdtsc help

## Recommended Posts

I was just wondering if this is the correct way to use the rdtsc
unsigned __int64 get_cpu_tick()
{
DWORD dwLow, dwHigh;

__asm
{
rdtsc
mov dwLow, eax
mov dwHigh, edx
}

return ((unsigned __int64) dwHigh << 32 | dwLow);
}


or am I missing something? Thanks. ~edit~ I found another function, unsigned __int64 __rdtsc(void); Which does what I need, another question, does QueryPerformanceFrequency() return the frequency of the processsor?

##### Share on other sites

>Which does what I need, another question, does QueryPerformanceFrequency()
>return the frequency of the processsor?
It returns the number of counts / second which the high-frequency timer has.

Beware that using rdtsc on multi-core cpu's _will_ give you problems
(actually qpc/qpf will also, if you don't install a special win-xp patch).

##### Share on other sites
Quote:
 Original post by Kitt3nBeware that using rdtsc on multi-core cpu's _will_ give you problems(actually qpc/qpf will also, if you don't install a special win-xp patch).

Maybe you can try RDTSCP, which will also return another value in ecx to identify the process (at least if your processor supports it, mine does not).

If you want to time very short functions (i.e. to get the cycle count), you may also want to put a serializing instruction in front of RDTSC, i.e. CPUID is one.

To time your code using RDTSC, you can set the thread affinity to keep it on a single cpu.

##### Share on other sites
Be forewarned: From experimentation RDTSC doesn't appear to work right on AMD64s or dual-core machines. I haven't bothered to research a solution yet.

##### Share on other sites
Another thing, can I convert the rdtsc into ms?

##### Share on other sites
Quote:
 Original post by simon10kAnother thing, can I convert the rdtsc into ms?

If you measured a reference value for your ticks, you can divide the number of ticks by the tick frequency.

##### Share on other sites
rdtsc also suffers from cpu's which throttle down (Pentium M?) to save energy

##### Share on other sites
>rdtsc also suffers from cpu's which throttle down (Pentium M?) to save energy
Same thing goes for qpc/qpf

>Maybe you can try RDTSCP, which will also return another value in ecx to
>identify the process (at least if your processor supports it, mine does not).
www.intel.com mentions a way to determine on which cpu code was just executed (look for APIC).

>To time your code using RDTSC, you can set the thread affinity to keep it
>on a single cpu.
Easiest way, yes - but using the APIC, you can actually make rdtsc multicore
safe. It involves running a thread on all cpu's in realtime priority and
letting them simultaneously get an initial/reference-timestamp.

From then on each time you get a timestamp, check which cpu it ran on
and subtract the initial timestamp.

This still will give problems with throttle-down though...

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628377
• Total Posts
2982326

• 10
• 9
• 14
• 24
• 11