reSIProcate/rutil  9694
Public Member Functions | Static Public Member Functions | Static Private Member Functions | Static Private Attributes
resip::ResipClock Class Reference

Clock used for timing in the Timer class and possibly other areas. More...

#include <Time.hxx>

List of all members.

Public Member Functions

 ResipClock (void)
 ~ResipClock (void)

Static Public Member Functions

static UInt64 getTimeMicroSec ()
 Returns the current clock time in microseconds.
static UInt64 getTimeMs ()
 Returns the current clock time in milliseconds.
static UInt64 getTimeSecs ()
 Returns the current clock time in seconds.
static UInt64 getRandomFutureTimeMs (UInt64 futureMs)
 Returns an absolute time in ms that is between 50% and 90% of passed in ms from now.
static UInt64 getForever ()
 Infinit time in future.
static unsigned getMaxSystemTimeWaitMs (void)
 Some monotonic clock implementations may internally only return 32-bit values that will wrap.
static void queryTimerInfo (unsigned &minRes, unsigned &maxRes, unsigned &actualRes, bool &isMonotonic)
 Gets the current clock's minimum, maximum and current/actual timer resolution and returns if the clock is known to be monotonic.

Static Private Member Functions

static UInt64 getSystemTime ()
 Returns the current clock time in microseconds.

Static Private Attributes

static unsigned mMaxSystemTimeWaitMs = UINT_MAX

Detailed Description

Clock used for timing in the Timer class and possibly other areas.

Depending on the OS and compile settings this clock may not be monotonic. Define _RESIP_MONOTONIC_CLOCK to enable monotonic timers. The precision of this clock is available in microseconds, but the accuracy depends on other factors such as the OS and hardware. The time values returned by this class should be considered independent of any other clock time, including the system time (ie OS date/time,uptime,epoch,etc).

OS Specific notes:

Windows

When _RESIP_MONOTONIC_CLOCK is defined, timeGetTime() is currently used as the underyling time source. The default resolution/accuracy of this timer on windows is usually the same as the system time clock. To query resolution information use ResipClock::queryTimerInfo(). timeBeginPeriod() can be used to increase the resolution of the timer and allow for up to 1ms accuracy. This will improve the accuracy of SIP related timers governed by resip::Timer, but also has other OS wide implications, please refer to Microsoft's documenation on timeBeginPeriod() for more information.

When _RESIP_MONOTONIC_CLOCK is not defined, ::GetSystemTime is used as the underlying time source. This value can jump forward or backward (not monotonic) if the OS system time is adjusted. The default resolution of this timer is the current system clock interrupt time and cannot be set to a higher resolution. The resolution has been observed at approx 15ms on XP and 1ms on Vista.

POSIX

When _RESIP_MONOTONIC_CLOCK is defined, clock_gettime() is used with the CLOCK_MONOTONIC clock.

When _RESIP_MONOTONIC_CLOCK is not defined, gettimeofday() is used as the underyling clock.

OS X

A monotonic clock is currently not implemented for OS X. There appear to be a couple of choices. http://www.wand.net.nz/~smr26/wordpress/2009/01/19/monotonic-time-in-mac-os-x/ http://www.meandmark.com/timing.pdf http://developer.apple.com/qa/qa2004/qa1398.html

See also:
resiprocate.org devlist discussion "Timers: why system time?";

Definition at line 50 of file Time.hxx.


Constructor & Destructor Documentation

ResipClock::ResipClock ( void  )

Definition at line 19 of file Time.cxx.

{
}
ResipClock::~ResipClock ( void  )

Definition at line 23 of file Time.cxx.

{
}

Member Function Documentation

UInt64 ResipClock::getForever ( ) [static]

Infinit time in future.

Definition at line 227 of file Time.cxx.

{
   assert( sizeof(UInt64) == 8 );
#if defined(WIN32) && !defined(__GNUC__)
   return 18446744073709551615ui64;
#else
   return 18446744073709551615ULL;
#endif
}
static unsigned resip::ResipClock::getMaxSystemTimeWaitMs ( void  ) [inline, static]

Some monotonic clock implementations may internally only return 32-bit values that will wrap.

See also:
Timer::getMaxSystemTimeWaitMs()

Definition at line 89 of file Time.hxx.

References mMaxSystemTimeWaitMs.

      {
         return mMaxSystemTimeWaitMs;
      }     
UInt64 ResipClock::getRandomFutureTimeMs ( UInt64  futureMs) [static]

Returns an absolute time in ms that is between 50% and 90% of passed in ms from now.

Definition at line 238 of file Time.cxx.

References resip::Random::getRandom(), and getTimeMs().

{
   UInt64 now = getTimeMs();

   // make r a random number between 5000 and 9000
   int r = Random::getRandom()%4000;
   r += 5000;

   UInt64 ret = now;
   ret += (futureMs*r)/10000;

   assert( ret >= now );
   assert( ret >= now+(futureMs/2) );
   assert( ret <= now+futureMs );

   return ret;
}

Here is the call graph for this function:

UInt64 ResipClock::getSystemTime ( ) [static, private]

Returns the current clock time in microseconds.

Does not guarantee that this is related to the actual OS system time (eg epoch or other time).

