NS2--Event Scheduler

13,927 views

Published on

Published in: Education, Technology
1 Comment
6 Likes
Statistics
Notes
No Downloads
Views
Total views
13,927
On SlideShare
0
From Embeds
0
Number of Embeds
8,592
Actions
Shares
0
Downloads
0
Comments
1
Likes
6
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • NS2--Event Scheduler

    1. 1. NS2 Event Scheduler Ns2Ultimate.com by Teerawat Issariyakul December 2011
    2. 2. RecapNS2 is a discrete-event simulator, where actions are associated with eventsrather than time. An event in a discrete-event simulator consists of execution NS2 is an event-driven simulationtime, a set of actions, and a reference to the next event (Fig. 4.1). These eventsconnect 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-drivensimulator, time between a pair of events does not need to be constant. Whenthe simulation starts, events in the chain are executed from left to right (i.e., Put events on the simulation timelinechronologically).1 In the next section, we will discuss the simulation concept ofNS2. In Sections 4.2, 4.3, and 4.4, we will explain the details of classes Eventand Handler, class Scheduler,time line and take actions when Move along the and class Simulator, respectively. Finally, wesummarize 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 7Fig. 4.1. A sample chain of events in a discrete-event simulation. Each event con- www.ns2ultimate.comtains execution time and a reference to the next event. In this figure, Event1 creates
    3. 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. 4. Event and HandlerAn 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. 5. Event and HandlerTerminology: “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. 6. What Exactly Does the Scheduler Do?
    7. 7. Scheduler Scheduler is defined in the C++ class Scheduler//~ns/common/scheduler.h The current timeclass 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. 8. Implementation of Discrete-Event Simulation in NS2Reference 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 eventsThere 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. 9. Reference to the SchedulerThere 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 theScheduler must be static. www.ns2ultimate.com
    10. 10. Unique IDEvery 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 toeach event.The pool of unique ID is the variable uid_ ofclass Schedule www.ns2ultimate.com
    11. 11. Unique IDThe Scheduler receives a new event Scheduler uid_ = 20 www.ns2ultimate.com
    12. 12. Unique ID The Scheduler receives a new event Event Scheduler uid_ = 20uid_=Null www.ns2ultimate.com
    13. 13. Unique IDThe 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. 14. Scheduler Three main tasks NS2Task 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. 15. Task 1 (Schedule):Put events on the simulation time line
    16. 16. Task 1: Schedule 4 Implementation of Discrete-Event Simulation in NS2The Scheduler receives Objects Usersevents 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 Eventat 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 Action5on the timeline Event1 time = 0.9 Action1 Event2 time = 2.2 Action2 Event3 time = 5 Action3 Event4 time = 6.8 Action4 Timechronologically. (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. 17. C++ Class Scheduler//~ns/common/scheduler.hclass 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. 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. 19. C++ Function Schedule(…)//~ns/common/scheduler.ccvoid 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. 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 NULLhandler." " Dont DO that at time %fn", clock_); abort(); }; www.ns2ultimate.com
    21. 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. 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. 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. 24. Step 2: Configurationvoid 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. 25. Task 2 (Dispatch):Execute actions associated with each event.
    26. 26. Dispatch The Scheduler “dispatches” a relevant handler to take default actions.//~ns/common/scheduler.ccvoid 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. 27. Task 3 (Move):Move to the Next Event
    28. 28. Move Forward Using a while loop Take an event “*p” from the event list.//~ns/common/scheduler.ccvoid Scheduler::run(){ instance_ = this; Event *p; while (!halted_ && (p = deque())) { dispatch(p, p->time_); }} Then dispatch the event www.ns2ultimate.com
    29. 29. For more information about NS2 Please see this book from SpringerT. Issaraiyakul and E. Hossain, “Introduction to Network Simulator NS2”, Springer 2009or visit www.ns2ultimate.com

    ×