Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // Package:     Utilities
0004 // Class  :     ChildrenCPUTimer
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/ChildrenCPUTimer.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 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 // member functions
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 // const member functions
0085 //
0086 double ChildrenCPUTimer::cpuTime() const {
0087   if (kStopped == state_) {
0088     return accumulatedCPUTime_;
0089   }
0090   return accumulatedCPUTime_ + calculateDeltaTime();
0091 }
0092 
0093 //
0094 // static member functions
0095 //