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 <ctime>
37 #include <iomanip>
38 #include <string>
39 
40 
41 using namespace ns3;
42 
43 NS_LOG_COMPONENT_DEFINE ("SampleShowProgress");
44 
45 namespace {
46 
53 class Hold : public SimpleRefCount<Hold>
54 {
55 public:
56 
64  Hold (Time wait, Time interval)
65  {
66  m_wait = wait;
67  m_interval = interval;
68 
69  m_rng = CreateObject<ExponentialRandomVariable> ();
70  m_rng->SetAttribute ("Mean", DoubleValue (m_wait.GetSeconds ()));
71  }
72 
79  : m_rng (rng)
80  {}
81 
83  void Event (void)
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  m_condition.TimedWait ( work.GetNanoSeconds () );
96 
97  }
98 
99 private:
108 
109 }; // class HOLD
110 
111 } // unnamed namespace
112 
113 
114 int
115 main (int argc, char ** argv)
116 {
117  Time stop = Seconds (100);
118  Time interval = Seconds (10);
119  Time wait = MilliSeconds (10);
120  bool verbose = false;
121 
122  CommandLine cmd (__FILE__);
123  cmd.AddValue ("stop", "Simulation duration in virtual time.", stop);
124  cmd.AddValue ("interval", "Approximate reporting interval, in wall clock time.", interval);
125  cmd.AddValue ("wait", "Wallclock time to burn on each event.", wait);
126  cmd.AddValue ("verbose", "Turn on verbose progress message.", verbose);
127  cmd.Parse (argc, argv);
128 
129  std::cout << "\n"
130  << cmd.GetName () << ":\n"
131  << "\n"
132  << "verbose progress message: " << (verbose ? "on\n" : "off\n")
133  << "target reporting interval: " << interval.As (Time::S) << "\n"
134  << "average event sleep time: " << wait.As (Time::MS) << "\n"
135  << "total simulation run time: " << stop.As (Time::S)
136  << std::endl;
137 
138  Ptr<Hold> h = Create<Hold> (wait, interval);
139  h->Event ();
140 
141  Simulator::Stop (stop);
142  ShowProgress spinner (interval);
143  spinner.SetVerbose (verbose);
144 
145  Simulator::Run ();
147 
148 }
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:557
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
Periodically print a status message indicating simulator progress.
Definition: show-progress.h:87
Hold(Time wait, Time interval)
Create a Hold with mean inter-event time wait, changing workload every interval.
High precision numerical type, implementing Q64.64 fixed precision.
Definition: int64x64-128.h:45
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1286
cmd
Definition: second.py:35
A class which provides a relatively platform-independent conditional-wait thread synchronization prim...
TimeWithUnit As(const enum Unit unit) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:389
void SetVerbose(bool verbose)
Set verbose mode to print real and virtual time intervals.
Hold(Ptr< RandomVariableStream > rng)
Create a hold with a specified random number generator for the wait time.
Parse command-line arguments.
Definition: command-line.h:226
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
SystemCondition m_condition
Timer to represent workload.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:391
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
Time m_interval
Time between switching workloads.
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1278
second
Definition: nstime.h:115
Ptr< RandomVariableStream > m_rng
The random number generator for the interval between events.
Execute a function periodically, which takes more or less time to run.
millisecond
Definition: nstime.h:116
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
A template-based reference counting class.
bool verbose
int64_t GetHigh(void) const
Get the integer portion.
Definition: int64x64-128.h:222