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...
 
virtual ~WallClockSynchronizer ()
 Destructor. More...
 
- Public Member Functions inherited from ns3::Synchronizer
 Synchronizer ()
 Constructor. More...
 
virtual ~Synchronizer ()
 Destructor. More...
 
uint64_t EventEnd (void)
 Ask the synchronizer to return the time step between the instant remembered during EventStart and now. More...
 
void EventStart (void)
 Ask the synchronizer to remember what time it is. More...
 
uint64_t GetCurrentRealtime (void)
 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 (void)
 Retrieve the value of the origin of the simulation time in Time.resolution units. More...
 
bool Realtime (void)
 Return true if this synchronizer is actually synchronizing to a realtime clock. More...
 
void SetCondition (bool)
 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 (void)
 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...
 
virtual ~Object ()
 Destructor. More...
 
void AggregateObject (Ptr< Object > other)
 Aggregate two Objects together. More...
 
void Dispose (void)
 Dispose of this Object. More...
 
AggregateIterator GetAggregateIterator (void) const
 Get an iterator to the Objects aggregated to this one. More...
 
virtual TypeId GetInstanceTypeId (void) const
 Get the most derived TypeId for this Object. More...
 
template<typename T >
Ptr< T > GetObject (void) const
 Get a pointer to the requested aggregated 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 () const
 Specialization of () for objects of type ns3::Object. More...
 
template<>
Ptr< ObjectGetObject (TypeId tid) const
 Specialization of (TypeId tid) for objects of type ns3::Object. More...
 
void Initialize (void)
 Invoke DoInitialize on all Objects aggregated to this one. More...
 
bool IsInitialized (void) 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)
 Copy constructor. More...
 
uint32_t GetReferenceCount (void) const
 Get the reference count of the object. More...
 
SimpleRefCountoperator= (const SimpleRefCount &o)
 Assignment operator. More...
 
void Ref (void) const
 Increment the reference count. More...
 
void Unref (void) 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 erros. 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 (void)
 Get the registered TypeId for this class. More...
 
- Static Public Member Functions inherited from ns3::Synchronizer
static TypeId GetTypeId (void)
 Get the registered TypeId for this class. More...
 
- Static Public Member Functions inherited from ns3::Object
static TypeId GetTypeId (void)
 Register this type. More...
 
- Static Public Member Functions inherited from ns3::ObjectBase
static TypeId GetTypeId (void)
 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

virtual uint64_t DoEventEnd (void)
 Return the amount of real time elapsed since the last call to EventStart. More...
 
virtual void DoEventStart (void)
 Record the normalized real time at which the current event is starting execution. More...
 
virtual uint64_t DoGetCurrentRealtime (void)
 Retrieve the value of the origin of the underlying normalized wall clock time in Time resolution units. More...
 
virtual int64_t DoGetDrift (uint64_t ns)
 Get the drift between the real time clock used to synchronize the simulation and the current simulation time. More...
 
virtual bool DoRealtime (void)
 Return true if this synchronizer is actually synchronizing to a realtime clock. More...
 
virtual void DoSetCondition (bool cond)
 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...
 
virtual void DoSetOrigin (uint64_t ns)
 Establish a correspondence between a simulation time and a wall-clock (real) time. More...
 
virtual void DoSignal (void)
 Tell a possible simulator thread waiting in the DoSynchronize method that an event has happened which demands a reevaluation of the wait time. More...
 
virtual bool DoSynchronize (uint64_t nsCurrent, uint64_t nsDelay)
 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 (void)
 Get the current normalized real time, in ns. More...
 
uint64_t GetRealtime (void)
 Get the current absolute real time (in ns since the epoch). More...
 
void NsToTimeval (int64_t ns, struct timeval *tv)
 Convert an absolute time in ns to a timeval. 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...
 
void TimevalAdd (struct timeval *tv1, struct timeval *tv2, struct timeval *result)
 Add two timeval. More...
 
uint64_t TimevalToNs (struct timeval *tv)
 Convert a timeval to absolute time, in ns. More...
 
- Protected Member Functions inherited from ns3::Object
 Object (const Object &o)
 Copy an Object. More...
 
virtual void DoDispose (void)
 Destructor implementation. More...
 
virtual void DoInitialize (void)
 Initialize() implementation. More...
 
virtual void NotifyNewAggregate (void)
 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 (void)
 Notifier called once the ObjectBase is fully constructed. More...
 

Protected Attributes

SystemCondition m_condition
 Thread synchronizer. More...
 
uint64_t m_jiffy
 Size of the system clock tick, as reported by clock_getres, in ns. 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.


Introspection did not find any typical Config paths.

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"));

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.
Internal:
Nanosleep takes a struct timeval as an input so we have to deal with conversion between Time and timeval here. They are both interpreted as elapsed times.


