CoinUtils  2.10.13
CoinTime.hpp
Go to the documentation of this file.
1 /* $Id: CoinTime.hpp 1372 2011-01-03 23:31:00Z lou $ */
2 // Copyright (C) 2002, International Business Machines
3 // Corporation and others. All Rights Reserved.
4 // This code is licensed under the terms of the Eclipse Public License (EPL).
5 
6 #ifndef _CoinTime_hpp
7 #define _CoinTime_hpp
8 
9 // Uncomment the next three lines for thorough memory initialisation.
10 // #ifndef ZEROFAULT
11 // # define ZEROFAULT
12 // #endif
13 
14 //#############################################################################
15 
16 #include <ctime>
17 #if defined(_MSC_VER)
18 // Turn off compiler warning about long names
19 # pragma warning(disable:4786)
20 #else
21 // MacOS-X and FreeBSD needs sys/time.h
22 #if defined(__MACH__) || defined (__FreeBSD__)
23 #include <sys/time.h>
24 #endif
25 #if !defined(__MSVCRT__)
26 #ifdef __KLIBC__
27 #include <sys/types.h>
28 #endif
29 #include <sys/resource.h>
30 #endif
31 #endif
32 
33 //#############################################################################
34 
35 #if defined(_MSC_VER)
36 
37 #if 0 // change this to 1 if want to use the win32 API
38 #include <windows.h>
39 #ifdef small
40 /* for some unfathomable reason (to me) rpcndr.h (pulled in by windows.h) does a
41  '#define small char' */
42 #undef small
43 #endif
44 #define TWO_TO_THE_THIRTYTWO 4294967296.0
45 #define DELTA_EPOCH_IN_SECS 11644473600.0
46 inline double CoinGetTimeOfDay()
47 {
48  FILETIME ft;
49 
50  GetSystemTimeAsFileTime(&ft);
51  double t = ft.dwHighDateTime * TWO_TO_THE_THIRTYTWO + ft.dwLowDateTime;
52  t = t/10000000.0 - DELTA_EPOCH_IN_SECS;
53  return t;
54 }
55 #else
56 #include <sys/types.h>
57 #include <sys/timeb.h>
58 inline double CoinGetTimeOfDay()
59 {
60  struct _timeb timebuffer;
61 #pragma warning(disable:4996)
62  _ftime( &timebuffer ); // C4996
63 #pragma warning(default:4996)
64  return timebuffer.time + timebuffer.millitm/1000.0;
65 }
66 #endif
67 
68 #else
69 
70 #include <sys/time.h>
71 
72 inline double CoinGetTimeOfDay()
73 {
74  struct timeval tv;
75  gettimeofday(&tv, NULL);
76  return static_cast<double>(tv.tv_sec) + static_cast<int>(tv.tv_usec)/1000000.0;
77 }
78 
79 #endif // _MSC_VER
80 
89 inline double CoinWallclockTime(double callType = 0)
90 {
91  double callTime = CoinGetTimeOfDay();
92  static const double firstCall = callType > 0 ? callType : callTime;
93  return callType < 0 ? firstCall : callTime - firstCall;
94 }
95 
96 //#############################################################################
97 
98 //#define HAVE_SDK // if SDK under Win32 is installed, for CPU instead of elapsed time under Win
99 #ifdef HAVE_SDK
100 #include <windows.h>
101 #ifdef small
102 /* for some unfathomable reason (to me) rpcndr.h (pulled in by windows.h) does a
103  '#define small char' */
104 #undef small
105 #endif
106 #define TWO_TO_THE_THIRTYTWO 4294967296.0
107 #endif
108 
109 static inline double CoinCpuTime()
110 {
111  double cpu_temp;
112 #if defined(_MSC_VER) || defined(__MSVCRT__)
113 #ifdef HAVE_SDK
114  FILETIME creation;
115  FILETIME exit;
116  FILETIME kernel;
117  FILETIME user;
118  GetProcessTimes(GetCurrentProcess(), &creation, &exit, &kernel, &user);
119  double t = user.dwHighDateTime * TWO_TO_THE_THIRTYTWO + user.dwLowDateTime;
120  return t/10000000.0;
121 #else
122  unsigned int ticksnow; /* clock_t is same as int */
123  ticksnow = (unsigned int)clock();
124  cpu_temp = (double)((double)ticksnow/CLOCKS_PER_SEC);
125 #endif
126 
127 #elif defined(__KLIBC__)
128 
129  unsigned int ticksnow; /* clock_t is same as int */
130  ticksnow = (unsigned int)clock();
131  cpu_temp = (double)((double)ticksnow/CLOCKS_PER_SEC);
132 
133 #else
134  struct rusage usage;
135 # ifdef ZEROFAULT
136  usage.ru_utime.tv_sec = 0 ;
137  usage.ru_utime.tv_usec = 0 ;
138 # endif
139  getrusage(RUSAGE_SELF,&usage);
140  cpu_temp = static_cast<double>(usage.ru_utime.tv_sec);
141  cpu_temp += 1.0e-6*(static_cast<double> (usage.ru_utime.tv_usec));
142 #endif
143  return cpu_temp;
144 }
145 
146 //#############################################################################
147 
148 
149 
150 static inline double CoinSysTime()
151 {
152  double sys_temp;
153 #if defined(_MSC_VER) || defined(__MSVCRT__) || defined(__KLIBC__)
154  sys_temp = 0.0;
155 #else
156  struct rusage usage;
157 # ifdef ZEROFAULT
158  usage.ru_utime.tv_sec = 0 ;
159  usage.ru_utime.tv_usec = 0 ;
160 # endif
161  getrusage(RUSAGE_SELF,&usage);
162  sys_temp = static_cast<double>(usage.ru_stime.tv_sec);
163  sys_temp += 1.0e-6*(static_cast<double> (usage.ru_stime.tv_usec));
164 #endif
165  return sys_temp;
166 }
167 
168 //#############################################################################
169 // On most systems SELF seems to include children threads, This is for when it doesn't
170 static inline double CoinCpuTimeJustChildren()
171 {
172  double cpu_temp;
173 #if defined(_MSC_VER) || defined(__MSVCRT__) || defined(__KLIBC__)
174  cpu_temp = 0.0;
175 #else
176  struct rusage usage;
177 # ifdef ZEROFAULT
178  usage.ru_utime.tv_sec = 0 ;
179  usage.ru_utime.tv_usec = 0 ;
180 # endif
181  getrusage(RUSAGE_CHILDREN,&usage);
182  cpu_temp = static_cast<double>(usage.ru_utime.tv_sec);
183  cpu_temp += 1.0e-6*(static_cast<double> (usage.ru_utime.tv_usec));
184 #endif
185  return cpu_temp;
186 }
187 //#############################################################################
188 
189 #include <fstream>
190 
207 {
208 private:
210  double start;
212  double limit;
213  double end;
214 #ifdef COIN_COMPILE_WITH_TRACING
215  std::fstream* stream;
216  bool write_stream;
217 #endif
218 
219 private:
220 #ifdef COIN_COMPILE_WITH_TRACING
221  inline bool evaluate(bool b_tmp) const {
222  int i_tmp = b_tmp;
223  if (stream) {
224  if (write_stream)
225  (*stream) << i_tmp << "\n";
226  else
227  (*stream) >> i_tmp;
228  }
229  return i_tmp;
230  }
231  inline double evaluate(double d_tmp) const {
232  if (stream) {
233  if (write_stream)
234  (*stream) << d_tmp << "\n";
235  else
236  (*stream) >> d_tmp;
237  }
238  return d_tmp;
239  }
240 #else
241  inline bool evaluate(const bool b_tmp) const {
242  return b_tmp;
243  }
244  inline double evaluate(const double d_tmp) const {
245  return d_tmp;
246  }
247 #endif
248 
249 public:
252  start(0), limit(1e100), end(1e100)
253 #ifdef COIN_COMPILE_WITH_TRACING
254  , stream(0), write_stream(true)
255 #endif
256  {}
257 
259  CoinTimer(double lim) :
260  start(CoinCpuTime()), limit(lim), end(start+lim)
261 #ifdef COIN_COMPILE_WITH_TRACING
262  , stream(0), write_stream(true)
263 #endif
264  {}
265 
266 #ifdef COIN_COMPILE_WITH_TRACING
267 
269  CoinTimer(std::fstream* s, bool write) :
270  start(0), limit(1e100), end(1e100),
271  stream(s), write_stream(write) {}
272 
275  CoinTimer(double lim, std::fstream* s, bool w) :
276  start(CoinCpuTime()), limit(lim), end(start+lim),
277  stream(s), write_stream(w) {}
278 #endif
279 
281  inline void restart() { start=CoinCpuTime(); end=start+limit; }
283  inline void reset() { restart(); }
285  inline void reset(double lim) { limit=lim; restart(); }
286 
289  inline bool isPastPercent(double pct) const {
290  return evaluate(start + limit * pct < CoinCpuTime());
291  }
294  inline bool isPast(double lim) const {
295  return evaluate(start + lim < CoinCpuTime());
296  }
299  inline bool isExpired() const {
300  return evaluate(end < CoinCpuTime());
301  }
302 
304  inline double timeLeft() const {
305  return evaluate(end - CoinCpuTime());
306  }
307 
309  inline double timeElapsed() const {
310  return evaluate(CoinCpuTime() - start);
311  }
312 
313  inline void setLimit(double l) {
314  limit = l;
315  return;
316  }
317 };
318 
319 #endif
void restart()
Restart the timer (keeping the same time limit)
Definition: CoinTime.hpp:281
double timeLeft() const
Return how much time is left on the timer.
Definition: CoinTime.hpp:304
double timeElapsed() const
Return how much time has elapsed.
Definition: CoinTime.hpp:309
bool isPast(double lim) const
Return whether the given amount of time has elapsed since the timer was started.
Definition: CoinTime.hpp:294
static double CoinCpuTime()
Definition: CoinTime.hpp:109
bool isExpired() const
Return whether the originally specified time limit has passed since the timer was started...
Definition: CoinTime.hpp:299
This class implements a timer that also implements a tracing functionality.
Definition: CoinTime.hpp:206
static double CoinCpuTimeJustChildren()
Definition: CoinTime.hpp:170
double CoinGetTimeOfDay()
Definition: CoinTime.hpp:72
CoinTimer(double lim)
Create a timer with the given time limit and with no tracing.
Definition: CoinTime.hpp:259
void reset()
An alternate name for restart()
Definition: CoinTime.hpp:283
void setLimit(double l)
Definition: CoinTime.hpp:313
double CoinWallclockTime(double callType=0)
Query the elapsed wallclock time since the first call to this function.
Definition: CoinTime.hpp:89
bool isPastPercent(double pct) const
Return whether the given percentage of the time limit has elapsed since the timer was started...
Definition: CoinTime.hpp:289
void reset(double lim)
Reset (and restart) the timer and change its time limit.
Definition: CoinTime.hpp:285
CoinTimer()
Default constructor creates a timer with no time limit and no tracing.
Definition: CoinTime.hpp:251
static double CoinSysTime()
Definition: CoinTime.hpp:150