libzypp  17.34.1
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 {
32  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  //
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 
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;
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 
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 
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 
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 
252  { _pimpl.reset(); }
253 
255  } // namespace debug
258 } // namespace zypp
std::string asString(const Patch::Category &obj)
Definition: Patch.cc:122
std::ostream & dumpMeasure(std::ostream &str_r, const std::string &tag_r=std::string()) const
Definition: Measure.cc:189
void start(std::string ident_r=std::string())
Start timer for ident_r string.
Definition: Measure.cc:238
std::string asString() const
Definition: Measure.cc:61
time_t _real
Real time via ::time.
Definition: Measure.cc:119
void elapsed(const std::string &tag_r=std::string()) const
Definition: Measure.cc:173
void stop()
Stop a running timer.
Definition: Measure.cc:251
RW_pointer< Impl > _pimpl
Pointer to implementation.
Definition: Measure.h:113
#define INT
Definition: Logger.h:102
std::ostream * _log
Definition: Measure.cc:211
double asSec(clock_t ticks_r) const
Definition: Measure.cc:85
String related utilities and Regular expression matching.
Definition: Arch.h:363
std::string timeStr(time_t sec_r) const
Definition: Measure.cc:88
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:37
Tm operator-(const Tm &rhs) const
Definition: Measure.cc:50
void restart()
re start the timer without reset-ing it.
Definition: Measure.cc:241
struct tms _proc
Process times via ::times.
Definition: Measure.cc:121
Measure()
Default Ctor does nothing.
Definition: Measure.cc:224
std::ostream & operator<<(std::ostream &str, const Tm &obj)
Tm Stream output.
Definition: Measure.cc:128
Times measured by Measure.
Definition: Measure.cc:37
Measure implementation.
Definition: Measure.cc:139
void get()
Definition: Measure.cc:44
void elapsed() const
Print elapsed time for a running timer.
Definition: Measure.cc:244
static std::string _glevel
Definition: Measure.cc:202
static const long ticks
Systems ticks per second.
Definition: Measure.cc:115
std::string stringIf(clock_t ticks_r, const std::string &tag_r) const
Definition: Measure.cc:74
Easy-to use interface to the ZYPP dependency resolver.
Definition: Application.cc:19
std::ostream * logp() const
Definition: Measure.cc:185
Impl & operator=(const Impl &)=delete
Impl(std::string &&ident_r, std::ostream *log_r=nullptr)
Definition: Measure.cc:142
std::ostream & log() const
Return the log stream.
Definition: Measure.cc:183
std::string timeStr(double sec_r) const
Definition: Measure.cc:101