Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:21

0001 // -*- C++ -*-
0002 //
0003 // Package:     Utilities
0004 // Class  :     CPUTimer
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Sun Apr 16 20:32:20 EDT 2006
0011 //
0012 
0013 // system include files
0014 #include <sys/resource.h>
0015 #include <cerrno>
0016 
0017 // user include files
0018 #include "FWCore/Utilities/interface/CPUTimer.h"
0019 #include "FWCore/Utilities/interface/Exception.h"
0020 
0021 //
0022 // constants, enums and typedefs
0023 //
0024 using namespace edm;
0025 
0026 //
0027 // static data member definitions
0028 //
0029 
0030 //
0031 // constructors and destructor
0032 //
0033 CPUTimer::CPUTimer()
0034     : state_(kStopped), startRealTime_(), startCPUTime_(), accumulatedRealTime_(0), accumulatedCPUTime_(0) {
0035 #ifdef USE_CLOCK_GETTIME
0036   startRealTime_.tv_sec = 0;
0037   startRealTime_.tv_nsec = 0;
0038   startCPUTime_.tv_sec = 0;
0039   startCPUTime_.tv_nsec = 0;
0040 #else
0041   startRealTime_.tv_sec = 0;
0042   startRealTime_.tv_usec = 0;
0043   startCPUTime_.tv_sec = 0;
0044   startCPUTime_.tv_usec = 0;
0045 #endif
0046 }
0047 
0048 // CPUTimer::CPUTimer(CPUTimer const& rhs) {
0049 //    // do actual copying here;
0050 // }
0051 
0052 CPUTimer::~CPUTimer() {}
0053 
0054 //
0055 // assignment operators
0056 //
0057 // CPUTimer const& CPUTimer::operator=(CPUTimer const& rhs) {
0058 //   //An exception safe implementation is
0059 //   CPUTimer temp(rhs);
0060 //   swap(rhs);
0061 //
0062 //   return *this;
0063 // }
0064 
0065 //
0066 // member functions
0067 //
0068 void CPUTimer::start() {
0069   if (kStopped == state_) {
0070 #ifdef USE_CLOCK_GETTIME
0071     clock_gettime(CLOCK_MONOTONIC, &startRealTime_);
0072     clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &startCPUTime_);
0073 #else
0074     gettimeofday(&startRealTime_, 0);
0075 
0076     rusage theUsage;
0077     if (0 != getrusage(RUSAGE_SELF, &theUsage)) {
0078       throw cms::Exception("CPUTimerFailed") << errno;
0079     }
0080     startCPUTime_.tv_sec = theUsage.ru_stime.tv_sec + theUsage.ru_utime.tv_sec;
0081     startCPUTime_.tv_usec = theUsage.ru_stime.tv_usec + theUsage.ru_utime.tv_usec;
0082 #endif
0083     state_ = kRunning;
0084   }
0085 }
0086 
0087 CPUTimer::Times CPUTimer::stop() {
0088   if (kRunning == state_) {
0089     Times t = calculateDeltaTime();
0090     accumulatedCPUTime_ += t.cpu_;
0091     accumulatedRealTime_ += t.real_;
0092 
0093     state_ = kStopped;
0094     return t;
0095   }
0096   return Times();
0097 }
0098 
0099 void CPUTimer::reset() {
0100   accumulatedCPUTime_ = 0;
0101   accumulatedRealTime_ = 0;
0102 }
0103 
0104 void CPUTimer::add(CPUTimer::Times const& t) {
0105   accumulatedCPUTime_ += t.cpu_;
0106   accumulatedRealTime_ += t.real_;
0107 }
0108 
0109 CPUTimer::Times CPUTimer::calculateDeltaTime() const {
0110   Times returnValue;
0111 #ifdef USE_CLOCK_GETTIME
0112   double const nanosecToSec = 1E-9;
0113   struct timespec tp;
0114 
0115   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);
0116   returnValue.cpu_ = tp.tv_sec - startCPUTime_.tv_sec + nanosecToSec * (tp.tv_nsec - startCPUTime_.tv_nsec);
0117 
0118   clock_gettime(CLOCK_MONOTONIC, &tp);
0119   returnValue.real_ = tp.tv_sec - startRealTime_.tv_sec + nanosecToSec * (tp.tv_nsec - startRealTime_.tv_nsec);
0120 #else
0121   rusage theUsage;
0122   if (0 != getrusage(RUSAGE_SELF, &theUsage)) {
0123     throw cms::Exception("CPUTimerFailed") << errno;
0124   }
0125   double const microsecToSec = 1E-6;
0126 
0127   struct timeval tp;
0128   gettimeofday(&tp, 0);
0129 
0130   returnValue.cpu_ = theUsage.ru_stime.tv_sec + theUsage.ru_utime.tv_sec - startCPUTime_.tv_sec +
0131                      microsecToSec * (theUsage.ru_stime.tv_usec + theUsage.ru_utime.tv_usec - startCPUTime_.tv_usec);
0132   returnValue.real_ = tp.tv_sec - startRealTime_.tv_sec + microsecToSec * (tp.tv_usec - startRealTime_.tv_usec);
0133 #endif
0134   return returnValue;
0135 }
0136 //
0137 // const member functions
0138 //
0139 double CPUTimer::realTime() const {
0140   if (kStopped == state_) {
0141     return accumulatedRealTime_;
0142   }
0143   return accumulatedRealTime_ + calculateDeltaTime().real_;
0144 }
0145 
0146 double CPUTimer::cpuTime() const {
0147   if (kStopped == state_) {
0148     return accumulatedCPUTime_;
0149   }
0150   return accumulatedCPUTime_ + calculateDeltaTime().cpu_;
0151 }
0152 
0153 //
0154 // static member functions
0155 //