Understanding Simulator::Schedule() Method

1,700 views
Skip to first unread message

PN

unread,
Aug 18, 2015, 7:14:35 PM8/18/15
to ns-3-users
I'm trying to figure out how Simulator::Schedule() Method works. I was under the impression that if the following call is made:

Simulator::Schedule(t1, &func1);

then when the program execution reaches this line, it would increment the current value of time by t1 and make a call to func1(). However, if that were true then after the following sequence of calls:

Simulator::Schedule(t1, &func1);
Simulator::Schedule(t2, &func2);
Simulator::Schedule(t3, &func3);

Time should be incremented by t1+t2+t3. However, it looks like the simulator sees t1, t2 and t3 as the actual values of time rather than increments (script is attached). Now assuming that the value of time given is an absolute value and not an increment, how does a call like the following work:

Simulator::Schedule (tEvent, &CsmaNetDevice::TransmitCompleteEvent, this);

where tEvent is the time it takes to complete the event? The above line is a part of CsmaNetDevice::TransmitStart().

Also, what happens if there is this following set of calls:

Simulator::Schedule (t1, &func1);
func2
();
Simulator::Schedule (t2, &func3);

will func2() get called before func1()? I know the answer is YES: you can see that through my script. I just want to know why.

I tried going through the Simulator::Schedule() script. However, there are more than one function calls involved and it was getting difficult for me to understand all of them. I guess that I'm facing the above questions due to my lack of knowledge on how ns 3 or a discrete event simulator works (what I know is sufficient to use and tweak/debug ns 3 at a basic level but at an advance level, I get stuck with questions such as those mentioned above).

If anyone can help me with this, it would be very much appreciated!

Additionally, if you could point me to a document/book that helps understand the working of a discrete event simulator such as ns 3, that would be fantastic. I couldn't find a good one myself. The manual is a great reference, but at certain points, it looks like it is still being completed.
Thank you.


sample.cc

Tom Henderson

unread,
Aug 18, 2015, 7:41:16 PM8/18/15
to ns-3-...@googlegroups.com
On 08/18/2015 04:14 PM, PN wrote:
> I'm trying to figure out how Simulator::Schedule() Method works. I was
> under the impression that if the following call is made:
>
> |
> Simulator::Schedule(t1,&func1);
> |
>
> then when the program execution reaches this line, it would increment
> the current value of time by t1 and make a call to func1().

The statement schedules func1 to execute after a delay t1 from the
current simulation time.

However, if
> that were true then after the following sequence of calls:
>
> |
> Simulator::Schedule(t1,&func1);
> Simulator::Schedule(t2,&func2);
> Simulator::Schedule(t3,&func3);
> |
>
> Time should be incremented by t1+t2+t3. However, it looks like the
> simulator sees t1, t2 and t3 as the actual values of time rather than
> increments (script is attached).

Not actual in the sense of absolute time, but in the sense of relative
time (delay) from the current simulation time reported by Simulator::Now().

Now assuming that the value of time
> given is an absolute value and not an increment, how does a call like
> the following work:
>
> |
> Simulator::Schedule(tEvent,&CsmaNetDevice::TransmitCompleteEvent,this);
> |
>
> where tEvent is the time it takes to complete the event? The above line
> is a part of CsmaNetDevice::TransmitStart().

TransmitCompleteEvent will execute tEvent time later than now.

>
> Also, what happens if there is this following set of calls:
>
> |
> Simulator::Schedule(t1,&func1);
> func2();
> Simulator::Schedule(t2,&func3);
> |
>
> will func2() get called before func1()? I know the answer is YES: you
> can see that through my script. I just want to know why.

Hopefully you can see now that func2() is executed now, func1() t1 from
now, and func3() t2 from now.

>
> I tried going through the Simulator::Schedule() script. However, there
> are more than one function calls involved and it was getting difficult
> for me to understand all of them. I guess that I'm facing the above
> questions due to my lack of knowledge on how ns 3 or a discrete event
> simulator works (what I know is sufficient to use and tweak/debug ns 3
> at a basic level but at an advance level, I get stuck with questions
> such as those mentioned above).
>
> If anyone can help me with this, it would be very much appreciated!
>
> Additionally, if you could point me to a document/book that helps
> understand the working of a discrete event simulator such as ns 3, that
> would be fantastic. I couldn't find a good one myself. The manual is a
> great reference, but at certain points, it looks like it is still being
> completed.

I'm not aware of any such books. You might want to look at the training
presentations or videos that the ns-3 Consortium has prepared.

https://www.nsnam.org/consortium/activities/training/

- Tom

Peshal Nayak

unread,
Aug 18, 2015, 8:52:53 PM8/18/15
to ns-3-...@googlegroups.com
Fantastic! Thank you for such a quick and informative response.

I have two additional questions:

