CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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), startRealTime_(), startCPUTime_(), accumulatedRealTime_(0), accumulatedCPUTime_(0) {
35 #ifdef USE_CLOCK_GETTIME
36  startRealTime_.tv_sec = 0;
37  startRealTime_.tv_nsec = 0;
38  startCPUTime_.tv_sec = 0;
39  startCPUTime_.tv_nsec = 0;
40 #else
41  startRealTime_.tv_sec = 0;
42  startRealTime_.tv_usec = 0;
43  startCPUTime_.tv_sec = 0;
44  startCPUTime_.tv_usec = 0;
45 #endif
46 }
47 
48 // CPUTimer::CPUTimer(CPUTimer const& rhs) {
49 // // do actual copying here;
50 // }
51 
53 
54 //
55 // assignment operators
56 //
57 // CPUTimer const& CPUTimer::operator=(CPUTimer const& rhs) {
58 // //An exception safe implementation is
59 // CPUTimer temp(rhs);
60 // swap(rhs);
61 //
62 // return *this;
63 // }
64 
65 //
66 // member functions
67 //
69  if (kStopped == state_) {
70 #ifdef USE_CLOCK_GETTIME
71  clock_gettime(CLOCK_MONOTONIC, &startRealTime_);
72  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &startCPUTime_);
73 #else
74  gettimeofday(&startRealTime_, 0);
75 
76  rusage theUsage;
77  if (0 != getrusage(RUSAGE_SELF, &theUsage)) {
78  throw cms::Exception("CPUTimerFailed") << errno;
79  }
80  startCPUTime_.tv_sec = theUsage.ru_stime.tv_sec + theUsage.ru_utime.tv_sec;
81  startCPUTime_.tv_usec = theUsage.ru_stime.tv_usec + theUsage.ru_utime.tv_usec;
82 #endif
83  state_ = kRunning;
84  }
85 }
86 
88  if (kRunning == state_) {
92 
93  state_ = kStopped;
94  return t;
95  }
96  return Times();
97 }
98 
102 }
103 
107 }
108 
110  Times returnValue;
111 #ifdef USE_CLOCK_GETTIME
112  double const nanosecToSec = 1E-9;
113  struct timespec tp;
114 
115  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);
116  returnValue.cpu_ = tp.tv_sec - startCPUTime_.tv_sec + nanosecToSec * (tp.tv_nsec - startCPUTime_.tv_nsec);
117 
118  clock_gettime(CLOCK_MONOTONIC, &tp);
119  returnValue.real_ = tp.tv_sec - startRealTime_.tv_sec + nanosecToSec * (tp.tv_nsec - startRealTime_.tv_nsec);
120 #else
121  rusage theUsage;
122  if (0 != getrusage(RUSAGE_SELF, &theUsage)) {
123  throw cms::Exception("CPUTimerFailed") << errno;
124  }
125  double const microsecToSec = 1E-6;
126 
127  struct timeval tp;
128  gettimeofday(&tp, 0);
129 
130  returnValue.cpu_ = theUsage.ru_stime.tv_sec + theUsage.ru_utime.tv_sec - startCPUTime_.tv_sec +
131  microsecToSec * (theUsage.ru_stime.tv_usec + theUsage.ru_utime.tv_usec - startCPUTime_.tv_usec);
132  returnValue.real_ = tp.tv_sec - startRealTime_.tv_sec + microsecToSec * (tp.tv_usec - startRealTime_.tv_usec);
133 #endif
134  return returnValue;
135 }
136 //
137 // const member functions
138 //
139 double CPUTimer::realTime() const {
140  if (kStopped == state_) {
141  return accumulatedRealTime_;
142  }
144 }
145 
146 double CPUTimer::cpuTime() const {
147  if (kStopped == state_) {
148  return accumulatedCPUTime_;
149  }
151 }
152 
153 //
154 // static member functions
155 //
void add(const Times &t)
Definition: CPUTimer.cc:104
void start()
Definition: CPUTimer.cc:68
enum edm::CPUTimer::State state_
struct timeval startCPUTime_
Definition: CPUTimer.h:74
void reset()
Definition: CPUTimer.cc:99
struct timeval startRealTime_
Definition: CPUTimer.h:73
double accumulatedCPUTime_
Definition: CPUTimer.h:78
Times stop()
Definition: CPUTimer.cc:87
double cpuTime() const
Definition: CPUTimer.cc:146
double accumulatedRealTime_
Definition: CPUTimer.h:77
Times calculateDeltaTime() const
Definition: CPUTimer.cc:109
double realTime() const
Definition: CPUTimer.cc:139