Issa sariyayakul ul erwat atteerawat/publications/NS2/W04-Scheduler.pdfsariya yakul ul se of Issa...

42
Event Driven Simulation in NS Event Driven Simulation in NS2 Event Driven Simulation in NS Event Driven Simulation in NS2 Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 1 ed pared Prepa P P ook: T. Iss y by at erwat i i Si Si Si Si Si Si Si m m m m m m m u u u u u u u Tee ul yakul sariya Issa

Transcript of Issa sariyayakul ul erwat atteerawat/publications/NS2/W04-Scheduler.pdfsariya yakul ul se of Issa...

Event Driven Simulation in NSEvent Driven Simulation in NS22Event Driven Simulation in NSEvent Driven Simulation in NS22mmmm

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 1ed

pared

Prepa

PPook: T. Iss

yby

at

erwat

iiSiSiSiSiSiSiSimmmmmmmuuuuuuu

Tee

mm

ulya

kul

sariy

a

Issa

Event Driven Simulation in NS2R D E T

Event Driven Simulation in NS2• Recap: Discrete Event v.s. Time

Driven• NS2 Simulation Concept• Events and HandlersEvents and Handlers• The Scheduler

h l• The Simulator• SummarySummary

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 2ed

y

pared

ry

Prepa

PPook: T. Iss

by yyator

at

erwatncept

ers

Teedlerer

ulak

ulin

sariy

T

n in

Issav.s. Ti

Event-Driven v s Time-DrivenEvent Driven v.s. Time Driven• Q: Time Driven = ( )Move from one time slot to anotherQ me Dr ven ( )

• Q: Event Driven = ( )• Time Driven or Discrete Time Simulation

Move from one t me slot to another

Move from one event to another

Time Driven or Discrete Time Simulation• Example: Packet arrivals and departures

Arrivals DeparturesArrivals Departures

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 3ed

vals

pared

pa

ivals

Prepa

ook: T. Issrep

yyby y y by

tTim

erwae Tim

vals a

TeeTeTeTeTeT

ulya

kul

Driv

sariy

aDr

Iss

Time-Driven SimulationTime Driven Simulation• Observe the buffer for every FIXEDObserve the buffer for every FIXED

period (e.g., 1 second)

No. of Packet in the Buffer

2

3

1Time (s)

1 2 3 4 5 6 7 8 9 10 11 12

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 4Ppa

repap

ed dededed4

Prepa

ook: T. Iss

ep

yby

bby

at

erwat

Tee

ulya

kul

on

ariyation

FIX

Iss

y FI

Time-Driven Simulation• Simulation event for every time slot (fixed

interval)• Example Psudo Codes:

For t = 1 to sim_time {

p

if (arrival)buffer = buffer + 1;buffer buffer 1;

if (departure) buff buff 1;buffer = buffer -1;

print(buffer);

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

}5

ed

(db

pared

pa

bp

Prepa

PPook: T. Iss

pa}

f

by f

deparat

erwatwa

m_tim

Teeval)r

ulya

kul

on

sariy

ame slo

Issa

Event-Driven SimulationEvent Driven Simulation• Go from one event to anotherGo from one event to another• Same Example

No. of Packet in the Buffer

2

3

0 8 1 5 2 4 5 2 5 5 7 4 9 4 10 5 11 5

1Time (s)

0.8 1.5 2.4 5.2 5.5 7.4 9.4 10.5 11.5

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 6P

ed d

pared

papPrep

aook: T. Iss

ep

yyby ybyyyy

at

erwat

TeeTT

ulya

kul

ion

sariy

aatioher

Issahe

Event-Driven Simulation• Use a Scheduler• Maintain a set of eventsMaintain a set of events• Example

CreateEvent();R ()

CreateEvent(){Pkt1.arr(0.8)

Run ();

Psudo Codes

( )Pkt2.arr(1.5)}Psudo Codes }

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 7ddCod

