libzypp: zypp/base/Measure.cc Source File

libzypp 17.25.10

Measure.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 extern "C"
13 {
14 #include <sys/times.h>
15 #include <unistd.h>
16 }
17 #include <iostream>
18
19 #include <zypp/base/Logger.h>
20 #include <zypp/base/Measure.h>
21 #include <zypp/base/String.h>
22
23 using std::endl;
24
25 #undef ZYPP_BASE_LOGGER_LOGGROUP
26 #define ZYPP_BASE_LOGGER_LOGGROUP "Measure"
27
29 namespace zypp
30 {
32 namespace debug
33 {
34
36 struct Tm
37 {
38 Tm()
39 : _real( 0 )
40 , _proc( tmsEmpty )
41 {}
42
43 void get()
44 {
45 _real = ::time(NULL);
46 ::times( &_proc );
47 }
48
49 Tm operator-( const Tm & rhs ) const
50 {
51 Tm ret( *this );
52 ret._real -= rhs._real;
53 ret._proc.tms_utime -= rhs._proc.tms_utime;
54 ret._proc.tms_stime -= rhs._proc.tms_stime;
55 ret._proc.tms_cutime -= rhs._proc.tms_cutime;
56 ret._proc.tms_cstime -= rhs._proc.tms_cstime;
57 return ret;
58 }
59
60 std::string asString() const
61 {
62 std::string ret( timeStr( _real ) );
63 ret += " (u ";
64 ret += timeStr( asSec( _proc.tms_utime ) );
65 ret += " s ";
66 ret += timeStr( asSec( _proc.tms_stime ) );
67 ret += " c ";
68 ret += timeStr( asSec( _proc.tms_cutime + _proc.tms_cstime ) );
69 ret += ")";
70 return ret;
71 }
72
73 std::string stringIf( clock_t ticks_r, const std::string & tag_r ) const
74 {
75 std::string ret;
76 if ( ticks_r )
77 {
78 ret += tag_r;
79 ret += timeStr( asSec( ticks_r ) );
80 }
81 return ret;
82 }
83
84 double asSec( clock_t ticks_r ) const
85 { return double(ticks_r) / ticks; }
86
87 std::string timeStr( time_t sec_r ) const
88 {
89 time_t h = sec_r/3600;
90 sec_r -= h*3600;
91 time_t m = sec_r/60;
92 sec_r -= m*60;
93 if ( h )
94 return str::form( "%lu:%02lu:%02lu", h, m, sec_r );
95 if ( m )
96 return str::form( "%lu:%02lu", m, sec_r );
97 return str::form( "%lu", sec_r );
98 }
99
100 std::string timeStr( double sec_r ) const
101 {
102 time_t h = time_t(sec_r)/3600;
103 sec_r -= h*3600;
104 time_t m = time_t(sec_r)/60;
105 sec_r -= m*60;
106 if ( h )
107 return str::form( "%lu:%02lu:%05.2lf", h, m, sec_r );
108 if ( m )
109 return str::form( "%lu:%05.2lf", m, sec_r );
110 return str::form( "%.2lf", sec_r );
111 }
112
114 static const long ticks;
116 static const struct tms tmsEmpty;
118 time_t _real;
120 struct tms _proc;
121 };
122
123 const struct tms Tm::tmsEmpty = { 0, 0, 0, 0 };
124 const long Tm::ticks = sysconf(_SC_CLK_TCK);
125
127 std::ostream & operator<<( std::ostream & str, const Tm & obj )
128 {
129 return str << obj.asString();
130 }
131
132
134 //
135 // CLASS NAME : Measure::Impl
136 //
138 class Measure::Impl
139 {
140 public:
141 Impl( const std::string & ident_r, std::ostream * log_r = nullptr )
142 : _ident ( ident_r )
143 , _level ( _glevel )
144 , _seq ( 0 )
145 , _log ( log_r )
146 {
147 _glevel += "..";
148 log() << _level << "START MEASURE(" << _ident << ")" << endl;
149 _start.get();
150 }
151
152 ~Impl()
153 {
154 _stop.get();
155 ++_seq;
156 std::ostream & str( log() << _level << "MEASURE(" << _ident << ") " );
157 dumpMeasure( str );
158 _glevel.erase( 0, 2 );
159 }
160
161 void restart()
162 {
163 log() << _level << "RESTART MEASURE(" << _ident << ")" << endl;
164 _start = _stop;
165 }
166
167 void elapsed( const std::string & tag_r = std::string() ) const
168 {
169 _stop.get();
170 ++_seq;
171 std::ostream & str( log() << _level << "ELAPSED(" << _ident << ") " );
172 dumpMeasure( str, tag_r );
173 _elapsed = _stop;
174 }
175
177 std::ostream & log() const
178 { return _log ? *_log : INT; }
179 std::ostream * logp() const
180 { return _log; }
181
182 private:
183 std::ostream & dumpMeasure( std::ostream & str_r, const std::string & tag_r = std::string() ) const
184 {
185 str_r << ( _stop - _start );
186 if ( _seq > 1 ) // diff to previous _elapsed
187 {
188 str_r << " [" << ( _stop - _elapsed ) << "]";
189 }
190 if ( ! tag_r.empty() )
191 str_r << " - " << tag_r;
192 return str_r << endl;
193 }
194
195 private:
196 static std::string _glevel;
197
198 std::string _ident;
199 std::string _level;
200 Tm _start;
201 mutable unsigned _seq;
202 mutable Tm _elapsed;
203 mutable Tm _stop;
204
205 std::ostream * _log = nullptr;
206 };
207
208 std::string Measure::Impl::_glevel;
209
211
213 //
214 // CLASS NAME : Measure
215 //
217
218 Measure::Measure()
219 {}
220
221 Measure::Measure( const std::string & ident_r )
222 : _pimpl( new Impl( ident_r ) )
223 {}
224
225 Measure::Measure( const std::string & ident_r, std::ostream & out_r )
226 : _pimpl( new Impl( ident_r, &out_r ) )
227 {}
228
229 Measure::~Measure()
230 {}
231
232 void Measure::start( const std::string & ident_r )
233 { stop(); _pimpl.reset( _pimpl ? new Impl( ident_r, _pimpl->logp() ) : new Impl( ident_r ) ); }
234
235 void Measure::restart()
236 { _pimpl->restart(); }
237
238 void Measure::elapsed() const
239 { if ( _pimpl ) _pimpl->elapsed(); }
240 void Measure::elapsed( const std::string & tag_r ) const
241 { if ( _pimpl ) _pimpl->elapsed( tag_r ); }
242 void Measure::elapsed( long tag_r ) const
243 { if ( _pimpl ) _pimpl->elapsed( asString( tag_r ) ); }
244
245 void Measure::stop()
246 { _pimpl.reset(); }
247
249 } // namespace debug
252 } // namespace zypp
Logger.h
INT
#define INT
Definition: Logger.h:95
Measure.h
String.h
tmsEmpty
zypp::debug::Measure::Impl
Measure implementation.
Definition: Measure.cc:139
zypp::debug::Measure::Impl::_elapsed
Tm _elapsed
Definition: Measure.cc:202
zypp::debug::Measure::Impl::_stop
Tm _stop
Definition: Measure.cc:203
zypp::debug::Measure::Impl::logp
std::ostream * logp() const
Definition: Measure.cc:179
zypp::debug::Measure::Impl::log
std::ostream & log() const
Return the log stream.
Definition: Measure.cc:177
zypp::debug::Measure::Impl::_log
std::ostream * _log
Definition: Measure.cc:205
zypp::debug::Measure::Impl::_glevel
static std::string _glevel
Definition: Measure.cc:196
zypp::debug::Measure::Impl::~Impl
~Impl()
Definition: Measure.cc:152
zypp::debug::Measure::Impl::elapsed
void elapsed(const std::string &tag_r=std::string()) const
Definition: Measure.cc:167
zypp::debug::Measure::Impl::dumpMeasure
std::ostream & dumpMeasure(std::ostream &str_r, const std::string &tag_r=std::string()) const
Definition: Measure.cc:183
zypp::debug::Measure::Impl::_level
std::string _level
Definition: Measure.cc:199
zypp::debug::Measure::Impl::_start
Tm _start
Definition: Measure.cc:200
zypp::debug::Measure::Impl::Impl
Impl(const std::string &ident_r, std::ostream *log_r=nullptr)
Definition: Measure.cc:141
zypp::debug::Measure::Impl::restart
void restart()
Definition: Measure.cc:161
zypp::debug::Measure::Impl::_ident
std::string _ident
Definition: Measure.cc:198
zypp::debug::Measure::Impl::_seq
unsigned _seq
Definition: Measure.cc:201
zypp::debug::Measure::Measure
Measure()
Default Ctor does nothing.
Definition: Measure.cc:218
zypp::debug::Measure::elapsed
void elapsed() const
Print elapsed time for a running timer.
Definition: Measure.cc:238
zypp::debug::Measure::stop
void stop()
Stop a running timer.
Definition: Measure.cc:245
zypp::debug::Measure::restart
void restart()
re start the timer without reset-ing it.
Definition: Measure.cc:235
zypp::debug::Measure::start
void start(const std::string &ident_r=std::string())
Start timer for ident_r string.
Definition: Measure.cc:232
zypp::debug::Measure::~Measure
~Measure()
Dtor.
Definition: Measure.cc:229
zypp::debug::Measure::_pimpl
RW_pointer< Impl > _pimpl
Pointer to implementation.
Definition: Measure.h:113
str
String related utilities and Regular expression matching.
zypp::debug::operator<<
std::ostream & operator<<(std::ostream &str, const Tm &obj)
\refers Tm Stream output.
Definition: Measure.cc:127
zypp::str::form
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:36
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
zypp::asString
std::string asString(const DefaultIntegral< Tp, TInitial > &obj)
Definition: DefaultIntegral.h:98
zypp::debug::Tm
Times measured by Measure.
Definition: Measure.cc:37
zypp::debug::Tm::stringIf
std::string stringIf(clock_t ticks_r, const std::string &tag_r) const
Definition: Measure.cc:73
zypp::debug::Tm::_real
time_t _real
Real time via ::time.
Definition: Measure.cc:118
zypp::debug::Tm::timeStr
std::string timeStr(double sec_r) const
Definition: Measure.cc:100
zypp::debug::Tm::_proc
struct tms _proc
Process times via ::times.
Definition: Measure.cc:120
zypp::debug::Tm::asString
std::string asString() const
Definition: Measure.cc:60
zypp::debug::Tm::ticks
static const long ticks
Systems ticks per second.
Definition: Measure.cc:114
zypp::debug::Tm::operator-
Tm operator-(const Tm &rhs) const
Definition: Measure.cc:49
zypp::debug::Tm::timeStr
std::string timeStr(time_t sec_r) const
Definition: Measure.cc:87
zypp::debug::Tm::asSec
double asSec(clock_t ticks_r) const
Definition: Measure.cc:84
zypp::debug::Tm::Tm
Tm()
Definition: Measure.cc:38
zypp::debug::Tm::get
void get()
Definition: Measure.cc:43