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/ChildrenCPUTimer.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 ChildrenCPUTimer::ChildrenCPUTimer() : state_(kStopped), startCPUTime_(), accumulatedCPUTime_(0) {
0034 startCPUTime_.tv_sec = 0;
0035 startCPUTime_.tv_usec = 0;
0036 }
0037
0038 ChildrenCPUTimer::~ChildrenCPUTimer() {}
0039
0040
0041
0042
0043 void ChildrenCPUTimer::start() {
0044 if (kStopped == state_) {
0045 rusage theUsage;
0046 if (0 != getrusage(RUSAGE_CHILDREN, &theUsage)) {
0047 throw cms::Exception("ChildrenCPUTimerFailed") << errno;
0048 }
0049 startCPUTime_.tv_sec = theUsage.ru_stime.tv_sec + theUsage.ru_utime.tv_sec;
0050 startCPUTime_.tv_usec = theUsage.ru_stime.tv_usec + theUsage.ru_utime.tv_usec;
0051 state_ = kRunning;
0052 }
0053 }
0054
0055 double ChildrenCPUTimer::stop() {
0056 if (kRunning == state_) {
0057 auto t = calculateDeltaTime();
0058 accumulatedCPUTime_ += t;
0059
0060 state_ = kStopped;
0061 return t;
0062 }
0063 return 0.;
0064 }
0065
0066 void ChildrenCPUTimer::reset() { accumulatedCPUTime_ = 0; }
0067
0068 void ChildrenCPUTimer::add(double t) { accumulatedCPUTime_ += t; }
0069
0070 double ChildrenCPUTimer::calculateDeltaTime() const {
0071 double returnValue;
0072 double const microsecToSec = 1E-6;
0073
0074 rusage theUsage;
0075 if (0 != getrusage(RUSAGE_CHILDREN, &theUsage)) {
0076 throw cms::Exception("CPUTimerFailed") << errno;
0077 }
0078
0079 returnValue = theUsage.ru_stime.tv_sec + theUsage.ru_utime.tv_sec - startCPUTime_.tv_sec +
0080 microsecToSec * (theUsage.ru_stime.tv_usec + theUsage.ru_utime.tv_usec - startCPUTime_.tv_usec);
0081 return returnValue;
0082 }
0083
0084
0085
0086 double ChildrenCPUTimer::cpuTime() const {
0087 if (kStopped == state_) {
0088 return accumulatedCPUTime_;
0089 }
0090 return accumulatedCPUTime_ + calculateDeltaTime();
0091 }
0092
0093
0094
0095