gravatar
deba@inf.elte.hu
deba@inf.elte.hu
MinGW compatible time measure + changes in its internals
0 1 0
default
1 file changed with 88 insertions and 53 deletions:
↑ Collapse diff ↑
Ignore white space 96 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_TIME_MEASURE_H
20 20
#define LEMON_TIME_MEASURE_H
21 21

	
22 22
///\ingroup timecount
23 23
///\file
24 24
///\brief Tools for measuring cpu usage
25 25

	
26
#ifdef WIN32
27
#include <windows.h>
28
#include <cmath>
29
#else
26 30
#include <sys/times.h>
31
#include <sys/time.h>
32
#endif
27 33

	
28
#include <sys/time.h>
29 34
#include <fstream>
30 35
#include <iostream>
31
#include <unistd.h>
32 36

	
33 37
namespace lemon {
34 38

	
35 39
  /// \addtogroup timecount
36 40
  /// @{
37 41

	
38 42
  /// A class to store (cpu)time instances.
39 43

	
40 44
  /// This class stores five time values.
41 45
  /// - a real time
42 46
  /// - a user cpu time
43 47
  /// - a system cpu time
44 48
  /// - a user cpu time of children
45 49
  /// - a system cpu time of children
46 50
  ///
47 51
  /// TimeStamp's can be added to or substracted from each other and
48 52
  /// they can be pushed to a stream.
49 53
  ///
50 54
  /// In most cases, perhaps the \ref Timer or the \ref TimeReport
51 55
  /// class is what you want to use instead.
52 56
  ///
53 57
  ///\author Alpar Juttner
54 58

	
55 59
  class TimeStamp
56 60
  {
57
    struct rtms 
58
    {
59
      double tms_utime;
60
      double tms_stime;
61
      double tms_cutime;
62
      double tms_cstime;
63
      rtms() {}
64
      rtms(tms ts) : tms_utime(ts.tms_utime), tms_stime(ts.tms_stime),
65
		     tms_cutime(ts.tms_cutime), tms_cstime(ts.tms_cstime) {}
66
    };
67
    rtms ts;
68
    double real_time;
61
    double utime;
62
    double stime;
63
    double cutime;
64
    double cstime;
65
    double rtime;
69 66
  
70
    rtms &getTms() {return ts;}
71
    const rtms &getTms() const {return ts;}
72

	
73 67
    void _reset() { 
74
      ts.tms_utime = ts.tms_stime = ts.tms_cutime = ts.tms_cstime = 0; 
75
      real_time = 0;
68
      utime = stime = cutime = cstime = rtime = 0;
76 69
    }
77 70

	
78 71
  public:
79 72

	
80 73
    ///Read the current time values of the process
81 74
    void stamp()
82 75
    {
76
#ifndef WIN32
83 77
      timeval tv;
84
      tms _ts;
85
      times(&_ts);
86
      gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
87
      ts=_ts;
78
      gettimeofday(&tv, 0);
79
      rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
80

	
81
      tms ts;
82
      double tck=sysconf(_SC_CLK_TCK);
83
      times(&ts);
84
      utime=ts.tms_utime/tck;
85
      stime=ts.tms_stime/tck;
86
      cutime=ts.tms_cutime/tck;
87
      cstime=ts.tms_cstime/tck;
88
#else
89
      static const double ch = 4294967296.0e-7;
90
      static const double cl = 1.0e-7;
91

	
92
      FILETIME system;
93
      GetSystemTimeAsFileTime(&system);
94
      rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
95

	
96
      FILETIME create, exit, kernel, user;
97
      if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
98
	utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
99
	stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
100
	cutime = 0;
101
	cstime = 0;
102
      } else {
103
	rtime = 0;
104
	utime = 0;
105
	stime = 0;
106
	cutime = 0;
107
	cstime = 0;
108
      }
109
#endif      
88 110
    }
89 111
  
90 112
    /// Constructor initializing with zero
91 113
    TimeStamp()
92 114
    { _reset(); }
93 115
    ///Constructor initializing with the current time values of the process
94 116
    TimeStamp(void *) { stamp();}
95 117
  
96 118
    ///Set every time value to zero
97 119
    TimeStamp &reset() {_reset();return *this;}
98 120

	
99 121
    ///\e
100 122
    TimeStamp &operator+=(const TimeStamp &b)
101 123
    {
102
      ts.tms_utime+=b.ts.tms_utime;
103
      ts.tms_stime+=b.ts.tms_stime;
104
      ts.tms_cutime+=b.ts.tms_cutime;
105
      ts.tms_cstime+=b.ts.tms_cstime;
106
      real_time+=b.real_time;
124
      utime+=b.utime;
125
      stime+=b.stime;
126
      cutime+=b.cutime;
127
      cstime+=b.cstime;
128
      rtime+=b.rtime;
107 129
      return *this;
108 130
    }
109 131
    ///\e
110 132
    TimeStamp operator+(const TimeStamp &b) const
111 133
    {
112 134
      TimeStamp t(*this);
113 135
      return t+=b;
114 136
    }
115 137
    ///\e
116 138
    TimeStamp &operator-=(const TimeStamp &b)
117 139
    {
118
      ts.tms_utime-=b.ts.tms_utime;
119
      ts.tms_stime-=b.ts.tms_stime;
120
      ts.tms_cutime-=b.ts.tms_cutime;
121
      ts.tms_cstime-=b.ts.tms_cstime;
122
      real_time-=b.real_time;
140
      utime-=b.utime;
141
      stime-=b.stime;
142
      cutime-=b.cutime;
143
      cstime-=b.cstime;
144
      rtime-=b.rtime;
123 145
      return *this;
124 146
    }
125 147
    ///\e
126 148
    TimeStamp operator-(const TimeStamp &b) const
127 149
    {
128 150
      TimeStamp t(*this);
129 151
      return t-=b;
130 152
    }
131 153
    ///\e
132 154
    TimeStamp &operator*=(double b)
133 155
    {
134
      ts.tms_utime*=b;
135
      ts.tms_stime*=b;
136
      ts.tms_cutime*=b;
137
      ts.tms_cstime*=b;
138
      real_time*=b;
156
      utime*=b;
157
      stime*=b;
158
      cutime*=b;
159
      cstime*=b;
160
      rtime*=b;
139 161
      return *this;
140 162
    }
141 163
    ///\e
142 164
    TimeStamp operator*(double b) const
143 165
    {
144 166
      TimeStamp t(*this);
145 167
      return t*=b;
146 168
    }
147 169
    friend TimeStamp operator*(double b,const TimeStamp &t);
148 170
    ///\e
149 171
    TimeStamp &operator/=(double b)
150 172
    {
151
      ts.tms_utime/=b;
152
      ts.tms_stime/=b;
153
      ts.tms_cutime/=b;
154
      ts.tms_cstime/=b;
155
      real_time/=b;
173
      utime/=b;
174
      stime/=b;
175
      cutime/=b;
176
      cstime/=b;
177
      rtime/=b;
156 178
      return *this;
157 179
    }
158 180
    ///\e
159 181
    TimeStamp operator/(double b) const
160 182
    {
161 183
      TimeStamp t(*this);
162 184
      return t/=b;
163 185
    }
164 186
    ///The time ellapsed since the last call of stamp()
165 187
    TimeStamp ellapsed() const
166 188
    {
167 189
      TimeStamp t(NULL);
168 190
      return t-*this;
169 191
    }
170 192
  
171 193
    friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
172 194
  
173 195
    ///Gives back the user time of the process
174 196
    double userTime() const
175 197
    {
176
      return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
198
      return utime;
177 199
    }
178 200
    ///Gives back the system time of the process
179 201
    double systemTime() const
180 202
    {
181
      return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
203
      return stime;
182 204
    }
183 205
    ///Gives back the user time of the process' children
206

	
207
    ///\note On <tt>WIN32</tt> platform this value is not calculated. 
208
    ///
184 209
    double cUserTime() const
185 210
    {
186
      return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
211
      return cutime;
187 212
    }
188 213
    ///Gives back the user time of the process' children
214

	
215
    ///\note On <tt>WIN32</tt> platform this value is not calculated. 
216
    ///
189 217
    double cSystemTime() const
190 218
    {
191
      return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
219
      return cstime;
192 220
    }
193 221
    ///Gives back the real time
194
    double realTime() const {return real_time;}
222
    double realTime() const {return rtime;}
195 223
  };
