A Discrete-Event Network Simulator
API
ns3::WallClockSynchronizer Class Reference

Class used for synchronizing the simulation events to a real-time "wall clock" using Posix clock functions. More...

#include "wall-clock-synchronizer.h"

+ Inheritance diagram for ns3::WallClockSynchronizer:
+ Collaboration diagram for ns3::WallClockSynchronizer:

Public Member Functions

 WallClockSynchronizer ()
 Constructor. More...
 
 ~WallClockSynchronizer () override
 Destructor. More...
 
- Public Member Functions inherited from ns3::Synchronizer
 Synchronizer ()
 Constructor. More...
 
 ~Synchronizer () override
 Destructor. More...
 
uint64_t EventEnd ()
 Ask the synchronizer to return the time step between the instant remembered during EventStart and now. More...
 
void EventStart ()
 Ask the synchronizer to remember what time it is. More...
 
uint64_t GetCurrentRealtime ()
 Retrieve the value of the origin of the underlying normalized wall clock time in simulator timestep units. More...
 
int64_t GetDrift (uint64_t ts)
 Retrieve the difference between the real time clock used to synchronize the simulation and the simulation time (in Time resolution units). More...
 
uint64_t GetOrigin ()
 Retrieve the value of the origin of the simulation time in Time.resolution units. More...
 
bool Realtime ()
 Return true if this synchronizer is actually synchronizing to a realtime clock. More...
 
void SetCondition (bool cond)
 Set the condition variable that tells a possible simulator thread waiting in the Synchronize method that an event has happened which demands a reevaluation of the wait time. More...
 
void SetOrigin (uint64_t ts)
 Establish a correspondence between a simulation time and the synchronizer real time. More...
 
void Signal ()
 Tell a possible simulator thread waiting in the Synchronize method that an event has happened which demands a reevaluation of the wait time. More...
 
bool Synchronize (uint64_t tsCurrent, uint64_t tsDelay)
 Wait until the real time is in sync with the specified simulation time or until the synchronizer is Signalled. More...
 
- Public Member Functions inherited from ns3::Object
 Object ()
 Constructor. More...
 
 ~Object () override
 Destructor. More...
 
void AggregateObject (Ptr< Object > other)
 Aggregate two Objects together. More...
 
void Dispose ()
 Dispose of this Object. More...
 
AggregateIterator GetAggregateIterator () const
 Get an iterator to the Objects aggregated to this one. More...
 
TypeId GetInstanceTypeId () const override
 Get the most derived TypeId for this Object. More...
 
template<typename T >
Ptr< T > GetObject () const
 Get a pointer to the requested aggregated Object. More...
 
template<>
Ptr< ObjectGetObject () const
 Specialization of () for objects of type ns3::Object. More...
 
template<typename T >
Ptr< T > GetObject (TypeId tid) const
 Get a pointer to the requested aggregated Object by TypeId. More...
 
template<>
Ptr< ObjectGetObject (TypeId tid) const
 Specialization of (TypeId tid) for objects of type ns3::Object. More...
 
void Initialize ()
 Invoke DoInitialize on all Objects aggregated to this one. More...
 
bool IsInitialized () const
 Check if the object has been initialized. More...
 
- Public Member Functions inherited from ns3::SimpleRefCount< Object, ObjectBase, ObjectDeleter >
 SimpleRefCount ()
 Default constructor. More...
 
 SimpleRefCount (const SimpleRefCount &o[[maybe_unused]])
 Copy constructor. More...
 
uint32_t GetReferenceCount () const
 Get the reference count of the object. More...
 
SimpleRefCountoperator= (const SimpleRefCount &o[[maybe_unused]])
 Assignment operator. More...
 
void Ref () const
 Increment the reference count. More...
 
void Unref () const
 Decrement the reference count. More...
 
- Public Member Functions inherited from ns3::ObjectBase
virtual ~ObjectBase ()
 Virtual destructor. More...
 
void GetAttribute (std::string name, AttributeValue &value) const
 Get the value of an attribute, raising fatal errors if unsuccessful. More...
 
bool GetAttributeFailSafe (std::string name, AttributeValue &value) const
 Get the value of an attribute without raising errors. More...
 
void SetAttribute (std::string name, const AttributeValue &value)
 Set a single attribute, raising fatal errors if unsuccessful. More...
 
bool SetAttributeFailSafe (std::string name, const AttributeValue &value)
 Set a single attribute without raising errors. More...
 
bool TraceConnect (std::string name, std::string context, const CallbackBase &cb)
 Connect a TraceSource to a Callback with a context. More...
 
