| The Gaudi Framework v30r0 (c919700c)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
30 {
32 constexpr double microsecond = 1; // unit here is microsecond
33 constexpr double millisecond = 1000 * microsecond;
34 constexpr double second = 1000 * millisecond;
35 constexpr double minute = 60 * second;
36 constexpr double hour = 60 * minute;
37 constexpr double day = 24 * hour;
38 constexpr double week = 7 * day;
39 constexpr double month = 30 * day;
40 constexpr double year = 365 * day;
41
42 constexpr double nanosecond = 0.001 * microsecond;
43 }
44 // ============================================================================
45 // start the chrono
46 // ============================================================================
47 IChronoSvc::ChronoStatus ChronoEntity::start()
48 {
49 if ( IChronoSvc::RUNNING == m_status ) {
50 return m_status;
51 }
52 //
53 // following lines could be platform dependent!
54 //
55 // Store in object the measured times
56 m_start = System::getProcessTime();
57
58 m_status = IChronoSvc::RUNNING;
59
60 return m_status;
61 }
62 // ============================================================================
63 // stop the chrono
64 // ============================================================================
65 IChronoSvc::ChronoStatus ChronoEntity::stop()
66 {
67 if ( IChronoSvc::RUNNING != m_status ) {
68 return m_status;
69 }
70
71 // following lines could be platform dependent!
72 m_delta = System::getProcessTime() - m_start;
73
74 // update the counters:
75
76 m_user += m_delta.userTime<TimeUnit>();
77 m_kernel += m_delta.kernelTime<TimeUnit>();
78 m_elapsed += m_delta.elapsedTime<TimeUnit>();
79
80 // set new status
81
82 m_status = IChronoSvc::STOPPED;
83
84 return m_status;
85 }
86 // ============================================================================
87 // print user time
88 // ============================================================================
89 std::string ChronoEntity::outputUserTime() const
90 {
91 return "Time User : " +
92 format( uTotalTime(), uMinimalTime(), uMeanTime(), uRMSTime(), uMaximalTime(), nOfMeasurements() );
93 }
94 // ============================================================================
95 // print system time
96 // ============================================================================
97 std::string ChronoEntity::outputSystemTime() const
98 {
99 return "Time System : " +
100 format( kTotalTime(), kMinimalTime(), kMeanTime(), kRMSTime(), kMaximalTime(), nOfMeasurements() );
101 }
102 // ============================================================================
103 // print time
104 std::string ChronoEntity::outputElapsedTime() const
105 {
106 return "TimeElapsed: " +
107 format( eTotalTime(), eMinimalTime(), eMeanTime(), eRMSTime(), eMaximalTime(), nOfMeasurements() );
108 }
109 // ============================================================================
110 // print the chrono
111 // ============================================================================
112 std::string ChronoEntity::format( const double total, const double minimal, const double mean, const double rms,
113 const double maximal, const unsigned long number ) const
114 {
115
117 boost::format fmt( "Tot=%2$5.3g%1$s %4$43s #=%3$3lu" );
118
119 static const auto tbl = {std::make_tuple( 500, microsecond, " [us]" ), std::make_tuple( 500, millisecond, " [ms]" ),
120 std::make_tuple( 500, second, " [s]" ), std::make_tuple( 500, minute, "[min]" ),
121 std::make_tuple( 500, hour, " [h]" ), std::make_tuple( 10, day, "[day]" ),
122 std::make_tuple( 5, week, " [w]" ), std::make_tuple( 20, month, "[mon]" ),
123 std::make_tuple( -1, year, " [y]" )};
124
125 auto i = std::find_if(
126 std::begin( tbl ), std::prev( std::end( tbl ) ),
127 [&]( const std::tuple<int, double, const char*>& i ) { return total < std::get<0>( i ) * std::get<1>( i ); } );
128 long double unit = std::get<1>( *i );
129 fmt % std::get<2>( *i ) % (double)( total / unit ) % number;
130
131 if ( number > 1 ) {
133 boost::format fmt1( "Ave/Min/Max=%2$5.3g(+-%3$5.3g)/%4$5.3g/%5$5.3g%1$s" );
134 auto i = std::find_if(
135 std::begin( tbl ), std::prev( std::end( tbl ) ),
136 [&]( const std::tuple<int, double, const char*>& i ) { return total < std::get<0>( i ) * std::get<1>( i ); } );
137 unit = std::get<1>( *i );
138 fmt1 % std::get<2>( *i ) % (double)( mean / unit ) % (double)( rms / unit ) % (double)( minimal / unit ) %
139 (double)( maximal / unit );
140 fmt % fmt1.str();
141 } else {
142 fmt % "";
143 }
144
145 return fmt.str();
146 }
147 // ============================================================================
148 // comparison operator
149 // ============================================================================
150 bool ChronoEntity::operator<( const ChronoEntity& e ) const
151 {
152 return ( &e == this )
153 ? false
154 : ( totalTime() < e.totalTime() )
155 ? true
156 : ( totalTime() > e.totalTime() )
157 ? false
158 : ( m_user < e.m_user )
159 ? true
160 : ( e.m_user < m_user ) ? false
161 : ( m_kernel < e.m_kernel )
162 ? true
163 : ( e.m_kernel < m_kernel )
164 ? false
165 : ( m_elapsed < e.m_elapsed )
166 ? true
167 : ( e.m_elapsed < m_elapsed ) ? false : false;
168 }
169 // ============================================================================
170 // compound assignment operator
171 // ============================================================================
172 ChronoEntity& ChronoEntity::operator+=( const ChronoEntity& e )
173 {
174 // System::ProcessTime type
175 m_delta += e.m_delta;
176
177 // Summing, massaging here does not make too much sense.
178 // This is done only for final reductions
179 // Keep by convention the original one.
180 // m_start += e.m_start;
181
182 // Tymevaluetypes type
183 m_user += e.m_user;
184 m_kernel += e.m_kernel;
185 m_elapsed += e.m_elapsed;
186
187 return *this;
188 }
189
190 // ============================================================================
191 /* print the chrono according the format and units
192 * @param typ the chrono type
193 * @param fmt the format string
194 * @param unit the unit
195 * @return the string representations
196 * @see boost::format
197 */
198 // ============================================================================
199 std::string ChronoEntity::outputTime( IChronoSvc::ChronoType typ, const std::string& fmt, System::TimeType unit ) const
200 {
201 boost::format _fmt( fmt );
202 // allow various number of arguments
203 using namespace boost::io;
204 _fmt.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
205 //
206 double _unit = microsecond;
207 switch ( unit ) {
208 case System::Year:
209 _unit = year;
210 break;
211 case System::Month:
212 _unit = month;
213 break;
214 case System::Day:
215 _unit = day;
216 break;
217 case System::Hour:
218 _unit = hour;
219 break;
220 case System::Min:
221 _unit = minute;
222 break;
223 case System::Sec:
224 _unit = second;
225 break;
226 case System::milliSec:
227 _unit = millisecond;
228 break;
229 case System::microSec:
230 _unit = microsecond;
231 break;
232 case System::nanoSec:
233 _unit = nanosecond;
234 break;
235 default:
236 _unit = microsecond;
237 break;
238 }
239 //
240 const StatEntity* stat = &m_user;
241 switch ( typ ) {
242 case IChronoSvc::USER:
243 stat = &m_user;
244 break;
245 case IChronoSvc::KERNEL:
246 stat = &m_kernel;
247 break;
248 case IChronoSvc::ELAPSED:
249 stat = &m_elapsed;
250 break;
251 default:
252 stat = &m_user;
253 break;
254 }
255 //
256 _fmt % ( stat->nEntries() ) // %1 : #entries
257 % ( stat->flag() / _unit ) // %2 : total time
258 % ( stat->flagMean() / _unit ) // %3 : mean time
259 % ( stat->flagRMS() / _unit ) // %4 : RMS time
260 % ( stat->flagMeanErr() / _unit ) // %5 : error in mean time
261 % ( stat->flagMin() / _unit ) // %6 : minimal time
262 % ( stat->flagMax() / _unit ); // %7 : maximal time
263 //
264 return _fmt.str();
265 }
266 // ==========================================================================
267
268 // ============================================================================
269 // The END
270 // ============================================================================
ChronoEntity::outputElapsedTime
std::string outputElapsedTime() const
print the chrono ;
Definition: ChronoEntity.cpp:104
System.h
ChronoEntity::kMaximalTime
double kMaximalTime() const
maximal measurement for kernel time
Definition: ChronoEntity.h:199
Kernel.h
ChronoEntity::uMinimalTime
double uMinimalTime() const
minimal measurement for user time
Definition: ChronoEntity.h:183
StatEntity::nEntries
const unsigned long & nEntries() const
getters – no synchronization!
Definition: StatEntity.h:92
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:120
ChronoEntity::m_status
IChronoSvc::ChronoStatus m_status
current status of this chrono object;
Definition: ChronoEntity.h:157
ChronoEntity::uTotalTime
double uTotalTime() const
total user time
Definition: ChronoEntity.h:207
ChronoEntity::m_elapsed
StatEntity m_elapsed
the actual storage of "elapsed" time
Definition: ChronoEntity.h:167
StatEntity::flagRMS
double flagRMS() const
r.m.s of flag
Definition: StatEntity.h:402
ChronoEntity
a small helper class for implementation of ChronoStatSvc service, It also could be used as some local...
Definition: ChronoEntity.h:21
System::ProcessTime::kernelTime
TimeValueType kernelTime() const
Retrieve the kernel time in the requested unit.
Definition: Timing.h:160
StatEntity::flagMin
double flagMin() const
minimal flag
Definition: StatEntity.h:406
System::Min
Definition: Timing.h:58
ChronoEntity::kTotalTime
double kTotalTime() const
total Kernel time
Definition: ChronoEntity.h:211
Gaudi::Units::microsecond
constexpr double microsecond
Definition: SystemOfUnits.h:125
IChronoSvc::ChronoStatus
ChronoStatus
Definition: IChronoSvc.h:46
System::ProcessTime::userTime
TimeValueType userTime() const
Retrieve the user time in the requested unit.
Definition: Timing.h:167
ChronoEntity::m_user
StatEntity m_user
the actual storage of "user" time
Definition: ChronoEntity.h:163
ChronoEntity::uRMSTime
double uRMSTime() const
r.m.s User Time
Definition: ChronoEntity.h:239
std::make_tuple
T make_tuple(T...args)
std::end
T end(T...args)
IChronoSvc::STOPPED
Definition: IChronoSvc.h:46
IChronoSvc::KERNEL
Definition: IChronoSvc.h:48
ChronoEntity::kMinimalTime
double kMinimalTime() const
minimal measurement for kernel time
Definition: ChronoEntity.h:187
StatEntity::flagMeanErr
double flagMeanErr() const
error in mean value of flag
Definition: StatEntity.h:404
ChronoEntity::eMinimalTime
double eMinimalTime() const
minimal measurement for elapsed time
Definition: ChronoEntity.h:191
System::Hour
Definition: Timing.h:58
ChronoEntity::kRMSTime
double kRMSTime() const
r.m.s Kernel Time
Definition: ChronoEntity.h:235
System::milliSec
Definition: Timing.h:58
ChronoEntity::outputSystemTime
std::string outputSystemTime() const
print the chrono ;
Definition: ChronoEntity.cpp:97
ChronoEntity::start
IChronoSvc::ChronoStatus start()
start the current chrono
Definition: ChronoEntity.cpp:47
System::Sec
Definition: Timing.h:58
StatEntity::flagMax
double flagMax() const
maximal flag
Definition: StatEntity.h:408
ChronoEntity::operator+=
ChronoEntity & operator+=(const ChronoEntity &entity)
Compound assignment operator.
Definition: ChronoEntity.cpp:172
std::prev
T prev(T...args)
Gaudi::Units::second
constexpr double second
Definition: SystemOfUnits.h:123
std::string
STL class.
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:199
IChronoSvc::USER
Definition: IChronoSvc.h:48
ChronoEntity::m_start
System::ProcessTime m_start
start stamp for current measurement of process times
Definition: ChronoEntity.h:161
ChronoEntity::m_delta
System::ProcessTime m_delta
delta process times
Definition: ChronoEntity.h:159
ChronoEntity::kMeanTime
double kMeanTime() const
average Kernel Time
Definition: ChronoEntity.h:223
ChronoEntity::eMeanTime
double eMeanTime() const
average Elapsed Time
Definition: ChronoEntity.h:231
System::microSec
Definition: Timing.h:58
GaudiPython.HistoUtils.mean
mean
Definition: HistoUtils.py:838
System::TimeType
TimeType
Time type for conversion.
Definition: Timing.h:58
StatEntity::flagMean
double flagMean() const
mean value of flag
Definition: StatEntity.h:400
ChronoEntity::m_kernel
StatEntity m_kernel
the actual storage of "kernel" time
Definition: ChronoEntity.h:165
ChronoEntity::eRMSTime
double eRMSTime() const
r.m.s Elapsed Time
Definition: ChronoEntity.h:243
IChronoSvc::ELAPSED
Definition: IChronoSvc.h:48
System::Month
Definition: Timing.h:58
std::tuple
ChronoEntity::uMeanTime
double uMeanTime() const
average User Time
Definition: ChronoEntity.h:227
ChronoEntity::outputUserTime
std::string outputUserTime() const
print the chrono ;
Definition: ChronoEntity.cpp:89
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:112
std::find_if
T find_if(T...args)
ChronoEntity::totalTime
double totalTime() const
total time
Definition: ChronoEntity.h:219
System::nanoSec
Definition: Timing.h:58
ChronoEntity::operator<
bool operator<(const ChronoEntity &entity) const
comparison operator
Definition: ChronoEntity.cpp:150
System::ProcessTime::elapsedTime
TimeValueType elapsedTime() const
Retrieve the elapsed time in the requested unit.
Definition: Timing.h:174
Gaudi::Units::nanosecond
constexpr double nanosecond
Definition: SystemOfUnits.h:122
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:208
ChronoEntity::eMaximalTime
double eMaximalTime() const
maximal measurement for elapsed time
Definition: ChronoEntity.h:203
Gaudi::Units::millisecond
constexpr double millisecond
Definition: SystemOfUnits.h:124
IChronoSvc::ChronoType
ChronoType
Definition: IChronoSvc.h:48
System::Year
Definition: Timing.h:58
ChronoEntity::TimeUnit
static const System::TimeType TimeUnit
internal unit used for the system time conversion (microseconds)
Definition: ChronoEntity.h:169
ChronoEntity::nOfMeasurements
unsigned long nOfMeasurements() const
number of chrono measurements
Definition: ChronoEntity.h:179
StatEntity
The basic counter used for Monitoring purposes.
Definition: StatEntity.h:65
ChronoEntity::stop
IChronoSvc::ChronoStatus stop()
stop the chrono
Definition: ChronoEntity.cpp:65
ChronoEntity::uMaximalTime
double uMaximalTime() const
maximal measurement for user time
Definition: ChronoEntity.h:195
IChronoSvc::RUNNING
Definition: IChronoSvc.h:46
ChronoEntity.h
ChronoEntity::eTotalTime
double eTotalTime() const
total Elapsed time
Definition: ChronoEntity.h:215
StatEntity::flag
double flag() const
accumulated "flag"
Definition: StatEntity.h:396
System::Day
Definition: Timing.h:58
GaudiPython.HistoUtils.rms
rms
Definition: HistoUtils.py:842