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