23 #include "system-condition.h"
25 #include "wall-clock-synchronizer.h"
31 WallClockSynchronizer::WallClockSynchronizer ()
63 clock_getres (CLOCK_REALTIME, &ts);
64 m_jiffy = ts.tv_sec * NS_PER_SEC + ts.tv_nsec;
71 WallClockSynchronizer::~WallClockSynchronizer ()
87 return GetNormalizedRealtime ();
102 m_realtimeOriginNano = GetRealtime ();
126 uint64_t nsNow = GetNormalizedRealtime ();
134 return (int64_t)(nsNow - ns);
142 return -(int64_t)(ns - nsNow);
172 uint64_t ns = DriftCorrect (nsCurrent, nsDelay);
185 uint64_t numberJiffies = ns / m_jiffy;
186 NS_LOG_INFO (
"Synchronize numberJiffies = " << numberJiffies);
208 if (numberJiffies > 3)
210 NS_LOG_INFO (
"SleepWait for " << numberJiffies * m_jiffy <<
" ns");
211 NS_LOG_INFO (
"SleepWait until " << nsCurrent + numberJiffies * m_jiffy
219 if (SleepWait ((numberJiffies - 3) * m_jiffy) ==
false)
242 int64_t nsDrift =
DoGetDrift (nsCurrent + nsDelay);
250 NS_LOG_INFO (
"Back from SleepWait: IML8 " << nsDrift);
260 NS_LOG_INFO (
"SpinWait until " << nsCurrent + nsDelay);
261 return SpinWait (nsCurrent + nsDelay);
281 WallClockSynchronizer::DoEventStart (
void)
284 m_nsEventStart = GetNormalizedRealtime ();
288 WallClockSynchronizer::DoEventEnd (
void)
291 return GetNormalizedRealtime () - m_nsEventStart;
295 WallClockSynchronizer::SpinWait (uint64_t ns)
309 if (GetNormalizedRealtime () >= ns)
323 WallClockSynchronizer::SleepWait (uint64_t ns)
349 WallClockSynchronizer::DriftCorrect (uint64_t nsNow, uint64_t nsDelay)
368 uint64_t correction = (uint64_t)drift;
369 if (correction <= nsDelay)
371 return nsDelay - correction;
380 WallClockSynchronizer::GetRealtime (
void)
383 struct timeval tvNow;
384 gettimeofday (&tvNow, NULL);
385 return TimevalToNs (&tvNow);
389 WallClockSynchronizer::GetNormalizedRealtime (
void)
392 return GetRealtime () - m_realtimeOriginNano;
396 WallClockSynchronizer::NsToTimeval (int64_t ns,
struct timeval *tv)
400 tv->tv_sec = ns / NS_PER_SEC;
401 tv->tv_usec = (ns % NS_PER_SEC) / US_PER_NS;
405 WallClockSynchronizer::TimevalToNs (
struct timeval *tv)
408 uint64_t nsResult = tv->tv_sec * NS_PER_SEC + tv->tv_usec * US_PER_NS;
414 WallClockSynchronizer::TimevalAdd (
417 struct timeval *result)
420 result->tv_sec = tv1->tv_sec + tv2->tv_sec;
421 result->tv_usec = tv1->tv_usec + tv2->tv_usec;
422 if (result->tv_usec > (int64_t)US_PER_SEC)
425 result->tv_usec %= US_PER_SEC;
#define NS_LOG_FUNCTION(parameters)
#define NS_ASSERT(condition)
#define NS_LOG_COMPONENT_DEFINE(name)
virtual int64_t DoGetDrift(uint64_t ns)
Declaration of method used to retrieve drift between the real time clock used to synchronize the simu...
virtual bool DoRealtime(void)
Return true if this synchronizer is actually synchronizing to a realtime clock. The simulator sometim...
void SetCondition(bool condition)
virtual uint64_t DoGetCurrentRealtime(void)
Retrieve the value of the origin of the underlying normalized wall clock time in nanosecond units...
virtual bool DoSynchronize(uint64_t nsCurrent, uint64_t nsDelay)
Wait until the real time is in sync with the specified simulation time.
bool TimedWait(uint64_t ns)
virtual void DoSetOrigin(uint64_t ns)
Establish a correspondence between a simulation time and a wall-clock (real) time.
virtual void DoSetCondition(bool cond)
Declaration of the method used to set the condition variable that tells a possible simulator thread w...
virtual void DoSignal(void)
Declaration of the method used to tell a possible simulator thread waiting in the DoSynchronize metho...