CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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 <errno.h>
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_REALTIME, &startRealTime_);
78  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &startCPUTime_);
79 #else
80  rusage theUsage;
81  if(0 != getrusage(RUSAGE_SELF, &theUsage)) {
82  throw cms::Exception("CPUTimerFailed")<<errno;
83  }
84  startCPUTime_.tv_sec =theUsage.ru_stime.tv_sec+theUsage.ru_utime.tv_sec;
85  startCPUTime_.tv_usec =theUsage.ru_stime.tv_usec+theUsage.ru_utime.tv_usec;
86 
87  gettimeofday(&startRealTime_, 0);
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  clock_gettime(CLOCK_REALTIME, &tp);
125  returnValue.real_ = tp.tv_sec - startRealTime_.tv_sec + nanosecToSec * (tp.tv_nsec - startRealTime_.tv_nsec);
126 
127  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);
128  returnValue.cpu_ = tp.tv_sec - startCPUTime_.tv_sec + nanosecToSec * (tp.tv_nsec - startCPUTime_.tv_nsec);
129 #else
130  rusage theUsage;
131  if(0 != getrusage(RUSAGE_SELF, &theUsage)) {
132  throw cms::Exception("CPUTimerFailed") << errno;
133  }
134  double const microsecToSec = 1E-6;
135 
136  struct timeval tp;
137  gettimeofday(&tp, 0);
138 
139  returnValue.cpu_ = theUsage.ru_stime.tv_sec + theUsage.ru_utime.tv_sec - startCPUTime_.tv_sec +
140  microsecToSec * (theUsage.ru_stime.tv_usec + theUsage.ru_utime.tv_usec - startCPUTime_.tv_usec);
141  returnValue.real_ = tp.tv_sec - startRealTime_.tv_sec + microsecToSec * (tp.tv_usec - startRealTime_.tv_usec);
142 #endif
143  return returnValue;
144 }
145 //
146 // const member functions
147 //
148 double
150  if(kStopped == state_) {
151  return accumulatedRealTime_;
152  }
154 }
155 
156 double
158  if(kStopped == state_) {
159  return accumulatedCPUTime_;
160  }
162 }
163 
164 //
165 // static member functions
166 //
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:80
void reset()
Definition: CPUTimer.cc:107
struct timeval startRealTime_
Definition: CPUTimer.h:79
#define CLOCK_PROCESS_CPUTIME_ID
Definition: TimerService.h:30
double accumulatedCPUTime_
Definition: CPUTimer.h:84
Times stop()
Definition: CPUTimer.cc:94
double cpuTime() const
Definition: CPUTimer.cc:157
double accumulatedRealTime_
Definition: CPUTimer.h:83
Times calculateDeltaTime() const
Definition: CPUTimer.cc:119
virtual ~CPUTimer()
Definition: CPUTimer.cc:56
double realTime() const
Definition: CPUTimer.cc:149
#define CLOCK_REALTIME
Definition: TimerService.h:28