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