Sign in to follow this  
simon10k

rdtsc help

Recommended Posts

simon10k    220
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 this post


Link to post
Share on other sites
Kitt3n    468

>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 this post


Link to post
Share on other sites
nmi    978
Quote:
Original post by Kitt3n
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).


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 this post


Link to post
Share on other sites
Ravuya    135
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 this post


Link to post
Share on other sites
nmi    978
Quote:
Original post by simon10k
Another 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 this post


Link to post
Share on other sites
Kitt3n    468
>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...


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this