196 224

	
197 225
  TimeStamp operator*(double b,const TimeStamp &t) 
198 226
  {
199 227
    return t*b;
200 228
  }
201 229
  
202 230
  ///Prints the time counters
203 231

	
204 232
  ///Prints the time counters in the following form:
205 233
  ///
206 234
  /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
207 235
  ///
208 236
  /// where the values are the
209 237
  /// \li \c u: user cpu time,
210 238
  /// \li \c s: system cpu time,
211 239
  /// \li \c cu: user cpu time of children,
212 240
  /// \li \c cs: system cpu time of children,
213 241
  /// \li \c real: real time.
214 242
  /// \relates TimeStamp
243
  /// \note On <tt>WIN32</tt> platform the cummulative values are not
244
  /// calculated.
215 245
  inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
216 246
  {
217
    long cls = sysconf(_SC_CLK_TCK);
218
    os << "u: " << double(t.getTms().tms_utime)/cls <<
219
      "s, s: " << double(t.getTms().tms_stime)/cls <<
220
      "s, cu: " << double(t.getTms().tms_cutime)/cls <<
221
      "s, cs: " << double(t.getTms().tms_cstime)/cls <<
247
    os << "u: " << t.userTime() <<
248
      "s, s: " << t.systemTime() <<
249
      "s, cu: " << t.cUserTime() <<
250
      "s, cs: " << t.cSystemTime() <<
222 251
      "s, real: " << t.realTime() << "s";
223 252
    return os;
224 253
  }
225 254

	
226 255
  ///Class for measuring the cpu time and real time usage of the process
227 256

	
228 257
  ///Class for measuring the cpu time and real time usage of the process.
229 258
  ///It is quite easy-to-use, here is a short example.
230 259
  ///\code
231 260
  /// #include<lemon/time_measure.h>
