A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
simulator.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#include "simulator.h"
9
10#include "assert.h"
11#include "des-metrics.h"
12#include "event-impl.h"
13#include "global-value.h"
14#include "log.h"
15#include "map-scheduler.h"
16#include "object-factory.h"
17#include "ptr.h"
18#include "scheduler.h"
19#include "simulator-impl.h"
20#include "string.h"
21
22#include "ns3/core-config.h"
23
24#include <cmath>
25#include <fstream>
26#include <iomanip>
27#include <iostream>
28#include <list>
29#include <vector>
30
31/**
32 * \file
33 * \ingroup simulator
34 * ns3::Simulator implementation, as well as implementation pointer,
35 * global scheduler implementation.
36 */
37
38namespace ns3
39{
40
41// Note: Logging in this file is largely avoided due to the
42// number of calls that are made to these functions and the possibility
43// of causing recursions leading to stack overflow
44NS_LOG_COMPONENT_DEFINE("Simulator");
45
47
48/**
49 * \ingroup simulator
50 * \anchor GlobalValueSimulatorImplementationType
51 * The specific simulator implementation to use.
52 *
53 * Must be derived from SimulatorImpl.
54 */
56 GlobalValue("SimulatorImplementationType",
57 "The object class to use as the simulator implementation",
58 StringValue("ns3::DefaultSimulatorImpl"),
60
61/**
62 * \ingroup scheduler
63 * \anchor GlobalValueSchedulerType
64 * The specific event scheduler implementation to use.
65 *
66 * Must be derived from Scheduler.
67 */
69 GlobalValue("SchedulerType",
70 "The object class to use as the scheduler implementation",
73
74/**
75 * \ingroup simulator
76 * \brief Get the static SimulatorImpl instance.
77 * \return The SimulatorImpl instance pointer.
78 */
79static SimulatorImpl**
81{
82 static SimulatorImpl* impl = nullptr;
83 return &impl;
84}
85
86/**
87 * \ingroup simulator
88 * \brief Get the SimulatorImpl singleton.
89 * \return The singleton pointer.
90 * \see Simulator::GetImplementation()
91 */
92static SimulatorImpl*
94{
95 SimulatorImpl** pimpl = PeekImpl();
96 /* Please, don't include any calls to logging macros in this function
97 * or pay the price, that is, stack explosions.
98 */
99 if (*pimpl == nullptr)
100 {
101 {
102 ObjectFactory factory;
103 StringValue s;
104
106 factory.SetTypeId(s.Get());
107 *pimpl = GetPointer(factory.Create<SimulatorImpl>());
108 }
109 {
110 ObjectFactory factory;
111 StringValue s;
113 factory.SetTypeId(s.Get());
114 (*pimpl)->SetScheduler(factory);
115 }
116
117 //
118 // Note: we call LogSetTimePrinter _after_ creating the implementation
119 // object because the act of creation can trigger calls to the logging
120 // framework which would call the TimePrinter function which would call
121 // Simulator::Now which would call Simulator::GetImpl, and, thus, get us
122 // in an infinite recursion until the stack explodes.
123 //
126 }
127 return *pimpl;
128}
129
130void
132{
134
135 SimulatorImpl** pimpl = PeekImpl();
136 if (*pimpl == nullptr)
137 {
138 return;
139 }
140 /* Note: we have to call LogSetTimePrinter (0) below because if we do not do
141 * this, and restart a simulation after this call to Destroy, (which is
142 * legal), Simulator::GetImpl will trigger again an infinite recursion until
143 * the stack explodes.
144 */
145 LogSetTimePrinter(nullptr);
146 LogSetNodePrinter(nullptr);
147 (*pimpl)->Destroy();
148 (*pimpl)->Unref();
149 *pimpl = nullptr;
150}
151
152void
154{
155 NS_LOG_FUNCTION(schedulerFactory);
156 GetImpl()->SetScheduler(schedulerFactory);
157}
158
159bool
165
166void
173
174void
176{
178 NS_LOG_LOGIC("stop");
179 GetImpl()->Stop();
180}
181
184{
185 NS_LOG_FUNCTION(delay);
186 m_stopEvent = GetImpl()->Stop(delay);
187 return m_stopEvent;
188}
189
192{
193 return m_stopEvent;
194}
195
196Time
198{
199 /* Please, don't include any calls to logging macros in this function
200 * or pay the price, that is, stack explosions.
201 */
202 return GetImpl()->Now();
203}
204
205Time
207{
208 NS_LOG_FUNCTION(&id);
209 return GetImpl()->GetDelayLeft(id);
210}
211
213Simulator::Schedule(const Time& delay, const Ptr<EventImpl>& event)
214{
215 return DoSchedule(delay, GetPointer(event));
216}
217
220{
221 return DoScheduleNow(GetPointer(ev));
222}
223
224void
226{
227#ifdef ENABLE_DES_METRICS
228 DesMetrics::Get()->TraceWithContext(context, Now(), delay);
229#endif
230 return GetImpl()->ScheduleWithContext(context, delay, impl);
231}
232
238
241{
242#ifdef ENABLE_DES_METRICS
243 DesMetrics::Get()->Trace(Now(), time);
244#endif
245 return GetImpl()->Schedule(time, impl);
246}
247
250{
251#ifdef ENABLE_DES_METRICS
252 DesMetrics::Get()->Trace(Now(), Time(0));
253#endif
254 return GetImpl()->ScheduleNow(impl);
255}
256
259{
260 return GetImpl()->ScheduleDestroy(impl);
261}
262
263void
265{
266 if (*PeekImpl() == nullptr)
267 {
268 return;
269 }
270 return GetImpl()->Remove(id);
271}
272
273void
275{
276 if (*PeekImpl() == nullptr)
277 {
278 return;
279 }
280 return GetImpl()->Cancel(id);
281}
282
283bool
285{
286 if (*PeekImpl() == nullptr)
287 {
288 return true;
289 }
290 return GetImpl()->IsExpired(id);
291}
292
293Time
295{
296 return Simulator::Now();
297}
298
299Time
305
308{
309 return GetImpl()->GetContext();
310}
311
312uint64_t
314{
315 return GetImpl()->GetEventCount();
316}
317
320{
322
323 if (*PeekImpl() != nullptr)
324 {
325 return GetImpl()->GetSystemId();
326 }
327 else
328 {
329 return 0;
330 }
331}
332
333void
335{
336 NS_LOG_FUNCTION(impl);
337 if (*PeekImpl() != nullptr)
338 {
340 "It is not possible to set the implementation after calling any Simulator:: function. "
341 "Call Simulator::SetImplementation earlier or after Simulator::Destroy.");
342 }
343 *PeekImpl() = GetPointer(impl);
344 // Set the default scheduler
345 ObjectFactory factory;
346 StringValue s;
348 factory.SetTypeId(s.Get());
349 impl->SetScheduler(factory);
350 //
351 // Note: we call LogSetTimePrinter _after_ creating the implementation
352 // object because the act of creation can trigger calls to the logging
353 // framework which would call the TimePrinter function which would call
354 // Simulator::Now which would call Simulator::GetImpl, and, thus, get us
355 // in an infinite recursion until the stack explodes.
356 //
359}
360
367
368} // namespace ns3
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
void Trace(const Time &now, const Time &delay)
Trace an event to self at the time it is scheduled.
void TraceWithContext(uint32_t context, const Time &now, const Time &delay)
Trace an event (with context) at the time it is scheduled.
An identifier for simulation events.
Definition event-id.h:45
A simulation event.
Definition event-impl.h:35
Hold a so-called 'global value'.
void GetValue(AttributeValue &value) const
Get the value.
static TypeId GetTypeId()
Register this type.
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Smart pointer class similar to boost::intrusive_ptr.
static EventId DoScheduleDestroy(EventImpl *event)
Implementation of the various ScheduleDestroy methods.
Definition simulator.cc:258
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static Ptr< SimulatorImpl > GetImplementation()
Get the SimulatorImpl singleton.
Definition simulator.cc:362
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition simulator.cc:274
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
static EventId m_stopEvent
Stop event (if present)
Definition simulator.h:523
static bool IsFinished()
Check if the simulation should finish.
Definition simulator.cc:160
static uint32_t GetSystemId()
Get the system id of this simulator.
Definition simulator.cc:319
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static bool IsExpired(const EventId &id)
Check if an event has already run or been cancelled.
Definition simulator.cc:284
static EventId ScheduleDestroy(FUNC f, Ts &&... args)
Schedule an event to run at the end of the simulation, when Simulator::Destroy() is called.
Definition simulator.h:611
static void SetScheduler(ObjectFactory schedulerFactory)
Set the scheduler type with an ObjectFactory.
Definition simulator.cc:153
static EventId DoScheduleNow(EventImpl *event)
Implementation of the various ScheduleNow methods.
Definition simulator.cc:249
static uint64_t GetEventCount()
Get the number of events executed.
Definition simulator.cc:313
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:594
static EventId DoSchedule(const Time &delay, EventImpl *event)
Implementation of the various Schedule methods.
Definition simulator.cc:240
static Time GetMaximumSimulationTime()
Get the maximum representable simulation time.
Definition simulator.cc:300
static void Remove(const EventId &id)
Remove an event from the event list.
Definition simulator.cc:264
static EventId GetStopEvent()
Returns the Stop Event, or an invalid event if the simulation does not have a scheduled stop time.
Definition simulator.cc:191
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
static void SetImplementation(Ptr< SimulatorImpl > impl)
Definition simulator.cc:334
static uint32_t GetContext()
Get the current simulation context.
Definition simulator.cc:307
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition simulator.cc:206
The SimulatorImpl base class.
virtual EventId ScheduleDestroy(EventImpl *event)=0
Schedule an event to run at the end of the simulation, after the Stop() time or condition has been re...
virtual Time GetDelayLeft(const EventId &id) const =0
Get the remaining time until this event will execute.
virtual void SetScheduler(ObjectFactory schedulerFactory)=0
Set the Scheduler to be used to manage the event list.
virtual uint32_t GetSystemId() const =0
Get the system id of this simulator.
virtual void Run()=0
Run the simulation.
virtual EventId Schedule(const Time &delay, EventImpl *event)=0
Schedule a future event execution (in the same context).
virtual bool IsExpired(const EventId &id) const =0
Check if an event has already run or been cancelled.
virtual void Remove(const EventId &id)=0
Remove an event from the event list.
virtual Time GetMaximumSimulationTime() const =0
Get the maximum representable simulation time.
virtual void ScheduleWithContext(uint32_t context, const Time &delay, EventImpl *event)=0
Schedule a future event execution (in a different context).
virtual EventId ScheduleNow(EventImpl *event)=0
Schedule an event to run at the current virtual time.
virtual Time Now() const =0
Return the current simulation virtual time.
virtual bool IsFinished() const =0
Check if the simulation should finish.
virtual uint64_t GetEventCount() const =0
Get the number of events executed.
virtual void Cancel(const EventId &id)=0
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
virtual uint32_t GetContext() const =0
Get the current simulation context.
virtual void Stop()=0
Tell the Simulator the calling event should be the last one executed.
static DesMetrics * Get()
Definition singleton.h:96
Hold variables of type string.
Definition string.h:45
std::string Get() const
Definition string.cc:20
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
static void ClearMarkedTimes()
Remove all MarkedTimes.
Definition time.cc:285
AttributeValue implementation for TypeId.
Definition type-id.h:617
ns3::DesMetrics declaration.
ns3::EventImpl declarations.
ns3::GlobalValue declaration.
Ptr< const AttributeChecker > MakeStringChecker()
Definition string.cc:19
Ptr< const AttributeChecker > MakeTypeIdChecker()
Definition type-id.cc:1320
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#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_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
void DefaultNodePrinter(std::ostream &os)
Default node id printer implementation.
static GlobalValue g_schedTypeImpl
The specific event scheduler implementation to use.
Definition simulator.cc:68
static SimulatorImpl ** PeekImpl()
Get the static SimulatorImpl instance.
Definition simulator.cc:80
static GlobalValue g_simTypeImpl
The specific simulator implementation to use.
Definition simulator.cc:55
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition simulator.cc:294
static SimulatorImpl * GetImpl()
Get the SimulatorImpl singleton.
Definition simulator.cc:93
Debug message logging.
ns3::MapScheduler declaration.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogSetTimePrinter(TimePrinter printer)
Set the TimePrinter function to be used to prepend log messages with the simulation time.
Definition log.cc:481
U * GetPointer(const Ptr< U > &p)
Definition ptr.h:450
void DefaultTimePrinter(std::ostream &os)
Default Time printer.
void LogSetNodePrinter(NodePrinter printer)
Set the LogNodePrinter function to be used to prepend log messages with the node id.
Definition log.cc:499
ns3::ObjectFactory class declaration.
ns3::Ptr smart pointer declaration and implementation.
ns3::Scheduler abstract base class, ns3::Scheduler::Event and ns3::Scheduler::EventKey declarations.
ns3::SimulatorImpl declaration.
ns3::Simulator declaration.
ns3::StringValue attribute value declarations.