A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
realtime-simulator-impl.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 REALTIME_SIMULATOR_IMPL_H
19#define REALTIME_SIMULATOR_IMPL_H
20
21#include "assert.h"
22#include "event-impl.h"
23#include "log.h"
24#include "ptr.h"
25#include "scheduler.h"
26#include "simulator-impl.h"
27#include "synchronizer.h"
28
29#include <list>
30#include <mutex>
31#include <thread>
32
33/**
34 * \file
35 * \ingroup realtime
36 * ns3::RealtimeSimulatorImpl declaration.
37 */
38
39namespace ns3
40{
41
42/**
43 * \ingroup simulator
44 * \defgroup realtime Realtime Simulator
45 *
46 * Realtime simulator implementation.
47 */
48
49/**
50 * \ingroup realtime
51 *
52 * Realtime version of SimulatorImpl.
53 */
55{
56 public:
57 /**
58 * Get the registered TypeId for this class.
59 * \returns The TypeId.
60 */
61 static TypeId GetTypeId();
62
63 /**
64 * What to do when we can't maintain real time synchrony.
65 */
67 {
68 /**
69 * Make a best effort to keep synced to real-time.
70 *
71 * If we fall behind, keep going.
72 */
74 /**
75 * Keep to real time within the hard limit tolerance configured
76 * with SetHardLimit, or die trying.
77 *
78 * Falling behind by more than the hard limit tolerance triggers
79 * a fatal error.
80 * \see SetHardLimit
81 */
83 };
84
85 /** Constructor. */
87 /** Destructor. */
88 ~RealtimeSimulatorImpl() override;
89
90 // Inherited from SimulatorImpl
91 void Destroy() override;
92 bool IsFinished() const override;
93 void Stop() override;
94 EventId Stop(const Time& delay) override;
95 EventId Schedule(const Time& delay, EventImpl* event) override;
96 void ScheduleWithContext(uint32_t context, const Time& delay, EventImpl* event) override;
97 EventId ScheduleNow(EventImpl* event) override;
98 EventId ScheduleDestroy(EventImpl* event) override;
99 void Remove(const EventId& ev) override;
100 void Cancel(const EventId& ev) override;
101 bool IsExpired(const EventId& ev) const override;
102 void Run() override;
103 Time Now() const override;
104 Time GetDelayLeft(const EventId& id) const override;
105 Time GetMaximumSimulationTime() const override;
106 void SetScheduler(ObjectFactory schedulerFactory) override;
107 uint32_t GetSystemId() const override;
108 uint32_t GetContext() const override;
109 uint64_t GetEventCount() const override;
110
111 /** \copydoc ScheduleWithContext(uint32_t,const Time&,EventImpl*) */
112 void ScheduleRealtimeWithContext(uint32_t context, const Time& delay, EventImpl* event);
113 /**
114 * Schedule a future event execution (in the same context).
115 *
116 * @param [in] delay Delay until the event expires.
117 * @param [in] event The event to schedule.
118 */
119 void ScheduleRealtime(const Time& delay, EventImpl* event);
120 /**
121 * Schedule an event to run at the current virtual time.
122 *
123 * \param [in] context Event context.
124 * \param [in] event The event to schedule.
125 */
127 /**
128 * Schedule an event to run at the current virtual time.
129 *
130 * @param [in] event The event to schedule.
131 */
132 void ScheduleRealtimeNow(EventImpl* event);
133 /**
134 * Get the current real time from the synchronizer.
135 * \returns The current real time.
136 */
137 Time RealtimeNow() const;
138
139 /**
140 * Set the SynchronizationMode.
141 *
142 * \param [in] mode The new SynchronizationMode.
143 */
145 /**
146 * Get the SynchronizationMode.
147 * \returns The current SynchronizationMode.
148 */
150
151 /**
152 * Set the fatal error threshold for SynchronizationMode SYNC_HARD_LIMIT.
153 *
154 * \param [in] limit The maximum amount of real time we are allowed to fall
155 * behind before we trigger a fatal error.
156 */
157 void SetHardLimit(Time limit);
158 /**
159 * Get the current fatal error threshold for SynchronizationMode
160 * SYNC_HARD_LIMIT.
161 *
162 * \returns The hard limit threshold.
163 */
164 Time GetHardLimit() const;
165
166 private:
167 /**
168 * Is the simulator running?
169 * \returns \c true if we are running.
170 */
171 bool Running() const;
172 /**
173 * Check that the Synchronizer is locked to the real time clock.
174 * \return \c true if the Synchronizer is locked.
175 */
176 bool Realtime() const;
177 /**
178 * Get the timestep of the next event.
179 * \returns The timestep of the next event.
180 */
181 uint64_t NextTs() const;
182 /** Process the next event. */
183 void ProcessOneEvent();
184 /** Destructor implementation. */
185 void DoDispose() override;
186
187 /** Container type for events to be run at destroy time. */
188 typedef std::list<EventId> DestroyEvents;
189 /** Container for events to be run at destroy time. */
191 /** Has the stopping condition been reached? */
192 bool m_stop;
193 /** Is the simulator currently running. */
195
196 /**
197 * \name Mutex-protected variables.
198 *
199 * These variables are protected by #m_mutex.
200 */
201 /**@{*/
202 /** The event list. */
204 /**< Number of events in the event list. */
206 /**< Unique id for the next event to be scheduled. */
208 /**< Unique id of the current event. */
210 /**< Timestep of the current event. */
211 uint64_t m_currentTs;
212 /**< Execution context. */
214 /** The event count. */
215 uint64_t m_eventCount;
216 /**@}*/
217
218 /** Mutex to control access to key state. */
219 mutable std::mutex m_mutex;
220
221 /** The synchronizer in use to track real time. */
223
224 /** SynchronizationMode policy. */
226
227 /** The maximum allowable drift from real-time in SYNC_HARD_LIMIT mode. */
229
230 /** Main thread. */
231 std::thread::id m_main;
232};
233
234} // namespace ns3
235
236#endif /* REALTIME_SIMULATOR_IMPL_H */
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
An identifier for simulation events.
Definition: event-id.h:55
A simulation event.
Definition: event-impl.h:46
Instantiate subclasses of ns3::Object.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Realtime version of SimulatorImpl.
Time RealtimeNow() const
Get the current real time from the synchronizer.
void ScheduleRealtime(const Time &delay, EventImpl *event)
Schedule a future event execution (in the same context).
uint64_t GetEventCount() const override
Get the number of events executed.
Ptr< Scheduler > m_events
The event list.
uint32_t GetContext() const override
Get the current simulation context.
DestroyEvents m_destroyEvents
Container for events to be run at destroy time.
uint32_t GetSystemId() const override
Get the system id of this simulator.
int m_unscheduledEvents
Unique id for the next event to be scheduled.
void Stop() override
Tell the Simulator the calling event should be the last one executed.
EventId ScheduleDestroy(EventImpl *event) override
Schedule an event to run at the end of the simulation, after the Stop() time or condition has been re...
Time GetHardLimit() const
Get the current fatal error threshold for SynchronizationMode SYNC_HARD_LIMIT.
void ScheduleRealtimeNow(EventImpl *event)
Schedule an event to run at the current virtual time.
void Run() override
Run the simulation.
bool m_running
Is the simulator currently running.
uint32_t m_currentContext
The event list.
bool IsExpired(const EventId &ev) const override
Check if an event has already run or been cancelled.
std::mutex m_mutex
Mutex to control access to key state.
SynchronizationMode m_synchronizationMode
SynchronizationMode policy.
void DoDispose() override
Destructor implementation.
uint64_t m_currentTs
Execution context.
void Cancel(const EventId &ev) override
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
EventId Schedule(const Time &delay, EventImpl *event) override
Schedule a future event execution (in the same context).
void SetHardLimit(Time limit)
Set the fatal error threshold for SynchronizationMode SYNC_HARD_LIMIT.
~RealtimeSimulatorImpl() override
Destructor.
uint32_t m_uid
Unique id of the current event.
uint64_t m_eventCount
The event count.
uint32_t m_currentUid
Timestep of the current event.
void ScheduleRealtimeNowWithContext(uint32_t context, EventImpl *event)
Schedule an event to run at the current virtual time.
bool m_stop
Has the stopping condition been reached?
Ptr< Synchronizer > m_synchronizer
The synchronizer in use to track real time.
bool IsFinished() const override
Check if the simulation should finish.
bool Realtime() const
Check that the Synchronizer is locked to the real time clock.
SynchronizationMode
What to do when we can't maintain real time synchrony.
@ SYNC_BEST_EFFORT
Make a best effort to keep synced to real-time.
@ SYNC_HARD_LIMIT
Keep to real time within the hard limit tolerance configured with SetHardLimit, or die trying.
Time m_hardLimit
The maximum allowable drift from real-time in SYNC_HARD_LIMIT mode.
static TypeId GetTypeId()
Get the registered TypeId for this class.
void ScheduleRealtimeWithContext(uint32_t context, const Time &delay, EventImpl *event)
Schedule a future event execution (in a different context).
Time GetDelayLeft(const EventId &id) const override
Get the remaining time until this event will execute.
void SetScheduler(ObjectFactory schedulerFactory) override
Set the Scheduler to be used to manage the event list.
void ScheduleWithContext(uint32_t context, const Time &delay, EventImpl *event) override
Schedule a future event execution (in a different context).
EventId ScheduleNow(EventImpl *event) override
Schedule an event to run at the current virtual time.
bool Running() const
Is the simulator running?
std::thread::id m_main
Main thread.
void ProcessOneEvent()
Process the next event.
Time Now() const override
Return the current simulation virtual time.
void Remove(const EventId &ev) override
Remove an event from the event list.
void Destroy() override
Execute the events scheduled with ScheduleDestroy().
void SetSynchronizationMode(RealtimeSimulatorImpl::SynchronizationMode mode)
Set the SynchronizationMode.
uint64_t NextTs() const
Get the timestep of the next event.
RealtimeSimulatorImpl::SynchronizationMode GetSynchronizationMode() const
Get the SynchronizationMode.
std::list< EventId > DestroyEvents
Container type for events to be run at destroy time.
Time GetMaximumSimulationTime() const override
Get the maximum representable simulation time.
The SimulatorImpl base class.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:59
ns3::EventImpl declarations.
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Ptr smart pointer declaration and implementation.
ns3::Scheduler abstract base class, ns3::Scheduler::Event and ns3::Scheduler::EventKey declarations.
ns3::SimulatorImpl declaration.
ns3::Synchronizer declaration.