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. 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< Object > | GetObject () 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< Object > | GetObject (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... | |
SimpleRefCount & | operator= (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 | |
Related Functions inherited from ns3::ObjectBase | |
static TypeId | GetObjectIid () |
Ensure the TypeId for ObjectBase gets fully configured to anchor the inheritance tree properly. More... | |
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 68 of file wall-clock-synchronizer.h.
ns3::WallClockSynchronizer::WallClockSynchronizer | ( | ) |
Constructor.
Definition at line 48 of file wall-clock-synchronizer.cc.
References m_jiffy, NS_LOG_FUNCTION, NS_LOG_INFO, and two-ray-to-three-gpp-ch-calibration::num.
|
override |
|
overrideprotectedvirtual |
Return the amount of real time elapsed since the last call to EventStart.
Implements ns3::Synchronizer.
Definition at line 305 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 298 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 96 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 119 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 89 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 291 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 103 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 276 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 159 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 347 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 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().
|
protected |
Get the current absolute real time (in ns since the epoch).
Definition at line 378 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 41 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 333 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 312 of file wall-clock-synchronizer.cc.
References GetNormalizedRealtime(), m_condition, and NS_LOG_FUNCTION.
Referenced by DoSynchronize().
|
protected |
The condition state.
Definition at line 174 of file wall-clock-synchronizer.h.
Referenced by DoSetCondition(), DoSignal(), SleepWait(), and SpinWait().
|
protected |
Condition variable for thread synchronizer.
Definition at line 170 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 165 of file wall-clock-synchronizer.h.
Referenced by WallClockSynchronizer(), and DoSynchronize().
|
protected |
Mutex controlling access to the condition variable.
Definition at line 172 of file wall-clock-synchronizer.h.
Referenced by DoSignal(), and SleepWait().
|
protected |
Time recorded by DoEventStart.
Definition at line 167 of file wall-clock-synchronizer.h.
Referenced by DoEventEnd(), and DoEventStart().
|
static |
Conversion constant between ns and s.
Definition at line 87 of file wall-clock-synchronizer.h.
|
static |
Conversion constant between μs and ns.
Definition at line 83 of file wall-clock-synchronizer.h.
|
static |
Conversion constant between μs and seconds.
Definition at line 85 of file wall-clock-synchronizer.h.