pared

Cod

Prepa

PPook: T. Iss

yby

b

at

erwat

erTeeTee

ulya

kul

sariy

aation

Issa

Event Driven SimulationEvent-Driven SimulationEvent ID 1 2 3 4 5 6

CreateEvent();

Type Arrival Arrival Arrival Arrival Arrival ArrivalTime 0.8 1.5 5.2 7.4 9.4 11.5

C t CreateRun();

Create departure

Create departure

Create departure

Event ID 7 8 9Type Departure Departure Departure

p

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 8

Time 2.4 5.5 10.5

ed

ed

pareddarpedvent I

Type

Prepa

ook: T. Iss

paepepTyyyby

yarture

at aat

7.4

erwat

TeeTTTTe

ulya

kul

on

sariy

ation

ssaar y5

Issa

ssIsrival A

E nt D i n Sim l ti n in NS2Event Driven Simulation in NS2R E D T D• Recap: Event Driven v.s. Time Driven

• NS2 Simulation Conceptm p• Events and Handlers

The Scheduler• The Scheduler• The Simulator• Summary

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 9ed

y

pared

Prepa

PPook: T. Iss

yby

at ept

erwatp

rs

Teer

ulya

kul

n N

sariyin N

Issa Time

NS2 Simulation Concept• Event-Driven Simulation• Recap: Simulation Main StepsRecap: Simulation Main Steps

– DesignSi l i– Simulation• Network Configuration Phase � CreateEvent()• Simulation Phase � Run()

– Result Compilationp

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 10

Com

pared

Prepa

PPook: T. Iss

yby ion Ph

p

tin S

erwaain S

Teenfigur

ulya

kul

sariy

acept

Issa

n

Simulation• Network Configuration Phase

– Create topologyCreate topology– Schedule event (e.g., CreateEvent())Si l ti Ph• Simulation Phase– Simulator::run() (e.g., Run())g– Execute the scheduled events

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 11ed

pared

Prepa

PPook: T. Iss

y

u

by

the sat

erwatg., Cre

Teeseun()

ulya

kul

sariy

a

Issa

Phase

Event Driven Simulation in NS2R D E T• Recap: Discrete Event v.s. Time Driven

• NS2 Simulation Concept• Events and HandlersEvents and Handlers• The Scheduler• Summary

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 12

pared

Prepa

PPook: T. Iss

yby Tee

at

erwatncept

ersdlerer

ulya

kul

sariy

an in TIss

av.s. Ti

Concepts of Events and Handlersp f• Event-driven simulation

P t t th i l ti ti li– Put event on the simulation timeline– Move forward in time– When finding an event, execute

associated actions• Main components

– Events � C++ class Event– Actions � C++ class Handler

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

Q: Give examples of events.13P

ed

paredar

Prepa

ook: T. Iss

y�by

at

erwatnt, e

s

TeeentsC

ulya

kul

lers

sariy

atiIss

aon tim

Event and Handler• Examples of Events

– Packet Arrivals/Departures– Start/Stop Application

• C++ Classes:– Event: Define events (packet arrival)– Handler: Define actions associated with an

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

Handler Define actions associated with an event (tell the node to receive the packet)

14Prepa

PP

ed s:

pared: Def

andler

ook: T. Iss

Handleevent

by

at wat

ulya

kul

sariy

a

Issa

Class Event

• Main variables: next : Next event

handle(){<actions>

}

handle(){<actions>

}– next_: Next event– uid_: Unique ID

ti Ti

handler handler

– time_: Time– handler_: Handler

uid time

handler_ next_

uid time

handler_ next_

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

uid_ time_event

uid_ time_event

15

paed

UniTi

arede_: Ti

andle

Prepa

PPook: T. Iss

ytby

xt que I

Tees:

ulya

kul

Example Events

“$ftp start” “$cbr start”Simulation time0.05 0.1

h dl

handle(){send FTP packets}

