Jpp  pmt_effective_area_update_2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTimer.hh
Go to the documentation of this file.
1 
2 #ifndef __JEEP__JTIMER__
3 #define __JEEP__JTIMER__
4 
5 #include <unistd.h>
6 #include <sys/time.h>
7 #include <sys/resource.h>
8 #include <string>
9 #include <ostream>
10 #include <iomanip>
11 
12 #include "JLang/JTitle.hh"
13 #include "Jeep/JPrint.hh"
14 #include "Jeep/JScale.hh"
15 
16 
17 /**
18  * \author mdejong
19  */
20 
21 namespace JEEP {}
22 namespace JPP { using namespace JEEP; }
23 
24 namespace JEEP {
25 
26  using JLANG::JTitle;
27 
28 
29  /**
30  * Auxiliary class for CPU timing and usage.
31  */
32  class JTimer :
33  public JTitle
34  {
35  public:
36 
37  typedef unsigned long long ull;
38 
39  /**
40  * Status of clock.
41  */
42  enum JStatus_t { CLK_STOPPED = 0, CLK_RUNNING = 1 };
43 
44 
45  /**
46  * Default constructor.
47  */
48  JTimer() :
49  JTitle(),
50  usec_wall(0ULL),
51  usec_ucpu(0ULL),
52  usec_scpu(0ULL),
53  counter (0ULL),
55  {}
56 
57 
58  /**
59  * Constructor.
60  *
61  * \param title title
62  */
63  JTimer(const JTitle& title) :
64  JTitle(title),
65  usec_wall(0ULL),
66  usec_ucpu(0ULL),
67  usec_scpu(0ULL),
68  counter (0ULL),
70  {}
71 
72 
73  /**
74  * Reset timer.
75  */
76  void reset()
77  {
78  usec_wall = 0;
79  usec_ucpu = 0;
80  usec_scpu = 0;
81  counter = 0;
83  }
84 
85 
86  /**
87  * Start timer.
88  */
89  void start()
90  {
91  if (status == CLK_STOPPED) {
92 
93  struct timeval tv;
94  struct rusage usage;
95 
96  gettimeofday(&tv, NULL);
97  getrusage(RUSAGE_SELF, &usage);
98 
100  usec_ucpu -= timeval_to_usec(usage.ru_utime);
101  usec_scpu -= timeval_to_usec(usage.ru_stime);
102 
104 
105  ++counter;
106  }
107  }
108 
109 
110  /**
111  * Stop timer.
112  */
113  void stop()
114  {
115  if (status == CLK_RUNNING) {
116 
117  struct timeval tv;
118  struct rusage usage;
119 
120  gettimeofday(&tv, NULL);
121  getrusage(RUSAGE_SELF, &usage);
122 
123  usec_wall += timeval_to_usec(tv);
124  usec_ucpu += timeval_to_usec(usage.ru_utime);
125  usec_scpu += timeval_to_usec(usage.ru_stime);
126  }
127 
129  }
130 
131 
132  /**
133  * Get CPU usage.
134  *
135  * \return CPU usage [%]
136  */
137  unsigned int timing_get_cpu_percentage() const
138  {
139  return (100ULL * (usec_ucpu + usec_scpu) / (usec_wall+1));
140  }
141 
142 
143  /**
144  * Convert timeval to micro-seconds.
145  *
146  * \param tv timeval
147  * \return time [us]
148  */
149  static inline unsigned long long timeval_to_usec(const struct timeval& tv)
150  {
151  return (unsigned long long) tv.tv_sec * 1000000ULL + (unsigned long long) tv.tv_usec;
152  }
153 
154 
155  /**
156  * Print timer data.
157  *
158  * \param out output stream
159  * \param scale scale
160  */
161  void print(std::ostream& out, const JScale_t scale = milli_t) const
162  {
163  print(out, 1.0, scale);
164  }
165 
166 
167  /**
168  * Print timer data.
169  *
170  * \param out output stream
171  * \param normalise normalise to number of starts
172  * \param scale scale
173  */
174  void print(std::ostream& out, const bool normalise, const JScale_t scale = milli_t) const
175  {
176  double factor = 1.0;
177 
178  if (normalise && counter != 0) {
179  factor = 1.0 / (double) counter;
180  }
181 
182  print(out, factor, scale);
183  }
184 
185 
186  /**
187  * Print timer data.
188  *
189  * \param out output stream
190  * \param factor multiplication factor
191  * \param scale scale
192  */
193  void print(std::ostream& out, const double factor, const JScale_t scale = milli_t) const
194  {
195  using namespace std;
196 
197  const double y = factor * 1e-3 * getValue(milli_t) / getValue(scale);
198  const char* u = getUnit(scale);
199 
200  out << getTitle() << endl;
201  out << FIXED(10,3) << usec_wall * y << " " << u << "s elapsed" << endl;
202  out << FIXED(10,3) << usec_ucpu * y << " " << u << "s user" << endl;
203  out << FIXED(10,3) << usec_scpu * y << " " << u << "s system" << endl;
204  out << setw( 3) << timing_get_cpu_percentage() << "%CPU" << endl;
205  }
206 
207 
208  /**
209  * Print timer data.
210  *
211  * \param out output stream
212  * \param timer timer
213  * \return output stream
214  */
215  friend inline std::ostream& operator<<(std::ostream& out, const JTimer& timer)
216  {
217  timer.print(out);
218 
219  return out;
220  }
221 
222 
223  public:
224  unsigned long long usec_wall; /* clock time */
225  unsigned long long usec_ucpu; /* CPU user time */
226  unsigned long long usec_scpu; /* CPU system time */
227  unsigned long long counter; /* counter */
228 
229  protected:
231  };
232 }
233 
234 #endif
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
unsigned int timing_get_cpu_percentage() const
Get CPU usage.
Definition: JTimer.hh:137
JStatus_t status
Definition: JTimer.hh:230
unsigned long long usec_ucpu
Definition: JTimer.hh:225
void print(std::ostream &out, const JScale_t scale=milli_t) const
Print timer data.
Definition: JTimer.hh:161
void print(std::ostream &out, const double factor, const JScale_t scale=milli_t) const
Print timer data.
Definition: JTimer.hh:193
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
const std::string & getTitle() const
Get title.
Definition: JTitle.hh:55
void stop()
Stop timer.
Definition: JTimer.hh:113
JTimer()
Default constructor.
Definition: JTimer.hh:48
unsigned long long usec_scpu
Definition: JTimer.hh:226
void reset()
Reset timer.
Definition: JTimer.hh:76
void print(std::ostream &out, const bool normalise, const JScale_t scale=milli_t) const
Print timer data.
Definition: JTimer.hh:174
milli
Definition: JScale.hh:30
I/O formatting auxiliaries.
Auxiliary class for title.
Definition: JTitle.hh:19
unsigned long long counter
Definition: JTimer.hh:227
Auxiliary class for CPU timing and usage.
Definition: JTimer.hh:32
unsigned long long ull
Definition: JTimer.hh:37
Enumeration for scaling of quantity.
const char * getUnit(const int scale)
Get textual unit corresponding to scale.
Definition: JScale.hh:59
std::string title
Definition: JTitle.hh:73
friend std::ostream & operator<<(std::ostream &out, const JTimer &timer)
Print timer data.
Definition: JTimer.hh:215
double u[N+1]
Definition: JPolint.hh:739
static unsigned long long timeval_to_usec(const struct timeval &tv)
Convert timeval to micro-seconds.
Definition: JTimer.hh:149
JStatus_t
Status of clock.
Definition: JTimer.hh:42
JTimer(const JTitle &title)
Constructor.
Definition: JTimer.hh:63
unsigned long long usec_wall
Definition: JTimer.hh:224
void start()
Start timer.
Definition: JTimer.hh:89
void scale(vector< double > &v, double c)
scale vector content