SlideShare una empresa de Scribd logo
1 de 29
NS2 Event Scheduler

     Ns2Ultimate.com
            by
    Teerawat Issariyakul


      December 2011
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
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
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
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
What Exactly Does the
   Scheduler Do?
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
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.
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
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
Unique ID
The Scheduler receives a new event

                Scheduler

                 uid_ = 20




                                     www.ns2ultimate.com
Unique ID
 The Scheduler receives a new event

 Event           Scheduler

                  uid_ = 20
uid_=Null




                                      www.ns2ultimate.com
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).
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
Task 1 (Schedule):

Put events on the simulation
         time line
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.
C++ Class Scheduler
//~ns/common/scheduler.h

class Scheduler : public TclObject {
public:                                Task1: Schedule
   static Scheduler& instance() {
      return (*instance_);
   }
   void schedule(Handler*, Event*, double delay);
   virtual void run();
   double clock() const {

                                     Task 2: Dispatch
      return (clock_);
   }
protected:
   void dispatch(Event*);
   void dispatch(Event*, double);
   Scheduler();
   int command(int argc, const char*const* argv);
   double clock_;
   int halted_;
   static Scheduler* instance_;
   static scheduler_uid_t uid_;
};
                                                    www.ns2ultimate.com
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
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
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
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
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
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
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
Task 2 (Dispatch):

Execute actions associated
     with each event.
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
Task 3 (Move):

Move to the Next Event
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
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

Más contenido relacionado

La actualidad más candente

Processes in unix
Processes in unixProcesses in unix
Processes in unix
miau_max
 
Interbloqueo sistemas operativos
Interbloqueo  sistemas operativosInterbloqueo  sistemas operativos
Interbloqueo sistemas operativos
Andy Lopez
 
Classical problem of synchronization
Classical problem of synchronizationClassical problem of synchronization
Classical problem of synchronization
Shakshi Ranawat
 

La actualidad más candente (20)

Chat server nitish nagar
Chat server nitish nagarChat server nitish nagar
Chat server nitish nagar
 
Data-Oriented Design과 유니티 DOTS
Data-Oriented Design과 유니티 DOTSData-Oriented Design과 유니티 DOTS
Data-Oriented Design과 유니티 DOTS
 
Infrastructure as Code with Terraform
Infrastructure as Code with TerraformInfrastructure as Code with Terraform
Infrastructure as Code with Terraform
 
MMOG Server-Side 충돌 및 이동처리 설계와 구현
MMOG Server-Side 충돌 및 이동처리 설계와 구현MMOG Server-Side 충돌 및 이동처리 설계와 구현
MMOG Server-Side 충돌 및 이동처리 설계와 구현
 
Processes in unix
Processes in unixProcesses in unix
Processes in unix
 
Interbloqueo sistemas operativos
Interbloqueo  sistemas operativosInterbloqueo  sistemas operativos
Interbloqueo sistemas operativos
 
[NDC21] <쿠키런: 킹덤> 서버 아키텍처 뜯어먹기! - 천만 왕국을 지탱하는 다섯가지 핵심 기술
[NDC21] <쿠키런: 킹덤> 서버 아키텍처 뜯어먹기! - 천만 왕국을 지탱하는 다섯가지 핵심 기술[NDC21] <쿠키런: 킹덤> 서버 아키텍처 뜯어먹기! - 천만 왕국을 지탱하는 다섯가지 핵심 기술
[NDC21] <쿠키런: 킹덤> 서버 아키텍처 뜯어먹기! - 천만 왕국을 지탱하는 다섯가지 핵심 기술
 
Dining philosopher problem operating system
Dining philosopher problem operating system Dining philosopher problem operating system
Dining philosopher problem operating system
 
Kernels and its types
Kernels and its typesKernels and its types
Kernels and its types
 
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
 
What is Game Server ?
What is Game Server ?What is Game Server ?
What is Game Server ?
 
게임서버프로그래밍 #8 - 성능 평가
게임서버프로그래밍 #8 - 성능 평가게임서버프로그래밍 #8 - 성능 평가
게임서버프로그래밍 #8 - 성능 평가
 
OS multiprocessing -.pptx
OS multiprocessing -.pptxOS multiprocessing -.pptx
OS multiprocessing -.pptx
 
System call (Fork +Exec)
System call (Fork +Exec)System call (Fork +Exec)
System call (Fork +Exec)
 
Quic을 이용한 네트워크 성능 개선
 Quic을 이용한 네트워크 성능 개선 Quic을 이용한 네트워크 성능 개선
Quic을 이용한 네트워크 성능 개선
 
RPC communication,thread and processes
RPC communication,thread and processesRPC communication,thread and processes
RPC communication,thread and processes
 
CQRS and Event Sourcing for Java Developers
CQRS and Event Sourcing for Java DevelopersCQRS and Event Sourcing for Java Developers
CQRS and Event Sourcing for Java Developers
 
[데브루키] 이벤트 드리븐 아키텍쳐
[데브루키] 이벤트 드리븐 아키텍쳐[데브루키] 이벤트 드리븐 아키텍쳐
[데브루키] 이벤트 드리븐 아키텍쳐
 