h dl

handle(){send CBR packets}

handler

h ndl r t

handler

h ndl r t…

uid_ time_

handler_0.051next_

uid_ time_

handler_0.12next_

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

event event16

ed dde

papared

pa

ndler

areare1

Prepa

PPook: T. Iss

papa

yyyby er

bybybybyb

at at

$c

erwatwawat

0wa

TeeTTetsTee

ulya

kul

sariy

a

Issa

br st

Event: Recap• Simulation steps:1. Put events on the simulation timeline

• Use the pointers “next_” of class Event2 Execute event2. Execute event

• Also called “fire” or “dispatch”• Take default actionsTake default actions• function handle() of class Handler

3 Move to the next event3. Move to the next event• Use the pointer “next_” of class Event

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 17

pad

nth

red

to thse the

Prepa

PPook: T. Iss

t

by lt a

handle

atatio

erwatxt_” o

Teeire” or

ulya

kul

sariy

a

Issa

Handlersh E• Associated with an Event

• Define Default ActionsDef ne Default Act ons� C++ function handle(Event*)

What is this?What is the purpose?

E l Cl N Obj t• Example: Class NsObject

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 18P

d

Prepook: T. Iss

yby Class

at

(E

wat

T

ulya

kul

sariy

a

Issa

Even

Events and HandlersSimulation S mulat ontime0.05

• When hitting an event e, execute handler_->handle(e)

• Define the action associated with an

handle(){send FTP packets}

Define the action associated with an event in the handler, NOT in the event

handler}

• We need mechanism to – Store events

handler_0.051next_ – Execute event at the right time

THIS FUNCTIONITY BELONGS TOuid_ time_event

THIS FUNCTIONITY BELONGS TO THE SCHEDULER

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 19ed

ededee

paredredare

de_eent

Prepa

PPook: T. Iss

yby

thhe ac

erwathe ac

in the nt

Tee• We

ulya

kul

sariy

a

Issag an eve

andle(

QuestionQuestion

• What is the main purpose of events ?• What happen if NS2 does not define• What happen if NS2 does not define

classes Event, Handler, and S h d l ?Scheduler?– Some events have not occurred; – Every event occurs at the same time.

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 20ed

pared

Prepa

PPook: T. Iss

y

s

by

ent oatdoe

erwat doe

ndle

Tees ha

ulya

kul

sariy

a

Issa

se of

Two Types of Eventsyp1. At Event: (Derived from Class Event)

E OT l d– Action: Execute an OTcl command– Examples:p

C Cl A E– C++ Class AtEvent– Create by instproc “at” with syntaxy p y

$ns at <time> <Tcl command>

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

$ m mm

21ed

e b

pared$ns

Prepa

PPook: T. Iss

$yAby

ss Aty

at at

Te

ulya

kul

sariy

aClass E

Issac com

Two Types of EventsTwo Types of Events

2. Packet: (Derived from Class Event)– Action: Receive a packetAction: Receive a packet

?? Type casting:Packet is a derived class

– C++ Class Packet

of class Event

C++ Class Packet

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 22dass

pared

Class

Prepa

PPook: T. Iss

by

tcket

waacket

Te

ulya

kul

sariy

ats

Issa

m Clas

Q tiQuestions

Q H d E h• Q: How do we put an AtEvent on the simulation timeline? ( )$ns at <time> <Tcl command>

• Q: Is it possible to put a Packet on the simulation timeline? Why or why the s mulat on t mel ne? Why or whynot? ( )

• How do we put events on theYes; Packet derived from class Event

• How do we put events on the simulation timeline? � Use THE SCHEDULERSCHEDULER

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 23P

d

nULE

pared

ULE

Prepa

ook: T. Iss

ep

u

by

e puttime

at

erwato put a

meli

Tee

me ed

ulya

kul

sariy

a

IssaEven

<t

Event Driven Simulation in NS2R D E T• Recap: Discrete Event v.s. Time Driven

• NS2 Simulation Concept• Events and HandlersEvents and Handlers• The Scheduler

h l• The Simulator • SummarySummary

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 24ed

y

pared

ry

Prepa

PPook: T. Iss

yby ator

Teeat

erwatncept

ersdlerer

ulya

kul

sariy

an in TIss

av.s. Ti

RecapRecap• Event = An indication of future eventEvent = An indication of future event• Handler defines the default action (i.e.,

handler(e))( ))• NS2 moves forwards in time and execute

