CoinTime.hpp 8.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
/* $Id$ */
// Copyright (C) 2002, International Business Machines
// Corporation and others.  All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).

#ifndef _CoinTime_hpp
#define _CoinTime_hpp

// Uncomment the next three lines for thorough memory initialisation.
// #ifndef ZEROFAULT
// # define ZEROFAULT
// #endif

//#############################################################################

#include <ctime>
#if defined(_MSC_VER)
// Turn off compiler warning about long names
#pragma warning(disable : 4786)
#else
// MacOS-X and FreeBSD needs sys/time.h
#if defined(__MACH__) || defined(__FreeBSD__)
#include <sys/time.h>
#endif
#if !defined(__MSVCRT__)
#include <sys/resource.h>
#endif
#endif

//#############################################################################

#if defined(_MSC_VER)

#if 0 // change this to 1 if want to use the win32 API
#include <windows.h>
#ifdef small
/* for some unfathomable reason (to me) rpcndr.h (pulled in by windows.h) does a
   '#define small char' */
#undef small
#endif
#define TWO_TO_THE_THIRTYTWO 4294967296.0
#define DELTA_EPOCH_IN_SECS 11644473600.0
inline double CoinGetTimeOfDay()
{
  FILETIME ft;
 
  GetSystemTimeAsFileTime(&ft);
  double t = ft.dwHighDateTime * TWO_TO_THE_THIRTYTWO + ft.dwLowDateTime;
  t = t/10000000.0 - DELTA_EPOCH_IN_SECS;
  return t;
}
#else
#include <sys/types.h>
#include <sys/timeb.h>
inline double CoinGetTimeOfDay()
{
  struct _timeb timebuffer;
#pragma warning(disable : 4996)
  _ftime(&timebuffer); // C4996
#pragma warning(default : 4996)
  return timebuffer.time + timebuffer.millitm / 1000.0;
}
#endif

#else

#include <sys/time.h>

inline double CoinGetTimeOfDay()
{
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return static_cast< double >(tv.tv_sec) + static_cast< int >(tv.tv_usec) / 1000000.0;
}

#endif // _MSC_VER

/**
   Query the elapsed wallclock time since the first call to this function. If
   a positive argument is passed to the function then the time of the first
   call is set to that value (this kind of argument is allowed only at the
   first call!). If a negative argument is passed to the function then it
   returns the time when it was set.
*/

inline double CoinWallclockTime(double callType = 0)
{
  double callTime = CoinGetTimeOfDay();
  static const double firstCall = callType > 0 ? callType : callTime;
  return callType < 0 ? firstCall : callTime - firstCall;
}

//#############################################################################

//#define HAVE_SDK // if SDK under Win32 is installed, for CPU instead of elapsed time under Win
#ifdef HAVE_SDK
#include <windows.h>
#ifdef small
/* for some unfathomable reason (to me) rpcndr.h (pulled in by windows.h) does a
   '#define small char' */
#undef small
#endif
#define TWO_TO_THE_THIRTYTWO 4294967296.0
#endif

static inline double CoinCpuTime()
{
#ifdef COIN_DOING_DIFFS
  // when trying to see differences between runs it can be helpful
  return 0.0;
#endif
  double cpu_temp;
#if defined(_MSC_VER) || defined(__MSVCRT__)
#ifdef HAVE_SDK
  FILETIME creation;
  FILETIME exit;
  FILETIME kernel;
  FILETIME user;
  GetProcessTimes(GetCurrentProcess(), &creation, &exit, &kernel, &user);
  double t = user.dwHighDateTime * TWO_TO_THE_THIRTYTWO + user.dwLowDateTime;
  return t / 10000000.0;
#else
  unsigned int ticksnow; /* clock_t is same as int */
  ticksnow = (unsigned int)clock();
  cpu_temp = (double)((double)ticksnow / CLOCKS_PER_SEC);
#endif

#else
  struct rusage usage;
#ifdef ZEROFAULT
  usage.ru_utime.tv_sec = 0;
  usage.ru_utime.tv_usec = 0;
#endif
  getrusage(RUSAGE_SELF, &usage);
  cpu_temp = static_cast< double >(usage.ru_utime.tv_sec);
  cpu_temp += 1.0e-6 * (static_cast< double >(usage.ru_utime.tv_usec));
#endif
  return cpu_temp;
}

//#############################################################################

static inline double CoinSysTime()
{
  double sys_temp;
#if defined(_MSC_VER) || defined(__MSVCRT__)
  sys_temp = 0.0;
#else
  struct rusage usage;
#ifdef ZEROFAULT
  usage.ru_utime.tv_sec = 0;
  usage.ru_utime.tv_usec = 0;
#endif
  getrusage(RUSAGE_SELF, &usage);
  sys_temp = static_cast< double >(usage.ru_stime.tv_sec);
  sys_temp += 1.0e-6 * (static_cast< double >(usage.ru_stime.tv_usec));
#endif
  return sys_temp;
}

