15 Replies Latest reply on Feb 27, 2009 7:53 AM by firstgrip

    How to read CPU clock

    johnt

      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?

        • How to read CPU clock
          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 .
            • How to read CPU clock
              johnt

               

              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

              • How to read CPU clock
                nismo

                 

                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?

              • How to read CPU clock
                avk
                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 .
                • How to read CPU clock
                  0r@ngE
                  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!
                  • How to read CPU clock
                    avk
                    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 .
                    • How to read CPU clock
                      mk678

                      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;

                                  }

                              }

                          }

                      }