All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ChronoEntity.cpp
Go to the documentation of this file.
1 #define GAUDIKERNEL_CHRONOENTITY_CPP 1
2 // ============================================================================
3 // include files
4 // ============================================================================
5 // STD & STL
6 // ============================================================================
7 #include <iostream>
8 #include <iomanip>
9 #include <cmath>
10 #include <algorithm>
11 #include <cstdio>
12 // ============================================================================
13 // GaudiKernel
14 // ============================================================================
15 #include "GaudiKernel/Kernel.h"
16 #include "GaudiKernel/System.h"
18 // ============================================================================
19 // Boost
20 // ============================================================================
21 #include "boost/format.hpp"
22 // ============================================================================
28 // ============================================================================
29 namespace
30 {
32  constexpr double microsecond = 1 ; // unit here is microsecond
33  constexpr double millisecond = 1000 * microsecond ;
34  constexpr double second = 1000 * millisecond ;
35  constexpr double minute = 60 * second ;
36  constexpr double hour = 60 * minute ;
37  constexpr double day = 24 * hour ;
38  constexpr double week = 7 * day ;
39  constexpr double month = 30 * day ;
40  constexpr double year = 365 * day ;
41 
42  constexpr double nanosecond = 0.001 * microsecond ;
43 }
44 // ============================================================================
45 // start the chrono
46 // ============================================================================
48 {
49  if ( IChronoSvc::RUNNING == m_status ) { return m_status ; }
50  //
51  // following lines could be platform dependent!
52  //
53  // Store in object the measured times
55 
57 
58  return m_status ;
59 }
60 // ============================================================================
61 // stop the chrono
62 // ============================================================================
64 {
65  if ( IChronoSvc::RUNNING != m_status ) { return m_status ; }
66 
67  // following lines could be platform dependent!
69 
70  // update the counters:
71 
75 
76  // set new status
77 
79 
80  return m_status ;
81 }
82 // ============================================================================
83 // print user time
84 // ============================================================================
86 {
87  return "Time User : " + format
88  ( uTotalTime () ,
89  uMinimalTime () ,
90  uMeanTime () ,
91  uRMSTime () ,
92  uMaximalTime () ,
93  nOfMeasurements() );
94 }
95 // ============================================================================
96 // print system time
97 // ============================================================================
99 {
100  return "Time System : " + format
101  ( kTotalTime () ,
102  kMinimalTime () ,
103  kMeanTime () ,
104  kRMSTime () ,
105  kMaximalTime () ,
106  nOfMeasurements() );
107 }
108 // ============================================================================
109 // print time
111 {
112  return "TimeElapsed: " + format
113  ( eTotalTime () ,
114  eMinimalTime () ,
115  eMeanTime () ,
116  eRMSTime () ,
117  eMaximalTime () ,
118  nOfMeasurements() );
119 }
120 // ============================================================================
121 // print the chrono
122 // ============================================================================
124 ( const double total ,
125  const double minimal ,
126  const double mean ,
127  const double rms ,
128  const double maximal ,
129  const unsigned long number ) const
130 {
131 
133  boost::format fmt("Tot=%2$5.3g%1$s %4$43s #=%3$3lu");
134 
135  static const auto tbl = { std::make_tuple( 500, microsecond, " [us]" ),
136  std::make_tuple( 500, millisecond, " [ms]" ),
137  std::make_tuple( 500, second, " [s]" ),
138  std::make_tuple( 500, minute, "[min]" ),
139  std::make_tuple( 500, hour, " [h]" ),
140  std::make_tuple( 10, day, "[day]" ),
141  std::make_tuple( 5, week, " [w]" ),
142  std::make_tuple( 20, month, "[mon]" ),
143  std::make_tuple( -1, year, " [y]" )};
144 
145  auto i = std::find_if( std::begin(tbl), std::prev(std::end(tbl)),
146  [&]( const std::tuple<int,double,const char*>& i) {
147  return total < std::get<0>(i)*std::get<1>(i);
148  });
149  long double unit = std::get<1>(*i) ;
150  fmt % std::get<2>(*i) % (double) (total / unit) % number;
151 
152  if( number > 1 )
153  {
155  boost::format fmt1("Ave/Min/Max=%2$5.3g(+-%3$5.3g)/%4$5.3g/%5$5.3g%1$s");
156  auto i = std::find_if( std::begin(tbl), std::prev(std::end(tbl)),
157  [&]( const std::tuple<int,double,const char*>& i) {
158  return total < std::get<0>(i)*std::get<1>(i);
159  });
160  unit = std::get<1>(*i);
161  fmt1 % std::get<2>(*i) % (double) ( mean / unit ) % (double) ( rms / unit )
162  % (double) ( minimal / unit ) % (double) ( maximal / unit );
163  fmt % fmt1.str();
164  } else {
165  fmt % "";
166  }
167 
168  return fmt.str();
169 }
170 // ============================================================================
171 // comparison operator
172 // ============================================================================
173 bool ChronoEntity::operator<( const ChronoEntity& e ) const
174 {
175  return
176  ( &e == this ) ? false :
177  ( totalTime () < e.totalTime () ) ? true :
178  ( totalTime () > e.totalTime () ) ? false :
179  ( m_user < e.m_user ) ? true :
180  ( e.m_user < m_user ) ? false :
181  ( m_kernel < e.m_kernel ) ? true :
182  ( e.m_kernel < m_kernel ) ? false :
183  ( m_elapsed < e.m_elapsed ) ? true :
184  ( e.m_elapsed < m_elapsed ) ? false : false ;
185 }
186 // ============================================================================
187 // compound assignment operator
188 // ============================================================================
190 {
191  // System::ProcessTime type
192  m_delta += e.m_delta;
193 
194  // Summing, massaging here does not make too much sense.
195  // This is done only for final reductions
196  // Keep by convention the original one.
197 // m_start += e.m_start;
198 
199  // Tymevaluetypes type
200  m_user += e.m_user;
201  m_kernel += e.m_kernel;
202  m_elapsed += e.m_elapsed;
203 
204  return *this;
205 }
206 
207 
208 // ============================================================================
209 /* print the chrono according the format and units
210  * @param typ the chrono type
211  * @param fmt the format string
212  * @param unit the unit
213  * @return the string representations
214  * @see boost::format
215  */
216 // ============================================================================
219  const std::string& fmt ,
220  System::TimeType unit ) const
221 {
222  boost::format _fmt ( fmt ) ;
223  // allow various number of arguments
224  using namespace boost::io ;
225  _fmt.exceptions ( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) ) ;
226  //
227  double _unit = microsecond ;
228  switch ( unit )
229  {
230  case System::Year : _unit = year ; break ;
231  case System::Month : _unit = month ; break ;
232  case System::Day : _unit = day ; break ;
233  case System::Hour : _unit = hour ; break ;
234  case System::Min : _unit = minute ; break ;
235  case System::Sec : _unit = second ; break ;
236  case System::milliSec : _unit = millisecond ; break ;
237  case System::microSec : _unit = microsecond ; break ;
238  case System::nanoSec : _unit = nanosecond ; break ;
239  default : _unit = microsecond ; break ;
240  }
241  //
242  const StatEntity* stat = &m_user;
243  switch ( typ )
244  {
245  case IChronoSvc::USER : stat = &m_user ; break ;
246  case IChronoSvc::KERNEL : stat = &m_kernel ; break ;
247  case IChronoSvc::ELAPSED : stat = &m_elapsed ; break ;
248  default : stat = &m_user ; break ;
249  }
250  //
251  _fmt
252  % ( stat -> nEntries () ) // %1 : #entries
253  % ( stat -> flag () / _unit ) // %2 : total time
254  % ( stat -> flagMean () / _unit ) // %3 : mean time
255  % ( stat -> flagRMS () / _unit ) // %4 : RMS time
256  % ( stat -> flagMeanErr () / _unit ) // %5 : error in mean time
257  % ( stat -> flagMin () / _unit ) // %6 : minimal time
258  % ( stat -> flagMax () / _unit ) ; // %7 : maximal time
259  //
260  return _fmt.str() ;
261 }
262 // ==========================================================================
263 
264 // ============================================================================
265 // The END
266 // ============================================================================
std::string outputElapsedTime() const
print the chrono ;
double kMaximalTime() const
maximal measurement for kernel time
Definition: ChronoEntity.h:219
double uMinimalTime() const
minimal measurement for user time
Definition: ChronoEntity.h:198
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
IChronoSvc::ChronoStatus m_status
current status of this chrono object;
Definition: ChronoEntity.h:170
double uTotalTime() const
total user time
Definition: ChronoEntity.h:229
StatEntity m_elapsed
the actual storage of "elapsed" time
Definition: ChronoEntity.h:180
a small helper class for implementation of ChronoStatSvc service, It also could be used as some local...
Definition: ChronoEntity.h:21
TimeValueType kernelTime() const
Retrieve the kernel time in the requested unit.
Definition: Timing.h:159
double kTotalTime() const
total Kernel time
Definition: ChronoEntity.h:234
constexpr double microsecond
TimeValueType userTime() const
Retrieve the user time in the requested unit.
Definition: Timing.h:163
StatEntity m_user
the actual storage of "user" time
Definition: ChronoEntity.h:176
double uRMSTime() const
r.m.s User Time
Definition: ChronoEntity.h:270
T make_tuple(T...args)
T end(T...args)
double kMinimalTime() const
minimal measurement for kernel time
Definition: ChronoEntity.h:204
double eMinimalTime() const
minimal measurement for elapsed time
Definition: ChronoEntity.h:209
double kRMSTime() const
r.m.s Kernel Time
Definition: ChronoEntity.h:265
std::string outputSystemTime() const
print the chrono ;
IChronoSvc::ChronoStatus start()
start the current chrono
ChronoEntity & operator+=(const ChronoEntity &entity)
Compound assignment operator.
T prev(T...args)
constexpr double second
STL class.
std::string outputTime(IChronoSvc::ChronoType typ, const std::string &fmt, System::TimeType unit) const
print the chrono according the format and units
System::ProcessTime m_start
start stamp for current measurement of process times
Definition: ChronoEntity.h:174
System::ProcessTime m_delta
delta process times
Definition: ChronoEntity.h:172
double kMeanTime() const
average Kernel Time
Definition: ChronoEntity.h:250
double eMeanTime() const
average Elapsed Time
Definition: ChronoEntity.h:260
TimeType
Time type for conversion.
Definition: Timing.h:57
StatEntity m_kernel
the actual storage of "kernel" time
Definition: ChronoEntity.h:178
double eRMSTime() const
r.m.s Elapsed Time
Definition: ChronoEntity.h:275
double uMeanTime() const
average User Time
Definition: ChronoEntity.h:255
std::string outputUserTime() const
print the chrono ;
std::string format(const double total, const double minimal, const double mean, const double rms, const double maximal, const unsigned long number) const
format
T find_if(T...args)
double totalTime() const
total time
Definition: ChronoEntity.h:244
bool operator<(const ChronoEntity &entity) const
comparison operator
TimeValueType elapsedTime() const
Retrieve the elapsed time in the requested unit.
Definition: Timing.h:167
constexpr double nanosecond
T begin(T...args)
GAUDI_API ProcessTime getProcessTime(long pid=-1)
Retrieve the process time data for a process.
Definition: Timing.cpp:177
double eMaximalTime() const
maximal measurement for elapsed time
Definition: ChronoEntity.h:224
constexpr double millisecond
static const System::TimeType TimeUnit
internal unit used for the system time conversion (microseconds)
Definition: ChronoEntity.h:182
unsigned long nOfMeasurements() const
number of chrono measurements
Definition: ChronoEntity.h:194
The basic counter used for Monitoring purposes.
Definition: StatEntity.h:65
IChronoSvc::ChronoStatus stop()
stop the chrono
double uMaximalTime() const
maximal measurement for user time
Definition: ChronoEntity.h:214
double eTotalTime() const
total Elapsed time
Definition: ChronoEntity.h:239