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 "event-impl.h"
11#include "fatal-error.h"
12#include "global-value.h"
13#include "log.h"
14#include "map-scheduler.h"
15#include "object-factory.h"
16#include "ptr.h"
17#include "scheduler.h"
18#include "simulator-impl.h"
19#include "string.h"
20
21#ifdef ENABLE_DES_METRICS
22#include "des-metrics.h"
23#endif
24
25/**
26 * @file
27 * @ingroup simulator
28 * ns3::Simulator implementation, as well as implementation pointer,
29 * global scheduler implementation.
30 */
31
32namespace ns3
33{
34
35// Note: Logging in this file is largely avoided due to the
36// number of calls that are made to these functions and the possibility
37// of causing recursions leading to stack overflow
38NS_LOG_COMPONENT_DEFINE("Simulator");
39
41
42/**
43 * @ingroup simulator
44 * @anchor GlobalValueSimulatorImplementationType
45 * The specific simulator implementation to use.
46 *
47 * Must be derived from SimulatorImpl.
48 */
50 GlobalValue("SimulatorImplementationType",
51 "The object class to use as the simulator implementation",
52 StringValue("ns3::DefaultSimulatorImpl"),
54
55/**
56 * @ingroup scheduler
57 * @anchor GlobalValueSchedulerType
58 * The specific event scheduler implementation to use.
59 *
60 * Must be derived from Scheduler.
61 */
63 GlobalValue("SchedulerType",
64 "The object class to use as the scheduler implementation",
67
68/**
69 * @ingroup simulator
70 * @brief Get the static SimulatorImpl instance.
71 * @return The SimulatorImpl instance pointer.
72 */
73static SimulatorImpl**
75{
76 static SimulatorImpl* impl = nullptr;
77 return &impl;
78}
79
80/**
81 * @ingroup simulator
82 * @brief Get the SimulatorImpl singleton.
83 * @return The singleton pointer.
84 * @see Simulator::GetImplementation()
85 */
86static SimulatorImpl*
88{
89 SimulatorImpl** pimpl = PeekImpl();
90 /* Please, don't include any calls to logging macros in this function
91 * or pay the price, that is, stack explosions.
92 */
93 if (*pimpl == nullptr)
94 {
95 {
96 ObjectFactory factory;
98
99 g_simTypeImpl.GetValue(s);
100 factory.SetTypeId(s.Get());
101 *pimpl = GetPointer(factory.Create<SimulatorImpl>());
102 }
103 {
104 ObjectFactory factory;
105 StringValue s;
106 g_schedTypeImpl.GetValue(s);
107 factory.SetTypeId(s.Get());
108 (*pimpl)->SetScheduler(factory);
109 }
110
111 //
112 // Note: we call LogSetTimePrinter _after_ creating the implementation
113 // object because the act of creation can trigger calls to the logging
114 // framework which would call the TimePrinter function which would call
115 // Simulator::Now which would call Simulator::GetImpl, and, thus, get us
116 // in an infinite recursion until the stack explodes.
117 //
120 }
121 return *pimpl;
122}
123
124void
126{
128
129 SimulatorImpl** pimpl = PeekImpl();
130 if (*pimpl == nullptr)
131 {
132 return;
133 }
134 /* Note: we have to call LogSetTimePrinter (0) below because if we do not do
135 * this, and restart a simulation after this call to Destroy, (which is
136 * legal), Simulator::GetImpl will trigger again an infinite recursion until
137 * the stack explodes.
138 */
139 LogSetTimePrinter(nullptr);
140 LogSetNodePrinter(nullptr);
141 (*pimpl)->Destroy();
142 (*pimpl)->Unref();
143 *pimpl = nullptr;
144}
145
146void
148{
149 NS_LOG_FUNCTION(schedulerFactory);
150 GetImpl()->SetScheduler(schedulerFactory);
151}
152
153bool
159
160void
167
168void
170{
172 NS_LOG_LOGIC("stop");
173 GetImpl()->Stop();
174}
175
178{
179 NS_LOG_FUNCTION(delay);
180 m_stopEvent = GetImpl()->Stop(delay);
181 return m_stopEvent;
182}
183
186{
187 return m_stopEvent;
188}
189
190Time
192{
193 /* Please, don't include any calls to logging macros in this function
194 * or pay the price, that is, stack explosions.
195 */
196 return GetImpl()->Now();
197}
198
199Time
201{
202 NS_LOG_FUNCTION(&id);
203 return GetImpl()->GetDelayLeft(id);
204}
205
207Simulator::Schedule(const Time& delay, const Ptr<EventImpl>& event)
208{
209 return DoSchedule(delay, GetPointer(event));
210}
211
214{
215 return DoScheduleNow(GetPointer(ev));
216}
217
218void
220{
221#ifdef ENABLE_DES_METRICS
222 DesMetrics::Get()->TraceWithContext(context, Now(), delay);
223#endif
224 return GetImpl()->ScheduleWithContext(context, delay, impl);
225}
226
232
235{
236#ifdef ENABLE_DES_METRICS
237 DesMetrics::Get()->Trace(Now(), time);
238#endif
239 return GetImpl()->Schedule(time, impl);
240}
241
244{
245#ifdef ENABLE_DES_METRICS
246 DesMetrics::Get()->Trace(Now(), Time(0));
247#endif
248 return GetImpl()->ScheduleNow(impl);
249}
250
253{
254 return GetImpl()->ScheduleDestroy(impl);
255}
256
257void
259{
260 if (*PeekImpl() == nullptr)
261 {
262 return;
263 }
264 return GetImpl()->Remove(id);
265}
266
267void
269{
270 if (*PeekImpl() == nullptr)
271 {
272 return;
273 }
274 return GetImpl()->Cancel(id);
275}
276
277bool
279{
280 if (*PeekImpl() == nullptr)
281 {
282 return true;
283 }
284 return GetImpl()->IsExpired(id);
285}
286
287Time
289{
290 return Simulator::Now();
291}
292
293Time
299
302{
303 return GetImpl()->GetContext();
304}
305
306uint64_t
308{
309 return GetImpl()->GetEventCount();
310}
311
314{
316
317 if (*PeekImpl() != nullptr)
318 {
319 return GetImpl()->GetSystemId();
320 }
321 else
322 {
323 return 0;
324 }
325}
326
327void
329{
330 NS_LOG_FUNCTION(impl);
331 if (*PeekImpl() != nullptr)
332 {
334 "It is not possible to set the implementation after calling any Simulator:: function. "
335 "Call Simulator::SetImplementation earlier or after Simulator::Destroy.");
336 }
337 *PeekImpl() = GetPointer(impl);
338 // Set the default scheduler
339 ObjectFactory factory;
340 StringValue s;
341 g_schedTypeImpl.GetValue(s);
342 factory.SetTypeId(s.Get());
343 impl->SetScheduler(factory);
344 //
345 // Note: we call LogSetTimePrinter _after_ creating the implementation
346 // object because the act of creation can trigger calls to the logging
347 // framework which would call the TimePrinter function which would call
348 // Simulator::Now which would call Simulator::GetImpl, and, thus, get us
349 // in an infinite recursion until the stack explodes.
350 //
353}
354
361
362} // namespace ns3
An identifier for simulation events.
Definition event-id.h:44
A simulation event.
Definition event-impl.h:35
Hold a so-called 'global 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.
Definition ptr.h:70
static EventId DoScheduleDestroy(EventImpl *event)
Implementation of the various ScheduleDestroy methods.
Definition simulator.cc:252
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:580
static Ptr< SimulatorImpl > GetImplementation()
Get the SimulatorImpl singleton.
Definition simulator.cc:356
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:268
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:597
static EventId m_stopEvent
Stop event (if present).
Definition simulator.h:541
static bool IsFinished()
Check if the simulation should finish.
Definition simulator.cc:154
static uint32_t GetSystemId()
Get the system id of this simulator.
Definition simulator.cc:313
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:191
static void Run()
Run the simulation.
Definition simulator.cc:161
static bool IsExpired(const EventId &id)
Check if an event has already run or been cancelled.
Definition simulator.cc:278
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:631
static void SetScheduler(ObjectFactory schedulerFactory)
Set the scheduler type with an ObjectFactory.
Definition simulator.cc:147
static EventId DoScheduleNow(EventImpl *event)
Implementation of the various ScheduleNow methods.
Definition simulator.cc:243
static uint64_t GetEventCount()
Get the number of events executed.
Definition simulator.cc:307
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:614
static EventId DoSchedule(const Time &delay, EventImpl *event)
Implementation of the various Schedule methods.
Definition simulator.cc:234
static Time GetMaximumSimulationTime()
Get the maximum representable simulation time.
Definition simulator.cc:294
static void Remove(const EventId &id)
Remove an event from the event list.
Definition simulator.cc:258
static EventId GetStopEvent()
Returns the Stop Event, or an invalid event if the simulation does not have a scheduled stop time.
Definition simulator.cc:185
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:169
static void SetImplementation(Ptr< SimulatorImpl > impl)
Definition simulator.cc:328
static uint32_t GetContext()
Get the current simulation context.
Definition simulator.cc:301
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition simulator.cc:200
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.
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:96
static void ClearMarkedTimes()
Remove all MarkedTimes.
Definition time.cc:290
AttributeValue implementation for TypeId.
Definition type-id.h:648
ns3::DesMetrics declaration.
ns3::EventImpl declarations.
NS_FATAL_x macro definitions.
ns3::GlobalValue declaration.
Ptr< const AttributeChecker > MakeStringChecker()
Definition string.cc:19
Ptr< const AttributeChecker > MakeTypeIdChecker()
Definition type-id.cc:1333
#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: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_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:62
static SimulatorImpl ** PeekImpl()
Get the static SimulatorImpl instance.
Definition simulator.cc:74
static GlobalValue g_simTypeImpl
The specific simulator implementation to use.
Definition simulator.cc:49
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition simulator.cc:288
static SimulatorImpl * GetImpl()
Get the SimulatorImpl singleton.
Definition simulator.cc:87
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:471
U * GetPointer(const Ptr< U > &p)
Definition ptr.h:470
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:489
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.