events • Execute = Fire = Dispatch

• What’s more? – How to put an event on the simulation timeline?

H t t th t d t– How to execute the stored events– The scheduler

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 25ed

ut a

redexecsched

Prepa

PPook: T. Iss

yby e?

n eve

at

erwatn time

TeeDispat

ulya

kul

sariy

ae eve

Issae ev

t act

The Scheduler• Maintain Simulation (virtual) time• Move forward in time (fn run())• Maintain the unique ID for eventsMaintain the unique ID for events• Put events on the timeline (fn schedule(.))

E t t th ti li (f di t h( ))• Execute events on the timeline (fn dispatch(.))event handler

uid time

handler_ handle(){…

}uid_ time_

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 26P

eded

ed

parededed

Prepa

ook: T. Iss

ep

yby hanbby

tor ev

erwaor e

eline (h t

Teen the

TeTeTTT

ulya

kul

sariy

ame

Issa

n())

C++ Class Scheduler

Current time

Unique ID: incremented for??

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

incremented for every new event( uniqueness )

27Pare

dpa

Prepook: T. Iss

epa

)

ulya

kul

iya

Functions Scheduler::schedule( )Functions Scheduler::schedule(.)• schedule(h e delay)• schedule(h,e,delay)

– Associate Event “e” with a handler “h”Put the Event “e” on the simulation time with delay– Put the Event e” on the simulation time with delay “delay”

< Checking for Error > New unique ID

Bind “e” and “h”

U d t tiUpdate time

Put “e” on the time line

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

Put e on the time line

28PrepPPook: T. Iss

yrobyy

at

anmulat

erwamulat

Te>

ulak

uledu

sariy

hedu

Issa

dler

Functions Scheduler::schedule(.)• schedule(h,e,delay): Checking for Error1. Null handler (i.e., h = 0)1. Null handler (i.e., h 0)2. uid_ of the event > 0 � Something wrong3 d l 0� G b k in tim3. delay < 0 � Go back in time4. uid_ < 0 � Use up the uid_

12

334

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 29PPreook: T. Iss

at

Soim

erwattime

he uid

ee

ulya

kul

le(.)

sariy

aor Err

Issa

omet

Function Scheduler::dispatch(.)• Execute the event

event handler Why put negative?handler

d

handler_Why put negative? � Let have a look at

the UID concept

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

uid_ time_*e *h

30P

eed

eddd

vent

pareredre

Prepa

ook: T. Iss

bybb

at

ulya

kul

sariy

