A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
default-simulator-impl.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005,2006 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8
10
11#include "assert.h"
12#include "log.h"
13#include "scheduler.h"
14#include "simulator.h"
15
16#include <cmath>
17
18/**
19 * \file
20 * \ingroup simulator
21 * ns3::DefaultSimulatorImpl implementation.
22 */
23
24namespace ns3
25{
26
27// Note: Logging in this file is largely avoided due to the
28// number of calls that are made to these functions and the possibility
29// of causing recursions leading to stack overflow
30NS_LOG_COMPONENT_DEFINE("DefaultSimulatorImpl");
31
32NS_OBJECT_ENSURE_REGISTERED(DefaultSimulatorImpl);
33
34TypeId
36{
37 static TypeId tid = TypeId("ns3::DefaultSimulatorImpl")
39 .SetGroupName("Core")
40 .AddConstructor<DefaultSimulatorImpl>();
41 return tid;
42}
43
57
62
63void
65{
66 NS_LOG_FUNCTION(this);
68
69 while (!m_events->IsEmpty())
70 {
71 Scheduler::Event next = m_events->RemoveNext();
72 next.impl->Unref();
73 }
74 m_events = nullptr;
76}
77
78void
80{
81 NS_LOG_FUNCTION(this);
82 while (!m_destroyEvents.empty())
83 {
84 Ptr<EventImpl> ev = m_destroyEvents.front().PeekEventImpl();
85 m_destroyEvents.pop_front();
86 NS_LOG_LOGIC("handle destroy " << ev);
87 if (!ev->IsCancelled())
88 {
89 ev->Invoke();
90 }
91 }
92}
93
94void
96{
97 NS_LOG_FUNCTION(this << schedulerFactory);
98 Ptr<Scheduler> scheduler = schedulerFactory.Create<Scheduler>();
99
100 if (m_events)
101 {
102 while (!m_events->IsEmpty())
103 {
104 Scheduler::Event next = m_events->RemoveNext();
105 scheduler->Insert(next);
106 }
107 }
108 m_events = scheduler;
109}
110
111// System ID for non-distributed simulation is always zero
114{
115 return 0;
116}
117
118void
120{
121 Scheduler::Event next = m_events->RemoveNext();
122
123 PreEventHook(EventId(next.impl, next.key.m_ts, next.key.m_context, next.key.m_uid));
124
125 NS_ASSERT(next.key.m_ts >= m_currentTs);
127 m_eventCount++;
128
129 NS_LOG_LOGIC("handle " << next.key.m_ts);
130 m_currentTs = next.key.m_ts;
132 m_currentUid = next.key.m_uid;
133 next.impl->Invoke();
134 next.impl->Unref();
135
137}
138
139bool
141{
142 return m_events->IsEmpty() || m_stop;
143}
144
145void
147{
149 {
150 return;
151 }
152
153 // swap queues
154 EventsWithContext eventsWithContext;
155 {
156 std::unique_lock lock{m_eventsWithContextMutex};
157 m_eventsWithContext.swap(eventsWithContext);
159 }
160 while (!eventsWithContext.empty())
161 {
162 EventWithContext event = eventsWithContext.front();
163 eventsWithContext.pop_front();
165 ev.impl = event.event;
166 ev.key.m_ts = m_currentTs + event.timestamp;
167 ev.key.m_context = event.context;
168 ev.key.m_uid = m_uid;
169 m_uid++;
171 m_events->Insert(ev);
172 }
173}
174
175void
177{
178 NS_LOG_FUNCTION(this);
179 // Set the current threadId as the main threadId
180 m_mainThreadId = std::this_thread::get_id();
182 m_stop = false;
183
184 while (!m_events->IsEmpty() && !m_stop)
185 {
187 }
188
189 // If the simulator stopped naturally by lack of events, make a
190 // consistency test to check that we didn't lose any events along the way.
191 NS_ASSERT(!m_events->IsEmpty() || m_unscheduledEvents == 0);
192}
193
194void
196{
197 NS_LOG_FUNCTION(this);
198 m_stop = true;
199}
200
203{
204 NS_LOG_FUNCTION(this << delay.GetTimeStep());
205 return Simulator::Schedule(delay, &Simulator::Stop);
206}
207
208//
209// Schedule an event for a _relative_ time in the future.
210//
213{
214 NS_LOG_FUNCTION(this << delay.GetTimeStep() << event);
215 NS_ASSERT_MSG(m_mainThreadId == std::this_thread::get_id(),
216 "Simulator::Schedule Thread-unsafe invocation!");
217
218 NS_ASSERT_MSG(delay.IsPositive(), "DefaultSimulatorImpl::Schedule(): Negative delay");
219 Time tAbsolute = delay + TimeStep(m_currentTs);
220
222 ev.impl = event;
223 ev.key.m_ts = (uint64_t)tAbsolute.GetTimeStep();
224 ev.key.m_context = GetContext();
225 ev.key.m_uid = m_uid;
226 m_uid++;
228 m_events->Insert(ev);
229 return EventId(event, ev.key.m_ts, ev.key.m_context, ev.key.m_uid);
230}
231
232void
234{
235 NS_LOG_FUNCTION(this << context << delay.GetTimeStep() << event);
236
237 if (m_mainThreadId == std::this_thread::get_id())
238 {
239 Time tAbsolute = delay + TimeStep(m_currentTs);
241 ev.impl = event;
242 ev.key.m_ts = (uint64_t)tAbsolute.GetTimeStep();
243 ev.key.m_context = context;
244 ev.key.m_uid = m_uid;
245 m_uid++;
247 m_events->Insert(ev);
248 }
249 else
250 {
252 ev.context = context;
253 // Current time added in ProcessEventsWithContext()
254 ev.timestamp = delay.GetTimeStep();
255 ev.event = event;
256 {
257 std::unique_lock lock{m_eventsWithContextMutex};
258 m_eventsWithContext.push_back(ev);
260 }
261 }
262}
263
266{
267 NS_ASSERT_MSG(m_mainThreadId == std::this_thread::get_id(),
268 "Simulator::ScheduleNow Thread-unsafe invocation!");
269
270 return Schedule(Time(0), event);
271}
272
275{
276 NS_ASSERT_MSG(m_mainThreadId == std::this_thread::get_id(),
277 "Simulator::ScheduleDestroy Thread-unsafe invocation!");
278
279 EventId id(Ptr<EventImpl>(event, false), m_currentTs, 0xffffffff, 2);
280 m_destroyEvents.push_back(id);
281 m_uid++;
282 return id;
283}
284
285Time
287{
288 // Do not add function logging here, to avoid stack overflow
289 return TimeStep(m_currentTs);
290}
291
292Time
294{
295 if (IsExpired(id))
296 {
297 return TimeStep(0);
298 }
299 else
300 {
301 return TimeStep(id.GetTs() - m_currentTs);
302 }
303}
304
305void
307{
308 if (id.GetUid() == EventId::UID::DESTROY)
309 {
310 // destroy events.
311 for (auto i = m_destroyEvents.begin(); i != m_destroyEvents.end(); i++)
312 {
313 if (*i == id)
314 {
315 m_destroyEvents.erase(i);
316 break;
317 }
318 }
319 return;
320 }
321 if (IsExpired(id))
322 {
323 return;
324 }
325 Scheduler::Event event;
326 event.impl = id.PeekEventImpl();
327 event.key.m_ts = id.GetTs();
328 event.key.m_context = id.GetContext();
329 event.key.m_uid = id.GetUid();
330 m_events->Remove(event);
331 event.impl->Cancel();
332 // whenever we remove an event from the event list, we have to unref it.
333 event.impl->Unref();
334
336}
337
338void
340{
341 if (!IsExpired(id))
342 {
343 id.PeekEventImpl()->Cancel();
344 }
345}
346
347bool
349{
350 if (id.GetUid() == EventId::UID::DESTROY)
351 {
352 if (id.PeekEventImpl() == nullptr || id.PeekEventImpl()->IsCancelled())
353 {
354 return true;
355 }
356 // destroy events.
357 for (auto i = m_destroyEvents.begin(); i != m_destroyEvents.end(); i++)
358 {
359 if (*i == id)
360 {
361 return false;
362 }
363 }
364 return true;
365 }
366 return id.PeekEventImpl() == nullptr || id.GetTs() < m_currentTs ||
367 (id.GetTs() == m_currentTs && id.GetUid() <= m_currentUid) ||
368 id.PeekEventImpl()->IsCancelled();
369}
370
371Time
373{
374 return TimeStep(0x7fffffffffffffffLL);
375}
376
382
383uint64_t
388
389} // namespace ns3
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
The default single process simulator implementation.
bool m_stop
Flag calling for the end of the simulation.
bool m_eventsWithContextEmpty
Flag true if all events with context have been moved to the primary event queue.
uint32_t m_currentContext
Execution context of the current event.
void ProcessEventsWithContext()
Move events from a different context into the main event queue.
static TypeId GetTypeId()
Register this type.
void Run() override
Run the simulation.
uint32_t GetSystemId() const override
Get the system id of this simulator.
DestroyEvents m_destroyEvents
The container of events to run at Destroy.
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.
Ptr< Scheduler > m_events
The event priority queue.
void Remove(const EventId &id) override
Remove an event from the event list.
Time GetDelayLeft(const EventId &id) const override
Get the remaining time until this event will execute.
uint64_t m_currentTs
Timestamp of the current event.
uint64_t GetEventCount() const override
Get the number of events executed.
std::list< EventWithContext > EventsWithContext
Container type for the events from a different context.
void Cancel(const EventId &id) override
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
uint32_t GetContext() const override
Get the current simulation context.
Time Now() const override
Return the current simulation virtual time.
uint64_t m_eventCount
The event count.
void DoDispose() override
Destructor implementation.
int m_unscheduledEvents
Number of events that have been inserted but not yet scheduled, not counting the Destroy events; this...
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 GetMaximumSimulationTime() const override
Get the maximum representable simulation time.
EventId Schedule(const Time &delay, EventImpl *event) override
Schedule a future event execution (in the same context).
void Destroy() override
Execute the events scheduled with ScheduleDestroy().
std::mutex m_eventsWithContextMutex
Mutex to control access to the list of events with context.
uint32_t m_uid
Next event unique id.
uint32_t m_currentUid
Unique id of the current event.
~DefaultSimulatorImpl() override
Destructor.
void Stop() override
Tell the Simulator the calling event should be the last one executed.
bool IsFinished() const override
Check if the simulation should finish.
void ProcessOneEvent()
Process the next event.
bool IsExpired(const EventId &id) const override
Check if an event has already run or been cancelled.
std::thread::id m_mainThreadId
Main execution thread.
void SetScheduler(ObjectFactory schedulerFactory) override
Set the Scheduler to be used to manage the event list.
EventsWithContext m_eventsWithContext
The container of events from a different context.
An identifier for simulation events.
Definition event-id.h:45
@ INVALID
Invalid UID value.
Definition event-id.h:51
@ VALID
Schedule(), etc.
Definition event-id.h:59
@ DESTROY
ScheduleDestroy() events.
Definition event-id.h:55
A simulation event.
Definition event-impl.h:35
void Invoke()
Called by the simulation engine to notify the event that it is time to execute.
Definition event-impl.cc:36
void Cancel()
Marks the event as 'canceled'.
Definition event-impl.cc:46
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
virtual void DoDispose()
Destructor implementation.
Definition object.cc:433
Smart pointer class similar to boost::intrusive_ptr.
Maintain the event list.
Definition scheduler.h:146
void Unref() const
Decrement the reference count.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
@ NO_CONTEXT
Flag for events not associated with any particular context.
Definition simulator.h:199
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
The SimulatorImpl base class.
virtual void PreEventHook(const EventId &id)
Hook called before processing each event.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
bool IsPositive() const
Exactly equivalent to t >= 0.
Definition nstime.h:322
int64_t GetTimeStep() const
Get the raw time value, in the current resolution unit.
Definition nstime.h:434
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
ns3::DefaultSimulatorImpl declaration.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition assert.h:75
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#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:35
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Scheduler abstract base class, ns3::Scheduler::Event and ns3::Scheduler::EventKey declarations.
ns3::Simulator declaration.
Wrap an event with its execution context.
EventImpl * event
The event implementation.
Scheduler event.
Definition scheduler.h:173
EventKey key
Key for sorting and ordering Events.
Definition scheduler.h:175
EventImpl * impl
Pointer to the event implementation.
Definition scheduler.h:174
uint32_t m_context
Event context.
Definition scheduler.h:162
uint64_t m_ts
Event time stamp.
Definition scheduler.h:160
uint32_t m_uid
Event unique id.
Definition scheduler.h:161