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 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
7#ifndef REALTIME_SIMULATOR_IMPL_H
8#define REALTIME_SIMULATOR_IMPL_H
9
10#include "assert.h"
11#include "event-impl.h"
12#include "log.h"
13#include "ptr.h"
14#include "scheduler.h"
15#include "simulator-impl.h"
16#include "synchronizer.h"
17
18#include <list>
19#include <mutex>
20#include <thread>
21
22/**
23 * \file
24 * \ingroup realtime
25 * ns3::RealtimeSimulatorImpl declaration.
26 */
27
28namespace ns3
29{
30
31/**
32 * \ingroup simulator
33 * \defgroup realtime Realtime Simulator
34 *
35 * Realtime simulator implementation.
36 */
37
38/**
39 * \ingroup realtime
40 *
41 * Realtime version of SimulatorImpl.
42 */
44{
45 public:
46 /**
47 * Get the registered TypeId for this class.
48 * \returns The TypeId.
49 */
50 static TypeId GetTypeId();
51
52 /**
53 * What to do when we can't maintain real time synchrony.
54 */
56 {
57 /**
58 * Make a best effort to keep synced to real-time.
59 *
60 * If we fall behind, keep going.
61 */
63 /**
64 * Keep to real time within the hard limit tolerance configured
65 * with SetHardLimit, or die trying.
66 *
67 * Falling behind by more than the hard limit tolerance triggers
68 * a fatal error.
69 * \see SetHardLimit
70 */
72 };
73
74 /** Constructor. */
76 /** Destructor. */
77 ~RealtimeSimulatorImpl() override;
78
79 // Inherited from SimulatorImpl
80 void Destroy() override;
81 bool IsFinished() const override;
82 void Stop() override;
83 EventId Stop(const Time& delay) override;
84 EventId Schedule(const Time& delay, EventImpl* event) override;
85 void ScheduleWithContext(uint32_t context, const Time& delay, EventImpl* event) override;
86 EventId ScheduleNow(EventImpl* event) override;
87 EventId ScheduleDestroy(EventImpl* event) override;
88 void Remove(const EventId& ev) override;
89 void Cancel(const EventId& ev) override;
90 bool IsExpired(const EventId& ev) const override;
91 void Run() override;
92 Time Now() const override;
93 Time GetDelayLeft(const EventId& id) const override;
94 Time GetMaximumSimulationTime() const override;
95 void SetScheduler(ObjectFactory schedulerFactory) override;
96 uint32_t GetSystemId() const override;
97 uint32_t GetContext() const override;
98 uint64_t GetEventCount() const override;
99
100 /** \copydoc ScheduleWithContext(uint32_t,const Time&,EventImpl*) */
101 void ScheduleRealtimeWithContext(uint32_t context, const Time& delay, EventImpl* event);
102 /**
103 * Schedule a future event execution (in the same context).
104 *
105 * @param [in] delay Delay until the event expires.
106 * @param [in] event The event to schedule.
107 */
108 void ScheduleRealtime(const Time& delay, EventImpl* event);
109 /**
110 * Schedule an event to run at the current virtual time.
111 *
112 * \param [in] context Event context.
113 * \param [in] event The event to schedule.
114 */
116 /**
117 * Schedule an event to run at the current virtual time.
118 *
119 * @param [in] event The event to schedule.
120 */
121 void ScheduleRealtimeNow(EventImpl* event);
122 /**
123 * Get the current real time from the synchronizer.
124 * \returns The current real time.
125 */
126 Time RealtimeNow() const;
127
128 /**
129 * Set the SynchronizationMode.
130 *
131 * \param [in] mode The new SynchronizationMode.
132 */
134 /**
135 * Get the SynchronizationMode.
136 * \returns The current SynchronizationMode.
137 */
139
140 /**
141 * Set the fatal error threshold for SynchronizationMode SYNC_HARD_LIMIT.
142 *
143 * \param [in] limit The maximum amount of real time we are allowed to fall
144 * behind before we trigger a fatal error.
145 */
146 void SetHardLimit(Time limit);
147 /**
148 * Get the current fatal error threshold for SynchronizationMode
149 * SYNC_HARD_LIMIT.
150 *
151 * \returns The hard limit threshold.
152 */
153 Time GetHardLimit() const;
154
155 private:
156 /**
157 * Is the simulator running?
158 * \returns \c true if we are running.
159 */
160 bool Running() const;
161 /**
162 * Check that the Synchronizer is locked to the real time clock.
163 * \return \c true if the Synchronizer is locked.
164 */
165 bool Realtime() const;
166 /**
167 * Get the timestep of the next event.
168 * \returns The timestep of the next event.
169 */
170 uint64_t NextTs() const;
171 /** Process the next event. */
172 void ProcessOneEvent();
173 /** Destructor implementation. */
174 void DoDispose() override;
175
176 /** Container type for events to be run at destroy time. */
177 typedef std::list<EventId> DestroyEvents;
178 /** Container for events to be run at destroy time. */
180 /** Has the stopping condition been reached? */
181 bool m_stop;
182 /** Is the simulator currently running. */
184
185 /**
186 * \name Mutex-protected variables.
187 *
188 * These variables are protected by #m_mutex.
189 */
190 /**@{*/
191 /** The event list. */
193 /**< Number of events in the event list. */
195 /**< Unique id for the next event to be scheduled. */
197 /**< Unique id of the current event. */
199 /**< Timestep of the current event. */
200 uint64_t m_currentTs;
201 /**< Execution context. */
203 /** The event count. */
204 uint64_t m_eventCount;
205 /**@}*/
206
207 /** Mutex to control access to key state. */
208 mutable std::mutex m_mutex;
209
210 /** The synchronizer in use to track real time. */
212
213 /** SynchronizationMode policy. */
215
216 /** The maximum allowable drift from real-time in SYNC_HARD_LIMIT mode. */
218
219 /** Main thread. */
220 std::thread::id m_main;
221};
222
223} // namespace ns3
224
225#endif /* REALTIME_SIMULATOR_IMPL_H */
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
An identifier for simulation events.
Definition event-id.h:45
A simulation event.
Definition event-impl.h:35
Instantiate subclasses of ns3::Object.
Smart pointer class similar to boost::intrusive_ptr.
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:94
a unique identifier for an interface.
Definition type-id.h:48
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.