CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/FWCore/Utilities/src/CPUTimer.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Utilities
00004 // Class  :     CPUTimer
00005 //
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  Chris Jones
00010 //         Created:  Sun Apr 16 20:32:20 EDT 2006
00011 //
00012 
00013 // system include files
00014 #include <sys/resource.h>
00015 #include <errno.h>
00016 
00017 // user include files
00018 #include "FWCore/Utilities/interface/CPUTimer.h"
00019 #include "FWCore/Utilities/interface/Exception.h"
00020 
00021 //
00022 // constants, enums and typedefs
00023 //
00024 using namespace edm;
00025 
00026 //
00027 // static data member definitions
00028 //
00029 
00030 //
00031 // constructors and destructor
00032 //
00033 CPUTimer::CPUTimer() :
00034 state_(kStopped),
00035 startRealTime_(),
00036 startCPUTime_(),
00037 accumulatedRealTime_(0),
00038 accumulatedCPUTime_(0) {
00039 #ifdef USE_CLOCK_GETTIME
00040   startRealTime_.tv_sec=0;
00041   startRealTime_.tv_nsec=0;
00042   startCPUTime_.tv_sec=0;
00043   startCPUTime_.tv_nsec=0;
00044 #else
00045   startRealTime_.tv_sec=0;
00046   startRealTime_.tv_usec=0;
00047   startCPUTime_.tv_sec=0;
00048   startCPUTime_.tv_usec=0;
00049 #endif
00050 }
00051 
00052 // CPUTimer::CPUTimer(CPUTimer const& rhs) {
00053 //    // do actual copying here;
00054 // }
00055 
00056 CPUTimer::~CPUTimer() {
00057 }
00058 
00059 //
00060 // assignment operators
00061 //
00062 // CPUTimer const& CPUTimer::operator=(CPUTimer const& rhs) {
00063 //   //An exception safe implementation is
00064 //   CPUTimer temp(rhs);
00065 //   swap(rhs);
00066 //
00067 //   return *this;
00068 // }
00069 
00070 //
00071 // member functions
00072 //
00073 void
00074 CPUTimer::start() {
00075   if(kStopped == state_) {
00076 #ifdef USE_CLOCK_GETTIME
00077     clock_gettime(CLOCK_REALTIME, &startRealTime_);
00078     clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &startCPUTime_);
00079 #else
00080     gettimeofday(&startRealTime_, 0);
00081 
00082     rusage theUsage;
00083     if(0 != getrusage(RUSAGE_SELF, &theUsage)) {
00084       throw cms::Exception("CPUTimerFailed")<<errno;
00085     }
00086     startCPUTime_.tv_sec =theUsage.ru_stime.tv_sec+theUsage.ru_utime.tv_sec;
00087     startCPUTime_.tv_usec =theUsage.ru_stime.tv_usec+theUsage.ru_utime.tv_usec;
00088 #endif
00089     state_ = kRunning;
00090   }
00091 }
00092 
00093 CPUTimer::Times
00094 CPUTimer::stop() {
00095   if(kRunning == state_) {
00096     Times t = calculateDeltaTime();
00097     accumulatedCPUTime_ += t.cpu_;
00098     accumulatedRealTime_ += t.real_;
00099 
00100     state_=kStopped;
00101     return t;
00102   }
00103   return Times();
00104 }
00105 
00106 void
00107 CPUTimer::reset() {
00108   accumulatedCPUTime_ = 0;
00109   accumulatedRealTime_ = 0;
00110 }
00111 
00112 void
00113 CPUTimer::add(CPUTimer::Times const& t) {
00114   accumulatedCPUTime_ += t.cpu_;
00115   accumulatedRealTime_ += t.real_;
00116 }
00117 
00118 CPUTimer::Times
00119 CPUTimer::calculateDeltaTime() const {
00120   Times returnValue;
00121 #ifdef USE_CLOCK_GETTIME
00122   double const nanosecToSec = 1E-9;
00123   struct timespec tp;
00124 
00125   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);
00126   returnValue.cpu_ = tp.tv_sec - startCPUTime_.tv_sec + nanosecToSec * (tp.tv_nsec - startCPUTime_.tv_nsec);
00127 
00128   clock_gettime(CLOCK_REALTIME, &tp);
00129   returnValue.real_ = tp.tv_sec - startRealTime_.tv_sec + nanosecToSec * (tp.tv_nsec - startRealTime_.tv_nsec);
00130 #else
00131   rusage theUsage;
00132   if(0 != getrusage(RUSAGE_SELF, &theUsage)) {
00133     throw cms::Exception("CPUTimerFailed") << errno;
00134   }
00135   double const microsecToSec = 1E-6;
00136 
00137   struct timeval tp;
00138   gettimeofday(&tp, 0);
00139 
00140   returnValue.cpu_ = theUsage.ru_stime.tv_sec + theUsage.ru_utime.tv_sec - startCPUTime_.tv_sec +
00141                      microsecToSec * (theUsage.ru_stime.tv_usec + theUsage.ru_utime.tv_usec - startCPUTime_.tv_usec);
00142   returnValue.real_ = tp.tv_sec - startRealTime_.tv_sec + microsecToSec * (tp.tv_usec - startRealTime_.tv_usec);
00143 #endif
00144   return returnValue;
00145 }
00146 //
00147 // const member functions
00148 //
00149 double
00150 CPUTimer::realTime() const {
00151   if(kStopped == state_) {
00152     return accumulatedRealTime_;
00153   }
00154   return accumulatedRealTime_ + calculateDeltaTime().real_;
00155 }
00156 
00157 double
00158 CPUTimer::cpuTime() const {
00159   if(kStopped == state_) {
00160     return accumulatedCPUTime_;
00161   }
00162   return accumulatedCPUTime_ + calculateDeltaTime().cpu_;
00163 }
00164 
00165 //
00166 // static member functions
00167 //