CoinTime.hpp

Go to the documentation of this file.
00001 /* $Id: CoinTime.hpp 1372 2011-01-03 23:31:00Z lou $ */
00002 // Copyright (C) 2002, International Business Machines
00003 // Corporation and others.  All Rights Reserved.
00004 // This code is licensed under the terms of the Eclipse Public License (EPL).
00005 
00006 #ifndef _CoinTime_hpp
00007 #define _CoinTime_hpp
00008 
00009 // Uncomment the next three lines for thorough memory initialisation.
00010 // #ifndef ZEROFAULT
00011 // # define ZEROFAULT
00012 // #endif
00013 
00014 //#############################################################################
00015 
00016 #include <ctime>
00017 #if defined(_MSC_VER)
00018 // Turn off compiler warning about long names
00019 #  pragma warning(disable:4786)
00020 #else
00021 // MacOS-X and FreeBSD needs sys/time.h
00022 #if defined(__MACH__) || defined (__FreeBSD__)
00023 #include <sys/time.h>
00024 #endif
00025 #if !defined(__MSVCRT__)
00026 #include <sys/resource.h>
00027 #endif
00028 #endif
00029 
00030 //#############################################################################
00031 
00032 #if defined(_MSC_VER)
00033 
00034 #if 0 // change this to 1 if want to use the win32 API
00035 #include <windows.h>
00036 #ifdef small
00037 /* for some unfathomable reason (to me) rpcndr.h (pulled in by windows.h) does a
00038    '#define small char' */
00039 #undef small
00040 #endif
00041 #define TWO_TO_THE_THIRTYTWO 4294967296.0
00042 #define DELTA_EPOCH_IN_SECS  11644473600.0
00043 inline double CoinGetTimeOfDay()
00044 {
00045   FILETIME ft;
00046  
00047   GetSystemTimeAsFileTime(&ft);
00048   double t = ft.dwHighDateTime * TWO_TO_THE_THIRTYTWO + ft.dwLowDateTime;
00049   t = t/10000000.0 - DELTA_EPOCH_IN_SECS;
00050   return t;
00051 }
00052 #else
00053 #include <sys/types.h>
00054 #include <sys/timeb.h>
00055 inline double CoinGetTimeOfDay()
00056 {
00057   struct _timeb timebuffer;
00058 #pragma warning(disable:4996)
00059   _ftime( &timebuffer ); // C4996
00060 #pragma warning(default:4996)
00061   return timebuffer.time + timebuffer.millitm/1000.0;
00062 }
00063 #endif
00064 
00065 #else
00066 
00067 #include <sys/time.h>
00068 
00069 inline double CoinGetTimeOfDay()
00070 {
00071     struct timeval tv;
00072     gettimeofday(&tv, NULL);
00073     return static_cast<double>(tv.tv_sec) + static_cast<int>(tv.tv_usec)/1000000.0;
00074 }
00075 
00076 #endif // _MSC_VER
00077 
00086 inline double CoinWallclockTime(double callType = 0)
00087 {
00088     double callTime = CoinGetTimeOfDay();
00089     static const double firstCall = callType > 0 ? callType : callTime;
00090     return callType < 0 ? firstCall : callTime - firstCall;
00091 }
00092 
00093 //#############################################################################
00094 
00095 //#define HAVE_SDK // if SDK under Win32 is installed, for CPU instead of elapsed time under Win 
00096 #ifdef HAVE_SDK
00097 #include <windows.h>
00098 #ifdef small
00099 /* for some unfathomable reason (to me) rpcndr.h (pulled in by windows.h) does a
00100    '#define small char' */
00101 #undef small
00102 #endif
00103 #define TWO_TO_THE_THIRTYTWO 4294967296.0
00104 #endif
00105 
00106 static inline double CoinCpuTime()
00107 {
00108   double cpu_temp;
00109 #if defined(_MSC_VER) || defined(__MSVCRT__)
00110 #ifdef HAVE_SDK
00111   FILETIME creation;
00112   FILETIME exit;
00113   FILETIME kernel;
00114   FILETIME user;
00115   GetProcessTimes(GetCurrentProcess(), &creation, &exit, &kernel, &user);
00116   double t = user.dwHighDateTime * TWO_TO_THE_THIRTYTWO + user.dwLowDateTime;
00117   return t/10000000.0;
00118 #else
00119   unsigned int ticksnow;        /* clock_t is same as int */
00120   ticksnow = (unsigned int)clock();
00121   cpu_temp = (double)((double)ticksnow/CLOCKS_PER_SEC);
00122 #endif
00123 
00124 #else
00125   struct rusage usage;
00126 # ifdef ZEROFAULT
00127   usage.ru_utime.tv_sec = 0 ;
00128   usage.ru_utime.tv_usec = 0 ;
00129 # endif
00130   getrusage(RUSAGE_SELF,&usage);
00131   cpu_temp = static_cast<double>(usage.ru_utime.tv_sec);
00132   cpu_temp += 1.0e-6*(static_cast<double> (usage.ru_utime.tv_usec));
00133 #endif
00134   return cpu_temp;
00135 }
00136 
00137 //#############################################################################
00138 
00139 
00140 
00141 static inline double CoinSysTime()
00142 {
00143   double sys_temp;
00144 #if defined(_MSC_VER) || defined(__MSVCRT__)
00145   sys_temp = 0.0;
00146 #else
00147   struct rusage usage;
00148 # ifdef ZEROFAULT
00149   usage.ru_utime.tv_sec = 0 ;
00150   usage.ru_utime.tv_usec = 0 ;
00151 # endif
00152   getrusage(RUSAGE_SELF,&usage);
00153   sys_temp = static_cast<double>(usage.ru_stime.tv_sec);
00154   sys_temp += 1.0e-6*(static_cast<double> (usage.ru_stime.tv_usec));
00155 #endif
00156   return sys_temp;
00157 }
00158 
00159 //#############################################################################
00160 // On most systems SELF seems to include children threads, This is for when it doesn't
00161 static inline double CoinCpuTimeJustChildren()
00162 {
00163   double cpu_temp;
00164 #if defined(_MSC_VER) || defined(__MSVCRT__)
00165   cpu_temp = 0.0;
00166 #else
00167   struct rusage usage;
00168 # ifdef ZEROFAULT
00169   usage.ru_utime.tv_sec = 0 ;
00170   usage.ru_utime.tv_usec = 0 ;
00171 # endif
00172   getrusage(RUSAGE_CHILDREN,&usage);
00173   cpu_temp = static_cast<double>(usage.ru_utime.tv_sec);
00174   cpu_temp += 1.0e-6*(static_cast<double> (usage.ru_utime.tv_usec));
00175 #endif
00176   return cpu_temp;
00177 }
00178 //#############################################################################
00179 
00180 #include <fstream>
00181 
00197 class CoinTimer
00198 {
00199 private:
00201    double start;
00203    double limit;
00204    double end;
00205 #ifdef COIN_COMPILE_WITH_TRACING
00206    std::fstream* stream;
00207    bool write_stream;
00208 #endif
00209 
00210 private:
00211 #ifdef COIN_COMPILE_WITH_TRACING
00212    inline bool evaluate(bool b_tmp) const {
00213       int i_tmp = b_tmp;
00214       if (stream) {
00215          if (write_stream)
00216             (*stream) << i_tmp << "\n";
00217          else 
00218             (*stream) >> i_tmp;
00219       }
00220       return i_tmp;
00221    }
00222    inline double evaluate(double d_tmp) const {
00223       if (stream) {
00224          if (write_stream)
00225             (*stream) << d_tmp << "\n";
00226          else 
00227             (*stream) >> d_tmp;
00228       }
00229       return d_tmp;
00230    }
00231 #else
00232    inline bool evaluate(const bool b_tmp) const {
00233       return b_tmp;
00234    }
00235    inline double evaluate(const double d_tmp) const {
00236       return d_tmp;
00237    }
00238 #endif   
00239 
00240 public:
00242    CoinTimer() :
00243       start(0), limit(1e100), end(1e100)
00244 #ifdef COIN_COMPILE_WITH_TRACING
00245       , stream(0), write_stream(true)
00246 #endif
00247    {}
00248 
00250    CoinTimer(double lim) :
00251       start(CoinCpuTime()), limit(lim), end(start+lim)
00252 #ifdef COIN_COMPILE_WITH_TRACING
00253       , stream(0), write_stream(true)
00254 #endif
00255    {}
00256 
00257 #ifdef COIN_COMPILE_WITH_TRACING
00258 
00260    CoinTimer(std::fstream* s, bool write) :
00261       start(0), limit(1e100), end(1e100),
00262       stream(s), write_stream(write) {}
00263    
00266    CoinTimer(double lim, std::fstream* s, bool w) :
00267       start(CoinCpuTime()), limit(lim), end(start+lim),
00268       stream(s), write_stream(w) {}
00269 #endif
00270    
00272    inline void restart() { start=CoinCpuTime(); end=start+limit; }
00274    inline void reset() { restart(); }
00276    inline void reset(double lim) { limit=lim; restart(); }
00277 
00280    inline bool isPastPercent(double pct) const {
00281       return evaluate(start + limit * pct < CoinCpuTime());
00282    }
00285    inline bool isPast(double lim) const {
00286       return evaluate(start + lim < CoinCpuTime());
00287    }
00290    inline bool isExpired() const {
00291       return evaluate(end < CoinCpuTime());
00292    }
00293 
00295    inline double timeLeft() const {
00296       return evaluate(end - CoinCpuTime());
00297    }
00298 
00300    inline double timeElapsed() const {
00301       return evaluate(CoinCpuTime() - start);
00302    }
00303 
00304    inline void setLimit(double l) {
00305       limit = l;
00306       return;
00307    }
00308 };
00309 
00310 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 28 Aug 2016 for CoinUtils by  doxygen 1.6.1