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), 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_) {
90  accumulatedCPUTime_ += t.cpu_;
91  accumulatedRealTime_ += t.real_;
92 
93  state_ = kStopped;
94  return t;
95  }
96  return Times();
97 }
98 
102 }
103 
105  accumulatedCPUTime_ += t.cpu_;
106  accumulatedRealTime_ += t.real_;
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 //
edm::CPUTimer::state_
enum edm::CPUTimer::State state_
edm::CPUTimer::Times::real_
double real_
Definition: CPUTimer.h:45
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::CPUTimer::Times
Definition: CPUTimer.h:43
edm::CPUTimer::start
void start()
Definition: CPUTimer.cc:68
edm::CPUTimer::startCPUTime_
struct timeval startCPUTime_
Definition: CPUTimer.h:76
OrderedSet.t
t
Definition: OrderedSet.py:90
cmsswSequenceInfo.tp
tp
Definition: cmsswSequenceInfo.py:17
edm::CPUTimer::reset
void reset()
Definition: CPUTimer.cc:99
edm::CPUTimer::~CPUTimer
~CPUTimer()
Definition: CPUTimer.cc:52
edm::CPUTimer::CPUTimer
CPUTimer()
Definition: CPUTimer.cc:33
edm::CPUTimer::stop
Times stop()
Definition: CPUTimer.cc:87
edm::CPUTimer::kRunning
Definition: CPUTimer.h:70
edm::CPUTimer::cpuTime
double cpuTime() const
Definition: CPUTimer.cc:146
Exception
Definition: hltDiff.cc:246
Exception.h
edm::CPUTimer::accumulatedCPUTime_
double accumulatedCPUTime_
Definition: CPUTimer.h:80
edm::CPUTimer::Times::cpu_
double cpu_
Definition: CPUTimer.h:46
edm::CPUTimer::kStopped
Definition: CPUTimer.h:70
edm::CPUTimer::realTime
double realTime() const
Definition: CPUTimer.cc:139
CPUTimer.h
edm::CPUTimer::startRealTime_
struct timeval startRealTime_
Definition: CPUTimer.h:75
edm::CPUTimer::add
void add(const Times &t)
Definition: CPUTimer.cc:104
edm::CPUTimer::accumulatedRealTime_
double accumulatedRealTime_
Definition: CPUTimer.h:79
edm::CPUTimer::calculateDeltaTime
Times calculateDeltaTime() const
Definition: CPUTimer.cc:109