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 // $Id: CPUTimer.cc,v 1.3 2007/06/14 02:01:01 wmtan Exp $ 00012 // 00013 00014 // system include files 00015 #include <sys/resource.h> 00016 #include <errno.h> 00017 00018 // user include files 00019 #include "FWCore/Utilities/interface/CPUTimer.h" 00020 #include "FWCore/Utilities/interface/Exception.h" 00021 00022 // 00023 // constants, enums and typedefs 00024 // 00025 using namespace edm; 00026 00027 // 00028 // static data member definitions 00029 // 00030 00031 // 00032 // constructors and destructor 00033 // 00034 CPUTimer::CPUTimer() : 00035 state_(kStopped), 00036 startRealTime_(), 00037 startCPUTime_(), 00038 accumulatedRealTime_(0), 00039 accumulatedCPUTime_(0) 00040 { 00041 startRealTime_.tv_sec=0; 00042 startRealTime_.tv_usec=0; 00043 startCPUTime_.tv_sec=0; 00044 startCPUTime_.tv_usec=0; 00045 } 00046 00047 // CPUTimer::CPUTimer(const CPUTimer& rhs) 00048 // { 00049 // // do actual copying here; 00050 // } 00051 00052 CPUTimer::~CPUTimer() 00053 { 00054 } 00055 00056 // 00057 // assignment operators 00058 // 00059 // const CPUTimer& CPUTimer::operator=(const CPUTimer& rhs) 00060 // { 00061 // //An exception safe implementation is 00062 // CPUTimer temp(rhs); 00063 // swap(rhs); 00064 // 00065 // return *this; 00066 // } 00067 00068 // 00069 // member functions 00070 // 00071 void 00072 CPUTimer::start() { 00073 if(kStopped == state_) { 00074 rusage theUsage; 00075 if( 0 != getrusage(RUSAGE_SELF, &theUsage)) { 00076 throw cms::Exception("CPUTimerFailed")<<errno; 00077 } 00078 startCPUTime_.tv_sec =theUsage.ru_stime.tv_sec+theUsage.ru_utime.tv_sec; 00079 startCPUTime_.tv_usec =theUsage.ru_stime.tv_usec+theUsage.ru_utime.tv_usec; 00080 00081 gettimeofday(&startRealTime_, 0); 00082 state_ = kRunning; 00083 } 00084 } 00085 00086 void 00087 CPUTimer::stop() { 00088 if(kRunning == state_) { 00089 Times t = calculateDeltaTime(); 00090 accumulatedCPUTime_ += t.cpu_; 00091 accumulatedRealTime_ += t.real_; 00092 state_=kStopped; 00093 } 00094 } 00095 00096 void 00097 CPUTimer::reset(){ 00098 accumulatedCPUTime_ =0; 00099 accumulatedRealTime_=0; 00100 } 00101 00102 CPUTimer::Times 00103 CPUTimer::calculateDeltaTime() const 00104 { 00105 rusage theUsage; 00106 if( 0 != getrusage(RUSAGE_SELF, &theUsage)) { 00107 throw cms::Exception("CPUTimerFailed")<<errno; 00108 } 00109 const double microsecToSec = 1E-6; 00110 00111 struct timeval tp; 00112 gettimeofday(&tp, 0); 00113 00114 Times returnValue; 00115 returnValue.cpu_ = theUsage.ru_stime.tv_sec+theUsage.ru_utime.tv_sec-startCPUTime_.tv_sec+microsecToSec*(theUsage.ru_stime.tv_usec+theUsage.ru_utime.tv_usec-startCPUTime_.tv_usec); 00116 returnValue.real_ = tp.tv_sec-startRealTime_.tv_sec+microsecToSec*(tp.tv_usec -startRealTime_.tv_usec); 00117 return returnValue; 00118 } 00119 // 00120 // const member functions 00121 // 00122 double 00123 CPUTimer::realTime() const 00124 { 00125 if(kStopped == state_) { 00126 return accumulatedRealTime_; 00127 } 00128 return accumulatedRealTime_ + calculateDeltaTime().real_; 00129 } 00130 00131 double 00132 CPUTimer::cpuTime() const 00133 { 00134 if(kStopped== state_) { 00135 return accumulatedCPUTime_; 00136 } 00137 return accumulatedCPUTime_+ calculateDeltaTime().cpu_; 00138 } 00139 00140 // 00141 // static member functions 00142 //