timing-nt.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "common/h/ntHeaders.h"
00034 #include <assert.h>
00035 #include <sys/types.h>
00036 #include <sys/timeb.h>
00037 #include <shlwapi.h>
00038 #include "common/h/timing.h"
00039
00040 #include "common/h/int64iostream.h"
00041
00042
00043 int64_t getRawTime1970() {
00044 struct _timeb timebuffer;
00045 int64_t us1970;
00046 _ftime(&timebuffer);
00047 us1970 = timebuffer.time;
00048 us1970 *= 1000000;
00049 us1970 += timebuffer.millitm*1000;
00050 return us1970;
00051 }
00052
00053 double calcCyclesPerSecond_sys() {
00054 HKEY hKey;
00055 #define REGLOC "HARDWARE\\DESCRIPTION\\System\\CentralProcessor"
00056 LONG result = RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT(REGLOC),0,KEY_READ,&hKey);
00057 if(result != ERROR_SUCCESS) {
00058 return cpsMethodNotAvailable;
00059 }
00060
00061 #define INITIALBUFSIZE 50
00062 TCHAR subKeyBuf[INITIALBUFSIZE], initialCPU[INITIALBUFSIZE];
00063 FILETIME ft;
00064 DWORD mhz = 0;
00065 for(DWORD curIndex = 0; ; curIndex++) {
00066 DWORD size = INITIALBUFSIZE;
00067 result = RegEnumKeyEx(hKey, curIndex, subKeyBuf, &size, NULL, NULL,
00068 NULL, &ft);
00069 if(result == ERROR_NO_MORE_ITEMS) {
00070 break;
00071 } else if (result != ERROR_SUCCESS) {
00072 return cpsMethodNotAvailable;
00073 }
00074
00075 DWORD valType, value, bufLen = sizeof(DWORD);
00076 HKEY subKey;
00077 result = RegOpenKeyEx(hKey, subKeyBuf, NULL, KEY_READ, &subKey);
00078 if(result != ERROR_SUCCESS) {
00079 return cpsMethodNotAvailable;
00080 }
00081
00082 result = RegQueryValueEx(subKey, TEXT("~MHz"), NULL, &valType,
00083 reinterpret_cast<LPBYTE>(&value), &bufLen);
00084 if(result != ERROR_SUCCESS) {
00085 return cpsMethodNotAvailable;
00086 }
00087
00088 if(mhz == 0) {
00089 mhz = value;
00090 strcpy(initialCPU, subKeyBuf);
00091 }
00092 else if(value != mhz) {
00093 cerr << "Warning: processor " << subKeyBuf << " has cycle rate of "
00094 << value << " while processor " << initialCPU
00095 << "\n has cycle rate of " << mhz
00096 << ". Using cycle rate of " << mhz << ".\n";
00097 }
00098 }
00099 RegCloseKey(hKey);
00100 return mhz * 1000000.0;
00101 }
00102
00103 double calcCyclesPerSecondOS()
00104 {
00105 double cps;
00106 cps = calcCyclesPerSecond_sys();
00107 if(cps == cpsMethodNotAvailable) {
00108 return 0.0;
00109 }
00110 return cps;
00111 }
00112
00113
00114