A Discrete-Event Network Simulator
API
synchronizer.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 University of Washington
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17 
18 #include "synchronizer.h"
19 
20 #include "log.h"
21 
28 namespace ns3
29 {
30 
31 NS_LOG_COMPONENT_DEFINE("Synchronizer");
32 
33 NS_OBJECT_ENSURE_REGISTERED(Synchronizer);
34 
35 TypeId
37 {
38  static TypeId tid = TypeId("ns3::Synchronizer").SetParent<Object>().SetGroupName("Core");
39  return tid;
40 }
41 
43  : m_realtimeOriginNano(0),
44  m_simOriginNano(0)
45 {
46  NS_LOG_FUNCTION(this);
47 }
48 
50 {
51  NS_LOG_FUNCTION(this);
52 }
53 
54 bool
56 {
57  NS_LOG_FUNCTION(this);
58  return DoRealtime();
59 }
60 
61 uint64_t
63 {
64  NS_LOG_FUNCTION(this);
66 }
67 
68 void
70 {
71  NS_LOG_FUNCTION(this << ts);
74 }
75 
76 uint64_t
78 {
79  NS_LOG_FUNCTION(this);
81 }
82 
83 int64_t
85 {
86  NS_LOG_FUNCTION(this << ts);
87  int64_t tDrift = DoGetDrift(TimeStepToNanosecond(ts));
88 
89  if (tDrift < 0)
90  {
91  return -static_cast<int64_t>(NanosecondToTimeStep(-tDrift));
92  }
93  else
94  {
95  return static_cast<int64_t>(NanosecondToTimeStep(tDrift));
96  }
97 }
98 
99 bool
100 Synchronizer::Synchronize(uint64_t tsCurrent, uint64_t tsDelay)
101 {
102  NS_LOG_FUNCTION(this << tsCurrent << tsDelay);
103  return DoSynchronize(TimeStepToNanosecond(tsCurrent), TimeStepToNanosecond(tsDelay));
104 }
105 
106 void
108 {
109  NS_LOG_FUNCTION(this);
110  DoSignal();
111 }
112 
113 void
115 {
116  NS_LOG_FUNCTION(this << cond);
117  DoSetCondition(cond);
118 }
119 
120 void
122 {
123  NS_LOG_FUNCTION(this);
124  DoEventStart();
125 }
126 
127 uint64_t
129 {
130  NS_LOG_FUNCTION(this);
132 }
133 
134 uint64_t
136 {
137  NS_LOG_FUNCTION(this << ts);
138  return TimeStep(ts).GetNanoSeconds();
139 }
140 
141 uint64_t
143 {
144  NS_LOG_FUNCTION(this << ns);
145  return NanoSeconds(ns).GetTimeStep();
146 }
147 
148 } // namespace ns3
A base class which provides memory management and object aggregation.
Definition: object.h:89
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.
Definition: synchronizer.cc:69
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...
Definition: synchronizer.cc:62
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.
Definition: synchronizer.cc:55
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.
Definition: synchronizer.cc:36
uint64_t GetOrigin()
Retrieve the value of the origin of the simulation time in Time.resolution units.
Definition: synchronizer.cc:77
uint64_t NanosecondToTimeStep(uint64_t ns)
Convert a normalized nanosecond time step into a simulator time step (in Time resolution units).
~Synchronizer() override
Destructor.
Definition: synchronizer.cc:49
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...
Definition: synchronizer.cc:84
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.
Definition: synchronizer.h:320
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.
Definition: synchronizer.cc:42
int64_t GetTimeStep() const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:445
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:46
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1362
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Synchronizer declaration.