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... | |
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 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 (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<> | |
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... | |
template<typename T > | |
Ptr< T > | GetObject (void) const |
Get a pointer to the requested aggregated 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[[maybe_unused]]) | |
Copy constructor. More... | |
uint32_t | GetReferenceCount (void) const |
Get the reference count of the object. More... | |
SimpleRefCount & | operator= ([[maybe_unused]] 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 | |
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 (void) |
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.
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.:
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.
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 160 bytes (on a 64-bit architecture).
Definition at line 72 of file wall-clock-synchronizer.h.
ns3::WallClockSynchronizer::WallClockSynchronizer | ( | ) |
Constructor.
Definition at line 52 of file wall-clock-synchronizer.cc.
References m_jiffy, NS_LOG_FUNCTION, NS_LOG_INFO, and NS_PER_SEC.
|
virtual |
|
protectedvirtual |
Return the amount of real time elapsed since the last call to EventStart.
Implements ns3::Synchronizer.
Definition at line 315 of file wall-clock-synchronizer.cc.
References GetNormalizedRealtime(), m_nsEventStart, and NS_LOG_FUNCTION.
|
protectedvirtual |
Record the normalized real time at which the current event is starting execution.
Implements ns3::Synchronizer.
Definition at line 308 of file wall-clock-synchronizer.cc.
References GetNormalizedRealtime(), m_nsEventStart, and NS_LOG_FUNCTION.
|
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.
Implements ns3::Synchronizer.
Definition at line 105 of file wall-clock-synchronizer.cc.
References GetNormalizedRealtime(), and NS_LOG_FUNCTION.
|
protectedvirtual |
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 128 of file wall-clock-synchronizer.cc.
References GetNormalizedRealtime(), and NS_LOG_FUNCTION.
Referenced by DoSynchronize(), and DriftCorrect().
|
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.
true
if locked with realtime, false
if not. Implements ns3::Synchronizer.
Definition at line 98 of file wall-clock-synchronizer.cc.
References NS_LOG_FUNCTION.
|
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.
[in] | cond | The new value for the condition variable. |
Implements ns3::Synchronizer.
Definition at line 301 of file wall-clock-synchronizer.cc.
References m_condition, and NS_LOG_FUNCTION.
|
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.
[in] | ns | The simulation time we need to use as the origin (normalized to nanosecond units). |
Implements ns3::Synchronizer.
Definition at line 112 of file wall-clock-synchronizer.cc.
References GetRealtime(), ns3::Synchronizer::m_realtimeOriginNano, NS_LOG_FUNCTION, and NS_LOG_INFO.
|
protectedvirtual |
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 286 of file wall-clock-synchronizer.cc.
References m_condition, m_conditionVariable, m_mutex, and NS_LOG_FUNCTION.
|
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.
[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 168 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 357 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 397 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 388 of file wall-clock-synchronizer.cc.
References NS_LOG_FUNCTION, and TimevalToNs().
Referenced by DoSetOrigin(), and GetNormalizedRealtime().
|
static |
Get the registered TypeId for this class.
Definition at line 43 of file wall-clock-synchronizer.cc.
References ns3::TypeId::SetParent().
|
protected |
Convert an absolute time in ns to a timeval
.
[in] | ns | Absolute time in ns. |
[out] | tv | Converted timeval . |
Definition at line 404 of file wall-clock-synchronizer.cc.
References NS_ASSERT, NS_LOG_FUNCTION, NS_PER_SEC, and US_PER_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
.
[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 343 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 322 of file wall-clock-synchronizer.cc.
References GetNormalizedRealtime(), m_condition, and NS_LOG_FUNCTION.
Referenced by DoSynchronize().
|
protected |
Add two timeval
.
[in] | tv1 | The first timeval . |
[in] | tv2 | The second timeval . |
[out] | result | The sum of tv1 and tv2 . |
Definition at line 422 of file wall-clock-synchronizer.cc.
References NS_LOG_FUNCTION, test-ns3::result, and US_PER_SEC.
|
protected |
Convert a timeval
to absolute time, in ns.
[in] | tv | The input timeval . |
Definition at line 413 of file wall-clock-synchronizer.cc.
References NS_ASSERT, NS_LOG_FUNCTION, NS_PER_SEC, and US_PER_NS.
Referenced by GetRealtime().
|
protected |
The condition state.
Definition at line 205 of file wall-clock-synchronizer.h.
Referenced by DoSetCondition(), DoSignal(), SleepWait(), and SpinWait().
|
protected |
Condition variable for thread synchronizer.
Definition at line 201 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 196 of file wall-clock-synchronizer.h.
Referenced by WallClockSynchronizer(), and DoSynchronize().
|
protected |
Mutex controlling access to the condition variable.
Definition at line 203 of file wall-clock-synchronizer.h.
Referenced by DoSignal(), and SleepWait().
|
protected |
Time recorded by DoEventStart.
Definition at line 198 of file wall-clock-synchronizer.h.
Referenced by DoEventEnd(), and DoEventStart().
|
static |
Conversion constant between ns and s.
Definition at line 91 of file wall-clock-synchronizer.h.
Referenced by WallClockSynchronizer(), NsToTimeval(), and TimevalToNs().
|
static |
Conversion constant between μs and ns.
Definition at line 87 of file wall-clock-synchronizer.h.
Referenced by NsToTimeval(), and TimevalToNs().
|
static |
Conversion constant between μs and seconds.
Definition at line 89 of file wall-clock-synchronizer.h.
Referenced by TimevalAdd().