| The Gaudi Framework v32r1 (f65d50dc)

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