The Gaudi Framework  v33r0 (d5ea422b)
ChronoEntity.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #define GAUDIKERNEL_CHRONOENTITY_CPP 1
12 // ============================================================================
13 // include files
14 // ============================================================================
15 // STD & STL
16 // ============================================================================
17 #include <algorithm>
18 #include <cmath>
19 #include <cstdio>
20 #include <iomanip>
21 #include <iostream>
22 // ============================================================================
23 // GaudiKernel
24 // ============================================================================
26 #include "GaudiKernel/Kernel.h"
27 #include "GaudiKernel/System.h"
28 // ============================================================================
29 // Boost
30 // ============================================================================
31 #include "boost/format.hpp"
32 // ============================================================================
38 // ============================================================================
39 namespace {
41  constexpr double microsecond = 1; // unit here is microsecond
42  constexpr double millisecond = 1000 * microsecond;
43  constexpr double second = 1000 * millisecond;
44  constexpr double minute = 60 * second;
45  constexpr double hour = 60 * minute;
46  constexpr double day = 24 * hour;
47  constexpr double week = 7 * day;
48  constexpr double month = 30 * day;
49  constexpr double year = 365 * day;
50 
51  constexpr double nanosecond = 0.001 * microsecond;
52 } // namespace
53 // ============================================================================
54 // start the chrono
55 // ============================================================================
57  if ( IChronoSvc::RUNNING == m_status ) { return m_status; }
58  //
59  // following lines could be platform dependent!
60  //
61  // Store in object the measured times
63 
65 
66  return m_status;
67 }
68 // ============================================================================
69 // stop the chrono
70 // ============================================================================
72  if ( IChronoSvc::RUNNING != m_status ) { return m_status; }
73 
74  // following lines could be platform dependent!
76 
77  // update the counters:
78 
82 
83  // set new status
84 
86 
87  return m_status;
88 }
89 // ============================================================================
90 // print user time
91 // ============================================================================
93  return "Time User : " +
95 }
96 // ============================================================================
97 // print system time
98 // ============================================================================
100  return "Time System : " +
102 }
103 // ============================================================================
104 // print time
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 
116  boost::format fmt( "Tot=%2$5.3g%1$s %4$43s #=%3$3lu" );
117 
118  static const auto tbl = {std::make_tuple( 500, microsecond, " [us]" ), std::make_tuple( 500, millisecond, " [ms]" ),
119  std::make_tuple( 500, second, " [s]" ), std::make_tuple( 500, minute, "[min]" ),
120  std::make_tuple( 500, hour, " [h]" ), std::make_tuple( 10, day, "[day]" ),
121  std::make_tuple( 5, week, " [w]" ), std::make_tuple( 20, month, "[mon]" ),
122  std::make_tuple( -1, year, " [y]" )};
123 
124  auto i = std::find_if(
125  std::begin( tbl ), std::prev( std::end( tbl ) ),
126  [&]( const std::tuple<int, double, const char*>& i ) { return total < std::get<0>( i ) * std::get<1>( i ); } );
127  long double unit = std::get<1>( *i );
128  fmt % std::get<2>( *i ) % (double)( total / unit ) % number;
129 
130  if ( number > 1 ) {
132  boost::format fmt1( "Ave/Min/Max=%2$5.3g(+-%3$5.3g)/%4$5.3g/%5$5.3g%1$s" );
133  i = std::find_if(
134  std::begin( tbl ), std::prev( std::end( tbl ) ),
135  [&]( const std::tuple<int, double, const char*>& i ) { return total < std::get<0>( i ) * std::get<1>( i ); } );
136  unit = std::get<1>( *i );
137  fmt1 % std::get<2>( *i ) % (double)( mean / unit ) % (double)( rms / unit ) % (double)( minimal / unit ) %
138  (double)( maximal / unit );
139  fmt % fmt1.str();
140  } else {
141  fmt % "";
142  }
143 
144  return fmt.str();
145 }
146 // ============================================================================
147 // compound assignment operator
148 // ============================================================================
150  // System::ProcessTime type
151  m_delta += e.m_delta;
152 
153  // Summing, massaging here does not make too much sense.
154  // This is done only for final reductions
155  // Keep by convention the original one.
156  // m_start += e.m_start;
157 
158  // Tymevaluetypes type
159  m_user += e.m_user;
160  m_kernel += e.m_kernel;
161  m_elapsed += e.m_elapsed;
162 
163  return *this;
164 }
165 
166 // ============================================================================
167 /* print the chrono according the format and units
168  * @param typ the chrono type
169  * @param fmt the format string
170  * @param unit the unit
171  * @return the string representations
172  * @see boost::format
173  */
174 // ============================================================================
176  System::TimeType unit ) const {
177  boost::format _fmt( fmt );
178  // allow various number of arguments
179  using namespace boost::io;
180  _fmt.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
181  //
182  double _unit = microsecond;
183  switch ( unit ) {
184  case System::Year:
185  _unit = year;
186  break;
187  case System::Month:
188  _unit = month;
189  break;
190  case System::Day:
191  _unit = day;
192  break;
193  case System::Hour:
194  _unit = hour;
195  break;
196  case System::Min:
197  _unit = minute;
198  break;
199  case System::Sec:
200  _unit = second;
201  break;
202  case System::milliSec:
203  _unit = millisecond;
204  break;
205  case System::microSec:
206  _unit = microsecond;
207  break;
208  case System::nanoSec:
209  _unit = nanosecond;
210  break;
211  default:
212  _unit = microsecond;
213  break;
214  }
215  //
216  const StatEntity* stat = &m_user;
217  switch ( typ ) {
218  case IChronoSvc::USER:
219  stat = &m_user;
220  break;
221  case IChronoSvc::KERNEL:
222  stat = &m_kernel;
223  break;
224  case IChronoSvc::ELAPSED:
225  stat = &m_elapsed;
226  break;
227  default:
228  stat = &m_user;
229  break;
230  }
231  //
232  _fmt % ( stat->nEntries() ) // %1 : #entries
233  % ( stat->flag() / _unit ) // %2 : total time
234  % ( stat->flagMean() / _unit ) // %3 : mean time
235  % ( stat->flagRMS() / _unit ) // %4 : RMS time
236  % ( stat->flagMeanErr() / _unit ) // %5 : error in mean time
237  % ( stat->flagMin() / _unit ) // %6 : minimal time
238  % ( stat->flagMax() / _unit ); // %7 : maximal time
239  //
240  return _fmt.str();
241 }
242 // ==========================================================================
243 
244 // ============================================================================
245 // The END
246 // ============================================================================
std::string outputTime(IChronoSvc::ChronoType typ, const std::string &fmt, System::TimeType unit) const
print the chrono according the format and units
TimeValueType userTime() const
Retrieve the user time in the requested unit.
Definition: Timing.h:174
double eRMSTime() const
r.m.s Elapsed Time
Definition: ChronoEntity.h:255
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:169
StatEntity m_elapsed
the actual storage of "elapsed" time
Definition: ChronoEntity.h:179
a small helper class for implementation of ChronoStatSvc service, It also could be used as some local...
Definition: ChronoEntity.h:31
double flagMean() const
Definition: StatEntity.h:99
constexpr double microsecond
TimeValueType elapsedTime() const
Retrieve the elapsed time in the requested unit.
Definition: Timing.h:180
StatEntity m_user
the actual storage of "user" time
Definition: ChronoEntity.h:175
T make_tuple(T... args)
double uMinimalTime() const
minimal measurement for user time
Definition: ChronoEntity.h:195
T end(T... args)
IChronoSvc::ChronoStatus start()
start the current chrono
ChronoEntity & operator+=(const ChronoEntity &entity)
Compound assignment operator.
T prev(T... args)
TimeValueType kernelTime() const
Retrieve the kernel time in the requested unit.
Definition: Timing.h:168
constexpr double second
STL class.
std::string format(const double total, const double minimal, const double mean, const double rms, const double maximal, const unsigned long number) const
format
System::ProcessTime m_start
start stamp for current measurement of process times
Definition: ChronoEntity.h:173
System::ProcessTime m_delta
delta process times
Definition: ChronoEntity.h:171
double kRMSTime() const
r.m.s Kernel Time
Definition: ChronoEntity.h:247
double eMaximalTime() const
maximal measurement for elapsed time
Definition: ChronoEntity.h:215
double uTotalTime() const
total user time
Definition: ChronoEntity.h:219
unsigned long nOfMeasurements() const
number of chrono measurements
Definition: ChronoEntity.h:191
TimeType
Time type for conversion.
Definition: Timing.h:67
double eMinimalTime() const
minimal measurement for elapsed time
Definition: ChronoEntity.h:203
StatEntity m_kernel
the actual storage of "kernel" time
Definition: ChronoEntity.h:177
double flagMeanErr() const
Definition: StatEntity.h:101
double uMeanTime() const
average User Time
Definition: ChronoEntity.h:239
double kMeanTime() const
average Kernel Time
Definition: ChronoEntity.h:235
double flagRMS() const
Definition: StatEntity.h:100
double eTotalTime() const
total Elapsed time
Definition: ChronoEntity.h:227
double flagMin() const
Definition: StatEntity.h:102
T find_if(T... args)
double kTotalTime() const
total Kernel time
Definition: ChronoEntity.h:223
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:207
double uMaximalTime() const
maximal measurement for user time
Definition: ChronoEntity.h:207
double kMinimalTime() const
minimal measurement for kernel time
Definition: ChronoEntity.h:199
constexpr double millisecond
double flagMax() const
Definition: StatEntity.h:103
static const System::TimeType TimeUnit
internal unit used for the system time conversion (microseconds)
Definition: ChronoEntity.h:181
double flag() const
Definition: StatEntity.h:97
double eMeanTime() const
average Elapsed Time
Definition: ChronoEntity.h:243
backward compatible StatEntity class.
Definition: StatEntity.h:18
IChronoSvc::ChronoStatus stop()
stop the chrono
double kMaximalTime() const
maximal measurement for kernel time
Definition: ChronoEntity.h:211
std::string outputElapsedTime() const
print the chrono ;
double uRMSTime() const
r.m.s User Time
Definition: ChronoEntity.h:251
std::string outputUserTime() const
print the chrono ;
std::string outputSystemTime() const
print the chrono ;