home *** CD-ROM | disk | FTP | other *** search
/ Clickx 47 / Clickx 47.iso / assets / software / sswitchxp152.exe / source / SpeedswitchXP / HTSpeed.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-06-14  |  3.6 KB  |  127 lines

  1. /*
  2.    SpeedswitchXP V1.5
  3.    - Windows XP CPU Frequency Control for Notebooks -
  4.  
  5.    Copyright(c) 2002-2005 Christian Diefer
  6.  
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License version 2 as 
  9.    published by the Free Software Foundation.
  10.    
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.    
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20.  
  21. #define _UNICODE
  22.  
  23. #include "stdafx.h"
  24. #include <stdio.h>
  25. #include <math.h>
  26. #include <limits.h>
  27.  
  28. #include "speed.h"
  29. #include "cpuid.h"
  30.  
  31. static int firstrun = 1;
  32. static ushort sproc;
  33. static DWORD sfeat;
  34.  
  35.  
  36. // compute CPU speed with TSC and performance counter
  37. int cpuSpeedHT()
  38. {
  39.     UINT64 t0;      // ticks at beginning of test
  40.   UINT64 t1;      // ticks at end of test
  41.   UINT64 stamp0;  // cycles at beginning of test
  42.   UINT64 stamp1;  // cycles at end of test
  43.   UINT64 countFreq;
  44.   UINT64 elapsedCycles;
  45.   UINT64 elapsedTicks;
  46.   double elapsedTime;
  47.     HANDLE hProcess = GetCurrentProcess();
  48.     HANDLE hThread = GetCurrentThread();
  49.     ULONG dwProcessMask;
  50.   ULONG dwSystemMask;
  51.  
  52.   if( firstrun )    // init once (at first call only)
  53.   {
  54.     sproc = wincpuid();
  55.     sfeat = wincpufeatures();
  56.     firstrun = 0;
  57.   }
  58.  
  59.   if( !(sfeat & TSC_SUPPORT) )    // no TSC support
  60.         return 0;
  61.  
  62.     // get the frequency of the high-resolution performance counter
  63.     if( !QueryPerformanceFrequency((LARGE_INTEGER*)&countFreq) )
  64.         return 0;
  65.  
  66.     // get current process & thread priorities for later restoration
  67.   ULONG dwCurPriorityClass = GetPriorityClass( hProcess );
  68.     int iCurThreadPriority = GetThreadPriority( hThread );
  69.  
  70.   // get process & system affinity masks for later restoration
  71.     GetProcessAffinityMask( hProcess, &dwProcessMask, &dwSystemMask );
  72.  
  73.     // set this process & thread to max priorities so we don't get interrupted too often
  74.   if( dwCurPriorityClass != 0 )
  75.     SetPriorityClass( hProcess, REALTIME_PRIORITY_CLASS );
  76.  
  77.   if( iCurThreadPriority != THREAD_PRIORITY_ERROR_RETURN )
  78.     SetThreadPriority( hThread, THREAD_PRIORITY_TIME_CRITICAL );
  79.  
  80.   // nail this process onto the first CPU
  81.     SetProcessAffinityMask( hProcess, 1 );
  82.  
  83.   // get starting ticks
  84.     QueryPerformanceCounter( (LARGE_INTEGER*)&t0 );
  85.  
  86.     // serialize for in-order-execution and get starting cycles 
  87.   __asm { 
  88.     push ebx
  89.     xor eax,eax
  90.     cpuid
  91.     rdtsc
  92.         mov dword ptr [stamp0],eax
  93.         mov dword ptr [stamp0+4],edx
  94.     pop ebx
  95.     }
  96.  
  97.     Sleep( 50 );    // wait for 50msec to let the ticks and cycles counters advance
  98.  
  99.   // get ending ticks
  100.   QueryPerformanceCounter( (LARGE_INTEGER*)&t1 );
  101.  
  102.     // serialize again for in-order-execution and get ending cycles 
  103.     __asm 
  104.     {
  105.     push ebx
  106.     xor eax,eax
  107.     cpuid
  108.     rdtsc
  109.         mov dword ptr [stamp1],eax
  110.         mov dword ptr [stamp1+4],edx
  111.     pop ebx
  112.     }
  113.  
  114.   // restore process affinity and process&thread priorities
  115.     SetProcessAffinityMask( hProcess, dwProcessMask );
  116.     SetThreadPriority( hThread, iCurThreadPriority );
  117.     SetPriorityClass( hProcess, dwCurPriorityClass );
  118.  
  119.   // now compute the frequency...
  120.     elapsedCycles = stamp1 - stamp0;
  121.     elapsedTicks = t1 - t0;
  122.   elapsedTime = (double)elapsedTicks / (double)countFreq;
  123.  
  124.   // MHz = elapsed cycles / elapsed time / 1 million
  125.   return (int)((UINT64)(((double)elapsedCycles) / elapsedTime) / 1000000);
  126. }
  127.