A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
synchronizer.h
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#ifndef SYNCHRONIZER_H
19#define SYNCHRONIZER_H
20
21#include "nstime.h"
22#include "object.h"
23
24#include <stdint.h>
25
26/**
27 * @file
28 * @ingroup realtime
29 * ns3::Synchronizer declaration.
30 */
31
32namespace ns3
33{
34
35/**
36 * @ingroup realtime
37 * @brief Base class used for synchronizing the simulation events to some
38 * real time "wall clock."
39 *
40 * The simulation clock is maintained as a 64-bit integer in a unit specified
41 * by the user through the Time::SetResolution function. This means that
42 * it is not possible to specify event expiration times with anything better
43 * than this user-specified accuracy. We use this clock for the simulation
44 * time.
45 *
46 * The real-time clock is maintained as a 64-bit integer count of nanoseconds.
47 *
48 * The synchronization between the simulation clock and the real-time clock
49 * is maintained using a combination of sleep-waiting, busy-waiting and a
50 * feedback loop.
51 */
52class Synchronizer : public Object
53{
54 public:
55 /**
56 * Get the registered TypeId for this class.
57 * @returns The TypeId.
58 */
59 static TypeId GetTypeId();
60
61 /** Constructor. */
63 /** Destructor. */
64 ~Synchronizer() override;
65
66 /**
67 * @brief Return true if this synchronizer is actually synchronizing to a
68 * realtime clock.
69 *
70 * The simulator sometimes needs to know this.
71 *
72 * @returns @c true if locked with realtime, @c false if not.
73 */
74 bool Realtime();
75
76 /**
77 * @brief Retrieve the value of the origin of the underlying normalized wall
78 * clock time in simulator timestep units.
79 *
80 * @returns The normalized wall clock time (in Time resolution units).
81 * @see SetOrigin
82 */
83 uint64_t GetCurrentRealtime();
84
85 /**
86 * @brief Establish a correspondence between a simulation time and the
87 * synchronizer real time.
88 *
89 * This method is expected to be called at the "instant" before simulation
90 * begins. At this point, simulation time = 0, and a
91 * set = 0 in this method. We then associate this time with the current
92 * value of the real time clock that will be used to actually perform the
93 * synchronization.
94 *
95 * Subclasses are expected to implement the corresponding DoSetOrigin pure
96 * virtual method to do the actual real-time-clock-specific work
97 * of making the correspondence mentioned above.
98 *
99 * @param [in] ts The simulation time we should use as the origin (in
100 * Time resolution units).
101 * @see DoSetOrigin
102 */
103 void SetOrigin(uint64_t ts);
104
105 /**
106 * @brief Retrieve the value of the origin of the simulation time in
107 * Time.resolution units.
108 *
109 * @returns The simulation time used as the origin (in Time resolution units).
110 * @see SetOrigin
111 */
112 uint64_t GetOrigin();
113
114 /**
115 * @brief Retrieve the difference between the real time clock used to
116 * synchronize the simulation and the simulation time (in
117 * Time resolution units).
118 *
119 * @param [in] ts Simulation time in Time resolution units.
120 * @returns Simulation Time (in Time resolution units)
121 * minus the origin time (stored internally in nanosecond units).
122 * @see SetOrigin
123 * @see DoGetDrift
124 */
125 int64_t GetDrift(uint64_t ts);
126
127 /**
128 * @brief Wait until the real time is in sync with the specified simulation
129 * time or until the synchronizer is Signalled.
130 *
131 * This is where the real work of synchronization is done. The @c tsCurrent
132 * argument is the simulation time. The job of Synchronize is to
133 * translate from simulation time to synchronizer time (in a perfect world
134 * this is the same time) and then figure out how long in real-time it needs
135 * to wait until that synchronizer / simulation time comes around.
136 *
137 * Subclasses are expected to implement the corresponding DoSynchronize pure
138 * virtual method to do the actual real-time-clock-specific work of waiting
139 * (either busy-waiting or sleeping, or some combination thereof) until the
140 * requested simulation time.
141 *
142 * @param [in] tsCurrent The current simulation time (in Time resolution units).
143 * @param [in] tsDelay The simulation time we need to wait for (in Time
144 * resolution units).
145 * @returns @c true if the function ran to completion,
146 * @c false if it was interrupted by a Signal.
147 * @see DoSynchronize
148 * @see Signal
149 */
150 bool Synchronize(uint64_t tsCurrent, uint64_t tsDelay);
151
152 /**
153 * @brief Tell a possible simulator thread waiting in the Synchronize method
154 * that an event has happened which demands a reevaluation of the wait time.
155 *
156 * This will cause the thread to wake and return to the simulator proper
157 * where it can get its bearings.
158 *
159 * @see Synchronize
160 * @see DoSignal
161 */
162 void Signal();
163
164 /**
165 * @brief Set the condition variable that tells a possible simulator thread
166 * waiting in the Synchronize method that an event has happened which demands
167 * a reevaluation of the wait time.
168 *
169 * @param [in] cond The new value for the condition variable.
170 *
171 * @see Signal
172 */
173 void SetCondition(bool cond);
174
175 /**
176 * @brief Ask the synchronizer to remember what time it is.
177 *
178 * Typically used with EventEnd to determine the real execution time
179 * of a simulation event.
180 *
181 * @see EventEnd
182 */
183 void EventStart();
184
185 /**
186 * @brief Ask the synchronizer to return the time step between the instant
187 * remembered during EventStart and now.
188 *
189 * Used in conjunction with EventStart to determine the real execution time
190 * of a simulation event.
191 *
192 * @returns The elapsed real time, in ns.
193 * @see EventStart
194 */
195 uint64_t EventEnd();
196
197 protected:
198 /**
199 * @brief Establish a correspondence between a simulation time and a
200 * wall-clock (real) time.
201 *
202 * There are three timelines involved here: the simulation (virtual) time,
203 * the (absolute) wall-clock time and the (relative) synchronizer real time.
204 * Calling this method makes a correspondence between the origin of the
205 * synchronizer time and the current wall-clock time.
206 *
207 * This method is expected to be called at the "instant" before simulation
208 * begins. At this point, simulation time = 0, and synchronizer time is
209 * set = 0 in this method. We then associate this time with the current
210 * value of the real time clock that will be used to actually perform the
211 * synchronization.
212 *
213 * Subclasses are expected to implement this method to do the actual
214 * real-time-clock-specific work of making the correspondence mentioned above.
215 * for example, this is where the differences between Time parameters and
216 * parameters to clock_nanosleep would be dealt with.
217 *
218 * @param [in] ns The simulation time we need to use as the origin (normalized to
219 * nanosecond units).
220 * @see SetOrigin
221 */
222 virtual void DoSetOrigin(uint64_t ns) = 0;
223
224 /**
225 * @brief Return @c true if this synchronizer is actually synchronizing to a
226 * realtime clock.
227 *
228 * The simulator sometimes needs to know this.
229 *
230 * Subclasses are expected to implement this method to tell the outside world
231 * whether or not they are synchronizing to a realtime clock.
232 *
233 * @returns @c true if locked with realtime, @c false if not.
234 */
235 virtual bool DoRealtime() = 0;
236
237 /**
238 * @brief Retrieve the value of the origin of the underlying normalized wall
239 * clock time in Time resolution units.
240 *
241 * Subclasses are expected to implement this method to do the actual
242 * real-time-clock-specific work of getting the current time.
243 *
244 * @returns The normalized wall clock time (in nanosecond units).
245 * @see SetOrigin
246 */
247 virtual uint64_t DoGetCurrentRealtime() = 0;
248
249 /**
250 * @brief Wait until the real time is in sync with the specified simulation
251 * time.
252 *
253 * This is where the real work of synchronization is done. The
254 * @c nsCurrent argument is the simulation time (in ns). The job of
255 * DoSynchronize is to translate from simulation time to synchronizer time
256 * (in a perfect world these are the same time) and then figure out
257 * how long in real-time it needs to wait until that
258 * synchronizer / simulation time comes around.
259 *
260 * Subclasses are expected to implement this method to do the actual
261 * real-time-clock-specific work of waiting (either busy-waiting or sleeping,
262 * or some combination) until the requested simulation time.
263 *
264 * @param [in] nsCurrent The current simulation time (in nanosecond units).
265 * @param [in] nsDelay The simulation time we need to wait for (normalized to
266 * nanosecond units).
267 * @returns @c true if the function ran to completion,
268 * @c false if it was interrupted by a Signal.
269 * @see Synchronize
270 * @see Signal
271 */
272 virtual bool DoSynchronize(uint64_t nsCurrent, uint64_t nsDelay) = 0;
273
274 /**
275 * @brief Tell a possible simulator thread waiting in the
276 * DoSynchronize method that an event has happened which
277 * demands a reevaluation of the wait time.
278 *
279 * @see Signal
280 */
281 virtual void DoSignal() = 0;
282
283 /**
284 * @brief Set the condition variable to tell a possible simulator thread
285 * waiting in the Synchronize method that an event has happened which
286 * demands a reevaluation of the wait time.
287 *
288 * @param [in] cond The new value for the condition variable.
289 * @see SetCondition
290 */
291 virtual void DoSetCondition(bool cond) = 0;
292
293 /**
294 * @brief Get the drift between the real time clock used to synchronize
295 * the simulation and the current simulation time.
296 *
297 * @param [in] ns Simulation time in ns.
298 * @returns Drift in ns units.
299 * @see SetOrigin
300 * @see GetDrift
301 */
302 virtual int64_t DoGetDrift(uint64_t ns) = 0;
303
304 /**
305 * @brief Record the normalized real time at which the current
306 * event is starting execution.
307 */
308 virtual void DoEventStart() = 0;
309 /**
310 * @brief Return the amount of real time elapsed since the last call
311 * to EventStart.
312 *
313 * @returns The elapsed real time, in ns.
314 */
315 virtual uint64_t DoEventEnd() = 0;
316
317 /** The real time, in ns, when SetOrigin was called. */
319 /** The simulation time, in ns, when SetOrigin was called. */
321
322 private:
323 /**
324 * @brief Convert a simulator time step (in Time resolution units)
325 * to a normalized time step in nanosecond units.
326 *
327 * @param [in] ts The simulation time step to be normalized.
328 * @returns The simulation time step normalized to nanosecond units.
329 */
330 uint64_t TimeStepToNanosecond(uint64_t ts);
331
332 /**
333 * @brief Convert a normalized nanosecond time step into a
334 * simulator time step (in Time resolution units).
335 *
336 * @param [in] ns The nanosecond count step to be converted
337 * @returns The simulation time step to be interpreted in appropriate units.
338 */
339 uint64_t NanosecondToTimeStep(uint64_t ns);
340};
341
342} // namespace ns3
343
344#endif /* SYNCHRONIZER_H */
A base class which provides memory management and object aggregation.
Definition: object.h:89
Base class used for synchronizing the simulation events to some real time "wall clock....
Definition: synchronizer.h:53
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 m_realtimeOriginNano
The real time, in ns, when SetOrigin was called.
Definition: synchronizer.h:318
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
a unique identifier for an interface.
Definition: type-id.h:59
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Declaration of classes ns3::Time and ns3::TimeWithUnit, and the TimeValue implementation classes.
ns3::Object class declaration, which is the root of the Object hierarchy and Aggregation.