The Gaudi Framework  v30r3 (a5ef0a68)
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 <algorithm>
8 #include <cmath>
9 #include <cstdio>
10 #include <iomanip>
11 #include <iostream>
12 // ============================================================================
13 // GaudiKernel
14 // ============================================================================
16 #include "GaudiKernel/Kernel.h"
17 #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 ) {
50  return m_status;
51  }
52  //
53  // following lines could be platform dependent!
54  //
55  // Store in object the measured times
57 
59 
60  return m_status;
61 }
62 // ============================================================================
63 // stop the chrono
64 // ============================================================================
66 {
67  if ( IChronoSvc::RUNNING != m_status ) {
68  return m_status;
69  }
70 
71  // following lines could be platform dependent!
73 
74  // update the counters:
75 
79 
80  // set new status
81 
83 
84  return m_status;
85 }
86 // ============================================================================
87 // print user time
88 // ============================================================================
90 {
91  return "Time User : " +
93 }
94 // ============================================================================
95 // print system time
96 // ============================================================================
98 {
99  return "Time System : " +
101 }
102 // ============================================================================
103 // print time
105 {
106  return "TimeElapsed: " +
108 }
109 // ============================================================================
110 // print the chrono
111 // ============================================================================
112 std::string ChronoEntity::format( const double total, const double minimal, const double mean, const double rms,
113  const double maximal, const unsigned long number ) const
114 {
115 
117  boost::format fmt( "Tot=%2$5.3g%1$s %4$43s #=%3$3lu" );
118 
119  static const auto tbl = {std::make_tuple( 500, microsecond, " [us]" ), std::make_tuple( 500, millisecond, " [ms]" ),
120  std::make_tuple( 500, second, " [s]" ), std::make_tuple( 500, minute, "[min]" ),
121  std::make_tuple( 500, hour, " [h]" ), std::make_tuple( 10, day, "[day]" ),
122  std::make_tuple( 5, week, " [w]" ), std::make_tuple( 20, month, "[mon]" ),
123  std::make_tuple( -1, year, " [y]" )};
124 
125  auto i = std::find_if(
126  std::begin( tbl ), std::prev( std::end( tbl ) ),
127  [&]( const std::tuple<int, double, const char*>& i ) { return total < std::get<0>( i ) * std::get<1>( i ); } );
128  long double unit = std::get<1>( *i );
129  fmt % std::get<2>( *i ) % (double)( total / unit ) % number;
130 
131  if ( number > 1 ) {
133  boost::format fmt1( "Ave/Min/Max=%2$5.3g(+-%3$5.3g)/%4$5.3g/%5$5.3g%1$s" );
134  auto i = std::find_if(
135  std::begin( tbl ), std::prev( std::end( tbl ) ),
136  [&]( const std::tuple<int, double, const char*>& i ) { return total < std::get<0>( i ) * std::get<1>( i ); } );
137  unit = std::get<1>( *i );
138  fmt1 % std::get<2>( *i ) % (double)( mean / unit ) % (double)( rms / unit ) % (double)( minimal / unit ) %
139  (double)( maximal / unit );
140  fmt % fmt1.str();
141  } else {
142  fmt % "";
143  }
144 
145  return fmt.str();
146 }
147 // ============================================================================
148 // comparison operator
149 // ============================================================================
150 bool ChronoEntity::operator<( const ChronoEntity& e ) const
151 {
152  return ( &e == this )
153  ? false
154  : ( totalTime() < e.totalTime() )
155  ? true
156  : ( totalTime() > e.totalTime() )
157  ? false
158  : ( m_user < e.m_user )
159  ? true
160  : ( e.m_user < m_user ) ? false
161  : ( m_kernel < e.m_kernel )
162  ? true
163  : ( e.m_kernel < m_kernel )
164  ? false
165  : ( m_elapsed < e.m_elapsed )
166  ? true
167  : ( e.m_elapsed < m_elapsed ) ? false : false;
168 }
169 // ============================================================================
170 // compound assignment operator
171 // ============================================================================
173 {
174  // System::ProcessTime type
175  m_delta += e.m_delta;
176 
177  // Summing, massaging here does not make too much sense.
178  // This is done only for final reductions
179  // Keep by convention the original one.
180  // m_start += e.m_start;
181 
182  // Tymevaluetypes type
183  m_user += e.m_user;
184  m_kernel += e.m_kernel;
185  m_elapsed += e.m_elapsed;
186 
187  return *this;
188 }
189 
190 // ============================================================================
191 /* print the chrono according the format and units
192  * @param typ the chrono type
193  * @param fmt the format string
194  * @param unit the unit
195  * @return the string representations
196  * @see boost::format
197  */
198 // ============================================================================
200 {
201  boost::format _fmt( fmt );
202  // allow various number of arguments
203  using namespace boost::io;
204  _fmt.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
205  //
206  double _unit = microsecond;
207  switch ( unit ) {
208  case System::Year:
209  _unit = year;
210  break;
211  case System::Month:
212  _unit = month;
213  break;
214  case System::Day:
215  _unit = day;
216  break;
217  case System::Hour:
218  _unit = hour;
219  break;
220  case System::Min:
221  _unit = minute;
222  break;
223  case System::Sec:
224  _unit = second;
225  break;
226  case System::milliSec:
227  _unit = millisecond;
228  break;
229  case System::microSec:
230  _unit = microsecond;
231  break;
232  case System::nanoSec:
233  _unit = nanosecond;
234  break;
235  default:
236  _unit = microsecond;
237  break;
238  }
239  //
240  const StatEntity* stat = &m_user;
241  switch ( typ ) {
242  case IChronoSvc::USER:
243  stat = &m_user;
244  break;
245  case IChronoSvc::KERNEL:
246  stat = &m_kernel;
247  break;
248  case IChronoSvc::ELAPSED:
249  stat = &m_elapsed;
250  break;
251  default:
252  stat = &m_user;
253  break;
254  }
255  //
256  _fmt % ( stat->nEntries() ) // %1 : #entries
257  % ( stat->flag() / _unit ) // %2 : total time
258  % ( stat->flagMean() / _unit ) // %3 : mean time
259  % ( stat->flagRMS() / _unit ) // %4 : RMS time
260  % ( stat->flagMeanErr() / _unit ) // %5 : error in mean time
261  % ( stat->flagMin() / _unit ) // %6 : minimal time
262  % ( stat->flagMax() / _unit ); // %7 : maximal time
263  //
264  return _fmt.str();
265 }
266 // ==========================================================================
267 
268 // ============================================================================
269 // The END
270 // ============================================================================
std::string outputElapsedTime() const
print the chrono ;
double kMaximalTime() const
maximal measurement for kernel time
Definition: ChronoEntity.h:199
double uMinimalTime() const
minimal measurement for user time
Definition: ChronoEntity.h:183
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:120
IChronoSvc::ChronoStatus m_status
current status of this chrono object;
Definition: ChronoEntity.h:157
double uTotalTime() const
total user time
Definition: ChronoEntity.h:207
StatEntity m_elapsed
the actual storage of "elapsed" time
Definition: ChronoEntity.h:167
double flagRMS() const
Definition: Counters.h:872
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:160
double flagMin() const
Definition: Counters.h:874
double kTotalTime() const
total Kernel time
Definition: ChronoEntity.h:211
constexpr double microsecond
TimeValueType userTime() const
Retrieve the user time in the requested unit.
Definition: Timing.h:167
StatEntity m_user
the actual storage of "user" time
Definition: ChronoEntity.h:163
double uRMSTime() const
r.m.s User Time
Definition: ChronoEntity.h:239
T make_tuple(T...args)
T end(T...args)
double kMinimalTime() const
minimal measurement for kernel time
Definition: ChronoEntity.h:187
double flagMeanErr() const
Definition: Counters.h:873
double eMinimalTime() const
minimal measurement for elapsed time
Definition: ChronoEntity.h:191
double kRMSTime() const
r.m.s Kernel Time
Definition: ChronoEntity.h:235
std::string outputSystemTime() const
print the chrono ;
IChronoSvc::ChronoStatus start()
start the current chrono
double flagMax() const
Definition: Counters.h:875
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:161
System::ProcessTime m_delta
delta process times
Definition: ChronoEntity.h:159
double kMeanTime() const
average Kernel Time
Definition: ChronoEntity.h:223
double eMeanTime() const
average Elapsed Time
Definition: ChronoEntity.h:231
TimeType
Time type for conversion.
Definition: Timing.h:58
double flagMean() const
Definition: Counters.h:871
StatEntity m_kernel
the actual storage of "kernel" time
Definition: ChronoEntity.h:165
double eRMSTime() const
r.m.s Elapsed Time
Definition: ChronoEntity.h:243
double uMeanTime() const
average User Time
Definition: ChronoEntity.h:227
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:219
bool operator<(const ChronoEntity &entity) const
comparison operator
TimeValueType elapsedTime() const
Retrieve the elapsed time in the requested unit.
Definition: Timing.h:174
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:208
double eMaximalTime() const
maximal measurement for elapsed time
Definition: ChronoEntity.h:203
constexpr double millisecond
static const System::TimeType TimeUnit
internal unit used for the system time conversion (microseconds)
Definition: ChronoEntity.h:169
unsigned long nOfMeasurements() const
number of chrono measurements
Definition: ChronoEntity.h:179
backward compatible StatEntity class.
Definition: Counters.h:777
IChronoSvc::ChronoStatus stop()
stop the chrono
double uMaximalTime() const
maximal measurement for user time
Definition: ChronoEntity.h:195
double eTotalTime() const
total Elapsed time
Definition: ChronoEntity.h:215
double flag() const
Definition: Counters.h:869