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/**
17 * @file
18 * @ingroup simulator
19 * ns3::DefaultSimulatorImpl implementation.
20 */
21
22namespace ns3
23{
24
25// Note: Logging in this file is largely avoided due to the
26// number of calls that are made to these functions and the possibility
27// of causing recursions leading to stack overflow
28NS_LOG_COMPONENT_DEFINE("DefaultSimulatorImpl");
29
31
34{
35 static TypeId tid = TypeId("ns3::DefaultSimulatorImpl")
37 .SetGroupName("Core")
38 .AddConstructor<DefaultSimulatorImpl>();
39 return tid;
40}
41
55
60
61void
63{
64 NS_LOG_FUNCTION(this);
66
67 while (!m_events->IsEmpty())
68 {
69 Scheduler::Event next = m_events->RemoveNext();
70 next.impl->Unref();
71 }
72 m_events = nullptr;
74}
75
76void
78{
79 NS_LOG_FUNCTION(this);
80 while (!m_destroyEvents.empty())
81 {
82 Ptr<EventImpl> ev = m_destroyEvents.front().PeekEventImpl();
83 m_destroyEvents.pop_front();
84 NS_LOG_LOGIC("handle destroy " << ev);
85 if (!ev->IsCancelled())
86 {
87 ev->Invoke();
88 }
89 }
90}
91
92void
94{
95 NS_LOG_FUNCTION(this << schedulerFactory);
96 Ptr<Scheduler> scheduler = schedulerFactory.Create<Scheduler>();
97
98 if (m_events)
99 {
100 while (!m_events->IsEmpty())
101 {
102 Scheduler::Event next = m_events->RemoveNext();
103 scheduler->Insert(next);
104 }
105 }
106 m_events = scheduler;
107}
108
109// System ID for non-distributed simulation is always zero
112{
113 return 0;
114}
115
116void
118{
119 Scheduler::Event next = m_events->RemoveNext();
120
121 PreEventHook(EventId(next.impl, next.key.m_ts, next.key.m_context, next.key.m_uid));
122
123 NS_ASSERT(next.key.m_ts >= m_currentTs);
125 m_eventCount++;
126
127 NS_LOG_LOGIC("handle " << next.key.m_ts);
128 m_currentTs = next.key.m_ts;
130 m_currentUid = next.key.m_uid;
131 next.impl->Invoke();
132 next.impl->Unref();
133
135}
136
137bool
139{
140 return m_events->IsEmpty() || m_stop;
141}
142
143void
145{
147 {
148 return;
149 }
150
151 // swap queues
152 EventsWithContext eventsWithContext;
153 {
154 std::unique_lock lock{m_eventsWithContextMutex};
155 m_eventsWithContext.swap(eventsWithContext);
157 }
158 while (!eventsWithContext.empty())
159 {
160 EventWithContext event = eventsWithContext.front();
161 eventsWithContext.pop_front();
163 ev.impl = event.event;
164 ev.key.m_ts = m_currentTs + event.timestamp;
165 ev.key.m_context = event.context;
166 ev.key.m_uid = m_uid;
167 m_uid++;
169 m_events->Insert(ev);
170 }
171}
172
173void
175{
176 NS_LOG_FUNCTION(this);
177 // Set the current threadId as the main threadId
178 m_mainThreadId = std::this_thread::get_id();
180 m_stop = false;
181
182 while (!m_events->IsEmpty() && !m_stop)
183 {
185 }
186
187 // If the simulator stopped naturally by lack of events, make a
188 // consistency test to check that we didn't lose any events along the way.
189 NS_ASSERT(!m_events->IsEmpty() || m_unscheduledEvents == 0);
190}
191
192void
194{
195 NS_LOG_FUNCTION(this);
196 m_stop = true;
197}
198
201{
202 NS_LOG_FUNCTION(this << delay.GetTimeStep());
203 return Simulator::Schedule(delay, &Simulator::Stop);
204}
205
206//
207// Schedule an event for a _relative_ time in the future.
208//
211{
212 NS_LOG_FUNCTION(this << delay.GetTimeStep() << event);
213 NS_ASSERT_MSG(m_mainThreadId == std::this_thread::get_id(),
214 "Simulator::Schedule Thread-unsafe invocation!");
215
216 NS_ASSERT_MSG(delay.IsPositive(), "DefaultSimulatorImpl::Schedule(): Negative delay");
217 Time tAbsolute = delay + TimeStep(m_currentTs);
218
220 ev.impl = event;
221 ev.key.m_ts = (uint64_t)tAbsolute.GetTimeStep();
222 ev.key.m_context = GetContext();
223 ev.key.m_uid = m_uid;
224 m_uid++;
226 m_events->Insert(ev);
227 return EventId(event, ev.key.m_ts, ev.key.m_context, ev.key.m_uid);
228}
229
230void
232{
233 NS_LOG_FUNCTION(this << context << delay.GetTimeStep() << event);
234
235 if (m_mainThreadId == std::this_thread::get_id())
236 {
237 Time tAbsolute = delay + TimeStep(m_currentTs);
239 ev.impl = event;
240 ev.key.m_ts = (uint64_t)tAbsolute.GetTimeStep();
241 ev.key.m_context = context;
242 ev.key.m_uid = m_uid;
243 m_uid++;
245 m_events->Insert(ev);
246 }
247 else
248 {
250 ev.context = context;
251 // Current time added in ProcessEventsWithContext()
252 ev.timestamp = delay.GetTimeStep();
253 ev.event = event;
254 {
255 std::unique_lock lock{m_eventsWithContextMutex};
256 m_eventsWithContext.push_back(ev);
258 }
259 }
260}
261
264{
265 NS_ASSERT_MSG(m_mainThreadId == std::this_thread::get_id(),
266 "Simulator::ScheduleNow Thread-unsafe invocation!");
267
268 return Schedule(Time(0), event);
269}
270
273{
274 NS_ASSERT_MSG(m_mainThreadId == std::this_thread::get_id(),
275 "Simulator::ScheduleDestroy Thread-unsafe invocation!");
276
277 EventId id(Ptr<EventImpl>(event, false), m_currentTs, 0xffffffff, 2);
278 m_destroyEvents.push_back(id);
279 m_uid++;
280 return id;
281}
282
283Time
285{
286 // Do not add function logging here, to avoid stack overflow
287 return TimeStep(m_currentTs);
288}
289
290Time
292{
293 if (IsExpired(id))
294 {
295 return TimeStep(0);
296 }
297 else
298 {
299 return TimeStep(id.GetTs() - m_currentTs);
300 }
301}
302
303void
305{
306 if (id.GetUid() == EventId::UID::DESTROY)
307 {
308 // destroy events.
309 for (auto i = m_destroyEvents.begin(); i != m_destroyEvents.end(); i++)
310 {
311 if (*i == id)
312 {
313 m_destroyEvents.erase(i);
314 break;
315 }
316 }
317 return;
318 }
319 if (IsExpired(id))
320 {
321 return;
322 }
323 Scheduler::Event event;
324 event.impl = id.PeekEventImpl();
325 event.key.m_ts = id.GetTs();
326 event.key.m_context = id.GetContext();
327 event.key.m_uid = id.GetUid();
328 m_events->Remove(event);
329 event.impl->Cancel();
330 // whenever we remove an event from the event list, we have to unref it.
331 event.impl->Unref();
332
334}
335
336void
338{
339 if (!IsExpired(id))
340 {
341 id.PeekEventImpl()->Cancel();
342 }
343}
344
345bool
347{
348 if (id.GetUid() == EventId::UID::DESTROY)
349 {
350 if (id.PeekEventImpl() == nullptr || id.PeekEventImpl()->IsCancelled())
351 {
352 return true;
353 }
354 // destroy events.
355 for (auto i = m_destroyEvents.begin(); i != m_destroyEvents.end(); i++)
356 {
357 if (*i == id)
358 {
359 return false;
360 }
361 }
362 return true;
363 }
364 return id.PeekEventImpl() == nullptr || id.GetTs() < m_currentTs ||
365 (id.GetTs() == m_currentTs && id.GetUid() <= m_currentUid) ||
366 id.PeekEventImpl()->IsCancelled();
367}
368
369Time
371{
372 return TimeStep(0x7fffffffffffffffLL);
373}
374
380
381uint64_t
386
387} // 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:44
@ INVALID
Invalid UID value.
Definition event-id.h:50
@ VALID
Schedule(), etc.
Definition event-id.h:58
@ DESTROY
ScheduleDestroy() events.
Definition event-id.h:54
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
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:430
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
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:580
@ NO_CONTEXT
Flag for events not associated with any particular context.
Definition simulator.h:207
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:169
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:96
bool IsPositive() const
Exactly equivalent to t >= 0.
Definition nstime.h:324
Time TimeStep(uint64_t ts)
Scheduler interface.
Definition nstime.h:1478
int64_t GetTimeStep() const
Get the raw time value, in the current resolution unit.
Definition nstime.h:441
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:999
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:194
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:274
#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