timing-nt.C

Go to the documentation of this file.
00001 /*
00002  * See the dyninst/COPYRIGHT file for copyright information.
00003  * 
00004  * We provide the Paradyn Tools (below described as "Paradyn")
00005  * on an AS IS basis, and do not warrant its validity or performance.
00006  * We reserve the right to update, modify, or discontinue this
00007  * software at any time.  We shall have no obligation to supply such
00008  * updates or modifications or any other form of support to you.
00009  * 
00010  * By your use of Paradyn, you understand and agree that we (or any
00011  * other person or entity with proprietary rights in Paradyn) are
00012  * under no obligation to provide either maintenance services,
00013  * update services, notices of latent defects, or correction of
00014  * defects for Paradyn.
00015  * 
00016  * This library is free software; you can redistribute it and/or
00017  * modify it under the terms of the GNU Lesser General Public
00018  * License as published by the Free Software Foundation; either
00019  * version 2.1 of the License, or (at your option) any later version.
00020  * 
00021  * This library is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00024  * Lesser General Public License for more details.
00025  * 
00026  * You should have received a copy of the GNU Lesser General Public
00027  * License along with this library; if not, write to the Free Software
00028  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00029  */
00030 
00031 
00032 // $Id: timing-nt.C,v 1.8 2007/05/30 19:20:33 legendre Exp $
00033 #include "common/h/ntHeaders.h"   // for LARGE_INTEGERS
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 // returns us since 1970 at ms granularity
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 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 12 Jul 2013 for SymtabAPI by  doxygen 1.6.1