If I want func1() to be called t1 from now and func2() to be called t2 from "then" and func3() t3 after func2() is called, how do I do it. The most obvious way would be to do the following:

Simulator::Schedule(t1, &func1);
Simulator::Schedule(t1+t2, &func2);
Simulator::Schedule(t1+t2+t3, &func3);

But is there a better way: one in which func1() is called before func2() is scheduled? It could be done by calling func2() from func1() as well, but what if they were being called from the main script or a module?
I'm asking this question as I'm faced with a similar problem while writing scripts for my research sometimes: Figuring out how function calls are being scheduled gets extremely difficult. I wonder if there is an easier way to figure it out in a complex script involving many modules.

My second question is: When executing a script, does the execution first consider all statements that don't involve Simulator::Schedule() first (because those would be the ones to be executed now) and then sequentially (in terms of time) execute the scheduled function calls? If that were true, then does the simulator increment time only when it hits the last line of the code being executed? So for instance if the code looked like the following:

Simulator::Schedule(t1, &func1);
std::cout << "bla bla bla" << std::endl;
.... (many more lines not involving Simulator::Schedule() or other scheduling methods)......

Simulator::Schedule(t2, &func2);
.... (many more lines not involving Simulator::Schedule() or other scheduling methods)......

std::cout << "The end" <<std::endl;

would time be incremented once the last line is reached. Given that it is a discrete event simulator, I believe that time is not incremented in slots but rather on the way function calls are scheduled (For instance, in the above script, time is incremented from 0 to t1 and then to t2 and does not attain any intermediate values). Is that correct?

Also, thanks very much for the sharing the videos. I'm sure they will prove to be a great resource.

 







--
You received this message because you are subscribed to a topic in the Google Groups "ns-3-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ns-3-users/COS9muQ_ufk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ns-3-users+...@googlegroups.com.
To post to this group, send email to ns-3-...@googlegroups.com.
Visit this group at http://groups.google.com/group/ns-3-users.
For more options, visit https://groups.google.com/d/optout.

Tom Henderson

unread,
Aug 19, 2015, 1:31:41 AM8/19/15
to ns-3-...@googlegroups.com
On 08/18/2015 05:52 PM, Peshal Nayak wrote:
> Fantastic! Thank you for such a quick and informative response.
>
> I have two additional questions:
>
> If I want func1() to be called t1 from now and func2() to be called t2
> from "then" and func3() t3 after func2() is called, how do I do it. The
> most obvious way would be to do the following:
>
> Simulator::Schedule(t1, &func1);
> Simulator::Schedule(t1+t2, &func2);
> Simulator::Schedule(t1+t2+t3, &func3);
>
> But is there a better way: one in which func1() is called before func2()
> is scheduled? It could be done by calling func2() from func1() as well,

Yes

> but what if they were being called from the main script or a module?

You could wrap func1 in a wrapper that both called func1 and scheduled
func2, and then schedule the wrapper instead.

> I'm asking this question as I'm faced with a similar problem while
> writing scripts for my research sometimes: Figuring out how function
> calls are being scheduled gets extremely difficult. I wonder if there is
> an easier way to figure it out in a complex script involving many modules.
>
> My second question is: When executing a script, does the execution first
> consider all statements that don't involve Simulator::Schedule() first
> (because those would be the ones to be executed now) and then
> sequentially (in terms of time) execute the scheduled function calls?

Yes.

If
> that were true, then does the simulator increment time only when it hits
> the last line of the code being executed? So for instance if the code
> looked like the following:
>
> Simulator::Schedule(t1, &func1);
> std::cout << "bla bla bla" << std::endl;
> .... (many more lines not involving Simulator::Schedule() or other
> scheduling methods)......
>
> Simulator::Schedule(t2, &func2);
> .... (many more lines not involving Simulator::Schedule() or other
> scheduling methods)......
>
> std::cout << "The end" <<std::endl;
>
> would time be incremented once the last line is reached.

Virtual time will be incremented after you call Simulator::Run() and the
simulator starts to remove the scheduled events from the event queue,
and the time will move forward in discrete jumps according to the
timestamps of the scheduled events.

Given that it
> is a discrete event simulator, I believe that time is not incremented in
> slots but rather on the way function calls are scheduled (For instance,
> in the above script, time is incremented from 0 to t1 and then to t2 and
> does not attain any intermediate values). Is that correct?

There won't be intermediate values if there are no other events
scheduled. For example, func1 might schedule something for a time in
between t1 and t2, but assuming not, then yes, virtual time will move
forward as you describe.

- Tom

pdbarnes

unread,
Aug 20, 2015, 11:33:58 AM8/20/15
to ns-3-users

> Additionally, if you could point me to a
> document/book that helps understand the
> working of a discrete event simulator such
> as ns 3, that would be fantastic.

The standard text is by Richard Fujimoto, "Parallel and Distributed Simulation Systems"

Peter

Reply all
Reply to author
Forward
0 new messages