A Discrete-Event Network Simulator
API
default-simulator-impl.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #include "simulator.h"
22 #include "default-simulator-impl.h"
23 #include "scheduler.h"
24 #include "event-impl.h"
25 
26 #include "ptr.h"
27 #include "pointer.h"
28 #include "assert.h"
29 #include "log.h"
30 
31 #include <cmath>
32 
33 
40 namespace ns3 {
41 
42 // Note: Logging in this file is largely avoided due to the
43 // number of calls that are made to these functions and the possibility
44 // of causing recursions leading to stack overflow
45 NS_LOG_COMPONENT_DEFINE ("DefaultSimulatorImpl");
46 
47 NS_OBJECT_ENSURE_REGISTERED (DefaultSimulatorImpl);
48 
49 TypeId
51 {
52  static TypeId tid = TypeId ("ns3::DefaultSimulatorImpl")
54  .SetGroupName ("Core")
55  .AddConstructor<DefaultSimulatorImpl> ()
56  ;
57  return tid;
58 }
59 
61 {
62  NS_LOG_FUNCTION (this);
63  m_stop = false;
64  // uids are allocated from 4.
65  // uid 0 is "invalid" events
66  // uid 1 is "now" events
67  // uid 2 is "destroy" events
68  m_uid = 4;
69  // before ::Run is entered, the m_currentUid will be zero
70  m_currentUid = 0;
71  m_currentTs = 0;
76 }
77 
79 {
80  NS_LOG_FUNCTION (this);
81 }
82 
83 void
85 {
86  NS_LOG_FUNCTION (this);
88 
89  while (!m_events->IsEmpty ())
90  {
91  Scheduler::Event next = m_events->RemoveNext ();
92  next.impl->Unref ();
93  }
94  m_events = 0;
96 }
97 void
99 {
100  NS_LOG_FUNCTION (this);
101  while (!m_destroyEvents.empty ())
102  {
103  Ptr<EventImpl> ev = m_destroyEvents.front ().PeekEventImpl ();
104  m_destroyEvents.pop_front ();
105  NS_LOG_LOGIC ("handle destroy " << ev);
106  if (!ev->IsCancelled ())
107  {
108  ev->Invoke ();
109  }
110  }
111 }
112 
113 void
115 {
116  NS_LOG_FUNCTION (this << schedulerFactory);
117  Ptr<Scheduler> scheduler = schedulerFactory.Create<Scheduler> ();
118 
119  if (m_events != 0)
120  {
121  while (!m_events->IsEmpty ())
122  {
123  Scheduler::Event next = m_events->RemoveNext ();
124  scheduler->Insert (next);
125  }
126  }
127  m_events = scheduler;
128 }
129 
130 // System ID for non-distributed simulation is always zero
131 uint32_t
133 {
134  return 0;
135 }
136 
137 void
139 {
140  Scheduler::Event next = m_events->RemoveNext ();
141 
142  NS_ASSERT (next.key.m_ts >= m_currentTs);
144 
145  NS_LOG_LOGIC ("handle " << next.key.m_ts);
146  m_currentTs = next.key.m_ts;
148  m_currentUid = next.key.m_uid;
149  next.impl->Invoke ();
150  next.impl->Unref ();
151 
153 }
154 
155 bool
157 {
158  return m_events->IsEmpty () || m_stop;
159 }
160 
161 void
163 {
165  {
166  return;
167  }
168 
169  // swap queues
170  EventsWithContext eventsWithContext;
171  {
173  m_eventsWithContext.swap(eventsWithContext);
175  }
176  while (!eventsWithContext.empty ())
177  {
178  EventWithContext event = eventsWithContext.front ();
179  eventsWithContext.pop_front ();
180  Scheduler::Event ev;
181  ev.impl = event.event;
182  ev.key.m_ts = m_currentTs + event.timestamp;
183  ev.key.m_context = event.context;
184  ev.key.m_uid = m_uid;
185  m_uid++;
187  m_events->Insert (ev);
188  }
189 }
190 
191 void
193 {
194  NS_LOG_FUNCTION (this);
195  // Set the current threadId as the main threadId
198  m_stop = false;
199 
200  while (!m_events->IsEmpty () && !m_stop)
201  {
202  ProcessOneEvent ();
203  }
204 
205  // If the simulator stopped naturally by lack of events, make a
206  // consistency test to check that we didn't lose any events along the way.
207  NS_ASSERT (!m_events->IsEmpty () || m_unscheduledEvents == 0);
208 }
209 
210 void
212 {
213  NS_LOG_FUNCTION (this);
214  m_stop = true;
215 }
216 
217 void
219 {
220  NS_LOG_FUNCTION (this << delay.GetTimeStep ());
222 }
223 
224 //
225 // Schedule an event for a _relative_ time in the future.
226 //
227 EventId
229 {
230  NS_LOG_FUNCTION (this << delay.GetTimeStep () << event);
231  NS_ASSERT_MSG (SystemThread::Equals (m_main), "Simulator::Schedule Thread-unsafe invocation!");
232 
233  NS_ASSERT_MSG (delay.IsPositive (), "DefaultSimulatorImpl::Schedule(): Negative delay");
234  Time tAbsolute = delay + TimeStep (m_currentTs);
235 
236  Scheduler::Event ev;
237  ev.impl = event;
238  ev.key.m_ts = (uint64_t) tAbsolute.GetTimeStep ();
239  ev.key.m_context = GetContext ();
240  ev.key.m_uid = m_uid;
241  m_uid++;
243  m_events->Insert (ev);
244  return EventId (event, ev.key.m_ts, ev.key.m_context, ev.key.m_uid);
245 }
246 
247 void
248 DefaultSimulatorImpl::ScheduleWithContext (uint32_t context, Time const &delay, EventImpl *event)
249 {
250  NS_LOG_FUNCTION (this << context << delay.GetTimeStep () << event);
251 
253  {
254  Time tAbsolute = delay + TimeStep (m_currentTs);
255  Scheduler::Event ev;
256  ev.impl = event;
257  ev.key.m_ts = (uint64_t) tAbsolute.GetTimeStep ();
258  ev.key.m_context = context;
259  ev.key.m_uid = m_uid;
260  m_uid++;
262  m_events->Insert (ev);
263  }
264  else
265  {
266  EventWithContext ev;
267  ev.context = context;
268  // Current time added in ProcessEventsWithContext()
269  ev.timestamp = delay.GetTimeStep ();
270  ev.event = event;
271  {
273  m_eventsWithContext.push_back(ev);
274  m_eventsWithContextEmpty = false;
275  }
276  }
277 }
278 
279 EventId
281 {
282  NS_ASSERT_MSG (SystemThread::Equals (m_main), "Simulator::ScheduleNow Thread-unsafe invocation!");
283 
284  Scheduler::Event ev;
285  ev.impl = event;
286  ev.key.m_ts = m_currentTs;
287  ev.key.m_context = GetContext ();
288  ev.key.m_uid = m_uid;
289  m_uid++;
291  m_events->Insert (ev);
292  return EventId (event, ev.key.m_ts, ev.key.m_context, ev.key.m_uid);
293 }
294 
295 EventId
297 {
298  NS_ASSERT_MSG (SystemThread::Equals (m_main), "Simulator::ScheduleDestroy Thread-unsafe invocation!");
299 
300  EventId id (Ptr<EventImpl> (event, false), m_currentTs, 0xffffffff, 2);
301  m_destroyEvents.push_back (id);
302  m_uid++;
303  return id;
304 }
305 
306 Time
308 {
309  // Do not add function logging here, to avoid stack overflow
310  return TimeStep (m_currentTs);
311 }
312 
313 Time
315 {
316  if (IsExpired (id))
317  {
318  return TimeStep (0);
319  }
320  else
321  {
322  return TimeStep (id.GetTs () - m_currentTs);
323  }
324 }
325 
326 void
328 {
329  if (id.GetUid () == 2)
330  {
331  // destroy events.
332  for (DestroyEvents::iterator i = m_destroyEvents.begin (); i != m_destroyEvents.end (); i++)
333  {
334  if (*i == id)
335  {
336  m_destroyEvents.erase (i);
337  break;
338  }
339  }
340  return;
341  }
342  if (IsExpired (id))
343  {
344  return;
345  }
346  Scheduler::Event event;
347  event.impl = id.PeekEventImpl ();
348  event.key.m_ts = id.GetTs ();
349  event.key.m_context = id.GetContext ();
350  event.key.m_uid = id.GetUid ();
351  m_events->Remove (event);
352  event.impl->Cancel ();
353  // whenever we remove an event from the event list, we have to unref it.
354  event.impl->Unref ();
355 
357 }
358 
359 void
361 {
362  if (!IsExpired (id))
363  {
364  id.PeekEventImpl ()->Cancel ();
365  }
366 }
367 
368 bool
370 {
371  if (id.GetUid () == 2)
372  {
373  if (id.PeekEventImpl () == 0 ||
374  id.PeekEventImpl ()->IsCancelled ())
375  {
376  return true;
377  }
378  // destroy events.
379  for (DestroyEvents::const_iterator i = m_destroyEvents.begin (); i != m_destroyEvents.end (); i++)
380  {
381  if (*i == id)
382  {
383  return false;
384  }
385  }
386  return true;
387  }
388  if (id.PeekEventImpl () == 0 ||
389  id.GetTs () < m_currentTs ||
390  (id.GetTs () == m_currentTs &&
391  id.GetUid () <= m_currentUid) ||
392  id.PeekEventImpl ()->IsCancelled ())
393  {
394  return true;
395  }
396  else
397  {
398  return false;
399  }
400 }
401 
402 Time
404 {
405  return TimeStep (0x7fffffffffffffffLL);
406 }
407 
408 uint32_t
410 {
411  return m_currentContext;
412 }
413 
414 } // namespace ns3
virtual Time GetDelayLeft(const EventId &id) const
Get the remaining time until this event will execute.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
EventsWithContext m_eventsWithContext
The container of events from a different context.
virtual bool IsFinished(void) const
Check if the simulation should finish.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
uint32_t m_currentUid
Unique id of the current event.
ns3::EventImpl declarations.
Ptr< Scheduler > m_events
The event priority queue.
void Unref(void) const
Decrement the reference count.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
int m_unscheduledEvents
Number of events that have been inserted but not yet scheduled, not counting the Destroy events; this...
virtual void DoDispose(void)
Destructor implementation.
uint64_t m_ts
Event time stamp.
Definition: scheduler.h:81
ns3::Ptr smart pointer declaration and implementation.
virtual void SetScheduler(ObjectFactory schedulerFactory)
Set the Scheduler to be used to manage the event list.
uint32_t m_uid
Next event unique id.
Wrap an event with its execution context.
static ThreadId Self(void)
Returns the current thread Id.
DestroyEvents m_destroyEvents
The container of events to run at Destroy.
EventImpl * impl
Pointer to the event implementation.
Definition: scheduler.h:94
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
SystemMutex m_eventsWithContextMutex
Mutex to control access to the list of events with context.
ns3::Simulator declaration.
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
virtual EventId ScheduleNow(EventImpl *event)
Schedule an event to run at the current virtual time.
virtual void Cancel(const EventId &id)
Set the cancel bit on this event: the event&#39;s associated function will not be invoked when it expires...
void Invoke(void)
Called by the simulation engine to notify the event that it is time to execute.
Definition: event-impl.cc:46
EventImpl * event
The event implementation.
ns3::Scheduler abstract base class, ns3::Scheduler::Event and ns3::Scheduler::EventKey declarations...
bool m_stop
Flag calling for the end of the simulation.
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1381
EventKey key
Key for sorting and ordering Events.
Definition: scheduler.h:95
static TypeId GetTypeId(void)
Register this type.
virtual uint32_t GetSystemId(void) const
Get the system id of this simulator.
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
A class which provides a simple way to implement a Critical Section.
Definition: system-mutex.h:118
uint32_t m_uid
Event unique id.
Definition: scheduler.h:82
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
ns3::DefaultSimulatorImpl declaration.
uint32_t m_currentContext
Execution context of the current event.
virtual Time GetMaximumSimulationTime(void) const
Get the maximum representable simulation time.
virtual uint32_t GetContext(void) const
Get the current simulation context.
Maintain the event list.
Definition: scheduler.h:66
virtual EventId ScheduleDestroy(EventImpl *event)
Schedule an event to run at the end of the simulation, after the Stop() time or condition has been re...
std::list< struct EventWithContext > EventsWithContext
Container type for the events from a different context.
Scheduler event.
Definition: scheduler.h:92
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Time TimeStep(uint64_t ts)
Definition: nstime.h:1071
virtual void Remove(const EventId &id)
Remove an event from the event list.
The default single process simulator implementation.
uint64_t m_currentTs
Timestamp of the current event.
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
static bool Equals(ThreadId id)
Compares an ThreadId with the current ThreadId .
virtual EventId Schedule(const Time &delay, EventImpl *event)
Schedule a future event execution (in the same context).
Instantiate subclasses of ns3::Object.
A simulation event.
Definition: event-impl.h:44
virtual void ScheduleWithContext(uint32_t context, const Time &delay, EventImpl *event)
Schedule a future event execution (in a different context).
virtual void Run(void)
Run the simulation.
An identifier for simulation events.
Definition: event-id.h:53
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
bool IsPositive(void) const
Definition: nstime.h:298
virtual void Destroy()
Execute the events scheduled with ScheduleDestroy().
Flag for events not associated with any particular context.
Definition: simulator.h:198
virtual Time Now(void) const
Return the current simulation virtual time.
SystemThread::ThreadId m_main
Main execution thread.
Debug message logging.
void ProcessOneEvent(void)
Process the next event.
bool m_eventsWithContextEmpty
Flag true if all events with context have been moved to the primary event queue.
ns3::PointerValue attribute value declarations and template implementations.
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
virtual bool IsExpired(const EventId &id) const
Check if an event has already run or been cancelled.
uint32_t m_context
Event context.
Definition: scheduler.h:83
The SimulatorImpl base class.
int64_t GetTimeStep(void) const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:391
void ProcessEventsWithContext(void)
Move events from a different context into the main event queue.