The Gaudi Framework  master (37c0b60a)
ChronoEntity.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 \***********************************************************************************/
12 #include <GaudiKernel/Kernel.h>
13 #include <GaudiKernel/System.h>
14 #include <algorithm>
15 #include <boost/format.hpp>
16 #include <cmath>
17 #include <cstdio>
18 #include <fmt/format.h>
19 #include <iomanip>
20 #include <iostream>
21 
22 // ============================================================================
28 // ============================================================================
29 namespace {
31  constexpr double microsecond = 1; // unit here is microsecond
32  constexpr double millisecond = 1000 * microsecond;
33  constexpr double second = 1000 * millisecond;
34  constexpr double minute = 60 * second;
35  constexpr double hour = 60 * minute;
36  constexpr double day = 24 * hour;
37  constexpr double week = 7 * day;
38  constexpr double month = 30 * day;
39  constexpr double year = 365 * day;
40 
41  constexpr double nanosecond = 0.001 * microsecond;
42 } // namespace
43 // ============================================================================
44 // start the chrono
45 // ============================================================================
47  if ( IChronoSvc::RUNNING == m_status ) { return m_status; }
48  //
49  // following lines could be platform dependent!
50  //
51  // Store in object the measured times
53 
55 
56  return m_status;
57 }
58 // ============================================================================
59 // stop the chrono
60 // ============================================================================
62  if ( IChronoSvc::RUNNING != m_status ) { return m_status; }
63 
64  // following lines could be platform dependent!
66 
67  // update the counters:
68 
72 
73  // set new status
74 
76 
77  return m_status;
78 }
79 // ============================================================================
80 // print user time
81 // ============================================================================
83  return "Time User : " +
85 }
86 // ============================================================================
87 // print system time
88 // ============================================================================
90  return "Time System : " +
92 }
93 // ============================================================================
94 // print time
96  return "TimeElapsed: " +
98 }
99 // ============================================================================
100 // print the chrono
101 // ============================================================================
102 std::string ChronoEntity::format( const double total, const double minimal, const double mean, const double rms,
103  const double maximal, const unsigned long number ) const {
104 
106  constexpr auto fmt = "Tot={1:5.3g}{0:5} {3} #={2:3}";
107  constexpr auto stat_fmt = "Ave/Min/Max={1:8.3g}(+-{2:8.3g})/{3:8.3g}/{4:8.3g}{0:5}";
108 
109  static const std::array<std::tuple<int, double, std::string_view>, 9> tbl{ { { 500, microsecond, " [us]" },
110  { 500, millisecond, " [ms]" },
111  { 500, second, " [s]" },
112  { 500, minute, "[min]" },
113  { 500, hour, " [h]" },
114  { 10, day, "[day]" },
115  { 5, week, " [w]" },
116  { 20, month, "[mon]" },
117  { -1, year, " [y]" } } };
118 
119  auto i = find_if( begin( tbl ), prev( end( tbl ) ),
120  [&]( const auto& i ) { return total < std::get<0>( i ) * std::get<1>( i ); } );
121  long double unit = std::get<1>( *i );
122  std::string_view unit_name = std::get<2>( *i );
123 
124  auto stats = [&]() -> std::string {
125  if ( number > 1 ) {
126  auto i = find_if( begin( tbl ), prev( end( tbl ) ),
127  [&]( const auto& i ) { return total < std::get<0>( i ) * std::get<1>( i ); } );
128  auto unit = std::get<1>( *i );
129  std::string_view unit_name = std::get<2>( *i );
130  return fmt::format( stat_fmt, unit_name, (double)( mean / unit ), (double)( rms / unit ),
131  (double)( minimal / unit ), (double)( maximal / unit ) );
132  } else {
133  return {};
134  }
135  };
136 
137  return fmt::format( fmt, unit_name, (double)( total / unit ), number, stats() );
138 }
139 // ============================================================================
140 // compound assignment operator
141 // ============================================================================
143  // System::ProcessTime type
144  m_delta += e.m_delta;
145 
146  // Summing, massaging here does not make too much sense.
147  // This is done only for final reductions
148  // Keep by convention the original one.
149  // m_start += e.m_start;
150 
151  // Tymevaluetypes type
152  m_user += e.m_user;
153  m_kernel += e.m_kernel;
154  m_elapsed += e.m_elapsed;
155 
156  return *this;
157 }
158 
159 // ============================================================================
160 /* print the chrono according the format and units
161  * @param typ the chrono type
162  * @param fmt the format string
163  * @param unit the unit
164  * @return the string representations
165  * @see boost::format
166  */
167 // ============================================================================
169  boost::format _fmt( std::string{ fmt } );
170  // allow various number of arguments
171  using namespace boost::io;
172  _fmt.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
173  //
174  double _unit = microsecond;
175  switch ( unit ) {
176  case System::Year:
177  _unit = year;
178  break;
179  case System::Month:
180  _unit = month;
181  break;
182  case System::Day:
183  _unit = day;
184  break;
185  case System::Hour:
186  _unit = hour;
187  break;
188  case System::Min:
189  _unit = minute;
190  break;
191  case System::Sec:
192  _unit = second;
193  break;
194  case System::milliSec:
195  _unit = millisecond;
196  break;
197  case System::microSec:
198  _unit = microsecond;
199  break;
200  case System::nanoSec:
201  _unit = nanosecond;
202  break;
203  default:
204  _unit = microsecond;
205  break;
206  }
207  //
208  const StatEntity* stat = &m_user;
209  switch ( typ ) {
210  case IChronoSvc::USER:
211  stat = &m_user;
212  break;
213  case IChronoSvc::KERNEL:
214  stat = &m_kernel;
215  break;
216  case IChronoSvc::ELAPSED:
217  stat = &m_elapsed;
218  break;
219  default:
220  stat = &m_user;
221  break;
222  }
223  //
224  _fmt % ( stat->nEntries() ) // %1 : #entries
225  % ( stat->flag() / _unit ) // %2 : total time
226  % ( stat->flagMean() / _unit ) // %3 : mean time
227  % ( stat->flagRMS() / _unit ) // %4 : RMS time
228  % ( stat->flagMeanErr() / _unit ) // %5 : error in mean time
229  % ( stat->flagMin() / _unit ) // %6 : minimal time
230  % ( stat->flagMax() / _unit ); // %7 : maximal time
231  //
232  return _fmt.str();
233 }
234 // ==========================================================================
235 
236 // ============================================================================
237 // The END
238 // ============================================================================
System::milliSec
@ milliSec
Definition: Timing.h:67
ChronoEntity::m_user
StatEntity m_user
the actual storage of "user" time
Definition: ChronoEntity.h:175
ChronoEntity::m_elapsed
StatEntity m_elapsed
the actual storage of "elapsed" time
Definition: ChronoEntity.h:179
std::string
STL class.
ChronoEntity::eMinimalTime
double eMinimalTime() const
minimal measurement for elapsed time
Definition: ChronoEntity.h:203
ChronoEntity
Definition: ChronoEntity.h:31
System::Hour
@ Hour
Definition: Timing.h:67
ChronoEntity::uMinimalTime
double uMinimalTime() const
minimal measurement for user time
Definition: ChronoEntity.h:195
ChronoEntity::stop
IChronoSvc::ChronoStatus stop()
stop the chrono
Definition: ChronoEntity.cpp:61
ChronoEntity::eRMSTime
double eRMSTime() const
r.m.s Elapsed Time
Definition: ChronoEntity.h:255
ChronoEntity::format
std::string format(const double total, const double minimal, const double mean, const double rms, const double maximal, const unsigned long number) const
format
Definition: ChronoEntity.cpp:102
System.h
System::Year
@ Year
Definition: Timing.h:67
IChronoSvc::ELAPSED
@ ELAPSED
Definition: IChronoSvc.h:56
StatEntity
backward compatible StatEntity class.
Definition: StatEntity.h:23
System::microSec
@ microSec
Definition: Timing.h:67
ChronoEntity::eMeanTime
double eMeanTime() const
average Elapsed Time
Definition: ChronoEntity.h:243
System::ProcessTime::kernelTime
TimeValueType kernelTime() const
Retrieve the kernel time in the requested unit.
Definition: Timing.h:168
Gaudi::Units::second
constexpr double second
Definition: SystemOfUnits.h:139
ChronoEntity::eTotalTime
double eTotalTime() const
total Elapsed time
Definition: ChronoEntity.h:227
ChronoEntity::kMinimalTime
double kMinimalTime() const
minimal measurement for kernel time
Definition: ChronoEntity.h:199
StatEntity::flagRMS
double flagRMS() const
Definition: StatEntity.h:99
IChronoSvc::ChronoType
ChronoType
Definition: IChronoSvc.h:56
System::TimeType
TimeType
Time type for conversion.
Definition: Timing.h:67
ChronoEntity::start
IChronoSvc::ChronoStatus start()
start the current chrono
Definition: ChronoEntity.cpp:46
ChronoEntity::m_delta
System::ProcessTime m_delta
delta process times
Definition: ChronoEntity.h:171
StatEntity::flag
double flag() const
Definition: StatEntity.h:96
System::nanoSec
@ nanoSec
Definition: Timing.h:67
ChronoEntity::nOfMeasurements
unsigned long nOfMeasurements() const
number of chrono measurements
Definition: ChronoEntity.h:191
System::Month
@ Month
Definition: Timing.h:67
ChronoEntity::m_status
IChronoSvc::ChronoStatus m_status
current status of this chrono object;
Definition: ChronoEntity.h:169
StatEntity::flagMax
double flagMax() const
Definition: StatEntity.h:102
ChronoEntity::uMeanTime
double uMeanTime() const
average User Time
Definition: ChronoEntity.h:239
Gaudi::Units::microsecond
constexpr double microsecond
Definition: SystemOfUnits.h:141
ChronoEntity::outputSystemTime
std::string outputSystemTime() const
print the chrono ;
Definition: ChronoEntity.cpp:89
StatEntity::flagMeanErr
double flagMeanErr() const
Definition: StatEntity.h:100
System::ProcessTime::elapsedTime
TimeValueType elapsedTime() const
Retrieve the elapsed time in the requested unit.
Definition: Timing.h:180
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:136
IChronoSvc::RUNNING
@ RUNNING
Definition: IChronoSvc.h:54
StatEntity::flagMean
double flagMean() const
Definition: StatEntity.h:98
ChronoEntity::kRMSTime
double kRMSTime() const
r.m.s Kernel Time
Definition: ChronoEntity.h:247
ChronoEntity::kTotalTime
double kTotalTime() const
total Kernel time
Definition: ChronoEntity.h:223
ChronoEntity::outputElapsedTime
std::string outputElapsedTime() const
print the chrono ;
Definition: ChronoEntity.cpp:95
ChronoEntity::outputUserTime
std::string outputUserTime() const
print the chrono ;
Definition: ChronoEntity.cpp:82
std::array
STL class.
ChronoEntity::m_start
System::ProcessTime m_start
start stamp for current measurement of process times
Definition: ChronoEntity.h:173
ChronoEntity::m_kernel
StatEntity m_kernel
the actual storage of "kernel" time
Definition: ChronoEntity.h:177
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
ChronoEntity::kMaximalTime
double kMaximalTime() const
maximal measurement for kernel time
Definition: ChronoEntity.h:211
ChronoEntity::uRMSTime
double uRMSTime() const
r.m.s User Time
Definition: ChronoEntity.h:251
System::Sec
@ Sec
Definition: Timing.h:67
System::Day
@ Day
Definition: Timing.h:67
ChronoEntity.h
System::Min
@ Min
Definition: Timing.h:67
IChronoSvc::KERNEL
@ KERNEL
Definition: IChronoSvc.h:56
Kernel.h
fmt
IChronoSvc::ChronoStatus
ChronoStatus
Definition: IChronoSvc.h:54
StatEntity::flagMin
double flagMin() const
Definition: StatEntity.h:101
Gaudi::Units::nanosecond
constexpr double nanosecond
Definition: SystemOfUnits.h:138
ChronoEntity::kMeanTime
double kMeanTime() const
average Kernel Time
Definition: ChronoEntity.h:235
IChronoSvc::STOPPED
@ STOPPED
Definition: IChronoSvc.h:54
Gaudi::Units::millisecond
constexpr double millisecond
Definition: SystemOfUnits.h:140
IOTest.end
end
Definition: IOTest.py:125
ChronoEntity::TimeUnit
static const System::TimeType TimeUnit
internal unit used for the system time conversion (microseconds)
Definition: ChronoEntity.h:181
ChronoEntity::uMaximalTime
double uMaximalTime() const
maximal measurement for user time
Definition: ChronoEntity.h:207
System::ProcessTime::userTime
TimeValueType userTime() const
Retrieve the user time in the requested unit.
Definition: Timing.h:174
System::getProcessTime
GAUDI_API ProcessTime getProcessTime(long pid=-1)
Retrieve the process time data for a process.
Definition: Timing.cpp:207
ChronoEntity::outputTime
std::string outputTime(IChronoSvc::ChronoType typ, std::string_view fmt, System::TimeType unit) const
print the chrono according the format and units
Definition: ChronoEntity.cpp:168
IChronoSvc::USER
@ USER
Definition: IChronoSvc.h:56
ChronoEntity::eMaximalTime
double eMaximalTime() const
maximal measurement for elapsed time
Definition: ChronoEntity.h:215
ChronoEntity::uTotalTime
double uTotalTime() const
total user time
Definition: ChronoEntity.h:219
ChronoEntity::operator+=
ChronoEntity & operator+=(const ChronoEntity &entity)
Compound assignment operator.
Definition: ChronoEntity.cpp:142