CMS 3D CMS Logo

CPUTimer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Utilities
4 // Class : CPUTimer
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 //
34 state_(kStopped),
35 startRealTime_(),
36 startCPUTime_(),
37 accumulatedRealTime_(0),
38 accumulatedCPUTime_(0) {
39 #ifdef USE_CLOCK_GETTIME
40  startRealTime_.tv_sec=0;
41  startRealTime_.tv_nsec=0;
42  startCPUTime_.tv_sec=0;
43  startCPUTime_.tv_nsec=0;
44 #else
45  startRealTime_.tv_sec=0;
46  startRealTime_.tv_usec=0;
47  startCPUTime_.tv_sec=0;
48  startCPUTime_.tv_usec=0;
49 #endif
50 }
51 
52 // CPUTimer::CPUTimer(CPUTimer const& rhs) {
53 // // do actual copying here;
54 // }
55 
57 }
58 
59 //
60 // assignment operators
61 //
62 // CPUTimer const& CPUTimer::operator=(CPUTimer const& rhs) {
63 // //An exception safe implementation is
64 // CPUTimer temp(rhs);
65 // swap(rhs);
66 //
67 // return *this;
68 // }
69 
70 //
71 // member functions
72 //
73 void
75  if(kStopped == state_) {
76 #ifdef USE_CLOCK_GETTIME
77  clock_gettime(CLOCK_MONOTONIC, &startRealTime_);
78  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &startCPUTime_);
79 #else
80  gettimeofday(&startRealTime_, 0);
81 
82  rusage theUsage;
83  if(0 != getrusage(RUSAGE_SELF, &theUsage)) {
84  throw cms::Exception("CPUTimerFailed")<<errno;
85  }
86  startCPUTime_.tv_sec = theUsage.ru_stime.tv_sec + theUsage.ru_utime.tv_sec;
87  startCPUTime_.tv_usec = theUsage.ru_stime.tv_usec + theUsage.ru_utime.tv_usec;
88 #endif
89  state_ = kRunning;
90  }
91 }
92 
95  if(kRunning == state_) {
99 
101  return t;
102  }
103  return Times();
104 }
105 
106 void
110 }
111 
112 void
116 }
117 
120  Times returnValue;
121 #ifdef USE_CLOCK_GETTIME
122  double const nanosecToSec = 1E-9;
123  struct timespec tp;
124 
125  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);
126  returnValue.cpu_ = tp.tv_sec - startCPUTime_.tv_sec + nanosecToSec * (tp.tv_nsec - startCPUTime_.tv_nsec);
127 
128  clock_gettime(CLOCK_MONOTONIC, &tp);
129  returnValue.real_ = tp.tv_sec - startRealTime_.tv_sec + nanosecToSec * (tp.tv_nsec - startRealTime_.tv_nsec);
130 #else
131  rusage theUsage;
132  if(0 != getrusage(RUSAGE_SELF, &theUsage)) {
133  throw cms::Exception("CPUTimerFailed") << errno;
134  }
135  double const microsecToSec = 1E-6;
136 
137  struct timeval tp;
138  gettimeofday(&tp, 0);
139 
140  returnValue.cpu_ = theUsage.ru_stime.tv_sec + theUsage.ru_utime.tv_sec - startCPUTime_.tv_sec +
141  microsecToSec * (theUsage.ru_stime.tv_usec + theUsage.ru_utime.tv_usec - startCPUTime_.tv_usec);
142  returnValue.real_ = tp.tv_sec - startRealTime_.tv_sec + microsecToSec * (tp.tv_usec - startRealTime_.tv_usec);
143 #endif
144  return returnValue;
145 }
146 //
147 // const member functions
148 //
149 double
151  if(kStopped == state_) {
152  return accumulatedRealTime_;
153  }
155 }
156 
157 double
159  if(kStopped == state_) {
160  return accumulatedCPUTime_;
161  }
163 }
164 
165 //
166 // static member functions
167 //
void add(const Times &t)
Definition: CPUTimer.cc:113
void start()
Definition: CPUTimer.cc:74
enum edm::CPUTimer::State state_
struct timeval startCPUTime_
Definition: CPUTimer.h:78
void reset()
Definition: CPUTimer.cc:107
struct timeval startRealTime_
Definition: CPUTimer.h:77
double accumulatedCPUTime_
Definition: CPUTimer.h:82
Times stop()
Definition: CPUTimer.cc:94
double cpuTime() const
Definition: CPUTimer.cc:158
double accumulatedRealTime_
Definition: CPUTimer.h:81
Times calculateDeltaTime() const
Definition: CPUTimer.cc:119
HLT enums.
double realTime() const
Definition: CPUTimer.cc:150