Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:14

0001 // -*- C++ -*-
0002 //
0003 // Package:     Utilities
0004 // Class  :     WallclockTimer
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/WallclockTimer.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 WallclockTimer::WallclockTimer() : state_(kStopped), startRealTime_(), accumulatedRealTime_(0) {
0034 #ifdef USE_CLOCK_GETTIME
0035   startRealTime_.tv_sec = 0;
0036   startRealTime_.tv_nsec = 0;
0037 #else
0038   startRealTime_.tv_sec = 0;
0039   startRealTime_.tv_usec = 0;
0040 #endif
0041 }
0042 
0043 // WallclockTimer::WallclockTimer(WallclockTimer const& rhs) {
0044 //    // do actual copying here;
0045 // }
0046 
0047 WallclockTimer::~WallclockTimer() {}
0048 
0049 //
0050 // assignment operators
0051 //
0052 // WallclockTimer const& WallclockTimer::operator=(WallclockTimer const& rhs) {
0053 //   //An exception safe implementation is
0054 //   WallclockTimer temp(rhs);
0055 //   swap(rhs);
0056 //
0057 //   return *this;
0058 // }
0059 
0060 //
0061 // member functions
0062 //
0063 void WallclockTimer::start() {
0064   if (kStopped == state_) {
0065 #ifdef USE_CLOCK_GETTIME
0066     clock_gettime(CLOCK_MONOTONIC, &startRealTime_);
0067 #else
0068     gettimeofday(&startRealTime_, 0);
0069 #endif
0070     state_ = kRunning;
0071   }
0072 }
0073 
0074 double WallclockTimer::stop() {
0075   if (kRunning == state_) {
0076     auto t = calculateDeltaTime();
0077     accumulatedRealTime_ += t;
0078 
0079     state_ = kStopped;
0080     return t;
0081   }
0082   return 0.;
0083 }
0084 
0085 void WallclockTimer::reset() { accumulatedRealTime_ = 0; }
0086 
0087 void WallclockTimer::add(double t) { accumulatedRealTime_ += t; }
0088 
0089 double WallclockTimer::calculateDeltaTime() const {
0090   double returnValue;
0091 #ifdef USE_CLOCK_GETTIME
0092   double const nanosecToSec = 1E-9;
0093   struct timespec tp;
0094 
0095   clock_gettime(CLOCK_MONOTONIC, &tp);
0096   returnValue = tp.tv_sec - startRealTime_.tv_sec + nanosecToSec * (tp.tv_nsec - startRealTime_.tv_nsec);
0097 #else
0098   double const microsecToSec = 1E-6;
0099 
0100   struct timeval tp;
0101   gettimeofday(&tp, 0);
0102 
0103   returnValue = tp.tv_sec - startRealTime_.tv_sec + microsecToSec * (tp.tv_usec - startRealTime_.tv_usec);
0104 #endif
0105   return returnValue;
0106 }
0107 //
0108 // const member functions
0109 //
0110 double WallclockTimer::realTime() const {
0111   if (kStopped == state_) {
0112     return accumulatedRealTime_;
0113   }
0114   return accumulatedRealTime_ + calculateDeltaTime();
0115 }
0116 
0117 //
0118 // static member functions
0119 //