A Discrete-Event Network Simulator
API
synchronizer.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #include "synchronizer.h"
20 #include "log.h"
21 
28 namespace ns3 {
29 
30 NS_LOG_COMPONENT_DEFINE ("Synchronizer");
31 
32 NS_OBJECT_ENSURE_REGISTERED (Synchronizer);
33 
34 TypeId
36 {
37  static TypeId tid = TypeId ("ns3::Synchronizer")
38  .SetParent<Object> ()
39  .SetGroupName ("Core")
40  ;
41  return tid;
42 }
43 
45  : m_realtimeOriginNano (0),
46  m_simOriginNano (0)
47 {
48  NS_LOG_FUNCTION (this);
49 }
50 
52 {
53  NS_LOG_FUNCTION (this);
54 }
55 
56 bool
58 {
59  NS_LOG_FUNCTION (this);
60  return DoRealtime ();
61 }
62 
63 uint64_t
65 {
66  NS_LOG_FUNCTION (this);
68 }
69 
70 void
72 {
73  NS_LOG_FUNCTION (this << ts);
76 }
77 
78 uint64_t
80 {
81  NS_LOG_FUNCTION (this);
83 }
84 
85 int64_t
87 {
88  NS_LOG_FUNCTION (this << ts);
89  int64_t tDrift = DoGetDrift (TimeStepToNanosecond (ts));
90 
91  if (tDrift < 0)
92  {
93  return -static_cast<int64_t> (NanosecondToTimeStep (-tDrift));
94  }
95  else
96  {
97  return static_cast<int64_t> (NanosecondToTimeStep (tDrift));
98  }
99 }
100 
101 bool
102 Synchronizer::Synchronize (uint64_t tsCurrent, uint64_t tsDelay)
103 {
104  NS_LOG_FUNCTION (this << tsCurrent << tsDelay);
105  return DoSynchronize (TimeStepToNanosecond (tsCurrent),
106  TimeStepToNanosecond (tsDelay));
107 }
108 
109 void
111 {
112  NS_LOG_FUNCTION (this);
113  DoSignal ();
114 }
115 
116 void
118 {
119  NS_LOG_FUNCTION (this << cond);
120  DoSetCondition (cond);
121 }
122 
123 void
125 {
126  NS_LOG_FUNCTION (this);
127  DoEventStart ();
128 }
129 
130 uint64_t
132 {
133  NS_LOG_FUNCTION (this);
134  return NanosecondToTimeStep (DoEventEnd ());
135 }
136 
137 uint64_t
139 {
140  NS_LOG_FUNCTION (this << ts);
141  return TimeStep (ts).GetNanoSeconds ();
142 }
143 
144 uint64_t
146 {
147  NS_LOG_FUNCTION (this << ns);
148  return NanoSeconds (ns).GetTimeStep ();
149 }
150 
151 } // namespace ns3
152 
153 
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 void DoSetCondition(bool)=0
Set the condition variable to tell a possible simulator thread waiting in the Synchronize method that...
#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:45
uint64_t NanosecondToTimeStep(uint64_t ns)
Convert a normalized nanosecond time step into a simulator time step (in Time resolution units)...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
uint64_t GetCurrentRealtime(void)
Retrieve the value of the origin of the underlying normalized wall clock time in simulator timestep u...
Definition: synchronizer.cc:64
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1302
void SetCondition(bool)
Set the condition variable that tells a possible simulator thread waiting in the Synchronize method t...
void EventStart(void)
Ask the synchronizer to remember what time it is.
uint64_t EventEnd(void)
Ask the synchronizer to return the time step between the instant remembered during EventStart and now...
virtual void DoSetOrigin(uint64_t ns)=0
Establish a correspondence between a simulation time and a wall-clock (real) time.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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...
bool Realtime(void)
Return true if this synchronizer is actually synchronizing to a realtime clock.
Definition: synchronizer.cc:57
uint64_t GetOrigin(void)
Retrieve the value of the origin of the simulation time in Time.resolution units. ...
Definition: synchronizer.cc:79
virtual uint64_t DoGetCurrentRealtime(void)=0
Retrieve the value of the origin of the underlying normalized wall clock time in Time resolution unit...
Synchronizer()
Constructor.
Definition: synchronizer.cc:44
ns3::Synchronizer declaration.
virtual void DoEventStart(void)=0
Record the normalized real time at which the current event is starting execution. ...
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:86
virtual void DoSignal(void)=0
Tell a possible simulator thread waiting in the DoSynchronize method that an event has happened which...
uint64_t m_simOriginNano
The simulation time, in ns, when SetOrigin was called.
Definition: synchronizer.h:316
static TypeId GetTypeId(void)
Get the registered TypeId for this class.
Definition: synchronizer.cc:35
void SetOrigin(uint64_t ts)
Establish a correspondence between a simulation time and the synchronizer real time.
Definition: synchronizer.cc:71
void Signal(void)
Tell a possible simulator thread waiting in the Synchronize method that an event has happened which d...
uint64_t TimeStepToNanosecond(uint64_t ts)
Convert a simulator time step (in Time resolution units) to a normalized time step in nanosecond unit...
A base class which provides memory management and object aggregation.
Definition: object.h:87
virtual uint64_t DoEventEnd(void)=0
Return the amount of real time elapsed since the last call to EventStart.
Debug message logging.
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
virtual ~Synchronizer()
Destructor.
Definition: synchronizer.cc:51
virtual bool DoRealtime(void)=0
Return true if this synchronizer is actually synchronizing to a realtime clock.
int64_t GetTimeStep(void) const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:415
virtual bool DoSynchronize(uint64_t nsCurrent, uint64_t nsDelay)=0
Wait until the real time is in sync with the specified simulation time.