Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Dynamic UID
1. Dynamic of Unique ID
Ns2Ultimate.com
by
Teerawat Issariyakul
January 2011
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
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
Schedule = Put an event on the simulation
(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
time line
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
Dispatch = Execute the 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
3. Recap
• Attributes of Event
handler handle(){ handle(){ handler
<actions> <actions>
} }
handler_ next_ handler_ next_
event uid_ time_ uid_ time_ event
How do we assign a unique ID (i.e., uid_) to the event?
www.ns2ultimate.com
4. Recap
• Attributes of Event
handler handle(){ handle(){ handler
<actions> <actions>
} }
handler_ next_ handler_ next_
event uid_ time_ uid_ time_ event
Using the Scheduler
www.ns2ultimate.com
5. Recap: Function schedule(h,e,delay)
Put an event *e on the timeline at “delay”second in
future, and associated the event with a handler “*h”.
Scheduler s& = Scheduler::instance();
s.schedule(h,e,delay);
Call here h e
associated
Handler Event
Delay
x
current time
www.ns2ultimate.com
6. Recap: Function schedule(h,e,delay)
//~ns/common/scheduler.cc
void Scheduler::schedule(Handler* h, Event* e, double delay)
{
< CHECKING FOR ERRORS >
e->uid_ = uid_++;
e->handler_ = h; 1. Increment the local variable uid_
double t = clock_ + delay; 2. Assigned the incremented value
e->time_ = t;
as the unique ID of the event
insert(e);
}
Scheduler Event
assign to
uid_ uid_
www.ns2ultimate.com
7. So, NS2 has two type of unique
IDs: Global Unique ID and
Event
8. T T
wo ypes of UID
Global UID
The variable uid_ of the Scheduler
One and only one for a simulation
Event UID or Local UID
The variable uid_ of an event
Every event has its own uid_
www.ns2ultimate.com
9. Global UID and Event UID
Simulation: In function Schedule(…)
Event
Event UID:
assign to uid_
- Unique to each
Scheduler event
uid_ assign to Event
uid_ - Many events in
assign to a simulation ➠
Event Many local UID
Global UID: uid_ in a simulation
Unique to the
simulation
www.ns2ultimate.com
10. Global UID and Event UID
Simulation: In function Schedule(…)
//~ns/common/scheduler.cc
Event
uid_ void Scheduler::schedule
assign to
(Handler* h, Event* e,
Scheduler double delay){
uid_ assign to Event Event UID is always a
uid_ positive new number
...
assign to e->uid_ = uid_++;
e->handler_ = h;
Event double t = clock_ +
uid_ delay;
e->time_ = t;
insert(e);
}
www.ns2ultimate.com
11. Event UID
Simulation: In function dispatch(…)
Event
x uid_ //~ns/common/scheduler.cc
void Scheduler::dispatch(Event*
p, double t){
Reversing
clock_ = t;
the sign
p->uid_ = -p->uid_;
p->handler_->handle(p);
Event
}
-x uid_
www.ns2ultimate.com
12. 84
Dynamic of Event UID
4 Implementation of Discrete-Event Simulation in NS2
schedule(...) and dispatch(...) functions. The sign toggling mechanism
of unique ID ensures that events will be scheduled and dispatched properly. If
a scheduled UIDis nota certain event changes its value like
A event of dispatched, or is dispatched twice, its unique ID will
be positive, and an attempt to schedule this undispatched event will cause an
this (a positive
error (Lines 5 and 6 in Program 4.7).
new value)
–2
(reversing
Fig. 4.2. Dynamics of Event unique ID (uid) : Take a positive value from
the sign)
Scheduler::uid when being scheduled, and invert the sign when being dispatched.
Increment upon schedule and inversion of sign upon dispatch.
www.ns2ultimate.com
13. T key questions
wo
1. Why is the “event UID” changes its value
like this?
2. What if the dynamic is different?
www.ns2ultimate.com
14. Dynamic of Event UID
It is a UID protection mechanism.
But before going further, let’s ask
another basic question:
WHAT IS AN EVENT?
www.ns2ultimate.com
15. EVENTS
NS2 has several type of events
Let’s focus on packet reception events
Event
... AtEvent Packet ...
Packet reception events
Source: http://www-rp.lip6.fr/ns-doc/ns226-doc/html/hierarchy.htm
www.ns2ultimate.com
16. Packet Reception Event
Packet
uid_
Time where the
packet is time_
received
handler_
a pointer to
node 2
Node 1 Node 2
www.ns2ultimate.com
17. Packet Event Reuse
One packet could be passed among
several Nodes
Event/
Packet
2 uid_
time_
handler_
Node 1 Node 2 Node 3
www.ns2ultimate.com
18. Packet Event Reuse
NS2 does not create a new event every time it forwards a
packet.
It
Changes the event UID, and
passes the same packet to the next object
Event/
Packet
6 uid_
time_
handler_
Node 1 Node 2 Node 3
www.ns2ultimate.com
19. An example of packet forwarding
time
0 5.1
2
Instance 1:
Event/ Event/
A packet with UID Packet Packet
“2” is scheduled to
2 uid_ 6 uid_
arrived at Node 2 at
1.2 time_ 5.1 time_
“0” s.
handler_ handler_
Node 1 Node 2 Node 3
www.ns2ultimate.com
20. An example of packet forwarding
time
1.2 5.1
2
Instance 2: Event/
Packet
A packet with UID
“6” is scheduled to 6 uid_
arrived at Node 3 at 5.1 time_
“5.1” s. handler_
Node 1 Node 2 Node 3
www.ns2ultimate.com
21. 84 4 Implementation of Discrete-Event Simulation in NS2
An example of packetThe sign toggling mechanism
schedule(...) and dispatch(...) functions.
forwarding
of unique ID ensures that events will be scheduled and dispatched properly. If
a scheduled event is not dispatched, or is dispatched twice, its unique ID will
be positive, and an attempt to schedule this undispatched event will cause an
error (Lines 5 and 6 in Program 4.7).
–2
t=0:
Fig. 4.2. Dynamics of Event unique ID (uid) : Take a positive value from
NS2 schedules being scheduled, and invert the sign when being dispatched.
Scheduler::uid when t=1.2:
packet reception at and inversion of sign upon dispatch.
Increment upon schedule packet is received
node 2 at 1.2 s at node 2
4.3.6 Scheduling-Dispatching Mechanism
www.ns2ultimate.com
22. 84 4 Implementation of Discrete-Event Simulation in NS2
An example of packetThe sign toggling mechanism
schedule(...) and dispatch(...) functions.
forwarding
of unique ID ensures that events will be scheduled and dispatched properly. If
a scheduled event is not dispatched, or already decided at “t=5” that
We’ve is dispatched twice, its unique ID will
be positive, and an attempt to schedule this undispatched event will cause an
the packet will be received at “t=5.1”.
error (Lines 5 and 6 in Program 4.7).
So nothing can happen here.
–2
t=5:
NS2 schedules t=5.1:
Fig. 4.2. Dynamics of Event unique ID (uid) : Take a positive value from
packet reception being scheduled, and invert the signpacket is received
Scheduler::uid when
atand inversion of sign upon dispatch. being dispatched.
Increment upon schedule
when
node 3 at 5.1 s at node 3
4.3.6 Scheduling-Dispatching Mechanism
www.ns2ultimate.com
23. An example of packet forwarding
We’ve already decided at “t=5” that
the packet will be received at “t=5.1”.
So nothing can happen here.
HOW DO WE KNOW THAT
NOTHING WILL HAPPEN AT
THESE PERIODS?
WE USE
“THE SIGN”
OF EVENT UID
www.ns2ultimate.com
24. The Sign of the Event UID
Positive (+):
The event is scheduled but not yet dispatched
Do not reschedule here. Otherwise, you will see the
following error message on the screen
Scheduler: Event UID not valid!
Negative (-):
The event has been dispatched.
It is now ready to be scheduled again.
www.ns2ultimate.com
25. Protection Mechanism
From within the function schedule(…),
NS2 checks for the error
if (e->uid_ > 0) {
printf("Scheduler: Event UID not valid!nn");
abort();
}
which will show an error message if the
event UID is positive.
www.ns2ultimate.com
26. For more
information
about NS2
Please see
this book
from Springer
T. Issaraiyakul and E. Hossain, “Introduction to Network Simulator NS2”, Second Edition,
Springer 2011, or visit www.ns2ultimate.com