File indexing completed on 2024-04-06 12:13:13
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/CPUTimer.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 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
0049
0050
0051
0052 CPUTimer::~CPUTimer() {}
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
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
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
0155