1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/Measure.cc
14 #include <sys/times.h>
19 #include <zypp/base/Logger.h>
20 #include <zypp/base/Measure.h>
21 #include <zypp/base/String.h>
25 #undef ZYPP_BASE_LOGGER_LOGGROUP
26 #define ZYPP_BASE_LOGGER_LOGGROUP "Measure"
28 ///////////////////////////////////////////////////////////////////
30 { /////////////////////////////////////////////////////////////////
31 ///////////////////////////////////////////////////////////////////
33 { /////////////////////////////////////////////////////////////////
35 /** Times measured by \ref Measure. */
49 Tm operator-( const Tm & rhs ) const
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;
60 std::string asString() const
62 std::string ret( timeStr( _real ) );
64 ret += timeStr( asSec( _proc.tms_utime ) );
66 ret += timeStr( asSec( _proc.tms_stime ) );
68 ret += timeStr( asSec( _proc.tms_cutime + _proc.tms_cstime ) );
73 std::string stringIf( clock_t ticks_r, const std::string & tag_r ) const
79 ret += timeStr( asSec( ticks_r ) );
84 double asSec( clock_t ticks_r ) const
85 { return double(ticks_r) / ticks; }
87 std::string timeStr( time_t sec_r ) const
89 time_t h = sec_r/3600;
94 return str::form( "%lu:%02lu:%02lu", h, m, sec_r );
96 return str::form( "%lu:%02lu", m, sec_r );
97 return str::form( "%lu", sec_r );
100 std::string timeStr( double sec_r ) const
102 time_t h = time_t(sec_r)/3600;
104 time_t m = time_t(sec_r)/60;
107 return str::form( "%lu:%02lu:%05.2lf", h, m, sec_r );
109 return str::form( "%lu:%05.2lf", m, sec_r );
110 return str::form( "%.2lf", sec_r );
113 /** Systems ticks per second. */
114 static const long ticks;
115 /** Empty struct tms. */
116 static const struct tms tmsEmpty;
117 /** Real time via \c ::time. */
119 /** Process times via \c ::times. */
123 const struct tms Tm::tmsEmpty = { 0, 0, 0, 0 };
124 const long Tm::ticks = sysconf(_SC_CLK_TCK);
126 /** \refers Tm Stream output. */
127 std::ostream & operator<<( std::ostream & str, const Tm & obj )
129 return str << obj.asString();
133 ///////////////////////////////////////////////////////////////////
135 // CLASS NAME : Measure::Impl
137 /** Measure implementation. */
141 Impl( const std::string & ident_r, std::ostream * log_r = nullptr )
148 log() << _level << "START MEASURE(" << _ident << ")" << endl;
156 std::ostream & str( log() << _level << "MEASURE(" << _ident << ") " );
158 _glevel.erase( 0, 2 );
163 log() << _level << "RESTART MEASURE(" << _ident << ")" << endl;
167 void elapsed( const std::string & tag_r = std::string() ) const
171 std::ostream & str( log() << _level << "ELAPSED(" << _ident << ") " );
172 dumpMeasure( str, tag_r );
176 /** Return the log stream. */
177 std::ostream & log() const
178 { return _log ? *_log : INT; }
179 std::ostream * logp() const
183 std::ostream & dumpMeasure( std::ostream & str_r, const std::string & tag_r = std::string() ) const
185 str_r << ( _stop - _start );
186 if ( _seq > 1 ) // diff to previous _elapsed
188 str_r << " [" << ( _stop - _elapsed ) << "]";
190 if ( ! tag_r.empty() )
191 str_r << " - " << tag_r;
192 return str_r << endl;
196 static std::string _glevel;
201 mutable unsigned _seq;
205 std::ostream * _log = nullptr;
208 std::string Measure::Impl::_glevel;
210 ///////////////////////////////////////////////////////////////////
212 ///////////////////////////////////////////////////////////////////
214 // CLASS NAME : Measure
216 ///////////////////////////////////////////////////////////////////
221 Measure::Measure( const std::string & ident_r )
222 : _pimpl( new Impl( ident_r ) )
225 Measure::Measure( const std::string & ident_r, std::ostream & out_r )
226 : _pimpl( new Impl( ident_r, &out_r ) )
232 void Measure::start( const std::string & ident_r )
233 { stop(); _pimpl.reset( _pimpl ? new Impl( ident_r, _pimpl->logp() ) : new Impl( ident_r ) ); }
235 void Measure::restart()
236 { _pimpl->restart(); }
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 ) ); }
248 /////////////////////////////////////////////////////////////////
250 ///////////////////////////////////////////////////////////////////
251 /////////////////////////////////////////////////////////////////
253 ///////////////////////////////////////////////////////////////////