bool TraceConnectWithoutContext (std::string name, const CallbackBase &cb)
 Connect a TraceSource to a Callback without a context. More...
 
bool TraceDisconnect (std::string name, std::string context, const CallbackBase &cb)
 Disconnect from a TraceSource a Callback previously connected with a context. More...
 
bool TraceDisconnectWithoutContext (std::string name, const CallbackBase &cb)
 Disconnect from a TraceSource a Callback previously connected without a context. More...
 

Static Public Member Functions

static TypeId GetTypeId ()
 Get the registered TypeId for this class. More...
 
- Static Public Member Functions inherited from ns3::Synchronizer
static TypeId GetTypeId ()
 Get the registered TypeId for this class. More...
 
- Static Public Member Functions inherited from ns3::Object
static TypeId GetTypeId ()
 Register this type. More...
 
- Static Public Member Functions inherited from ns3::ObjectBase
static TypeId GetTypeId ()
 Get the type ID. More...
 

Static Public Attributes

static const uint64_t NS_PER_SEC = (uint64_t)1000000000
 Conversion constant between ns and s. More...
 
static const uint64_t US_PER_NS = (uint64_t)1000
 Conversion constant between μs and ns. More...
 
static const uint64_t US_PER_SEC = (uint64_t)1000000
 Conversion constant between μs and seconds. More...
 

Protected Member Functions

uint64_t DoEventEnd () override
 Return the amount of real time elapsed since the last call to EventStart. More...
 
void DoEventStart () override
 Record the normalized real time at which the current event is starting execution. More...
 
uint64_t DoGetCurrentRealtime () override
 Retrieve the value of the origin of the underlying normalized wall clock time in Time resolution units. More...
 
int64_t DoGetDrift (uint64_t ns) override
 Get the drift between the real time clock used to synchronize the simulation and the current simulation time. More...
 
bool DoRealtime () override
 Return true if this synchronizer is actually synchronizing to a realtime clock. More...
 
void DoSetCondition (bool cond) override
 Set the condition variable to tell a possible simulator thread waiting in the Synchronize method that an event has happened which demands a reevaluation of the wait time. More...
 
void DoSetOrigin (uint64_t ns) override
 Establish a correspondence between a simulation time and a wall-clock (real) time. More...
 
void DoSignal () override
 Tell a possible simulator thread waiting in the DoSynchronize method that an event has happened which demands a reevaluation of the wait time. More...
 
bool DoSynchronize (uint64_t nsCurrent, uint64_t nsDelay) override
 Wait until the real time is in sync with the specified simulation time. More...
 
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. More...
 
uint64_t GetNormalizedRealtime ()
 Get the current normalized real time, in ns. More...
 
uint64_t GetRealtime ()
 Get the current absolute real time (in ns since the epoch). More...
 
bool SleepWait (uint64_t ns)
 Put our process to sleep for some number of nanoseconds. More...
 
bool SpinWait (uint64_t ns)
 Do a busy-wait until the normalized realtime equals the argument or the condition variable becomes true. More...
 
- Protected Member Functions inherited from ns3::Object
 Object (const Object &o)
 Copy an Object. More...
 
virtual void DoDispose ()
 Destructor implementation. More...
 
virtual void DoInitialize ()
 Initialize() implementation. More...
 
virtual void NotifyNewAggregate ()
 Notify all Objects aggregated to this one of a new Object being aggregated. More...
 
- Protected Member Functions inherited from ns3::ObjectBase
void ConstructSelf (const AttributeConstructionList &attributes)
 Complete construction of ObjectBase; invoked by derived classes. More...
 
virtual void NotifyConstructionCompleted ()
 Notifier called once the ObjectBase is fully constructed. More...
 

Protected Attributes

bool m_condition
 The condition state. More...
 
std::condition_variable m_conditionVariable
 Condition variable for thread synchronizer. More...
 
uint64_t m_jiffy
 Size of the system clock tick, as reported by clock_getres, in ns. More...
 
std::mutex m_mutex
 Mutex controlling access to the condition variable. More...
 
uint64_t m_nsEventStart
 Time recorded by DoEventStart. More...
 
- Protected Attributes inherited from ns3::Synchronizer
uint64_t m_realtimeOriginNano
 The real time, in ns, when SetOrigin was called. More...
 
uint64_t m_simOriginNano
 The simulation time, in ns, when SetOrigin was called. More...
 

Additional Inherited Members

Detailed Description

