File indexing completed on 2024-04-06 12:13:14
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <sys/resource.h>
0015 #include <cerrno>
0016
0017
0018 #include "FWCore/Utilities/interface/WallclockTimer.h"
0019 #include "FWCore/Utilities/interface/Exception.h"
0020
0021
0022
0023
0024 using namespace edm;
0025
0026
0027
0028
0029
0030
0031
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
0044
0045
0046
0047 WallclockTimer::~WallclockTimer() {}
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
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
0109
0110 double WallclockTimer::realTime() const {
0111 if (kStopped == state_) {
0112 return accumulatedRealTime_;
0113 }
0114 return accumulatedRealTime_ + calculateDeltaTime();
0115 }
0116
0117
0118
0119