2. Recap
NS2 is a discrete-event simulator, where actions are associated with events
rather than time. An event in a discrete-event simulator consists of execution
NS2 is an event-driven simulation
time, a set of actions, and a reference to the next event (Fig. 4.1). These events
connect to each other and form a chain of events on the simulation timeline
Main concept [see here]:
(e.g., that in Fig. 4.1). Unlike a time-driven simulator, in an event-driven
simulator, time between a pair of events does not need to be constant. When
the simulation starts, events in the chain are executed from left to right (i.e.,
Put events on the simulation timeline
chronologically).1 In the next section, we will discuss the simulation concept of
NS2. In Sections 4.2, 4.3, and 4.4, we will explain the details of classes Event
and Handler, class Scheduler,time line and take actions when
Move along the and class Simulator, respectively. Finally, we
summarize this chapter in Section 4.6.
confronting an event
insert Event5
create event
event time = 3.7
Action5
Event1 Event2 Event3 Event4
time = 0.9 time = 2.2 time = 5 time = 6.8
Action1 Action2 Action3 Action4
Time
(second)
1 2 3 4 5 6 7
Fig. 4.1. A sample chain of events in a discrete-event simulation. Each event con-
www.ns2ultimate.com
tains execution time and a reference to the next event. In this figure, Event1 creates
3. NS2 is a discrete-event simulator, where actions are associated with events
Recap
rather than time. An event in a discrete-event simulator consists of execution
time, a set of actions, and a reference to the next event (Fig. 4.1). These events
connect to each other and form a chain of events on the simulation timeline
(e.g., that in Fig. 4.1). Unlike a time-driven simulator, in an event-driven
simulator, time between a pair of events does not need to be constant. When
the simulation starts, events in the chain are executed from left to right (i.e.,
• An event indicates what happens
chronologically).1 In the next section, we will discuss the simulation concept of
NS2. In Sections 4.2, 4.3, and 4.4, we will explain the details of classes Event
• A handler indicates what to do
and Handler, class Scheduler, and class Simulator, respectively. Finally, we
summarize this chapter in Section 4.6.
insert Event5
create event
event time = 3.7
Action5
Event1 Event2 Event3 Event4
time = 0.9 time = 2.2 time = 5 time = 6.8
Action1 Action2 Action3 Action4
Time
(second)
1 2 3 4 5 6 7
Fig. 4.1. A sample chain of events in a discrete-event simulation. Each event con-
Handler
Handler Handler Handler Handler
tains execution time and a reference to the next event. In this figure, Event1 creates
1 2 4
and inserts Event5 after Event2 (the execution 3
5 time of Event 5 is at 3.7 second).
www.ns2ultimate.com
4. Event and Handler
An event is associated with a handler [see here]
handler handle(){ handle(){ handler
<actions> <actions>
} }
handler_ next_ handler_ next_
event uid_ time_ uid_ time_ event
www.ns2ultimate.com
5. Event and Handler
Terminology: “Dispatching time”
- Time where the event is scheduled to occur
- A.K.A. Firing time
- A.K.A. Executing time
- Stored in the variable “time_” of each event
www.ns2ultimate.com
7. Scheduler
Scheduler is defined in the C++ class
Scheduler
//~ns/common/scheduler.h The current time
class Scheduler : public TclObject {
... 0, if the Scheduler
protected: is running
double clock_;
The reference to
int halted_;
the Scheduler
static Scheduler* instance_;
static scheduler_uid_t uid_; Unique ID which shall be
}; assigned to events
www.ns2ultimate.com
8. Implementation of Discrete-Event Simulation
in NS2
Reference to the Scheduler
NS2 is a discrete-event simulator, where actions are associated with events
rather than time. An event in a discrete-event simulator consists of execution
time, a set of actions, and a reference to the next event (Fig. 4.1). These events
There is exactly one Scheduler in a simulation
connect to each other and form a chain of events on the simulation timeline
(e.g., that in Fig. 4.1). Unlike a time-driven simulator, in an event-driven
simulator, time between a pair of events does not need to be constant. When
Scheduler
the simulation starts, events in the chain are executed from left to right (i.e.,
chronologically).1 In the next section, we will discuss the simulation concept of
NS2. In Sections 4.2, 4.3, and 4.4, we will explain the details of classes Event
and Handler, class Scheduler, and class Simulator, respectively. Finally, we
summarize this chapter in Section 4.6.
insert Event5
create event
event time = 3.7
Action5
Event1 Event2 Event3 Event4
time = 0.9 time = 2.2 time = 5 time = 6.8
Action1 Action2 Action3 Action4
Time
(second)
1 2 3 4 5 6 7
Fig. 4.1. A sample chain of events in a discrete-event simulation. Each event con-
tains execution time and a reference to the next event. In this figure, Event1 creates
and inserts Event5 after Event2 (the execution time of Event 5 is at 3.7 second).
www.ns2ultimate.com
1
By execution, we mean taking actions associated with an event.
9. Reference to the Scheduler
There is exactly one Scheduler in a simulation
//~ns/common/scheduler.h
class Scheduler : public TclObject {
...
protected:
double clock_;
int halted_;
static Scheduler* instance_;
static scheduler_uid_t uid_;
};
So, the pointer “instance_” to the
Scheduler must be static.
www.ns2ultimate.com
10. Unique ID
Every event is tagged with a unique ID.
NS2 assigns the ID by
- Maintaining a common pool of unique ID
-Take an ID from the pool and assign it to
each event.
The pool of unique ID is the variable uid_ of
class Schedule
www.ns2ultimate.com
12. Unique ID
The Scheduler receives a new event
Event Scheduler
uid_ = 20
uid_=Null
www.ns2ultimate.com
13. Unique ID
The Scheduler
4
copies its uid_ to the event’s uid_, and
Implementation of Discrete-Event Simulation
in NS2
Increments its uid_ by one. Event
Scheduler
NS2 is a discrete-event simulator, where actions are associated with events
uid_=20
rather than time. An event in a discrete-event simulator consists of execution
time, a set of actions, and a reference to the next event (Fig. 4.1). These events
connect to each other and form a chain of events on the simulation timeline
(e.g., that in Fig. 4.1). Unlike a time-driven simulator, in an event-driven
uid_ = 21
simulator, time between a pair of events does not need to be constant. When
the simulation starts, events in the chain are executed from left to right (i.e.,
chronologically).1 In the next section, we will discuss the simulation concept of
NS2. In Sections 4.2, 4.3, and 4.4, we will explain the details of classes Event
and Handler, class Scheduler, and class Simulator, respectively. Finally, we
summarize this chapter in Section 4.6.
insert Event5
create event
event time = 3.7
Action5
Event1 Event2 Event3 Event4
time = 0.9 time = 2.2 time = 5 time = 6.8
Action1 Action2 Action3 Action4
Time
(second)
1 2 3 4 5 6 7
Fig. 4.1. A sample chain of events in a discrete-event simulation. Each event con-
tains execution time and a reference to the next event. In this figure, Event1 creates
www.ns2ultimate.com
and inserts Event5 after Event2 (the execution time of Event 5 is at 3.7 second).
14. Scheduler
Three main tasks
NS2
Task Description
Terminology
Put event on the simulation
1 Schedule
time line
Execute actions associated
2 Dispatch
with each events
Move to the next event and
3 Move
“dispatch” it.
www.ns2ultimate.com
16. Task 1: Schedule 4
Implementation of Discrete-Event Simulation
in NS2
The Scheduler receives
Objects Users
events from users (e.g., NS2 is a discrete-event simulator, where actions are associated with events
rather than time. An event in a discrete-event simulator consists of execution
time, a set of actions, and a reference to the next event (Fig. 4.1). These events
connect to each other and form a chain of events on the simulation timeline
Event Event
at events) or objects (e.g., that in Fig. 4.1). Unlike a time-driven simulator, in an event-driven
simulator, time between a pair of events does not need to be constant. When
Scheduler
the simulation starts, events in the chain are executed from left to right (i.e.,
chronologically).1 In the next section, we will discuss the simulation concept of
(e.g., packet). NS2. In Sections 4.2, 4.3, and 4.4, we will explain the details of classes Event
and Handler, class Scheduler, and class Simulator, respectively. Finally, we
summarize this chapter in Section 4.6.
It, then, places events create
event
insert
event
Event5
time = 3.7
Action5
on the timeline Event1
time = 0.9
Action1
Event2
time = 2.2
Action2
Event3
time = 5
Action3
Event4
time = 6.8
Action4
Time
chronologically.
(second)
1 2 3 4 5 6 7
Fig. 4.1. A sample chain of events in a discrete-event simulation. Each event con-
tains execution time and a reference to the next event. In this figure, Event1 creates
and inserts Event5 after Event2 (the execution time of Event 5 is at 3.7 second).
1
www.ns2ultimate.com
By execution, we mean taking actions associated with an event.
18. C++ Function Schedule(…)
Input
Handler (*h): Specify associated actions
Event (*e): Event to be placed on the timeline
Delay (d): Delay time (from the current time) when the
event will occur
Main steps
1. Check for error
2. Configure events and handler
3. Put the event on the time line
www.ns2ultimate.com
19. C++ Function Schedule(…)
//~ns/common/scheduler.cc
void Scheduler::schedule(Handler* h, Event* e, double delay)
{
< CHECKING FOR ERRORS >
Step 1: Error checking
e->uid_ = uid_++;
e->handler_ = h;
double t = clock_ + delay; Step 2: Configuration
e->time_ = t;
insert(e);
}
Step 3: Put the event “e” on the time line
www.ns2ultimate.com
20. Step 1: Checking Error
Four major types of error
1. Null handler:
- Actions associated with handlers are not specified
if (!h) {
fprintf(stderr,
"Scheduler: attempt to schedule an event with a NULL
handler."
" Don't DO that at time %fn", clock_);
abort();
};
www.ns2ultimate.com
21. Step 1: Checking Error
Four major types of error
2. Undispatched event:
- The unique ID of the event must be a positive number
- I’ll talk about this in the next post :)
if (e->uid_ > 0) {
printf("Scheduler: Event UID not valid!nn");
abort();
}
Note: This is a common error message!
Now you know how to fix the error!
www.ns2ultimate.com
22. Step 1: Checking Error
Four major types of error
3. Move backward in time:
- You can only schedule event to occur in future, not in the past!
if (delay < 0) {
fprintf(stderr,
"warning: ns Scheduler::schedule: scheduling eventnt"
"with negative delay (%f) at time %f.n", delay,
clock_);
}
www.ns2ultimate.com
23. Step 1: Checking Error
Four major types of error
4. uid_ value overflow:
- uid_ of the Scheduler is initialized to ‘1’ and incremented by one for
every new event
- Its value can never be negative.
- The only reason for its negative value is an overflow of its value
space--meaning we use up all available UIDs.
if (uid_ < 0) {
fprintf(stderr, "Scheduler: UID space exhausted!n");
abort();
}
www.ns2ultimate.com
24. Step 2: Configuration
void Scheduler::schedule(Handler* h, Event* e, double delay)
{
... Draw a unique ID from the
e->uid_ = uid_++;
e->handler_ = h; common pool and associated
double t = clock_ + delay;
e->time_ = t;
it with the event
...
}
Associated the input handler
*h with the event
Update the
dispatching time
www.ns2ultimate.com
26. Dispatch
The Scheduler “dispatches” a relevant handler to take
default actions.
//~ns/common/scheduler.cc
void Scheduler::dispatch(Event* p, double t)
{ Update the current time
if (t < clock_) {
Invert the sign of the uid_
< ERROR >
}
Execute actions specified in the
clock_ = t;
p->uid_ = -p->uid_; function handle(p) of the
p->handler_->handle(p); associated handler
}
www.ns2ultimate.com
28. Move Forward
Using a while loop
Take an event “*p”
from the event list.
//~ns/common/scheduler.cc
void Scheduler::run()
{
instance_ = this;
Event *p;
while (!halted_ && (p = deque())) {
dispatch(p, p->time_);
}
}
Then dispatch the event
www.ns2ultimate.com
29. For more
information
about NS2
Please see
this book
from Springer
T. Issaraiyakul and E. Hossain, “Introduction to Network Simulator NS2”, Springer 2009
or visit www.ns2ultimate.com