Class used for synchronizing the simulation events to a real-time "wall clock" using Posix clock functions. More...
#include "wall-clock-synchronizer.h"
Public Member Functions | |
WallClockSynchronizer () | |
Constructor. | |
~WallClockSynchronizer () override | |
Destructor. | |
Public Member Functions inherited from ns3::Synchronizer | |
Synchronizer () | |
Constructor. | |
~Synchronizer () override | |
Destructor. | |
uint64_t | EventEnd () |
Ask the synchronizer to return the time step between the instant remembered during EventStart and now. | |
void | EventStart () |
Ask the synchronizer to remember what time it is. | |
uint64_t | GetCurrentRealtime () |
Retrieve the value of the origin of the underlying normalized wall clock time in simulator timestep units. | |
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). | |
uint64_t | GetOrigin () |
Retrieve the value of the origin of the simulation time in Time.resolution units. | |
bool | Realtime () |
Return true if this synchronizer is actually synchronizing to a realtime clock. | |
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. | |
void | SetOrigin (uint64_t ts) |
Establish a correspondence between a simulation time and the synchronizer real time. | |
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. | |
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. | |
Public Member Functions inherited from ns3::Object | |
Object () | |
Constructor. | |
~Object () override | |
Destructor. | |
void | AggregateObject (Ptr< Object > other) |
Aggregate two Objects together. | |
void | Dispose () |
Dispose of this Object. | |
AggregateIterator | GetAggregateIterator () const |
Get an iterator to the Objects aggregated to this one. | |
TypeId | GetInstanceTypeId () const override |
Get the most derived TypeId for this Object. | |
template<typename T > | |
Ptr< T > | GetObject () const |
Get a pointer to the requested aggregated Object. | |
template<> | |
Ptr< Object > | GetObject () const |
Specialization of () for objects of type ns3::Object. | |
template<typename T > | |
Ptr< T > | GetObject (TypeId tid) const |
Get a pointer to the requested aggregated Object by TypeId. | |
template<> | |
Ptr< Object > | GetObject (TypeId tid) const |
Specialization of (TypeId tid) for objects of type ns3::Object. | |
void | Initialize () |
Invoke DoInitialize on all Objects aggregated to this one. | |
bool | IsInitialized () const |
Check if the object has been initialized. | |
void | UnidirectionalAggregateObject (Ptr< Object > other) |
Aggregate an Object to another Object. | |
Public Member Functions inherited from ns3::SimpleRefCount< Object, ObjectBase, ObjectDeleter > | |
SimpleRefCount () | |
Default constructor. | |
SimpleRefCount (const SimpleRefCount &o) | |
Copy constructor. | |
uint32_t | GetReferenceCount () const |
Get the reference count of the object. | |
SimpleRefCount & | operator= (const SimpleRefCount &o) |
Assignment operator. | |
void | Ref () const |
Increment the reference count. | |
void | Unref () const |
Decrement the reference count. | |
Public Member Functions inherited from ns3::ObjectBase | |
virtual | ~ObjectBase () |
Virtual destructor. | |
void | GetAttribute (std::string name, AttributeValue &value, bool permissive=false) const |
Get the value of an attribute, raising fatal errors if unsuccessful. | |
bool | GetAttributeFailSafe (std::string name, AttributeValue &value) const |
Get the value of an attribute without raising errors. | |
void | SetAttribute (std::string name, const AttributeValue &value) |
Set a single attribute, raising fatal errors if unsuccessful. | |
bool | SetAttributeFailSafe (std::string name, const AttributeValue &value) |
Set a single attribute without raising errors. | |
bool | TraceConnect (std::string name, std::string context, const CallbackBase &cb) |
Connect a TraceSource to a Callback with a context. | |
bool | TraceConnectWithoutContext (std::string name, const CallbackBase &cb) |
Connect a TraceSource to a Callback without a context. | |
bool | TraceDisconnect (std::string name, std::string context, const CallbackBase &cb) |
Disconnect from a TraceSource a Callback previously connected with a context. | |
bool | TraceDisconnectWithoutContext (std::string name, const CallbackBase &cb) |
Disconnect from a TraceSource a Callback previously connected without a context. | |
Static Public Member Functions | |
static TypeId | GetTypeId () |
Get the registered TypeId for this class. | |
Static Public Member Functions inherited from ns3::Synchronizer | |
static TypeId | GetTypeId () |
Get the registered TypeId for this class. | |
Static Public Member Functions inherited from ns3::Object | |
static TypeId | GetTypeId () |
Register this type. | |
Static Public Member Functions inherited from ns3::ObjectBase | |
static TypeId | GetTypeId () |
Get the type ID. | |
Static Public Attributes | |
static const uint64_t | NS_PER_SEC = (uint64_t)1000000000 |
Conversion constant between ns and s. | |
static const uint64_t | US_PER_NS = (uint64_t)1000 |
Conversion constant between μs and ns. | |
static const uint64_t | US_PER_SEC = (uint64_t)1000000 |
Conversion constant between μs and seconds. | |
Protected Member Functions | |
uint64_t | DoEventEnd () override |
Return the amount of real time elapsed since the last call to EventStart. | |
void | DoEventStart () override |
Record the normalized real time at which the current event is starting execution. | |
uint64_t | DoGetCurrentRealtime () override |
Retrieve the value of the origin of the underlying normalized wall clock time in Time resolution units. | |
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. | |
bool | DoRealtime () override |
Return true if this synchronizer is actually synchronizing to a realtime clock. | |
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. | |
void | DoSetOrigin (uint64_t ns) override |
Establish a correspondence between a simulation time and a wall-clock (real) time. | |
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. | |
bool | DoSynchronize (uint64_t nsCurrent, uint64_t nsDelay) override |
Wait until the real time is in sync with the specified simulation time. | |
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. | |
uint64_t | GetNormalizedRealtime () |
Get the current normalized real time, in ns. | |
uint64_t | GetRealtime () |
Get the current absolute real time (in ns since the epoch). | |
bool | SleepWait (uint64_t ns) |
Put our process to sleep for some number of nanoseconds. | |
bool | SpinWait (uint64_t ns) |
Do a busy-wait until the normalized realtime equals the argument or the condition variable becomes true . | |
Protected Member Functions inherited from ns3::Object | |
Object (const Object &o) | |
Copy an Object. | |
virtual void | DoDispose () |
Destructor implementation. | |
virtual void | DoInitialize () |
Initialize() implementation. | |
virtual void | NotifyNewAggregate () |
Notify all Objects aggregated to this one of a new Object being aggregated. | |
Protected Member Functions inherited from ns3::ObjectBase | |
void | ConstructSelf (const AttributeConstructionList &attributes) |
Complete construction of ObjectBase; invoked by derived classes. | |
virtual void | NotifyConstructionCompleted () |
Notifier called once the ObjectBase is fully constructed. | |
Protected Attributes | |
bool | m_condition |
The condition state. | |
std::condition_variable | m_conditionVariable |
Condition variable for thread synchronizer. | |
uint64_t | m_jiffy |
Size of the system clock tick, as reported by clock_getres , in ns. | |
std::mutex | m_mutex |
Mutex controlling access to the condition variable. | |
uint64_t | m_nsEventStart |
Time recorded by DoEventStart. | |
Protected Attributes inherited from ns3::Synchronizer | |
uint64_t | m_realtimeOriginNano |
The real time, in ns, when SetOrigin was called. | |
uint64_t | m_simOriginNano |
The simulation time, in ns, when SetOrigin was called. | |
Additional Inherited Members | |
Related Symbols inherited from ns3::ObjectBase | |
static TypeId | GetObjectIid () |
Ensure the TypeId for ObjectBase gets fully configured to anchor the inheritance tree properly. | |
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.:
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.
Definition at line 57 of file wall-clock-synchronizer.h.
ns3::WallClockSynchronizer::WallClockSynchronizer | ( | ) |
Constructor.
Definition at line 37 of file wall-clock-synchronizer.cc.
References m_jiffy, NS_LOG_FUNCTION, and NS_LOG_INFO.
|
override |
|
overrideprotectedvirtual |
Return the amount of real time elapsed since the last call to EventStart.
Implements ns3::Synchronizer.
Definition at line 294 of file wall-clock-synchronizer.cc.
References GetNormalizedRealtime(), m_nsEventStart, and NS_LOG_FUNCTION.
|
overrideprotectedvirtual |
Record the normalized real time at which the current event is starting execution.
Implements ns3::Synchronizer.
Definition at line 287 of file wall-clock-synchronizer.cc.
References GetNormalizedRealtime(), m_nsEventStart, and NS_LOG_FUNCTION.
|
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.
Implements ns3::Synchronizer.
Definition at line 85 of file wall-clock-synchronizer.cc.
References GetNormalizedRealtime(), and NS_LOG_FUNCTION.
|
overrideprotectedvirtual |
Get the drift between the real time clock used to synchronize the simulation and the current simulation time.
[in] | ns | Simulation time in ns. |
Implements ns3::Synchronizer.
Definition at line 108 of file wall-clock-synchronizer.cc.
References GetNormalizedRealtime(), and NS_LOG_FUNCTION.
Referenced by DoSynchronize(), and DriftCorrect().
|
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.
true
if locked with realtime, false
if not. Implements ns3::Synchronizer.
Definition at line 78 of file wall-clock-synchronizer.cc.
References NS_LOG_FUNCTION.
|
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.
[in] | cond | The new value for the condition variable. |
Implements ns3::Synchronizer.
Definition at line 280 of file wall-clock-synchronizer.cc.
References m_condition, and NS_LOG_FUNCTION.
|
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.
[in] | ns | The simulation time we need to use as the origin (normalized to nanosecond units). |
Implements ns3::Synchronizer.
Definition at line 92 of file wall-clock-synchronizer.cc.
References GetRealtime(), ns3::Synchronizer::m_realtimeOriginNano, NS_LOG_FUNCTION, and NS_LOG_INFO.
|
overrideprotectedvirtual |
Tell a possible simulator thread waiting in the DoSynchronize method that an event has happened which demands a reevaluation of the wait time.
Implements ns3::Synchronizer.
Definition at line 265 of file wall-clock-synchronizer.cc.
References m_condition, m_conditionVariable, m_mutex, and NS_LOG_FUNCTION.
|
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.
[in] | nsCurrent | The current simulation time (in nanosecond units). |
[in] | nsDelay | The simulation time we need to wait for (normalized to nanosecond units). |
true
if the function ran to completion, false
if it was interrupted by a Signal. Implements ns3::Synchronizer.
Definition at line 148 of file wall-clock-synchronizer.cc.
References DoGetDrift(), DriftCorrect(), m_jiffy, NS_LOG_FUNCTION, NS_LOG_INFO, SleepWait(), and SpinWait().
|
protected |
Compute a correction to the nominal delay to account for realtime drift since the last DoSynchronize.
[in] | nsNow | The current simulation time (in nanosecond units). |
[in] | nsDelay | The simulation time we need to wait for (normalized to nanosecond units). |
Definition at line 336 of file wall-clock-synchronizer.cc.
References DoGetDrift(), and NS_LOG_FUNCTION.
Referenced by DoSynchronize().
|
protected |
Get the current normalized real time, in ns.
Definition at line 375 of file wall-clock-synchronizer.cc.
References GetRealtime(), ns3::Synchronizer::m_realtimeOriginNano, and NS_LOG_FUNCTION.
Referenced by DoEventEnd(), DoEventStart(), DoGetCurrentRealtime(), DoGetDrift(), and SpinWait().
|
protected |
Get the current absolute real time (in ns since the epoch).
Definition at line 367 of file wall-clock-synchronizer.cc.
References NS_LOG_FUNCTION.
Referenced by DoSetOrigin(), and GetNormalizedRealtime().
|
static |
Get the registered TypeId for this class.
Definition at line 30 of file wall-clock-synchronizer.cc.
References ns3::TypeId::SetParent().
|
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
.
[in] | ns | The target normalized real time we should wait for. |
true
if we reached the target time, false
if we returned because the condition was set. Definition at line 322 of file wall-clock-synchronizer.cc.
References m_condition, m_conditionVariable, m_mutex, and NS_LOG_FUNCTION.
Referenced by DoSynchronize().
|
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.
[in] | ns | The target normalized real time we should wait for. |
true
if we reached the target time, false
if we returned because the condition was set. Definition at line 301 of file wall-clock-synchronizer.cc.
References GetNormalizedRealtime(), m_condition, and NS_LOG_FUNCTION.
Referenced by DoSynchronize().
|
protected |
The condition state.
Definition at line 163 of file wall-clock-synchronizer.h.
Referenced by DoSetCondition(), DoSignal(), SleepWait(), and SpinWait().
|
protected |
Condition variable for thread synchronizer.
Definition at line 159 of file wall-clock-synchronizer.h.
Referenced by DoSignal(), and SleepWait().
|
protected |
Size of the system clock tick, as reported by clock_getres
, in ns.
Definition at line 154 of file wall-clock-synchronizer.h.
Referenced by WallClockSynchronizer(), and DoSynchronize().
|
protected |
Mutex controlling access to the condition variable.
Definition at line 161 of file wall-clock-synchronizer.h.
Referenced by DoSignal(), and SleepWait().
|
protected |
Time recorded by DoEventStart.
Definition at line 156 of file wall-clock-synchronizer.h.
Referenced by DoEventEnd(), and DoEventStart().
|
static |
Conversion constant between ns and s.
Definition at line 76 of file wall-clock-synchronizer.h.
|
static |
Conversion constant between μs and ns.
Definition at line 72 of file wall-clock-synchronizer.h.
|
static |
Conversion constant between μs and seconds.
Definition at line 74 of file wall-clock-synchronizer.h.