A Discrete-Event Network Simulator
API
sample-show-progress.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Lawrence Livermore National Laboratory
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Gustavo Carneiro <gjc@inescporto.pt>
18  * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
19  */
20 
33 #include "ns3/core-module.h"
34 
35 #include <chrono>
36 #include <iomanip>
37 #include <string>
38 #include <thread>
39 
40 using namespace ns3;
41 
42 NS_LOG_COMPONENT_DEFINE("SampleShowProgress");
43 
44 namespace
45 {
46 
53 class Hold : public SimpleRefCount<Hold>
54 {
55  public:
63  Hold(Time wait, Time interval)
64  {
65  m_wait = wait;
66  m_interval = interval;
67 
68  m_rng = CreateObject<ExponentialRandomVariable>();
69  m_rng->SetAttribute("Mean", DoubleValue(m_wait.GetSeconds()));
70  }
71 
78  : m_rng(rng)
79  {
80  }
81 
83  void Event()
84  {
85  double delta = m_rng->GetValue();
86  Time delay = Seconds(delta);
87  NS_LOG_LOGIC("event delay: " << delay);
88 
89  Simulator::Schedule(delay, &Hold::Event, this);
90 
91  // Switch work load every 10 * m_interval of simulation time
92  int64x64_t ratio = (Simulator::Now() / m_interval) / 10;
93  bool even = (ratio.GetHigh() % 2);
94  Time work = m_wait * (even ? 3 : 1);
95  std::this_thread::sleep_for(std::chrono::nanoseconds(work.GetNanoSeconds()));
96  }
97 
98  private:
105 
106 }; // class HOLD
107 
108 } // unnamed namespace
109 
110 int
111 main(int argc, char** argv)
112 {
113  Time stop = Seconds(100);
114  Time interval = Seconds(10);
115  Time wait = MilliSeconds(10);
116  bool verbose = false;
117 
118  CommandLine cmd(__FILE__);
119  cmd.AddValue("stop", "Simulation duration in virtual time.", stop);
120  cmd.AddValue("interval", "Approximate reporting interval, in wall clock time.", interval);
121  cmd.AddValue("wait", "Wallclock time to burn on each event.", wait);
122  cmd.AddValue("verbose", "Turn on verbose progress message.", verbose);
123  cmd.Parse(argc, argv);
124 
125  std::cout << "\n"
126  << cmd.GetName() << ":\n"
127  << "\n"
128  << "verbose progress message: " << (verbose ? "on\n" : "off\n")
129  << "target reporting interval: " << interval.As(Time::S) << "\n"
130  << "average event sleep time: " << wait.As(Time::MS) << "\n"
131  << "total simulation run time: " << stop.As(Time::S) << std::endl;
132 
133  Ptr<Hold> h = Create<Hold>(wait, interval);
134  h->Event();
135 
137  ShowProgress spinner(interval);
138  spinner.SetVerbose(verbose);
139 
140  Simulator::Run();
142 
143  return 0;
144 }
Execute a function periodically, which takes more or less time to run.
Time m_interval
Time between switching workloads.
Ptr< RandomVariableStream > m_rng
The random number generator for the interval between events.
Hold(Ptr< RandomVariableStream > rng)
Create a hold with a specified random number generator for the wait time.
Hold(Time wait, Time interval)
Create a Hold with mean inter-event time wait, changing workload every interval.
Parse command-line arguments.
Definition: command-line.h:232
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
Periodically print a status message indicating simulator progress.
Definition: show-progress.h:94
void SetVerbose(bool verbose)
Set verbose mode to print real and virtual time intervals.
A template-based reference counting class.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:418
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:415
@ MS
millisecond
Definition: nstime.h:117
@ S
second
Definition: nstime.h:116
High precision numerical type, implementing Q64.64 fixed precision.
int64_t GetHigh() const
Get the integer portion.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:40
bool verbose