//#############################################################################
// On most systems SELF seems to include children threads, This is for when it doesn't
static inline double CoinCpuTimeJustChildren()
{
  double cpu_temp;
#if defined(_MSC_VER) || defined(__MSVCRT__)
  cpu_temp = 0.0;
#else
  struct rusage usage;
#ifdef ZEROFAULT
  usage.ru_utime.tv_sec = 0;
  usage.ru_utime.tv_usec = 0;
#endif
  getrusage(RUSAGE_CHILDREN, &usage);
  cpu_temp = static_cast< double >(usage.ru_utime.tv_sec);
  cpu_temp += 1.0e-6 * (static_cast< double >(usage.ru_utime.tv_usec));
#endif
  return cpu_temp;
}
//#############################################################################

#include <fstream>

/**
 This class implements a timer that also implements a tracing functionality.

 The timer stores the start time of the timer, for how much time it was set to
 and when does it expire (start + limit = end). Queries can be made that tell
 whether the timer is expired, is past an absolute time, is past a percentage
 of the length of the timer. All times are given in seconds, but as double
 numbers, so there can be fractional values.

 The timer can also be initialized with a stream and a specification whether
 to write to or read from the stream. In the former case the result of every
 query is written into the stream, in the latter case timing is not tested at
 all, rather the supposed result is read out from the stream. This makes it
 possible to exactly retrace time sensitive program execution.
*/
class CoinTimer {
private:
  /// When the timer was initialized/reset/restarted
  double start;
  ///
  double limit;
  double end;
#ifdef COIN_COMPILE_WITH_TRACING
  std::fstream *stream;
  bool write_stream;
#endif

private:
#ifdef COIN_COMPILE_WITH_TRACING
  inline bool evaluate(bool b_tmp) const
  {
    int i_tmp = b_tmp;
    if (stream) {
      if (write_stream)
        (*stream) << i_tmp << "\n";
      else
        (*stream) >> i_tmp;
    }
    return i_tmp;
  }
  inline double evaluate(double d_tmp) const
  {
    if (stream) {
      if (write_stream)
        (*stream) << d_tmp << "\n";
      else
        (*stream) >> d_tmp;
    }
    return d_tmp;
  }
#else
  inline bool evaluate(const bool b_tmp) const
  {
    return b_tmp;
  }
  inline double evaluate(const double d_tmp) const
  {
    return d_tmp;
  }
#endif

public:
  /// Default constructor creates a timer with no time limit and no tracing
  CoinTimer()
    : start(0)
    , limit(1e100)
    , end(1e100)
#ifdef COIN_COMPILE_WITH_TRACING
    , stream(0)
    , write_stream(true)
#endif
  {
  }

  /// Create a timer with the given time limit and with no tracing
  CoinTimer(double lim)
    : start(CoinCpuTime())
    , limit(lim)
    , end(start + lim)
#ifdef COIN_COMPILE_WITH_TRACING
    , stream(0)
    , write_stream(true)
#endif
  {
  }

#ifdef COIN_COMPILE_WITH_TRACING
  /** Create a timer with no time limit and with writing/reading the trace
       to/from the given stream, depending on the argument \c write. */
  CoinTimer(std::fstream *s, bool write)
    : start(0)
    , limit(1e100)
    , end(1e100)
    , stream(s)
    , write_stream(write)
  {
  }

  /** Create a timer with the given time limit and with writing/reading the
       trace to/from the given stream, depending on the argument \c write. */
  CoinTimer(double lim, std::fstream *s, bool w)
    : start(CoinCpuTime())
    , limit(lim)
    , end(start + lim)
    , stream(s)
    , write_stream(w)
  {
  }
#endif

  /// Restart the timer (keeping the same time limit)
  inline void restart()
  {
    start = CoinCpuTime();
    end = start + limit;
  }
  /// An alternate name for \c restart()
  inline void reset() { restart(); }
  /// Reset (and restart) the timer and change its time limit
  inline void reset(double lim)
  {
    limit = lim;
    restart();
  }

  /** Return whether the given percentage of the time limit has elapsed since
       the timer was started */
  inline bool isPastPercent(double pct) const
  {
    return evaluate(start + limit * pct < CoinCpuTime());
  }
  /** Return whether the given amount of time has elapsed since the timer was
       started */
  inline bool isPast(double lim) const
  {
    return evaluate(start + lim < CoinCpuTime());
  }
  /** Return whether the originally specified time limit has passed since the
       timer was started */
  inline bool isExpired() const
  {
    return evaluate(end < CoinCpuTime());
  }

  /** Return how much time is left on the timer */
  inline double timeLeft() const
  {
    return evaluate(end - CoinCpuTime());
  }

  /** Return how much time has elapsed */
  inline double timeElapsed() const
  {
    return evaluate(CoinCpuTime() - start);
  }

  inline void setLimit(double l)
  {
    limit = l;
    return;
  }
};

#endif

/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
*/