A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
synchronizer.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 University of Washington
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
7#include "synchronizer.h"
8
9#include "log.h"
10
11/**
12 * \file
13 * \ingroup realtime
14 * ns3::Synchronizer implementation.
15 */
16
17namespace ns3
18{
19
20NS_LOG_COMPONENT_DEFINE("Synchronizer");
21
22NS_OBJECT_ENSURE_REGISTERED(Synchronizer);
23
24TypeId
26{
27 static TypeId tid = TypeId("ns3::Synchronizer").SetParent<Object>().SetGroupName("Core");
28 return tid;
29}
30
32 : m_realtimeOriginNano(0),
33 m_simOriginNano(0)
34{
35 NS_LOG_FUNCTION(this);
36}
37
42
43bool
45{
46 NS_LOG_FUNCTION(this);
47 return DoRealtime();
48}
49
50uint64_t
56
57void
64
65uint64_t
71
72int64_t
74{
75 NS_LOG_FUNCTION(this << ts);
76 int64_t tDrift = DoGetDrift(TimeStepToNanosecond(ts));
77
78 if (tDrift < 0)
79 {
80 return -static_cast<int64_t>(NanosecondToTimeStep(-tDrift));
81 }
82 else
83 {
84 return static_cast<int64_t>(NanosecondToTimeStep(tDrift));
85 }
86}
87
88bool
89Synchronizer::Synchronize(uint64_t tsCurrent, uint64_t tsDelay)
90{
91 NS_LOG_FUNCTION(this << tsCurrent << tsDelay);
92 return DoSynchronize(TimeStepToNanosecond(tsCurrent), TimeStepToNanosecond(tsDelay));
93}
94
95void
101
102void
104{
105 NS_LOG_FUNCTION(this << cond);
106 DoSetCondition(cond);
107}
108
109void
115
116uint64_t
122
123uint64_t
125{
126 NS_LOG_FUNCTION(this << ts);
127 return TimeStep(ts).GetNanoSeconds();
128}
129
130uint64_t
132{
133 NS_LOG_FUNCTION(this << ns);
134 return NanoSeconds(ns).GetTimeStep();
135}
136
137} // namespace ns3
A base class which provides memory management and object aggregation.
Definition object.h:78
virtual void DoSignal()=0
Tell a possible simulator thread waiting in the DoSynchronize method that an event has happened which...
void Signal()
Tell a possible simulator thread waiting in the Synchronize method that an event has happened which d...
void SetOrigin(uint64_t ts)
Establish a correspondence between a simulation time and the synchronizer real time.
virtual void DoSetCondition(bool cond)=0
Set the condition variable to tell a possible simulator thread waiting in the Synchronize method that...
virtual void DoEventStart()=0
Record the normalized real time at which the current event is starting execution.
uint64_t GetCurrentRealtime()
Retrieve the value of the origin of the underlying normalized wall clock time in simulator timestep u...
virtual uint64_t DoEventEnd()=0
Return the amount of real time elapsed since the last call to EventStart.
bool Realtime()
Return true if this synchronizer is actually synchronizing to a realtime clock.
void EventStart()
Ask the synchronizer to remember what time it is.
virtual bool DoSynchronize(uint64_t nsCurrent, uint64_t nsDelay)=0
Wait until the real time is in sync with the specified simulation time.
uint64_t TimeStepToNanosecond(uint64_t ts)
Convert a simulator time step (in Time resolution units) to a normalized time step in nanosecond unit...
static TypeId GetTypeId()
Get the registered TypeId for this class.
uint64_t GetOrigin()
Retrieve the value of the origin of the simulation time in Time.resolution units.
uint64_t NanosecondToTimeStep(uint64_t ns)
Convert a normalized nanosecond time step into a simulator time step (in Time resolution units).
~Synchronizer() override
Destructor.
virtual int64_t DoGetDrift(uint64_t ns)=0
Get the drift between the real time clock used to synchronize the simulation and the current simulati...
virtual uint64_t DoGetCurrentRealtime()=0
Retrieve the value of the origin of the underlying normalized wall clock time in Time resolution unit...
int64_t GetDrift(uint64_t ts)
Retrieve the difference between the real time clock used to synchronize the simulation and the simula...
virtual bool DoRealtime()=0
Return true if this synchronizer is actually synchronizing to a realtime clock.
uint64_t m_simOriginNano
The simulation time, in ns, when SetOrigin was called.
uint64_t EventEnd()
Ask the synchronizer to return the time step between the instant remembered during EventStart and now...
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 S...
void SetCondition(bool cond)
Set the condition variable that tells a possible simulator thread waiting in the Synchronize method t...
virtual void DoSetOrigin(uint64_t ns)=0
Establish a correspondence between a simulation time and a wall-clock (real) time.
Synchronizer()
Constructor.
int64_t GetTimeStep() const
Get the raw time value, in the current resolution unit.
Definition nstime.h:434
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Synchronizer declaration.