| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| Assembly Questions and Answers for Assembly here! |
![]() |
|
|
Thread Tools | Search this Thread |
Rating:
|
Display Modes |
|
#1
|
||||
|
||||
|
[RESOLVED] CPU Clock Frequency
Next function uses high-resolution performance counter and RDTSC (read time-stamp counter) instruction to get the CPU frequency.
Code:
UINT CPU::GetCPUFrequency()
{
LARGE_INTEGER nCntFrequency;
if(!::QueryPerformanceFrequency(&nCntFrequency))
{
return 0; // high-resolution performance counter
// not supported
}
LARGE_INTEGER nCnt0, nCnt1;
ULONG nTs0, nTs1;
HANDLE hThread = ::GetCurrentThread();
int nPriority = ::GetThreadPriority(hThread);
::SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
::QueryPerformanceCounter(&nCnt1);
__asm
{
_emit 0x0f _asm _emit 0x31 // RDTSC
MOV nTs0, EAX
}
nCnt0.LowPart = nCnt1.LowPart;
while(((ULONG)nCnt1.LowPart - (ULONG)nCnt0.LowPart) < 10000)
{
::QueryPerformanceCounter(&nCnt1);
__asm
{
_emit 0x0f _asm _emit 0x31 // RDTSC
MOV nTs1, EAX
}
}
::SetThreadPriority(hThread, nPriority);
ULONG nCycles = nTs1 - nTs0;
ULONG nTicks = (ULONG)nCnt1.LowPart - nCnt0.LowPart;
nTicks *= 10000;
nTicks /= (nCntFrequency.LowPart / 100);
UINT nFrequency = nCycles / nTicks;
return nFrequency; // MHz
}
QUESTION: Does anybody know if there is another more accurate method, i.e. to read the exact frequency from somewhere?
__________________
Ovidiu Cucu
|
|
#2
|
||||
|
||||
|
what do you mean with not exact?
__________________
Visit my page: http://usuarios.lycos.es/hernandp and my blog... http://hernandp.blogspot.com |
|
#3
|
||||
|
||||
|
Re: CPU Clock Frequency
Quote:
So reformulating: Just... does anybody know another method(s) to get the processor frequency?
__________________
Ovidiu Cucu
|
|
#4
|
|||
|
|||
|
Re: CPU Clock Frequency
Quote:
__________________
VOTE HERE! |
|
#5
|
||||
|
||||
|
Re: CPU Clock Frequency
Quote:
Anyhow, thanks for testing
__________________
Ovidiu Cucu
|
|
#6
|
|||
|
|||
|
Re: CPU Clock Frequency
|
|
#7
|
|||
|
|||
|
Re: CPU Clock Frequency
Hi,
Here are several ideas: 1. To correct result move RDTSC from "while" cycle like this while(((ULONG)nCnt1.LowPart - (ULONG)nCnt0.LowPart) < 10000) { ::QueryPerformanceCounter(&nCnt1); } __asm { _emit 0x0f _asm _emit 0x31 // RDTSC MOV nTs1, EAX } 2. Use Sleep function when QueryPerformanceFrequency fails 3. For Windows NT such an info can be found in HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0 |
|
#8
|
|||
|
|||
|
Re: CPU Clock Frequency
Quote:
Quote:
Quote:
The kicker is that there is a skew between processors in a multiprocessor system, so you still need to know which processor it is you're taking information from. One thread can be assigned to one processor, another thread using that data can be assigned to a different processor. Also, you must be careful, laptop systems and some chips with smart power management will actually slow down when they get hot or they go to sleep. When they wake back up, there's a "leap" the processor makes in the "cycles" to compensate for the time it was down. This "leap" might make you think you've got more time than there really was between two reads of the RDTSC. Does anyone have a better solution for a high performance counter than this (other than a hardware solution from a plug-in card)? |
|
#9
|
||||
|
||||
|
Re: [RESOLVED] CPU Clock Frequency
Resolved.
__________________
Ovidiu Cucu
|
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|