Class used for synchronizing the simulation events to a real-time "wall clock" using Posix clock functions.

This synchronizer is used as part of the RealtimeSimulatorImpl. It is typically not explicitly enabled by users but instead is implicitly enabled when the simulator implementation type is set to real-time; e.g.:

GlobalValue::Bind ("SimulatorImplementationType",
StringValue ("ns3::RealtimeSimulatorImpl"));
static void Bind(std::string name, const AttributeValue &value)
Iterate over the set of GlobalValues until a matching name is found and then set its value with Globa...

before calling any simulator functions.

There are a couple of more issues at this level. Posix clocks provide access to several clocks we could use as a wall clock. We don't care about time in the sense of 04:30 CEST, we care about some piece of hardware that ticks at some regular period. The most accurate posix clock in this respect is the CLOCK_PROCESS_CPUTIME_ID clock. This is a high-resolution register in the CPU. For example, on Intel machines this corresponds to the timestamp counter (TSC) register. The resolution of this counter will be on the order of nanoseconds.

Now, just because we can measure time in nanoseconds doesn't mean we can put our process to sleep to nanosecond resolution. We are eventually going to use the function clock_nanosleep() to sleep until a simulation Time specified by the caller.

Todo:
Add more on jiffies, sleep, processes, etc.

Definition at line 68 of file wall-clock-synchronizer.h.

Constructor & Destructor Documentation

◆ WallClockSynchronizer()

ns3::WallClockSynchronizer::WallClockSynchronizer ( )

◆ ~WallClockSynchronizer()

ns3::WallClockSynchronizer::~WallClockSynchronizer ( )
override

Destructor.

Definition at line 83 of file wall-clock-synchronizer.cc.

References NS_LOG_FUNCTION.

Member Function Documentation

◆ DoEventEnd()

uint64_t ns3::WallClockSynchronizer::DoEventEnd ( )
overrideprotectedvirtual

Return the amount of real time elapsed since the last call to EventStart.

Returns
The elapsed real time, in ns.

Implements ns3::Synchronizer.

Definition at line 305 of file wall-clock-synchronizer.cc.

References GetNormalizedRealtime(), m_nsEventStart, and NS_LOG_FUNCTION.

+ Here is the call graph for this function:

◆ DoEventStart()

void ns3::WallClockSynchronizer::DoEventStart ( )
overrideprotectedvirtual

Record the normalized real time at which the current event is starting execution.

Implements ns3::Synchronizer.

Definition at line 298 of file wall-clock-synchronizer.cc.

References GetNormalizedRealtime(), m_nsEventStart, and NS_LOG_FUNCTION.

+ Here is the call graph for this function:

◆ DoGetCurrentRealtime()

uint64_t ns3::WallClockSynchronizer::DoGetCurrentRealtime ( )
overrideprotectedvirtual

Retrieve the value of the origin of the underlying normalized wall clock time in Time resolution units.

Subclasses are expected to implement this method to do the actual real-time-clock-specific work of getting the current time.

Returns
The normalized wall clock time (in nanosecond units).
See also
SetOrigin

Implements ns3::Synchronizer.

Definition at line 96 of file wall-clock-synchronizer.cc.

References GetNormalizedRealtime(), and NS_LOG_FUNCTION.

+ Here is the call graph for this function:

◆ DoGetDrift()

int64_t ns3::WallClockSynchronizer::DoGetDrift ( uint64_t  ns)
overrideprotectedvirtual

Get the drift between the real time clock used to synchronize the simulation and the current simulation time.

Parameters
[in]nsSimulation time in ns.
Returns
Drift in ns units.
See also
SetOrigin
GetDrift

Implements ns3::Synchronizer.

Definition at line 119 of file wall-clock-synchronizer.cc.

References GetNormalizedRealtime(), and NS_LOG_FUNCTION.

Referenced by DoSynchronize(), and DriftCorrect().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ DoRealtime()

bool ns3::WallClockSynchronizer::DoRealtime ( )
overrideprotectedvirtual

Return true if this synchronizer is actually synchronizing to a realtime clock.

The simulator sometimes needs to know this.

Subclasses are expected to implement this method to tell the outside world whether or not they are synchronizing to a realtime clock.

Returns
true if locked with realtime, false if not.

Implements ns3::Synchronizer.

Definition at line 89 of file wall-clock-synchronizer.cc.

References NS_LOG_FUNCTION.

◆ DoSetCondition()

void ns3::WallClockSynchronizer::DoSetCondition ( bool  cond)
overrideprotectedvirtual

