All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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  const double microsecond = 1 ; // unit here is microsecond
33  const double millisecond = 1000 * microsecond ;
34  const double second = 1000 * millisecond ;
35  const double minute = 60 * second ;
36  const double hour = 60 * minute ;
37  const double day = 24 * hour ;
38  const double week = 7 * day ;
39  const double month = 30 * day ;
40  const double year = 365 * day ;
41  const double nanosecond = 0.001 * microsecond ;
42 }
43 // ============================================================================
44 // Constructor
45 // ============================================================================
47  // current status of this chrono object;
48  : m_status( IChronoSvc::UNKNOWN ) // current status
49 {}
50 // ============================================================================
51 // start the chrono
52 // ============================================================================
54 {
55  if ( IChronoSvc::RUNNING == m_status ) { return m_status ; }
56  //
57  // following lines could be platform dependent!
58  //
59  // Store in object the measured times
61 
63 
64  return m_status ;
65 }
66 // ============================================================================
67 // stop the chrono
68 // ============================================================================
70 {
71  if ( IChronoSvc::RUNNING != m_status ) { return m_status ; }
72 
73  // following lines could be platform dependent!
75 
76  // update the counters:
77 
81 
82  // set new status
83 
85 
86  return m_status ;
87 }
88 // ============================================================================
89 // print user time
90 // ============================================================================
91 std::string ChronoEntity::outputUserTime () const
92 {
93  std::string res ("Time User : ") ;
94  return res += format
95  ( uTotalTime () ,
96  uMinimalTime () ,
97  uMeanTime () ,
98  uRMSTime () ,
99  uMaximalTime () ,
100  nOfMeasurements() );
101 }
102 // ============================================================================
103 // print system time
104 // ============================================================================
105 std::string ChronoEntity::outputSystemTime () const
106 {
107  std::string res ("Time System : ") ;
108  return res += format
109  ( kTotalTime () ,
110  kMinimalTime () ,
111  kMeanTime () ,
112  kRMSTime () ,
113  kMaximalTime () ,
114  nOfMeasurements() );
115 }
116 // ============================================================================
117 // print time
119 {
120  std::string res ("TimeElapsed: ") ;
121  return res += format
122  ( eTotalTime () ,
123  eMinimalTime () ,
124  eMeanTime () ,
125  eRMSTime () ,
126  eMaximalTime () ,
127  nOfMeasurements() );
128 }
129 // ============================================================================
130 // print the chrono
131 // ============================================================================
132 std::string ChronoEntity::format
133 ( const double total ,
134  const double minimal ,
135  const double mean ,
136  const double rms ,
137  const double maximal ,
138  const unsigned long number ) const
139 {
140 
142  boost::format fmt("Tot=%2$5.3g%1$s %4$43s #=%3$3lu");
143 
144  long double unit = 1.0 ;
145 
146  if ( total / microsecond < 500 )
147  { unit = microsecond ; fmt % " [us]" ; }
148  else if( total / millisecond < 500 )
149  { unit = millisecond ; fmt % " [ms]" ; }
150  else if( total / second < 500 )
151  { unit = second ; fmt % " [s]" ; }
152  else if( total / minute < 500 )
153  { unit = minute ; fmt % "[min]" ; }
154  else if( total / hour < 500 )
155  { unit = hour ; fmt % " [h]" ; }
156  else if( total / day < 10 )
157  { unit = day ; fmt % "[day]" ; }
158  else if( total / week < 5 )
159  { unit = week ; fmt % " [w]" ; }
160  else if( total / month < 20 )
161  { unit = month ; fmt % "[mon]" ; }
162  else
163  { unit = year ; fmt % " [y]" ; }
164 
165  fmt % (double) (total / unit) % number;
166 
167  if( 1 < number )
168  {
170  boost::format fmt1("Ave/Min/Max=%2$5.3g(+-%3$5.3g)/%4$5.3g/%5$5.3g%1$s");
171  if ( mean / microsecond < 500 )
172  { unit = microsecond ; fmt1 % " [us]" ; }
173  else if( mean / millisecond < 500 )
174  { unit = millisecond ; fmt1 % " [ms]" ; }
175  else if( mean / second < 500 )
176  { unit = second ; fmt1 % " [s]" ; }
177  else if( mean / minute < 500 )
178  { unit = minute ; fmt1 % "[min]" ; }
179  else if( mean / hour < 500 )
180  { unit = hour ; fmt1 % " [h]" ; }
181  else if( mean / day < 10 )
182  { unit = day ; fmt1 % "[day]" ; }
183  else if( mean / week < 5 )
184  { unit = week ; fmt1 % " [w]" ; }
185  else if( mean / month < 20 )
186  { unit = month ; fmt1 % "[mon]" ; }
187  else
188  { unit = year ; fmt1 % " [y]" ; }
189 
190  fmt1 % (double) ( mean / unit ) % (double) ( rms / unit )
191  % (double) ( minimal / unit ) % (double) ( maximal / unit );
192  fmt % fmt1.str();
193  }
194  else {
195  fmt % "";
196  }
197 
198  return fmt.str();
199 }
200 // ============================================================================
201 // comparison operator
202 // ============================================================================
203 bool ChronoEntity::operator<( const ChronoEntity& e ) const
204 {
205  return
206  ( &e == this ) ? false :
207  ( totalTime () < e.totalTime () ) ? true :
208  ( totalTime () > e.totalTime () ) ? false :
209  ( m_user < e.m_user ) ? true :
210  ( e.m_user < m_user ) ? false :
211  ( m_kernel < e.m_kernel ) ? true :
212  ( e.m_kernel < m_kernel ) ? false :
213  ( m_elapsed < e.m_elapsed ) ? true :
214  ( e.m_elapsed < m_elapsed ) ? false : false ;
215 }
216 // ============================================================================
217 /* print the chrono according the format and units
218  * @param typ the chrono type
219  * @param fmt the format string
220  * @param unit the unit
221  * @return the string representations
222  * @see boost::format
223  */
224 // ============================================================================
225 std::string ChronoEntity::outputTime
227  const std::string& fmt ,
228  System::TimeType unit ) const
229 {
230  boost::format _fmt ( fmt ) ;
231  // allow various number of arguments
232  using namespace boost::io ;
233  _fmt.exceptions ( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) ) ;
234  //
235  double _unit = microsecond ;
236  switch ( unit )
237  {
238  case System::Year : _unit = year ; break ;
239  case System::Month : _unit = month ; break ;
240  case System::Day : _unit = day ; break ;
241  case System::Hour : _unit = hour ; break ;
242  case System::Min : _unit = minute ; break ;
243  case System::Sec : _unit = second ; break ;
244  case System::milliSec : _unit = millisecond ; break ;
245  case System::microSec : _unit = microsecond ; break ;
246  case System::nanoSec : _unit = nanosecond ; break ;
247  default : _unit = microsecond ; break ;
248  }
249  //
250  const StatEntity* stat = &m_user;
251  switch ( typ )
252  {
253  case IChronoSvc::USER : stat = &m_user ; break ;
254  case IChronoSvc::KERNEL : stat = &m_kernel ; break ;
255  case IChronoSvc::ELAPSED : stat = &m_elapsed ; break ;
256  default : stat = &m_user ; break ;
257  }
258  //
259  _fmt
260  % ( stat -> nEntries () ) // %1 : #entries
261  % ( stat -> flag () / _unit ) // %2 : total time
262  % ( stat -> flagMean () / _unit ) // %3 : mean time
263  % ( stat -> flagRMS () / _unit ) // %4 : RMS time
264  % ( stat -> flagMeanErr () / _unit ) // %5 : error in mean time
265  % ( stat -> flagMin () / _unit ) // %6 : minimal time
266  % ( stat -> flagMax () / _unit ) ; // %7 : maximal time
267  //
268  return _fmt.str() ;
269 }
270 // ==========================================================================
271 
272 // ============================================================================
273 // The END
274 // ============================================================================
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:133
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:146
double kTotalTime() const
total Kernel time
Definition: ChronoEntity.h:234
TimeValueType userTime() const
Retrieve the user time in the requested unit.
Definition: Timing.h:150
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
double kMinimalTime() const
minimal measurement for kernel time
Definition: ChronoEntity.h:204
double eMinimalTime() const
minimal measurement for elapsed time
Definition: ChronoEntity.h:209
ChronoEntity()
default constructor
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
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:51
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
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:154
GAUDI_API ProcessTime getProcessTime(long pid=-1)
Retrieve the process time data for a process.
Definition: Timing.cpp:179
double eMaximalTime() const
maximal measurement for elapsed time
Definition: ChronoEntity.h:224
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:68
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
"Chrono"-related part of interface IChronoStatSvc
Definition: IChronoSvc.h:33