232 261
  /// #include<iostream>
233 262
  ///
234 263
  /// int main()
235 264
  /// {
236 265
  ///
237 266
  ///   ...
238 267
  ///
239 268
  ///   Timer t;
240 269
  ///   doSomething();
241 270
  ///   std::cout << t << '\n';
242 271
  ///   t.restart();
243 272
  ///   doSomethingElse();
244 273
  ///   std::cout << t << '\n';
245 274
  ///
246 275
  ///   ...
247 276
  ///
248 277
  /// }
249 278
  ///\endcode
250 279
  ///
251 280
  ///The \ref Timer can also be \ref stop() "stopped" and
252 281
  ///\ref start() "started" again, so it is possible to compute collected
253 282
  ///running times.
254 283
  ///
255 284
  ///\warning Depending on the operation system and its actual configuration
256 285
  ///the time counters have a certain (10ms on a typical Linux system)
257 286
  ///granularity.
258 287
  ///Therefore this tool is not appropriate to measure very short times.
259 288
  ///Also, if you start and stop the timer very frequently, it could lead to
260 289
  ///distorted results.
261 290
  ///
262 291
  ///\note If you want to measure the running time of the execution of a certain
263 292
  ///function, consider the usage of \ref TimeReport instead.
264 293
  ///
265 294
  ///\todo This shouldn't be Unix (Linux) specific.
266 295
  ///\sa TimeReport
267 296
  ///
268 297
  ///\author Alpar Juttner
269 298
  class Timer
... ...
@@ -359,101 +388,107 @@
359 388
    {
360 389
      if(_running) {
361 390
	_running=0;
362 391
	TimeStamp t;
363 392
	t.stamp();
364 393
	start_time=t-start_time;
365 394
      }
366 395
    }
367 396

	
368 397
    ///Returns the running state of the timer
369 398

	
370 399
    ///This function returns the number of stop() exections that is
371 400
    ///necessary to really stop the timer.
372 401
    ///For example the timer
373 402
    ///is running if and only if the return value is \c true
374 403
    ///(i.e. greater than
375 404
    ///zero).
376 405
    int running()  { return _running; }
377 406
    
378 407
    
379 408
    ///Restart the time counters
380 409

	
381 410
    ///This function is a shorthand for
382 411
    ///a reset() and a start() calls.
383 412
    ///
384 413
    void restart() 
385 414
    {
386 415
      reset();
387 416
      start();
388 417
    }
389 418
    
390 419
    ///@}
391 420

	
392 421
    ///\name Query Functions for the ellapsed time
393 422

	
394 423
    ///@{
395 424

	
396 425
    ///Gives back the ellapsed user time of the process
397 426
    double userTime() const
398 427
    {
399 428
      return operator TimeStamp().userTime();
400 429
    }
401 430
    ///Gives back the ellapsed system time of the process
402 431
    double systemTime() const
403 432
    {
404 433
      return operator TimeStamp().systemTime();
405 434
    }
406 435
    ///Gives back the ellapsed user time of the process' children
436

	
437
    ///\note On <tt>WIN32</tt> platform this value is not calculated. 
438
    ///
407 439
    double cUserTime() const
408 440
    {
409 441
      return operator TimeStamp().cUserTime();
410 442
    }
411 443
    ///Gives back the ellapsed user time of the process' children
444

	
445
    ///\note On <tt>WIN32</tt> platform this value is not calculated. 
446
    ///
412 447
    double cSystemTime() const
413 448
    {
414 449
      return operator TimeStamp().cSystemTime();
415 450
    }
416 451
    ///Gives back the ellapsed real time
417 452
    double realTime() const
418 453
    {
419 454
      return operator TimeStamp().realTime();
420 455
    }
421 456
    ///Computes the ellapsed time
422 457

	
423 458
    ///This conversion computes the ellapsed time, therefore you can print
424 459
    ///the ellapsed time like this.
425 460
    ///\code
426 461
    ///  Timer t;
427 462
    ///  doSomething();
428 463
    ///  std::cout << t << '\n';
429 464
    ///\endcode
430 465
    operator TimeStamp () const
431 466
    {
432 467
      TimeStamp t;
433 468
      t.stamp();
434 469
      return _running?t-start_time:start_time;
435 470
    }
436 471

	
437 472

	
438 473
    ///@}
439 474
  };
440 475

	
441 476
  ///Same as \ref Timer but prints a report on destruction.
442 477

	
443 478
  ///Same as \ref Timer but prints a report on destruction.
444 479
  ///This example shows its usage.
445 480
  ///\code
446 481
  ///  void myAlg(ListGraph &g,int n)
447 482
  ///  {
448 483
  ///    TimeReport tr("Running time of myAlg: ");
449 484
  ///    ... //Here comes the algorithm
450 485
  ///  }
451 486
  ///\endcode
452 487
  ///
453 488
  ///\sa Timer
454 489
  ///\sa NoTimeReport
455 490
  ///\todo There is no test case for this
456 491
  class TimeReport : public Timer 
457 492
  {
458 493
    std::string _title;
459 494
    std::ostream &_os;
0 comments (0 inline)