26 #include <condition_variable>
45 static TypeId tid =
TypeId (
"ns3::WallClockSynchronizer")
47 .SetGroupName (
"Core")
84 clock_getres (CLOCK_REALTIME, &ts);
155 return (int64_t)(nsNow - ns);
163 return -(int64_t)(ns - nsNow);
206 uint64_t numberJiffies = ns /
m_jiffy;
207 NS_LOG_INFO (
"Synchronize numberJiffies = " << numberJiffies);
229 if (numberJiffies > 3)
263 int64_t nsDrift =
DoGetDrift (nsCurrent + nsDelay);
271 NS_LOG_INFO (
"Back from SleepWait: IML8 " << nsDrift);
281 NS_LOG_INFO (
"SpinWait until " << nsCurrent + nsDelay);
282 return SpinWait (nsCurrent + nsDelay);
290 std::unique_lock<std::mutex> lock (
m_mutex);
347 std::unique_lock<std::mutex> lock (
m_mutex);
350 std::chrono::nanoseconds (ns),
353 return finishedWaiting;
376 uint64_t correction = (uint64_t)drift;
377 if (correction <= nsDelay)
379 return nsDelay - correction;
391 struct timeval tvNow;
392 gettimeofday (&tvNow, NULL);
408 tv->tv_sec =
static_cast<long> (ns /
NS_PER_SEC);
428 result->tv_sec = tv1->tv_sec + tv2->tv_sec;
429 result->tv_usec = tv1->tv_usec + tv2->tv_usec;
Base class used for synchronizing the simulation events to some real time "wall clock....
uint64_t m_realtimeOriginNano
The real time, in ns, when SetOrigin was called.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
virtual void DoSetOrigin(uint64_t ns)
Establish a correspondence between a simulation time and a wall-clock (real) time.
static const uint64_t NS_PER_SEC
Conversion constant between ns and s.
uint64_t GetNormalizedRealtime(void)
Get the current normalized real time, in ns.
bool SleepWait(uint64_t ns)
Put our process to sleep for some number of nanoseconds.
virtual ~WallClockSynchronizer()
Destructor.
void NsToTimeval(int64_t ns, struct timeval *tv)
Convert an absolute time in ns to a timeval.
virtual bool DoSynchronize(uint64_t nsCurrent, uint64_t nsDelay)
Wait until the real time is in sync with the specified simulation time.
uint64_t m_jiffy
Size of the system clock tick, as reported by clock_getres, in ns.
virtual bool DoRealtime(void)
Return true if this synchronizer is actually synchronizing to a realtime clock.
uint64_t TimevalToNs(struct timeval *tv)
Convert a timeval to absolute time, in ns.
bool m_condition
The condition state.
static const uint64_t US_PER_NS
Conversion constant between μs and ns.
virtual void DoSignal(void)
Tell a possible simulator thread waiting in the DoSynchronize method that an event has happened which...
static TypeId GetTypeId(void)
Get the registered TypeId for this class.
virtual uint64_t DoGetCurrentRealtime(void)
Retrieve the value of the origin of the underlying normalized wall clock time in Time resolution unit...
virtual int64_t DoGetDrift(uint64_t ns)
Get the drift between the real time clock used to synchronize the simulation and the current simulati...
uint64_t GetRealtime(void)
Get the current absolute real time (in ns since the epoch).
uint64_t m_nsEventStart
Time recorded by DoEventStart.
void TimevalAdd(struct timeval *tv1, struct timeval *tv2, struct timeval *result)
Add two timeval.
bool SpinWait(uint64_t ns)
Do a busy-wait until the normalized realtime equals the argument or the condition variable becomes tr...
static const uint64_t US_PER_SEC
Conversion constant between μs and seconds.
std::condition_variable m_conditionVariable
Condition variable for thread synchronizer.
virtual void DoEventStart(void)
Record the normalized real time at which the current event is starting execution.
virtual void DoSetCondition(bool cond)
Set the condition variable to tell a possible simulator thread waiting in the Synchronize method that...
virtual uint64_t DoEventEnd(void)
Return the amount of real time elapsed since the last call to EventStart.
WallClockSynchronizer()
Constructor.
uint64_t DriftCorrect(uint64_t nsNow, uint64_t nsDelay)
Compute a correction to the nominal delay to account for realtime drift since the last DoSynchronize.
std::mutex m_mutex
Mutex controlling access to the condition variable.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::WallClockSynchronizer declaration.