| The Gaudi Framework v27r1

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