Skip to main content
gamedev.net
🔒 Locked

rdtsc help

Started by simon10k 6 replies 4.1k views
Original Post
simon10k
simon10k
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?
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
Kitt3n
Kitt3n

>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).

visit my website at www.kalmiya.com
nmi
nmi
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.
Ravuya
Ravuya
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.
simon10k
simon10k
Another thing, can I convert the rdtsc into ms?
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
nmi
nmi
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.
Rockoon1
Rockoon1
rdtsc also suffers from cpu's which throttle down (Pentium M?) to save energy

Kitt3n
Kitt3n
>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...


visit my website at www.kalmiya.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.