No Attributes are defined for this type.
No TraceSources are defined for this type.
Size of this type is 72 bytes (on a 64-bit architecture).

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

Constructor & Destructor Documentation

◆ WallClockSynchronizer()

ns3::WallClockSynchronizer::WallClockSynchronizer ( )

Constructor.

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

References m_jiffy, NS_LOG_FUNCTION, NS_LOG_INFO, and NS_PER_SEC.

◆ ~WallClockSynchronizer()

ns3::WallClockSynchronizer::~WallClockSynchronizer ( )
virtual

Destructor.

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

References NS_LOG_FUNCTION.

Member Function Documentation

◆ DoEventEnd()

uint64_t ns3::WallClockSynchronizer::DoEventEnd ( void  )
protectedvirtual

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 308 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 ( void  )
protectedvirtual

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

Implements ns3::Synchronizer.

Definition at line 301 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 ( void  )
protectedvirtual

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 104 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)
protectedvirtual

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 127 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 ( void  )
protectedvirtual

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 97 of file wall-clock-synchronizer.cc.

References NS_LOG_FUNCTION.

◆ DoSetCondition()

void ns3::WallClockSynchronizer::DoSetCondition ( bool  )
protectedvirtual

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.

See also
SetCondition

Implements ns3::Synchronizer.

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

References m_condition, NS_LOG_FUNCTION, and ns3::SystemCondition::SetCondition().

+ Here is the call graph for this function:

◆ DoSetOrigin()

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

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 111 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 ( void  )
protectedvirtual

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 285 of file wall-clock-synchronizer.cc.

References m_condition, NS_LOG_FUNCTION, ns3::SystemCondition::SetCondition(), and ns3::SystemCondition::Signal().

+ Here is the call graph for this function:

◆ DoSynchronize()

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

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 167 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 343 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 ( void  )
protected

Get the current normalized real time, in ns.

Returns
The current normalized real time, in ns.

Definition at line 383 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 ( void  )
protected

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

Returns
The current real time, in ns.

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

References NS_LOG_FUNCTION, and TimevalToNs().

Referenced by DoSetOrigin(), and GetNormalizedRealtime().

+ Here is the call graph for this function:
+ 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 42 of file wall-clock-synchronizer.cc.

References ns3::TypeId::SetParent().

+ Here is the call graph for this function:

◆ NsToTimeval()

void ns3::WallClockSynchronizer::NsToTimeval ( int64_t  ns,
struct timeval *  tv 
)
protected

Convert an absolute time in ns to a timeval.

Parameters
[in]nsAbsolute time in ns.
[out]tvConverted timeval.

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

References NS_ASSERT, NS_LOG_FUNCTION, NS_PER_SEC, and US_PER_NS.

◆ 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 SystemCondition we have saved in m_condition takes 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 336 of file wall-clock-synchronizer.cc.

References m_condition, NS_LOG_FUNCTION, and ns3::SystemCondition::TimedWait().

Referenced by DoSynchronize().

+ Here is the call graph for this function:
+ 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 315 of file wall-clock-synchronizer.cc.

References ns3::SystemCondition::GetCondition(), 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:

◆ TimevalAdd()

void ns3::WallClockSynchronizer::TimevalAdd ( struct timeval *  tv1,
struct timeval *  tv2,
struct timeval *  result 
)
protected

Add two timeval.

Parameters
[in]tv1The first timeval.
[in]tv2The second timeval.
[out]resultThe sum of tv1 and tv2.

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

References NS_LOG_FUNCTION, and US_PER_SEC.

◆ TimevalToNs()

uint64_t ns3::WallClockSynchronizer::TimevalToNs ( struct timeval *  tv)
protected

Convert a timeval to absolute time, in ns.

Parameters
[in]tvThe input timeval.
Returns
The absolute time, in ns.

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

References NS_ASSERT, NS_LOG_FUNCTION, NS_PER_SEC, and US_PER_NS.

Referenced by GetRealtime().

+ Here is the caller graph for this function:

Member Data Documentation

◆ m_condition

SystemCondition ns3::WallClockSynchronizer::m_condition
protected

Thread synchronizer.

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

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

◆ 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 193 of file wall-clock-synchronizer.h.

Referenced by DoSynchronize(), and WallClockSynchronizer().

◆ m_nsEventStart

uint64_t ns3::WallClockSynchronizer::m_nsEventStart
protected

Time recorded by DoEventStart.

Definition at line 195 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 89 of file wall-clock-synchronizer.h.

Referenced by NsToTimeval(), TimevalToNs(), and WallClockSynchronizer().

◆ US_PER_NS

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

Conversion constant between μs and ns.

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

Referenced by NsToTimeval(), and TimevalToNs().

◆ US_PER_SEC

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

Conversion constant between μs and seconds.

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

Referenced by TimevalAdd().


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