Schedule a function to run every x seconds during simulation

319 views
Skip to first unread message

SMalta

unread,
Apr 24, 2021, 6:58:29 AM4/24/21
to ns-3-users
Hello,

I want to run a specific function every X second during simulation time.

I have coded the below example, it works, but I would like to be advised if this is the best approach to do it? 

What are your opinions guys?

BR
smalta

***********************************************************

NS_LOG_COMPONENT_DEFINE ("Schedule");

 static void
 someEvent (Time interval)
 {
   NS_LOG_INFO ("event a time :" << Simulator::Now ().GetSeconds () );

   Simulator::Schedule (interval ,&someEvent, interval);
 }
 

int main(int argc, char *argv[])
{

  bool verbose = false;
   //Simulation Time In Seconds
  double SimTime = 10;
  uint32_t interval = 1;
  Time eventInterval = Seconds(interval);

  CommandLine cmd (__FILE__);
  cmd.AddValue("SimTime", "Simulation Time in Seconds", SimTime);
  cmd.AddValue("Interval", "Tell Function to run every X second", interval);
  cmd.AddValue("verbose", "Tell aplication to log if true", verbose);
 
  cmd.Parse (argc,argv);

  Time::SetResolution (Time::NS);
 
  if (verbose)
    {
      LogComponentEnableAll (LOG_PREFIX_TIME);
      LogComponentEnable ("Schedule", LOG_INFO);
    }

  eventInterval = Seconds(interval);
  Simulator::Schedule(Seconds(interval), &someEvent, eventInterval);

  Simulator::Stop (Seconds(SimTime));
  std::cout << "Simulation Time :" << SimTime << std::endl;
  Simulator::Run ();
 
  Simulator::Destroy ();
  return 0;
}

Tom Henderson

unread,
Apr 24, 2021, 2:07:14 PM4/24/21
to ns-3-...@googlegroups.com, SMalta
On 4/24/21 3:58 AM, SMalta wrote:
Hello,

I want to run a specific function every X second during simulation time.

I have coded the below example, it works, but I would like to be advised if this is the best approach to do it? 

What are your opinions guys?
This will work but there are possibly a few improvements (see inline below).


BR
smalta

***********************************************************

NS_LOG_COMPONENT_DEFINE ("Schedule");

 static void
 someEvent (Time interval)
 {
   NS_LOG_INFO ("event a time :" << Simulator::Now ().GetSeconds () );

   Simulator::Schedule (interval ,&someEvent, interval);
 }
 

int main(int argc, char *argv[])
{

  bool verbose = false;
   //Simulation Time In Seconds
  double SimTime = 10;

you can also declare this as:

  Time SimTime = Seconds (10);

There is no problem with using ns3::Time objects at the command line; in fact, there are some possible benefits because it naturally handles units, such as:

--SimTime=60s

or

--SimTime=1min

In general, we recommend to store time values in ns3::Time units where possible (and do arithmetic on them without converting back to basic types, if possible).

  uint32_t interval = 1;

declare this as:

  Time interval = Seconds (1);

  Time eventInterval = Seconds(interval);
There is no need for this variable if you declare 'interval' as a ns3::Time.


  CommandLine cmd (__FILE__);
  cmd.AddValue("SimTime", "Simulation Time in Seconds", SimTime);
  cmd.AddValue("Interval", "Tell Function to run every X second", interval);
  cmd.AddValue("verbose", "Tell aplication to log if true", verbose);
 
  cmd.Parse (argc,argv);

  Time::SetResolution (Time::NS);
The above is not strictly needed, as this is the default resolution.

 
  if (verbose)
    {
      LogComponentEnableAll (LOG_PREFIX_TIME);
      LogComponentEnable ("Schedule", LOG_INFO);
    }

  eventInterval = Seconds(interval);
  Simulator::Schedule(Seconds(interval), &someEvent, eventInterval);

The above two statements can then be compressed into one:

  Simulator::Schedule (interval, &someEvent, interval);


  Simulator::Stop (Seconds(SimTime));

This would become:

Simulator::Stop (SimTime);

  std::cout << "Simulation Time :" << SimTime << std::endl;
  Simulator::Run ();
 
  Simulator::Destroy ();
  return 0;
}
--
Posting to this group should follow these guidelines https://www.nsnam.org/wiki/Ns-3-users-guidelines-for-posting
---
You received this message because you are subscribed to the Google Groups "ns-3-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ns-3-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ns-3-users/d8b02862-96cb-492b-a94c-3b47ce08e242n%40googlegroups.com.


SMalta

unread,
Apr 24, 2021, 2:42:20 PM4/24/21
to ns-3-users
Ok Tom,

Many thanks for the advices.
I will follow them.

BR
Smalta
Reply all
Reply to author
Forward
0 new messages