Set the condition variable to tell a possible simulator thread waiting in the Synchronize method that an event has happened which demands a reevaluation of the wait time.

Parameters
[in]condThe new value for the condition variable.
See also
SetCondition

Implements ns3::Synchronizer.

Definition at line 291 of file wall-clock-synchronizer.cc.

References m_condition, and NS_LOG_FUNCTION.

◆ DoSetOrigin()

void ns3::WallClockSynchronizer::DoSetOrigin ( uint64_t  ns)
overrideprotectedvirtual

Establish a correspondence between a simulation time and a wall-clock (real) time.

There are three timelines involved here: the simulation (virtual) time, the (absolute) wall-clock time and the (relative) synchronizer real time. Calling this method makes a correspondence between the origin of the synchronizer time and the current wall-clock time.

This method is expected to be called at the "instant" before simulation begins. At this point, simulation time = 0, and synchronizer time is set = 0 in this method. We then associate this time with the current value of the real time clock that will be used to actually perform the synchronization.

Subclasses are expected to implement this method to do the actual real-time-clock-specific work of making the correspondence mentioned above. for example, this is where the differences between Time parameters and parameters to clock_nanosleep would be dealt with.

Parameters
[in]nsThe simulation time we need to use as the origin (normalized to nanosecond units).
See also
SetOrigin

Implements ns3::Synchronizer.

Definition at line 103 of file wall-clock-synchronizer.cc.

References GetRealtime(), ns3::Synchronizer::m_realtimeOriginNano, NS_LOG_FUNCTION, and NS_LOG_INFO.

+ Here is the call graph for this function:

◆ DoSignal()

void ns3::WallClockSynchronizer::DoSignal ( )
overrideprotectedvirtual

Tell a possible simulator thread waiting in the DoSynchronize method that an event has happened which demands a reevaluation of the wait time.

See also
Signal

Implements ns3::Synchronizer.

Definition at line 276 of file wall-clock-synchronizer.cc.

References m_condition, m_conditionVariable, m_mutex, and NS_LOG_FUNCTION.

◆ DoSynchronize()

bool ns3::WallClockSynchronizer::DoSynchronize ( uint64_t  nsCurrent,
uint64_t  nsDelay 
)
overrideprotectedvirtual

Wait until the real time is in sync with the specified simulation time.

This is where the real work of synchronization is done. The nsCurrent argument is the simulation time (in ns). The job of DoSynchronize is to translate from simulation time to synchronizer time (in a perfect world these are the same time) and then figure out how long in real-time it needs to wait until that synchronizer / simulation time comes around.

Subclasses are expected to implement this method to do the actual real-time-clock-specific work of waiting (either busy-waiting or sleeping, or some combination) until the requested simulation time.

Parameters
[in]nsCurrentThe current simulation time (in nanosecond units).
[in]nsDelayThe simulation time we need to wait for (normalized to nanosecond units).
Returns
true if the function ran to completion, false if it was interrupted by a Signal.
See also
Synchronize
Signal

Implements ns3::Synchronizer.

Definition at line 159 of file wall-clock-synchronizer.cc.

References DoGetDrift(), DriftCorrect(), m_jiffy, NS_LOG_FUNCTION, NS_LOG_INFO, SleepWait(), and SpinWait().

+ Here is the call graph for this function:

◆ DriftCorrect()

uint64_t ns3::WallClockSynchronizer::DriftCorrect ( uint64_t  nsNow,
uint64_t  nsDelay 
)
protected

Compute a correction to the nominal delay to account for realtime drift since the last DoSynchronize.

Parameters
[in]nsNowThe current simulation time (in nanosecond units).
[in]nsDelayThe simulation time we need to wait for (normalized to nanosecond units).
Returns
The corrected delay.

Definition at line 347 of file wall-clock-synchronizer.cc.

References DoGetDrift(), and NS_LOG_FUNCTION.

Referenced by DoSynchronize().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetNormalizedRealtime()

uint64_t ns3::WallClockSynchronizer::GetNormalizedRealtime ( )
protected

Get the current normalized real time, in ns.

Returns
The current normalized real time, in ns.

Definition at line 386 of file wall-clock-synchronizer.cc.

References GetRealtime(), ns3::Synchronizer::m_realtimeOriginNano, and NS_LOG_FUNCTION.

Referenced by DoEventEnd(), DoEventStart(), DoGetCurrentRealtime(), DoGetDrift(), and SpinWait().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetRealtime()

uint64_t ns3::WallClockSynchronizer::GetRealtime ( )
protected

