Jpp 19.3.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JTimer.hh
Go to the documentation of this file.
1#ifndef __JEEP__JTIMER__
2#define __JEEP__JTIMER__
3
4#include <unistd.h>
5#include <sys/time.h>
6#include <sys/resource.h>
7#include <string>
8#include <ostream>
9#include <iomanip>
10
11#include "JLang/JTitle.hh"
12#include "JLang/JManip.hh"
13#include "Jeep/JScale.hh"
14
15
16/**
17 * \author mdejong
18 */
19
20namespace JEEP {}
21namespace JPP { using namespace JEEP; }
22
23namespace JEEP {
24
25 using JLANG::JTitle;
26
27
28 /**
29 * Auxiliary class for CPU timing and usage.
30 */
31 class JTimer :
32 public JTitle
33 {
34 public:
35
36 typedef unsigned long long ull;
37
38 /**
39 * Status of clock.
40 */
42
43
44 /**
45 * Default constructor.
46 */
48 JTitle(),
49 usec_wall(0ULL),
50 usec_ucpu(0ULL),
51 usec_scpu(0ULL),
52 counter (0ULL),
54 {}
55
56
57 /**
58 * Constructor.
59 *
60 * \param title title
61 */
64 usec_wall(0ULL),
65 usec_ucpu(0ULL),
66 usec_scpu(0ULL),
67 counter (0ULL),
69 {}
70
71
72 /**
73 * Enable timers.
74 */
75 static void enable()
76 {
77 get_status() = true;
78 }
79
80
81 /**
82 * Disable timers.
83 */
84 static void disable()
85 {
86 get_status() = false;
87 }
88
89
90 /**
91 * Reset timer.
92 */
93 void reset()
94 {
95 usec_wall = 0;
96 usec_ucpu = 0;
97 usec_scpu = 0;
98 counter = 0;
100 }
101
102
103 /**
104 * Start timer.
105 */
106 void start()
107 {
108 if (status == CLK_STOPPED && get_status()) {
109
110 gettimeofday(&tv, NULL);
111 getrusage(RUSAGE_SELF, &usage);
112
114 usec_ucpu -= timeval_to_usec(usage.ru_utime);
115 usec_scpu -= timeval_to_usec(usage.ru_stime);
116
118
119 ++counter;
120 }
121 }
122
123
124 /**
125 * Stop timer.
126 */
127 void stop()
128 {
129 if (status == CLK_RUNNING && get_status()) {
130
131 gettimeofday(&tv, NULL);
132 getrusage(RUSAGE_SELF, &usage);
133
135 usec_ucpu += timeval_to_usec(usage.ru_utime);
136 usec_scpu += timeval_to_usec(usage.ru_stime);
137 }
138
140 }
141
142
143 /**
144 * Get CPU usage.
145 *
146 * \return CPU usage [%]
147 */
148 unsigned int timing_get_cpu_percentage() const
149 {
150 return (100ULL * (usec_ucpu + usec_scpu) / (usec_wall+1));
151 }
152
153
154 /**
155 * Convert timeval to micro-seconds.
156 *
157 * \param tv timeval
158 * \return time [us]
159 */
160 static inline unsigned long long timeval_to_usec(const struct timeval& tv)
161 {
162 return (unsigned long long) tv.tv_sec * 1000000ULL + (unsigned long long) tv.tv_usec;
163 }
164
165
166 /**
167 * Print timer data.
168 *
169 * \param out output stream
170 * \param scale scale
171 */
172 void print(std::ostream& out, const JScale_t scale = milli_t) const
173 {
174 print(out, 1.0, scale);
175 }
176
177
178 /**
179 * Print timer data.
180 *
181 * \param out output stream
182 * \param normalise normalise to number of starts
183 * \param scale scale
184 */
185 void print(std::ostream& out, const bool normalise, const JScale_t scale = milli_t) const
186 {
187 double factor = 1.0;
188
189 if (normalise && counter != 0) {
190 factor = 1.0 / (double) counter;
191 }
192
193 print(out, factor, scale);
194 }
195
196
197 /**
198 * Print timer data.
199 *
200 * \param out output stream
201 * \param factor multiplication factor
202 * \param scale scale
203 */
204 void print(std::ostream& out, const double factor, const JScale_t scale = milli_t) const
205 {
206 using namespace std;
207 using namespace JPP;
208
209 if (get_status()) {
210
211 const double y = factor * 1e-3 * getValue(milli_t) / getValue(scale);
212 const char* u = getUnit(scale);
213
214 out << getTitle() << endl;
215 out << FIXED(10,3) << usec_wall * y << " " << u << "s elapsed" << endl;
216 out << FIXED(10,3) << usec_ucpu * y << " " << u << "s user" << endl;
217 out << FIXED(10,3) << usec_scpu * y << " " << u << "s system" << endl;
218 out << setw( 3) << timing_get_cpu_percentage() << "%CPU" << endl;
219 }
220 }
221
222
223 /**
224 * Print timer data.
225 *
226 * \param out output stream
227 * \param timer timer
228 * \return output stream
229 */
230 friend inline std::ostream& operator<<(std::ostream& out, const JTimer& timer)
231 {
232 timer.print(out);
233
234 return out;
235 }
236
237
238 unsigned long long usec_wall; /* clock time */
239 unsigned long long usec_ucpu; /* CPU user time */
240 unsigned long long usec_scpu; /* CPU system time */
241 unsigned long long counter; /* counter */
242
243 protected:
245
246 private:
247 struct timeval tv;
248 struct rusage usage;
249
250 /**
251 * Get status.
252 *
253 * \return status
254 */
255 static bool& get_status()
256 {
257 static bool status = true;
258
259 return status;
260 }
261 };
262}
263
264#endif
void scale(vector< double > &v, double c)
scale vector content
I/O manipulators.
Enumeration for scaling of quantity.
Auxiliary class for CPU timing and usage.
Definition JTimer.hh:33
unsigned long long usec_ucpu
Definition JTimer.hh:239
void print(std::ostream &out, const JScale_t scale=milli_t) const
Print timer data.
Definition JTimer.hh:172
JTimer(const JTitle &title)
Constructor.
Definition JTimer.hh:62
unsigned int timing_get_cpu_percentage() const
Get CPU usage.
Definition JTimer.hh:148
static unsigned long long timeval_to_usec(const struct timeval &tv)
Convert timeval to micro-seconds.
Definition JTimer.hh:160
unsigned long long usec_wall
Definition JTimer.hh:238
static bool & get_status()
Get status.
Definition JTimer.hh:255
void print(std::ostream &out, const bool normalise, const JScale_t scale=milli_t) const
Print timer data.
Definition JTimer.hh:185
void stop()
Stop timer.
Definition JTimer.hh:127
JTimer()
Default constructor.
Definition JTimer.hh:47
unsigned long long counter
Definition JTimer.hh:241
JStatus_t
Status of clock.
Definition JTimer.hh:41
static void disable()
Disable timers.
Definition JTimer.hh:84
void reset()
Reset timer.
Definition JTimer.hh:93
static void enable()
Enable timers.
Definition JTimer.hh:75
unsigned long long ull
Definition JTimer.hh:36
unsigned long long usec_scpu
Definition JTimer.hh:240
void start()
Start timer.
Definition JTimer.hh:106
friend std::ostream & operator<<(std::ostream &out, const JTimer &timer)
Print timer data.
Definition JTimer.hh:230
JStatus_t status
Definition JTimer.hh:244
struct timeval tv
Definition JTimer.hh:247
struct rusage usage
Definition JTimer.hh:248
void print(std::ostream &out, const double factor, const JScale_t scale=milli_t) const
Print timer data.
Definition JTimer.hh:204
Auxiliary class for title.
Definition JTitle.hh:19
std::string title
Definition JTitle.hh:73
const std::string & getTitle() const
Get title.
Definition JTitle.hh:55
General puprpose classes and methods.
double getValue(const JScale_t scale)
Get numerical value corresponding to scale.
Definition JScale.hh:47
JScale_t
Enumeration for scaling of quantity.
Definition JScale.hh:24
@ milli_t
milli
Definition JScale.hh:30
const char * getUnit(const int scale)
Get textual unit corresponding to scale.
Definition JScale.hh:59
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448