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