CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/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     rusage theUsage;
00081     if(0 != getrusage(RUSAGE_SELF, &theUsage)) {
00082       throw cms::Exception("CPUTimerFailed")<<errno;
00083     }
00084     startCPUTime_.tv_sec =theUsage.ru_stime.tv_sec+theUsage.ru_utime.tv_sec;
00085     startCPUTime_.tv_usec =theUsage.ru_stime.tv_usec+theUsage.ru_utime.tv_usec;
00086 
00087     gettimeofday(&startRealTime_, 0);
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   clock_gettime(CLOCK_REALTIME, &tp);
00125   returnValue.real_ = tp.tv_sec - startRealTime_.tv_sec + nanosecToSec * (tp.tv_nsec - startRealTime_.tv_nsec);
00126 
00127   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);
00128   returnValue.cpu_ = tp.tv_sec - startCPUTime_.tv_sec + nanosecToSec * (tp.tv_nsec - startCPUTime_.tv_nsec);
00129 #else
00130   rusage theUsage;
00131   if(0 != getrusage(RUSAGE_SELF, &theUsage)) {
00132     throw cms::Exception("CPUTimerFailed") << errno;
00133   }
00134   double const microsecToSec = 1E-6;
00135 
00136   struct timeval tp;
00137   gettimeofday(&tp, 0);
00138 
00139   returnValue.cpu_ = theUsage.ru_stime.tv_sec + theUsage.ru_utime.tv_sec - startCPUTime_.tv_sec +
00140                      microsecToSec * (theUsage.ru_stime.tv_usec + theUsage.ru_utime.tv_usec - startCPUTime_.tv_usec);
00141   returnValue.real_ = tp.tv_sec - startRealTime_.tv_sec + microsecToSec * (tp.tv_usec - startRealTime_.tv_usec);
00142 #endif
00143   return returnValue;
00144 }
00145 //
00146 // const member functions
00147 //
00148 double
00149 CPUTimer::realTime() const {
00150   if(kStopped == state_) {
00151     return accumulatedRealTime_;
00152   }
00153   return accumulatedRealTime_ + calculateDeltaTime().real_;
00154 }
00155 
00156 double
00157 CPUTimer::cpuTime() const {
00158   if(kStopped == state_) {
00159     return accumulatedCPUTime_;
00160   }
00161   return accumulatedCPUTime_ + calculateDeltaTime().cpu_;
00162 }
00163 
00164 //
00165 // static member functions
00166 //