aatch(

Issa

Two types of Unique ID (UID)Two types of Unique ID (UID)1 Scheduler: Global UID1. Scheduler: Global UID

2 Event: Individual UID assigned by the2. Event: Individual UID assigned by the Scheduler

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 31P

d ed

Prepa

ook: T. Iss

by aua

erwTee

ulak

ulUID

sariy

(UID

Issa

Global UIDGlobal UID• For the Scheduler• For the Scheduler• Always Positive• Incremented for every new event (fn

schedule(.))

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 32P

dpa

re

Prepook: T. Iss

b

at

erwaty new

Tee

ulya

kul

sariy

a

Issa

Individual UIDIndividual UID• Unique to each eventq

– Assigned by the scheduler within fn schedule(.)– Negative when being dispatched fn dispatch(.)Negat ve when be ng d spatched fn d spatch(.)

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 33

papare

PPreook: T. Iss

epa

yyby

ttched

watche

ulya

kul

sariy

a

Issa

thin fn

Individual UIDIndividual UID• Unique to each eventq

– Positive: assigned by fn schedule(.)– Negative: dispatched fn dispatch(.)Negat ve d spatched fn d spatch(.)– Dynamics: uid_ is switching between +/- values

dispatch( )

•schedule(.)•If negative �( Event UID not valid )?

dispatch(.)� Positive uid_

�( Event UID not valid )?

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 34PPreook: T. Iss

otnot atspatc

erwaspat

hing be

Tee

ulya

kul

sariy

a

Issa

ule(.)

Function Scheduler::run()Function Scheduler::run()

• Start simulation

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 35ed

pared

Prepa

PPook: T. Iss

at

ulya

kul

)

sariy

an()

Issa

Event Driven Simulation in NS2R D E T• Recap: Discrete Event v.s. Time Driven

• NS2 Simulation Concept• Events and HandlersEvents and Handlers• The Scheduler

h l• The Simulator• SummarySummary

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 36ed

y

pared

ry

Prepa

PPook: T. Iss

yby ator

Teeat

erwatncept

ersdlerer

ulya

kul

sariy

an in TIss

av.s. Ti

The SimulatorM i i hi h h d• Maintain assets which are shared among simulation objectsOT l d C Cl Si l• OTcl and C++ Classes Simulator

• Instvar– scheduler_: The schduler– nullAgent_: The packet destruction object

N d ( d d ) d b– Node_(<nodeid>) stores node objects– link_(sid:did) stores link objects connecting

two nodestwo nodes

sid did

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 37ed

:dies

pared

des

rere

Prepa

PPook: T. Iss

ydby deid>)

d) sto

at

erwatimula

Teeschdule pack

ulya

kul

sariy

ar

dIssashared

The SimulatorThe Simulator• Suppose $ns is a simulator objectSuppose $ns s a s mulator object• Three main instprocs 1 Constructor: init{}1. Constructor: init{}2. run{}: Start simulation

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 38PPrepa

ook: T. Iss

ep

at

erwation

ee

ulya

kul

sariy

abject

Iss

obje

Event Driven Simulation in NS2R D E T• Recap: Discrete Event v.s. Time Driven

• NS2 Simulation Concept• Events and HandlersEvents and Handlers• The Scheduler

h l• The Simulator• SummarySummary

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 39ed

y

pared

ry

Prepa

PPook: T. Iss

yby ator

Teeat

erwatncept

ersdlerer

ulya

kul

sariy

an in TIss

av.s. Ti

SummarySummary

N l E D• NS2 Simulator is Event Driven• Event

– Unique ID + Time + Handler– Two derived classes: ( AtEvent and Packet )( )

• Handlers– ( default actions )( default actions )– ( function handle(e) )

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 40ed

h

pared

Prepa

PPook: T. Iss

ysby ns

bandle(

at

erwatdler

( AtE

Tee

(

ulya

kul

sariy

a

Issaven

SummarySummary

h d l• Scheduler– schedule(.): ( Put an event in the list )– dispatch(.): ( Executes an event )– run(): ( Start simulation )

• Event UID Dynamics– Schedule()� +– Schedule() � +,

– dispatch() � -• Simulator

– Maintain all common objects

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

j– Start the simulation (e.g., “$ns run”)

41PrePP

ed ) �

paredor

intain

epa

ook: T. Iss

Starty+

by ,

at

in

erwatan ev

simul

Teemics

ulya

kul

sariy

a

Issa

th

Related files• ~ns/common/schedule.h,cc• ~ns/common/simulator.h,ccns/common/simulator.h,cc• ~ns/tcl/lib/ns-lib.tcl

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 42ed

pared

Prepa

PPook: T. Iss

yby

at cc

erwatcc

Tee

ulya

kul

sariy

a

Issa