Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ChronoEntity.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 <fmt/format.h>
17 
18 namespace {
20  constexpr double microsecond = 1; // unit here is microsecond
21  constexpr double millisecond = 1000 * microsecond;
22  constexpr double second = 1000 * millisecond;
23  constexpr double minute = 60 * second;
24  constexpr double hour = 60 * minute;
25  constexpr double day = 24 * hour;
26  constexpr double week = 7 * day;
27  constexpr double month = 30 * day;
28  constexpr double year = 365 * day;
29 
30  constexpr double nanosecond = 0.001 * microsecond;
31 } // namespace
32 // ============================================================================
33 // start the chrono
34 // ============================================================================
36  if ( IChronoSvc::RUNNING == m_status ) { return m_status; }
37  //
38  // following lines could be platform dependent!
39  //
40  // Store in object the measured times
42 
44 
45  return m_status;
46 }
47 // ============================================================================
48 // stop the chrono
49 // ============================================================================
51  if ( IChronoSvc::RUNNING != m_status ) { return m_status; }
52 
53  // following lines could be platform dependent!
55 
56  // update the counters:
57 
61 
62  // set new status
63 
65 
66  return m_status;
67 }
68 std::string ChronoEntity::outputUserTime() const {
69  return "Time User : " +
71 }
72 std::string ChronoEntity::outputSystemTime() const {
73  return "Time System : " +
75 }
76 std::string ChronoEntity::outputElapsedTime() const {
77  return "TimeElapsed: " +
79 }
80 std::string ChronoEntity::format( const double total, const double minimal, const double mean, const double rms,
81  const double maximal, const unsigned long number ) const {
82 
84  constexpr auto fmt = "Tot={1:5.3g}{0:5} {3} #={2:3}";
85  constexpr auto stat_fmt = "Ave/Min/Max={1:8.3g}(+-{2:8.3g})/{3:8.3g}/{4:8.3g}{0:5}";
86 
87  static const std::array<std::tuple<int, double, std::string_view>, 9> tbl{ { { 500, microsecond, " [us]" },
88  { 500, millisecond, " [ms]" },
89  { 500, second, " [s]" },
90  { 500, minute, "[min]" },
91  { 500, hour, " [h]" },
92  { 10, day, "[day]" },
93  { 5, week, " [w]" },
94  { 20, month, "[mon]" },
95  { -1, year, " [y]" } } };
96 
97  auto i = find_if( begin( tbl ), prev( end( tbl ) ),
98  [&]( const auto& i ) { return total < std::get<0>( i ) * std::get<1>( i ); } );
99  long double unit = std::get<1>( *i );
100  std::string_view unit_name = std::get<2>( *i );
101 
102  auto stats = [&]() -> std::string {
103  if ( number > 1 ) {
104  auto i = find_if( begin( tbl ), prev( end( tbl ) ),
105  [&]( const auto& i ) { return total < std::get<0>( i ) * std::get<1>( i ); } );
106  auto unit = std::get<1>( *i );
107  std::string_view unit_name = std::get<2>( *i );
108  return fmt::format( stat_fmt, unit_name, (double)( mean / unit ), (double)( rms / unit ),
109  (double)( minimal / unit ), (double)( maximal / unit ) );
110  } else {
111  return {};
112  }
113  };
114 
115  return fmt::format( fmt, unit_name, (double)( total / unit ), number, stats() );
116 }
118  // System::ProcessTime type
119  m_delta += e.m_delta;
120 
121  // Summing, massaging here does not make too much sense.
122  // This is done only for final reductions
123  // Keep by convention the original one.
124  // m_start += e.m_start;
125 
126  // Tymevaluetypes type
127  m_user += e.m_user;
128  m_kernel += e.m_kernel;
129  m_elapsed += e.m_elapsed;
130 
131  return *this;
132 }
133 
134 std::string ChronoEntity::outputTime( IChronoSvc::ChronoType typ, std::string_view fmt, System::TimeType unit ) const {
135  boost::format _fmt( std::string{ fmt } );
136  // allow various number of arguments
137  using namespace boost::io;
138  _fmt.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
139  double _unit = microsecond;
140  switch ( unit ) {
141  case System::Year:
142  _unit = year;
143  break;
144  case System::Month:
145  _unit = month;
146  break;
147  case System::Day:
148  _unit = day;
149  break;
150  case System::Hour:
151  _unit = hour;
152  break;
153  case System::Min:
154  _unit = minute;
155  break;
156  case System::Sec:
157  _unit = second;
158  break;
159  case System::milliSec:
160  _unit = millisecond;
161  break;
162  case System::microSec:
163  _unit = microsecond;
164  break;
165  case System::nanoSec:
166  _unit = nanosecond;
167  break;
168  default:
169  _unit = microsecond;
170  break;
171  }
172  const StatEntity* stat = &m_user;
173  switch ( typ ) {
174  case IChronoSvc::USER:
175  stat = &m_user;
176  break;
177  case IChronoSvc::KERNEL:
178  stat = &m_kernel;
179  break;
180  case IChronoSvc::ELAPSED:
181  stat = &m_elapsed;
182  break;
183  default:
184  stat = &m_user;
185  break;
186  }
187  _fmt % ( stat->nEntries() ) // %1 : #entries
188  % ( stat->flag() / _unit ) // %2 : total time
189  % ( stat->flagMean() / _unit ) // %3 : mean time
190  % ( stat->flagRMS() / _unit ) // %4 : RMS time
191  % ( stat->flagMeanErr() / _unit ) // %5 : error in mean time
192  % ( stat->flagMin() / _unit ) // %6 : minimal time
193  % ( stat->flagMax() / _unit ); // %7 : maximal time
194  return _fmt.str();
195 }
System::milliSec
@ milliSec
Definition: Timing.h:45
ChronoEntity::m_user
StatEntity m_user
the actual storage of "user" time
Definition: ChronoEntity.h:154
ChronoEntity::m_elapsed
StatEntity m_elapsed
the actual storage of "elapsed" time
Definition: ChronoEntity.h:158
ChronoEntity::eMinimalTime
double eMinimalTime() const
minimal measurement for elapsed time
Definition: ChronoEntity.h:167
ChronoEntity
Definition: ChronoEntity.h:26
System::Hour
@ Hour
Definition: Timing.h:45
ChronoEntity::uMinimalTime
double uMinimalTime() const
minimal measurement for user time
Definition: ChronoEntity.h:165
ChronoEntity::stop
IChronoSvc::ChronoStatus stop()
stop the chrono
Definition: ChronoEntity.cpp:50
ChronoEntity::eRMSTime
double eRMSTime() const
r.m.s Elapsed Time
Definition: ChronoEntity.h:180
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:80
System.h
System::Year
@ Year
Definition: Timing.h:45
IChronoSvc::ELAPSED
@ ELAPSED
Definition: IChronoSvc.h:44
StatEntity
backward compatible StatEntity class.
Definition: StatEntity.h:23
System::microSec
@ microSec
Definition: Timing.h:45
ChronoEntity::eMeanTime
double eMeanTime() const
average Elapsed Time
Definition: ChronoEntity.h:177
System::ProcessTime::kernelTime
TimeValueType kernelTime() const
Retrieve the kernel time in the requested unit.
Definition: Timing.h:146
Gaudi::Units::second
constexpr double second
Definition: SystemOfUnits.h:138
ChronoEntity::eTotalTime
double eTotalTime() const
total Elapsed time
Definition: ChronoEntity.h:173
ChronoEntity::kMinimalTime
double kMinimalTime() const
minimal measurement for kernel time
Definition: ChronoEntity.h:166
StatEntity::flagRMS
double flagRMS() const
Definition: StatEntity.h:99
IChronoSvc::ChronoType
ChronoType
Definition: IChronoSvc.h:44
System::TimeType
TimeType
Time type for conversion.
Definition: Timing.h:45
ChronoEntity::start
IChronoSvc::ChronoStatus start()
start the current chrono
Definition: ChronoEntity.cpp:35
ChronoEntity::m_delta
System::ProcessTime m_delta
delta process times
Definition: ChronoEntity.h:150
StatEntity::flag
double flag() const
Definition: StatEntity.h:96
System::nanoSec
@ nanoSec
Definition: Timing.h:45
ChronoEntity::nOfMeasurements
unsigned long nOfMeasurements() const
number of chrono measurements
Definition: ChronoEntity.h:164
System::Month
@ Month
Definition: Timing.h:45
ChronoEntity::m_status
IChronoSvc::ChronoStatus m_status
current status of this chrono object;
Definition: ChronoEntity.h:148
StatEntity::flagMax
double flagMax() const
Definition: StatEntity.h:102
ChronoEntity::uMeanTime
double uMeanTime() const
average User Time
Definition: ChronoEntity.h:176
Gaudi::Units::microsecond
constexpr double microsecond
Definition: SystemOfUnits.h:140
ChronoEntity::outputSystemTime
std::string outputSystemTime() const
print the chrono ;
Definition: ChronoEntity.cpp:72
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:158
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:135
IChronoSvc::RUNNING
@ RUNNING
Definition: IChronoSvc.h:42
StatEntity::flagMean
double flagMean() const
Definition: StatEntity.h:98
ChronoEntity::kRMSTime
double kRMSTime() const
r.m.s Kernel Time
Definition: ChronoEntity.h:178
ChronoEntity::kTotalTime
double kTotalTime() const
total Kernel time
Definition: ChronoEntity.h:172
ChronoEntity::outputElapsedTime
std::string outputElapsedTime() const
print the chrono ;
Definition: ChronoEntity.cpp:76
ChronoEntity::outputUserTime
std::string outputUserTime() const
print the chrono ;
Definition: ChronoEntity.cpp:68
ChronoEntity::m_start
System::ProcessTime m_start
start stamp for current measurement of process times
Definition: ChronoEntity.h:152
ChronoEntity::m_kernel
StatEntity m_kernel
the actual storage of "kernel" time
Definition: ChronoEntity.h:156
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:93
ChronoEntity::kMaximalTime
double kMaximalTime() const
maximal measurement for kernel time
Definition: ChronoEntity.h:169
ChronoEntity::uRMSTime
double uRMSTime() const
r.m.s User Time
Definition: ChronoEntity.h:179
System::Sec
@ Sec
Definition: Timing.h:45
System::Day
@ Day
Definition: Timing.h:45
ChronoEntity.h
System::Min
@ Min
Definition: Timing.h:45
IChronoSvc::KERNEL
@ KERNEL
Definition: IChronoSvc.h:44
Kernel.h
fmt
IChronoSvc::ChronoStatus
ChronoStatus
Definition: IChronoSvc.h:42
StatEntity::flagMin
double flagMin() const
Definition: StatEntity.h:101
Gaudi::Units::nanosecond
constexpr double nanosecond
Definition: SystemOfUnits.h:137
ChronoEntity::kMeanTime
double kMeanTime() const
average Kernel Time
Definition: ChronoEntity.h:175
IChronoSvc::STOPPED
@ STOPPED
Definition: IChronoSvc.h:42
Gaudi::Units::millisecond
constexpr double millisecond
Definition: SystemOfUnits.h:139
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:160
ChronoEntity::uMaximalTime
double uMaximalTime() const
maximal measurement for user time
Definition: ChronoEntity.h:168
System::ProcessTime::userTime
TimeValueType userTime() const
Retrieve the user time in the requested unit.
Definition: Timing.h:152
System::getProcessTime
GAUDI_API ProcessTime getProcessTime(long pid=-1)
Retrieve the process time data for a process.
Definition: Timing.cpp:192
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:134
IChronoSvc::USER
@ USER
Definition: IChronoSvc.h:44
ChronoEntity::eMaximalTime
double eMaximalTime() const
maximal measurement for elapsed time
Definition: ChronoEntity.h:170
ChronoEntity::uTotalTime
double uTotalTime() const
total user time
Definition: ChronoEntity.h:171
ChronoEntity::operator+=
ChronoEntity & operator+=(const ChronoEntity &entity)
Compound assignment operator.
Definition: ChronoEntity.cpp:117