Classical problem of synchronization
Classical problem of synchronizationClassical problem of synchronization
Classical problem of synchronization
 
Unite2019 HLOD를 활용한 대규모 씬 제작 방법
Unite2019 HLOD를 활용한 대규모 씬 제작 방법Unite2019 HLOD를 활용한 대규모 씬 제작 방법
Unite2019 HLOD를 활용한 대규모 씬 제작 방법
 

Similar a NS2--Event Scheduler (10)

NS2: Events and Handlers
NS2: Events and HandlersNS2: Events and Handlers
NS2: Events and Handlers
 
Dynamic UID
Dynamic UIDDynamic UID
Dynamic UID
 
Foundations and methods of stochastic simulation
Foundations and methods of stochastic simulationFoundations and methods of stochastic simulation
Foundations and methods of stochastic simulation
 
The Power Of Event Chapter 5
The Power Of Event Chapter 5The Power Of Event Chapter 5
The Power Of Event Chapter 5
 
Concurrency 2010
Concurrency 2010Concurrency 2010
Concurrency 2010
 
Multimedia Video Systems report
Multimedia Video Systems reportMultimedia Video Systems report
Multimedia Video Systems report
 
Running a Scalable And Reliable Symfony2 Application in Cloud (Symfony Sweden...
Running a Scalable And Reliable Symfony2 Application in Cloud (Symfony Sweden...Running a Scalable And Reliable Symfony2 Application in Cloud (Symfony Sweden...
Running a Scalable And Reliable Symfony2 Application in Cloud (Symfony Sweden...
 
Stephens-L
Stephens-LStephens-L
Stephens-L
 
Predictable Java af Anders P Ravn, CISS og Hans Søndergaard, ViaUC
Predictable Java af Anders P Ravn, CISS og Hans Søndergaard, ViaUCPredictable Java af Anders P Ravn, CISS og Hans Søndergaard, ViaUC
Predictable Java af Anders P Ravn, CISS og Hans Søndergaard, ViaUC
 
Ooad 3
Ooad 3Ooad 3
Ooad 3
 

Más de Teerawat Issariyakul (12)

Trump-Style Negotiation: Powerful Strategies and Tactics for Mastering Every ...
Trump-Style Negotiation: Powerful Strategies and Tactics for Mastering Every ...Trump-Style Negotiation: Powerful Strategies and Tactics for Mastering Every ...
Trump-Style Negotiation: Powerful Strategies and Tactics for Mastering Every ...
 
Intelligent entrepreneurs by Bill Murphy Jr.
Intelligent entrepreneurs by Bill Murphy Jr.Intelligent entrepreneurs by Bill Murphy Jr.
Intelligent entrepreneurs by Bill Murphy Jr.
 
20111126 ns2 installation
20111126 ns2 installation20111126 ns2 installation
20111126 ns2 installation
 
20111107 ns2-required cygwinpkg
20111107 ns2-required cygwinpkg20111107 ns2-required cygwinpkg
20111107 ns2-required cygwinpkg
 
packet destruction in NS2
packet destruction in NS2packet destruction in NS2
packet destruction in NS2
 
Basic Packet Forwarding in NS2
Basic Packet Forwarding in NS2Basic Packet Forwarding in NS2
Basic Packet Forwarding in NS2
 
NS2 Object Construction
NS2 Object ConstructionNS2 Object Construction
NS2 Object Construction
 
NS2 Shadow Object Construction
NS2 Shadow Object ConstructionNS2 Shadow Object Construction
NS2 Shadow Object Construction
 
20100712-OTcl Command -- Getting Started
20100712-OTcl Command -- Getting Started20100712-OTcl Command -- Getting Started
20100712-OTcl Command -- Getting Started
 
NS2: Binding C++ and OTcl variables
NS2: Binding C++ and OTcl variablesNS2: Binding C++ and OTcl variables
NS2: Binding C++ and OTcl variables
 
NS2 Classifiers
NS2 ClassifiersNS2 Classifiers
NS2 Classifiers
 
Ns-2.35 Installation
Ns-2.35 InstallationNs-2.35 Installation
Ns-2.35 Installation
 

Último

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Último (20)

Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 

NS2--Event Scheduler

  • 1. NS2 Event Scheduler Ns2Ultimate.com by Teerawat Issariyakul December 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 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
  • 6. What Exactly Does the Scheduler Do?
  • 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
  • 11. Unique ID The Scheduler receives a new event Scheduler uid_ = 20 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
  • 15. Task 1 (Schedule): Put events on the simulation time line
  • 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.
  • 17. C++ Class Scheduler //~ns/common/scheduler.h class Scheduler : public TclObject { public: Task1: Schedule static Scheduler& instance() { return (*instance_); } void schedule(Handler*, Event*, double delay); virtual void run(); double clock() const { Task 2: Dispatch return (clock_); } protected: void dispatch(Event*); void dispatch(Event*, double); Scheduler(); int command(int argc, const char*const* argv); double clock_; int halted_; static Scheduler* instance_; static scheduler_uid_t uid_; }; www.ns2ultimate.com
  • 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
  • 25. Task 2 (Dispatch): Execute actions associated with each event.
  • 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
  • 27. Task 3 (Move): Move to the Next Event
  • 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

Notas del editor

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n