cgma
CpuTimer.cpp
Go to the documentation of this file.
00001 #include <cstdio>
00002 #include "CpuTimer.hpp"
00003 
00004 #include "CubitMessage.hpp"
00005 
00006 // Added by Cat for WIN32 port
00007 #ifndef _WIN32
00008   #include <sys/param.h>
00009   #include <sys/times.h>
00010 #endif
00011 
00012 
00013 #include <time.h>
00014 #ifdef SOLARIS
00015 #include <unistd.h>
00016 #endif
00017 
00018 #ifndef _WIN32
00019 #ifndef HZ
00020 #ifdef CLK_TCK
00021 #define HZ CLK_TCK
00022 #else
00023 #define HZ 60
00024 #endif
00025 #endif
00026 #else
00027 #define HZ CLOCKS_PER_SEC
00028 #endif
00029 
00030 
00031 
00032 // Added by Cat for WIN32 port
00033 #ifdef _WIN32
00034 struct tms {
00035        clock_t tms_utime;       /* user time */
00036        clock_t tms_stime;       /* system time */
00037        clock_t tms_cutime;      /* user time, children */
00038        clock_t tms_cstime;      /* system time, children */
00039       };
00040 
00041   #include <windows.h>
00042 
00043 #endif
00044 
00045 
00046 CpuTimer::CpuTimer()
00047 {
00048   tms current;
00049 
00050   // Added by Cat for WIN32 port
00051 #ifdef _WIN32
00052   nt_times(&current);
00053   wallTimeInitial = clock();
00054   wallTime = wallTimeInitial;
00055 #else
00056   gettimeofday(&wallTimeInitial, NULL);
00057   wallTime = wallTimeInitial;
00058   times( &current );
00059 #endif
00060   
00061   // Store current value of cpu time
00062   cpu = current.tms_utime +
00063         current.tms_stime +
00064         current.tms_cutime +
00065         current.tms_cstime;
00066   cpuInitial = cpu;
00067 }
00068 
00069 // Return time values
00070 
00071 double
00072 CpuTimer::cpu_secs()
00073 {
00074   tms current;
00075 
00076   // Added by Cat for WIN32 port
00077 #ifdef _WIN32
00078   nt_times(&current);    
00079 #else
00080   times( &current );
00081 #endif
00082   
00083   time_t cpu_now = current.tms_utime +
00084     current.tms_stime +
00085     current.tms_cutime +
00086     current.tms_cstime;
00087   time_t delta = cpu_now - cpu;
00088   cpu   = cpu_now;
00089 #ifdef _WIN32
00090   if( delta == 0 )
00091      delta = 1;
00092 #endif
00093   return (double) delta / HZ;
00094 }
00095 
00096 double
00097 CpuTimer::clock_secs()
00098 {
00099   double elapsed_time;
00100 #ifdef _WIN32
00101   clock_t current;
00102   current = clock();
00103   elapsed_time = double( current - wallTime );
00104   elapsed_time = elapsed_time / HZ;
00105 
00106   wallTime = current;
00107 #else
00108   timeval current;
00109   gettimeofday( &current, NULL );
00110   double prev_time = wallTime.tv_sec;
00111   prev_time += double( wallTime.tv_usec ) / 1000000.0;
00112   
00113   elapsed_time = current.tv_sec;
00114   elapsed_time += double( current.tv_usec ) / 1000000.0;
00115   elapsed_time = elapsed_time - prev_time;
00116   
00117   wallTime = current;
00118 #endif
00119   
00120   return elapsed_time;
00121 }
00122 
00123 double
00124 CpuTimer::elapsed(bool wall_time)
00125 {
00126   if( wall_time )
00127   {
00128     double elapsed;
00129 #ifdef _WIN32
00130     clock_t current;
00131     current = clock();
00132     elapsed = double( current - wallTimeInitial);
00133     elapsed = elapsed / HZ;
00134 #else    
00135     double current_time;
00136     timeval current;
00137     gettimeofday( &current, NULL );
00138     double initial_time;
00139     initial_time = wallTimeInitial.tv_sec;
00140     initial_time += double(wallTimeInitial.tv_usec) / 1000000.0;
00141     current_time = current.tv_sec;
00142     current_time += double( current.tv_usec ) / 1000000.0;
00143     
00144     elapsed = current_time - initial_time;
00145 #endif
00146     return elapsed;
00147   }
00148   else
00149   {
00150     tms current;
00151 
00152 // Added by Cat for WIN32 port
00153 #ifdef _WIN32
00154     nt_times(&current);    
00155 #else
00156     times( &current );
00157 #endif
00158 // Store totals
00159   
00160     time_t cpu_now = current.tms_utime +
00161         current.tms_stime +
00162         current.tms_cutime +
00163         current.tms_cstime;
00164     time_t elapsed = cpu_now - cpuInitial;
00165     return (double) elapsed / HZ;
00166   }
00167 }
00168 
00169 
00170 // Added by Cat for WIN32 port
00171 #ifdef _WIN32
00172 
00173 void CpuTimer::nt_times(tms *sys)
00174 {  
00175   HANDLE current_process = GetCurrentProcess();
00176 
00177   FILETIME ftCreation, ftExit, ftKernel, ftUser;  
00178   GetProcessTimes(current_process, &ftCreation, &ftExit, &ftKernel, &ftUser);  
00179 
00180   //kernel and user times are amounts of time rather than an actual time period. 
00181   //The value in the FILETIME structure is expressed in 100-nanosecond units.
00182   //Divide by 10 million to convert to seconds.
00183 
00184   __int64 i64 = *((__int64 *) &ftKernel);  
00185   double dw_kernel = (i64 / 10000000.0);
00186 
00187   i64 = *((__int64 *) &ftUser);
00188   double dw_user = (i64 / 10000000.0);
00189 
00190     sys->tms_utime = dw_user*HZ;
00191     sys->tms_stime = dw_kernel*HZ;
00192     sys->tms_cutime = 0;        
00193     sys->tms_cstime = 0;
00194         //PRINT_DEBUG( CUBIT_DEBUG_3,"clock returned %d\n", sys->tms_stime );  
00195 }
00196 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines