CMS 3D CMS Logo

ChildrenCPUTimer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Utilities
4 // Class : ChildrenCPUTimer
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Chris Jones
10 // Created: Sun Apr 16 20:32:20 EDT 2006
11 //
12 
13 // system include files
14 #include <sys/resource.h>
15 #include <cerrno>
16 
17 // user include files
20 
21 //
22 // constants, enums and typedefs
23 //
24 using namespace edm;
25 
26 //
27 // static data member definitions
28 //
29 
30 //
31 // constructors and destructor
32 //
33 ChildrenCPUTimer::ChildrenCPUTimer() : state_(kStopped), startCPUTime_(), accumulatedCPUTime_(0) {
34  startCPUTime_.tv_sec = 0;
35  startCPUTime_.tv_usec = 0;
36 }
37 
39 
40 //
41 // member functions
42 //
44  if (kStopped == state_) {
45  rusage theUsage;
46  if (0 != getrusage(RUSAGE_CHILDREN, &theUsage)) {
47  throw cms::Exception("ChildrenCPUTimerFailed") << errno;
48  }
49  startCPUTime_.tv_sec = theUsage.ru_stime.tv_sec + theUsage.ru_utime.tv_sec;
50  startCPUTime_.tv_usec = theUsage.ru_stime.tv_usec + theUsage.ru_utime.tv_usec;
51  state_ = kRunning;
52  }
53 }
54 
56  if (kRunning == state_) {
57  auto t = calculateDeltaTime();
59 
60  state_ = kStopped;
61  return t;
62  }
63  return 0.;
64 }
65 
67 
69 
71  double returnValue;
72  double const microsecToSec = 1E-6;
73 
74  rusage theUsage;
75  if (0 != getrusage(RUSAGE_CHILDREN, &theUsage)) {
76  throw cms::Exception("CPUTimerFailed") << errno;
77  }
78 
79  returnValue = theUsage.ru_stime.tv_sec + theUsage.ru_utime.tv_sec - startCPUTime_.tv_sec +
80  microsecToSec * (theUsage.ru_stime.tv_usec + theUsage.ru_utime.tv_usec - startCPUTime_.tv_usec);
81  return returnValue;
82 }
83 //
84 // const member functions
85 //
86 double ChildrenCPUTimer::cpuTime() const {
87  if (kStopped == state_) {
88  return accumulatedCPUTime_;
89  }
91 }
92 
93 //
94 // static member functions
95 //
double calculateDeltaTime() const
enum edm::ChildrenCPUTimer::State state_
struct timeval startCPUTime_
HLT enums.