The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
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
18namespace {
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
58 m_user += m_delta.userTime<TimeUnit>();
59 m_kernel += m_delta.kernelTime<TimeUnit>();
60 m_elapsed += m_delta.elapsedTime<TimeUnit>();
61
62 // set new status
63
65
66 return m_status;
67}
68std::string ChronoEntity::outputUserTime() const {
69 return "Time User : " +
71}
72std::string ChronoEntity::outputSystemTime() const {
73 return "Time System : " +
75}
77 return "TimeElapsed: " +
79}
80std::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
134std::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;
178 stat = &m_kernel;
179 break;
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}
std::string outputSystemTime() const
print the chrono ;
double kMaximalTime() const
maximal measurement for kernel time
std::string outputTime(IChronoSvc::ChronoType typ, std::string_view fmt, System::TimeType unit) const
print the chrono according the format and units
double kTotalTime() const
total Kernel time
System::ProcessTime m_start
start stamp for current measurement of process times
double eMinimalTime() const
minimal measurement for elapsed time
std::string format(const double total, const double minimal, const double mean, const double rms, const double maximal, const unsigned long number) const
format
IChronoSvc::ChronoStatus start()
start the current chrono
ChronoEntity()=default
default constructor
IChronoSvc::ChronoStatus stop()
stop the chrono
double eMeanTime() const
average Elapsed Time
double uMeanTime() const
average User Time
double uMaximalTime() const
maximal measurement for user time
unsigned long nOfMeasurements() const
number of chrono measurements
double uTotalTime() const
total user time
StatEntity m_elapsed
the actual storage of "elapsed" time
double uRMSTime() const
r.m.s User Time
static const System::TimeType TimeUnit
internal unit used for the system time conversion (microseconds)
StatEntity m_kernel
the actual storage of "kernel" time
double eMaximalTime() const
maximal measurement for elapsed time
System::ProcessTime m_delta
delta process times
double kMinimalTime() const
minimal measurement for kernel time
std::string outputElapsedTime() const
print the chrono ;
ChronoEntity & operator+=(const ChronoEntity &entity)
Compound assignment operator.
StatEntity m_user
the actual storage of "user" time
double kMeanTime() const
average Kernel Time
double uMinimalTime() const
minimal measurement for user time
double eTotalTime() const
total Elapsed time
double eRMSTime() const
r.m.s Elapsed Time
std::string outputUserTime() const
print the chrono ;
IChronoSvc::ChronoStatus m_status
current status of this chrono object;
double kRMSTime() const
r.m.s Kernel Time
backward compatible StatEntity class.
Definition StatEntity.h:23
double flag() const
Definition StatEntity.h:96
double flagRMS() const
Definition StatEntity.h:99
double flagMax() const
Definition StatEntity.h:102
double flagMin() const
Definition StatEntity.h:101
double flagMeanErr() const
Definition StatEntity.h:100
double flagMean() const
Definition StatEntity.h:98
constexpr double nanosecond
constexpr double millisecond
constexpr double second
constexpr double microsecond
GAUDI_API ProcessTime getProcessTime(long pid=-1)
Retrieve the process time data for a process.
Definition Timing.cpp:192
TimeType
Time type for conversion.
Definition Timing.h:45
@ milliSec
Definition Timing.h:45
@ Min
Definition Timing.h:45
@ Year
Definition Timing.h:45
@ Sec
Definition Timing.h:45
@ nanoSec
Definition Timing.h:45
@ Hour
Definition Timing.h:45
@ Month
Definition Timing.h:45
@ microSec
Definition Timing.h:45
@ Day
Definition Timing.h:45