Get the current absolute real time (in ns since the epoch).

Returns
The current real time, in ns.

Definition at line 378 of file wall-clock-synchronizer.cc.

References NS_LOG_FUNCTION.

Referenced by DoSetOrigin(), and GetNormalizedRealtime().

+ Here is the caller graph for this function:

◆ GetTypeId()

TypeId ns3::WallClockSynchronizer::GetTypeId ( void  )
static

Get the registered TypeId for this class.

Returns
The TypeId.

Definition at line 41 of file wall-clock-synchronizer.cc.

References ns3::TypeId::SetParent().

+ Here is the call graph for this function:

◆ SleepWait()

bool ns3::WallClockSynchronizer::SleepWait ( uint64_t  ns)
protected

Put our process to sleep for some number of nanoseconds.

Typically this will be some time equal to an integral number of jiffies. We will usually follow a call to SleepWait with a call to SpinWait to get the kind of accuracy we want.

We have to have some mechanism to wake up this sleep in case an external event happens that causes a Schedule event in the simulator. This newly scheduled event might be before the time we are waiting until, so we have to break out of both the SleepWait and the following SpinWait to go back and reschedule/resynchronize taking the new event into account. The condition we have saved in m_condition, along with the condition variable m_conditionVariable take care of this for us.

This call will return if the timeout expires OR if the condition is set true by a call to SetCondition (true) followed by a call to Signal(). In either case, we are done waiting. If the timeout happened, we return true; if a Signal happened we return false.

Parameters
[in]nsThe target normalized real time we should wait for.
Returns
true if we reached the target time, false if we returned because the condition was set.

Definition at line 333 of file wall-clock-synchronizer.cc.

References m_condition, m_conditionVariable, m_mutex, and NS_LOG_FUNCTION.

Referenced by DoSynchronize().

+ Here is the caller graph for this function:

◆ SpinWait()

bool ns3::WallClockSynchronizer::SpinWait ( uint64_t  ns)
protected

Do a busy-wait until the normalized realtime equals the argument or the condition variable becomes true.

The condition becomes true if an outside entity (a network device receives a packet), sets the condition and signals the scheduler it needs to re-evaluate.

Parameters
[in]nsThe target normalized real time we should wait for.
Returns
true if we reached the target time, false if we returned because the condition was set.

Definition at line 312 of file wall-clock-synchronizer.cc.

References GetNormalizedRealtime(), m_condition, and NS_LOG_FUNCTION.

Referenced by DoSynchronize().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ m_condition

bool ns3::WallClockSynchronizer::m_condition
protected

The condition state.

Definition at line 174 of file wall-clock-synchronizer.h.

Referenced by DoSetCondition(), DoSignal(), SleepWait(), and SpinWait().

◆ m_conditionVariable

std::condition_variable ns3::WallClockSynchronizer::m_conditionVariable
protected

Condition variable for thread synchronizer.

Definition at line 170 of file wall-clock-synchronizer.h.

Referenced by DoSignal(), and SleepWait().

◆ m_jiffy

uint64_t ns3::WallClockSynchronizer::m_jiffy
protected

Size of the system clock tick, as reported by clock_getres, in ns.

Definition at line 165 of file wall-clock-synchronizer.h.

Referenced by WallClockSynchronizer(), and DoSynchronize().

◆ m_mutex

std::mutex ns3::WallClockSynchronizer::m_mutex
protected

Mutex controlling access to the condition variable.

Definition at line 172 of file wall-clock-synchronizer.h.

Referenced by DoSignal(), and SleepWait().

◆ m_nsEventStart

uint64_t ns3::WallClockSynchronizer::m_nsEventStart
protected

Time recorded by DoEventStart.

Definition at line 167 of file wall-clock-synchronizer.h.

Referenced by DoEventEnd(), and DoEventStart().

◆ NS_PER_SEC

const uint64_t ns3::WallClockSynchronizer::NS_PER_SEC = (uint64_t)1000000000
static

Conversion constant between ns and s.

Definition at line 87 of file wall-clock-synchronizer.h.

◆ US_PER_NS

const uint64_t ns3::WallClockSynchronizer::US_PER_NS = (uint64_t)1000
static

Conversion constant between μs and ns.

Definition at line 83 of file wall-clock-synchronizer.h.

◆ US_PER_SEC

const uint64_t ns3::WallClockSynchronizer::US_PER_SEC = (uint64_t)1000000
static

Conversion constant between μs and seconds.

Definition at line 85 of file wall-clock-synchronizer.h.


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