The Gaudi Framework  v33r1 (b1225454)
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 // ============================================================================
24 // GaudiKernel
25 // ============================================================================
27 #include "GaudiKernel/Kernel.h"
28 #include "GaudiKernel/System.h"
29 // ============================================================================
30 // Boost
31 // ============================================================================
32 #include "boost/format.hpp"
33 // ============================================================================
39 // ============================================================================
40 namespace {
42  constexpr double microsecond = 1; // unit here is microsecond
43  constexpr double millisecond = 1000 * microsecond;
44  constexpr double second = 1000 * millisecond;
45  constexpr double minute = 60 * second;
46  constexpr double hour = 60 * minute;
47  constexpr double day = 24 * hour;
48  constexpr double week = 7 * day;
49  constexpr double month = 30 * day;
50  constexpr double year = 365 * day;
51 
52  constexpr double nanosecond = 0.001 * microsecond;
53 } // namespace
54 // ============================================================================
55 // start the chrono
56 // ============================================================================
58  if ( IChronoSvc::RUNNING == m_status ) { return m_status; }
59  //
60  // following lines could be platform dependent!
61  //
62  // Store in object the measured times
64 
66 
67  return m_status;
68 }
69 // ============================================================================
70 // stop the chrono
71 // ============================================================================
73  if ( IChronoSvc::RUNNING != m_status ) { return m_status; }
74 
75  // following lines could be platform dependent!
77 
78  // update the counters:
79 
83 
84  // set new status
85 
87 
88  return m_status;
89 }
90 // ============================================================================
91 // print user time
92 // ============================================================================
94  return "Time User : " +
96 }
97 // ============================================================================
98 // print system time
99 // ============================================================================
101  return "Time System : " +
103 }
104 // ============================================================================
105 // print time
107  return "TimeElapsed: " +
109 }
110 // ============================================================================
111 // print the chrono
112 // ============================================================================
113 std::string ChronoEntity::format( const double total, const double minimal, const double mean, const double rms,
114  const double maximal, const unsigned long number ) const {
115 
117  boost::format fmt( "Tot=%2$5.3g%1$s %4$43s #=%3$3lu" );
118 
119  static const std::array<std::tuple<int, double, std::string_view>, 9> tbl{{{500, microsecond, " [us]"},
120  {500, millisecond, " [ms]"},
121  {500, second, " [s]"},
122  {500, minute, "[min]"},
123  {500, hour, " [h]"},
124  {10, day, "[day]"},
125  {5, week, " [w]"},
126  {20, month, "[mon]"},
127  {-1, year, " [y]"}}};
128  auto i = std::find_if( begin( tbl ), std::prev( end( tbl ) ),
129  [&]( const auto& i ) { return total < std::get<0>( i ) * std::get<1>( i ); } );
130  long double unit = std::get<1>( *i );
131  fmt % std::get<2>( *i ) % (double)( total / unit ) % number;
132 
133  if ( number > 1 ) {
135  boost::format fmt1( "Ave/Min/Max=%2$5.3g(+-%3$5.3g)/%4$5.3g/%5$5.3g%1$s" );
136  i = std::find_if( std::begin( tbl ), std::prev( std::end( tbl ) ),
137  [&]( const auto& i ) { return total < std::get<0>( i ) * std::get<1>( i ); } );
138  unit = std::get<1>( *i );
139  fmt1 % std::get<2>( *i ) % (double)( mean / unit ) % (double)( rms / unit ) % (double)( minimal / unit ) %
140  (double)( maximal / unit );
141  fmt % fmt1.str();
142  } else {
143  fmt % "";
144  }
145 
146  return fmt.str();
147 }
148 // ============================================================================
149 // compound assignment operator
150 // ============================================================================
152  // System::ProcessTime type
153  m_delta += e.m_delta;
154 
155  // Summing, massaging here does not make too much sense.
156  // This is done only for final reductions
157  // Keep by convention the original one.
158  // m_start += e.m_start;
159 
160  // Tymevaluetypes type
161  m_user += e.m_user;
162  m_kernel += e.m_kernel;
163  m_elapsed += e.m_elapsed;
164 
165  return *this;
166 }
167 
168 // ============================================================================
169 /* print the chrono according the format and units
170  * @param typ the chrono type
171  * @param fmt the format string
172  * @param unit the unit
173  * @return the string representations
174  * @see boost::format
175  */
176 // ============================================================================
178  boost::format _fmt( std::string{fmt} );
179  // allow various number of arguments
180  using namespace boost::io;
181  _fmt.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
182  //
183  double _unit = microsecond;
184  switch ( unit ) {
185  case System::Year:
186  _unit = year;
187  break;
188  case System::Month:
189  _unit = month;
190  break;
191  case System::Day:
192  _unit = day;
193  break;
194  case System::Hour:
195  _unit = hour;
196  break;
197  case System::Min:
198  _unit = minute;
199  break;
200  case System::Sec:
201  _unit = second;
202  break;
203  case System::milliSec:
204  _unit = millisecond;
205  break;
206  case System::microSec:
207  _unit = microsecond;
208  break;
209  case System::nanoSec:
210  _unit = nanosecond;
211  break;
212  default:
213  _unit = microsecond;
214  break;
215  }
216  //
217  const StatEntity* stat = &m_user;
218  switch ( typ ) {
219  case IChronoSvc::USER:
220  stat = &m_user;
221  break;
222  case IChronoSvc::KERNEL:
223  stat = &m_kernel;
224  break;
225  case IChronoSvc::ELAPSED:
226  stat = &m_elapsed;
227  break;
228  default:
229  stat = &m_user;
230  break;
231  }
232  //
233  _fmt % ( stat->nEntries() ) // %1 : #entries
234  % ( stat->flag() / _unit ) // %2 : total time
235  % ( stat->flagMean() / _unit ) // %3 : mean time
236  % ( stat->flagRMS() / _unit ) // %4 : RMS time
237  % ( stat->flagMeanErr() / _unit ) // %5 : error in mean time
238  % ( stat->flagMin() / _unit ) // %6 : minimal time
239  % ( stat->flagMax() / _unit ); // %7 : maximal time
240  //
241  return _fmt.str();
242 }
243 // ==========================================================================
244 
245 // ============================================================================
246 // The END
247 // ============================================================================
std::string outputTime(IChronoSvc::ChronoType typ, std::string_view 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
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
def end
Definition: IOTest.py:123
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
STL class.
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
AttribStringParser::Iterator begin(const AttribStringParser &parser)
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 ;