cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

johnt
Journeyman III

How to read CPU clock

I have an Athlon 64 X2 processor and i am curious on how i can write a simple Visual Basic program that reads the frequency of each core (similar to "amd clock" program).

 

I have some knowledge about programming but i can not figure out what

should i download in terms of libraries/drivers/SDK that are specific to the

Athlon 64 X2.

 

So, except Visual Studio what else (CPU SDK, libraries etc) must i  download?

0 Likes
15 Replies
avk
Adept III

You'll need to read some MSDN online articles about Win32 API functions: QueryPerformanceCounter, QueryPerformanceFrequency, SetThreadAffinityMask and so on. All of these are available at www.msdn.com. Maybe you'll need to download the Platform SDK from the mentioned site. Good luck .
0 Likes
johnt
Journeyman III

Originally posted by: avk You'll need to read some MSDN online articles about Win32 API functions: QueryPerformanceCounter, QueryPerformanceFrequency, SetThreadAffinityMask and so on. All of these are available at www.msdn.com. Maybe you'll need to download the Platform SDK from the mentioned site. Good luck .


 

Thank you!

I will try that

0 Likes

While up at MSDN you might also try the Visual Basic forum. This link is a discussion which mentions RDTSC. Its not exactly what you're looking for but these folks are pretty low level.

http://forums.msdn.microsoft.com/en-US/vblanguage/thread/e0eda611-35f1-4f46-a414-c7830ff1d6f0

You could also check Framewave. I don't think it has a processor frequency function, but it does provide access to RDTSC and CPUID. Look at the file system.cpp in ..\Framewave\domain\fwBase\src. Framewave is available here: http://sourceforge.net/projects/framewave/ It is not Visual Basic, so you would have to figure out how to call in to it.

Regards,

    Randy [AMD]

 

0 Likes
nismo
Journeyman III

Originally posted by: avk You'll need to read some MSDN online articles about Win32 API functions: QueryPerformanceCounter, QueryPerformanceFrequency, SetThreadAffinityMask and so on. All of these are available at www.msdn.com. Maybe you'll need to download the Platform SDK from the mentioned site. Good luck .


Do you know about linux same functions

 

especially the SetThreadAffinityMask?

0 Likes
avk
Adept III

johnt: BTW, you can acquire the CPU frequency simply by reading the Registry key "HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0".

Randy: Although the RDTSC instruction can be used for acquiring the CPU frequency, it maybe not so easy as it looks, especially for VB programmer who sits on multicore CPU PC .
0 Likes
0r
Journeyman III

johnt

Sorry for my English, i'm from Ukraine 😃

You may try my function, it's good enought

qword CalculateClockSpeed( int cpu )
{
SetThreadAffinityMask( ::GetCurrentThread(), ulong( 1 << cpu ) );
::Sleep( 0 ); // Ensure system to switch to the right CPU

const int numResults = 5;
qword results[numResults] = {0};
qword ret = 0;

int i;
for ( i = 0; i < numResults; i++ )
{
LARGE_INTEGER waitTime, startCount, curCount;
QueryPerformanceCounter( &startCount );
QueryPerformanceFrequency( &waitTime );
int scale = 5; // Take 1/32 of a second for the measurement.
waitTime.QuadPart >>= scale;

qword start = GetCycleNumber();
do
{
QueryPerformanceCounter( &curCount );
} while ( curCount.QuadPart - startCount.QuadPart < waitTime.QuadPart );
qword end = GetCycleNumber();

results = ( ( end - start ) << scale );
}

for ( i = 0; i < numResults; i++ )
if ( results
> ret )
ret = results;

return ret;
}

//.....

inline qword GetCycleNumber( void )
{
qword count = 0;
__asm
{
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
cpuid // syncronization
rdtsc
lea edi, [count]
mov dword ptr[edi + 0], eax
mov dword ptr[edi + 4], edx
xor eax, eax
cpuid
}
return count;
}

good luck!
0 Likes

Thanks for this Tips !!!!!!!!!!

0 Likes
avk
Adept III

nismo: Maybe Linux has the same API functions (I'm not a Linux programmer), but johnt did ask for Windows ones, because he used Visual Basic .
0 Likes
mk678
Journeyman III

I am trying to show you that how how to read CPU clock
I hope it will work for you.
You can use the managed WMI classes found in the System.Managment namespace to obtain the # of CPU, speed, stepping, etc...

System.Management.ManagementScope ms = new System.Management.ManagementScope("\\\\localhost\\root\\cimv2");

System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_Processor");

ManagementObjectSearcher query = new ManagementObjectSearcher(ms, oq);

ManagementObjectCollection queryCollection = query.Get();

foreach (ManagementObject mo in queryCollection)

{

    if (3 == Convert.ToUInt16(mo["ProcessorType"], System.Globalization.CultureInfo.InvariantCulture))

    {

        foreach (PropertyData prop in mo.Properties)

        {

            switch (prop.Name)

            {

                case "CurrentClockSpeed":

               clockSpeed = prop.Value;

               break;

            }

        }

    }

}

0 Likes

mk678 - good idea!

0 Likes

thanks you guys, I'm also looking for something like this but for Linux machines.

Edit: Removed Advertising from the post

0 Likes

I'm also looking for something similar on a linux machine. I'll let you know if I work it out.

0 Likes
joe1
Journeyman III

Perfect, mk678, this was what I am looking for.

 

 

Edit:Removed Advertising from the post


0 Likes

mk678, I've tried and working!

Thank you.

0 Likes

There are some softwares to read CPU clock. I used it before but lost the file.

Edit:Removed Advertising from the post

0 Likes