Definition at line 176 of file Time.cxx.

Referenced by getTimeMicroSec(), getTimeMs(), and getTimeSecs().

{
   assert(sizeof(UInt64) == 64/8);

#if defined(WIN32) || defined(UNDER_CE)
#ifdef _RESIP_MONOTONIC_CLOCK
   static ResipClock::WinMonoClock clockInit;
   return WinMonoClock::GetClock64() * 1000;
#else
   FILETIME ft;

#ifdef UNDER_CE
   SYSTEMTIME st;
   ::GetSystemTime(&st);
   ::SystemTimeToFileTime(&st,&ft);
#else
   ::GetSystemTimeAsFileTime(&ft);
#endif

   ULARGE_INTEGER li;
   li.LowPart = ft.dwLowDateTime;
   li.HighPart = ft.dwHighDateTime;
   return li.QuadPart/10;

#endif //_RESIP_MONOTONIC_CLOCK

#else //#if defined(WIN32) || defined(UNDER_CE)

   UInt64 time=0;
#ifdef _RESIP_MONOTONIC_CLOCK
   struct timespec now_monotonic;
   if (clock_gettime( CLOCK_MONOTONIC, &now_monotonic ) == 0)
//   if ( syscall( __NR_clock_gettime, CLOCK_MONOTONIC, &now_monotonic ) == 0 )
   {
      time = now_monotonic.tv_sec;
      time = time*1000000;
      time += now_monotonic.tv_nsec/1000;
      return time;
   }
#endif
   struct timeval now;
   gettimeofday( &now , NULL );
   //assert( now );
   time = now.tv_sec;
   time = time*1000000;
   time += now.tv_usec;
   return time;
#endif
}
static UInt64 resip::ResipClock::getTimeMicroSec ( ) [inline, static]

Returns the current clock time in microseconds.

Definition at line 58 of file Time.hxx.

References getSystemTime().

Referenced by resip::Random::getSimpleSeed().

      {
         return getSystemTime();
      }

Here is the call graph for this function:

static UInt64 resip::ResipClock::getTimeMs ( ) [inline, static]

Returns the current clock time in milliseconds.

Definition at line 66 of file Time.hxx.

References getSystemTime().

Referenced by getRandomFutureTimeMs().

      {
         return getSystemTime()/1000LL;
      }

Here is the call graph for this function:

static UInt64 resip::ResipClock::getTimeSecs ( ) [inline, static]

Returns the current clock time in seconds.

Definition at line 73 of file Time.hxx.

References getSystemTime().

      {
         return getSystemTime()/1000000LL;
      }

Here is the call graph for this function:

void ResipClock::queryTimerInfo ( unsigned &  minRes,
unsigned &  maxRes,
unsigned &  actualRes,
bool &  isMonotonic 
) [static]

Gets the current clock's minimum, maximum and current/actual timer resolution and returns if the clock is known to be monotonic.

If min, max or actual return 0 then that information is not available. min max and actual are in units of microseconds.

Definition at line 257 of file Time.cxx.

{  
   min = max = actual = 0;
   isMonotonic = false;

#if defined(WIN32) 
#if defined(_RESIP_MONOTONIC_CLOCK)
#if !defined(NTSTATUS)
#define NTSTATUS DWORD
#endif
   typedef NTSTATUS (WINAPI*PNTQTR)(PULONG,PULONG,PULONG);  
        
        HMODULE hm = ::LoadLibrary("ntdll");
        
   if (hm != NULL)
   {
      PNTQTR ntqtr = (PNTQTR)::GetProcAddress(hm,"NtQueryTimerResolution");

      if (ntqtr)
      {
         ntqtr((PULONG)&min,(PULONG)&max,(PULONG)&actual);
         min /= 10;
         max /= 10;
         actual /= 10;
      }
   
      ::FreeLibrary(hm);
      hm = NULL;
   }   
   isMonotonic = true;
#else
   DWORD timeAdjustment=0;
        BOOL timeAdjustmentDisabled=0;
   //on Vista it looks like GetSystemTime has 1ms resolution, but GetSystemTimeAdjustment still returns 15ms
   //so just set the min resolution to whatever is reported, no actual resolution can be consistently found.
   ::GetSystemTimeAdjustment(&timeAdjustment,(PDWORD)&min,&timeAdjustmentDisabled);
   min /= 10;
   isMonotonic = false;   
#endif
#else //WIN32
#ifdef __APPLE__
   //@TODO 
#else
   clockid_t clock = CLOCK_REALTIME; //need to test/verify CLOCK_REALTIME returns the gettimeofday resolution.  
#if defined(_RESIP_MONOTONIC_CLOCK)
   clock = CLOCK_MONOTONIC;
   isMonotonic = true;
#endif
   struct timespec res;
   if (clock_getres(clock,&res) == 0)
   {
      actual = (res.tv_sec * 1000000) + (res.tv_nsec / 1000);
   } 
#endif
#endif
}

Member Data Documentation

unsigned ResipClock::mMaxSystemTimeWaitMs = UINT_MAX [static, private]

Definition at line 107 of file Time.hxx.

Referenced by getMaxSystemTimeWaitMs().


The documentation for this class was generated from the following files: