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 }
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
ns3::Simulator::Now
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
anonymous_namespace{sample-show-progress.cc}::Hold::m_wait
Time m_wait
Mean inter-event time.
Definition: sample-show-progress.cc:105
ns3::SimpleRefCount
A template-based reference counting class.
Definition: simple-ref-count.h:74
anonymous_namespace{sample-show-progress.cc}::Hold::Hold
Hold(Time wait, Time interval)
Create a Hold with mean inter-event time wait, changing workload every interval.
Definition: sample-show-progress.cc:64
ns3::SystemCondition
A class which provides a relatively platform-independent conditional-wait thread synchronization prim...
Definition: system-condition.h:71
ns3::Time::As
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:429
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
ns3::int64x64_t
High precision numerical type, implementing Q64.64 fixed precision.
Definition: int64x64-128.h:46
ns3::DoubleValue
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
ns3::ShowProgress::SetVerbose
void SetVerbose(bool verbose)
Set verbose mode to print real and virtual time intervals.
Definition: show-progress.cc:97
ns3::Ptr< RandomVariableStream >
anonymous_namespace{sample-show-progress.cc}::Hold::Hold
Hold(Ptr< RandomVariableStream > rng)
Create a hold with a specified random number generator for the wait time.
Definition: sample-show-progress.cc:78
ns3::Simulator::Stop
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
sample-rng-plot.rng
rng
Definition: sample-rng-plot.py:31
ns3::MilliSeconds
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1297
verbose
bool verbose
Definition: openflow-switch.cc:50
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
second.cmd
cmd
Definition: second.py:35
ns3::Time::GetNanoSeconds
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:392
anonymous_namespace{sample-show-progress.cc}::Hold::m_interval
Time m_interval
Time between switching workloads.
Definition: sample-show-progress.cc:107
anonymous_namespace{sample-show-progress.cc}::Hold::m_rng
Ptr< RandomVariableStream > m_rng
The random number generator for the interval between events.
Definition: sample-show-progress.cc:101
NS_LOG_LOGIC
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
anonymous_namespace{sample-show-progress.cc}::Hold
Execute a function periodically, which takes more or less time to run.
Definition: sample-show-progress.cc:54
ns3::int64x64_t::GetHigh
int64_t GetHigh(void) const
Get the integer portion.
Definition: int64x64-128.h:222
anonymous_namespace{sample-show-progress.cc}::Hold::m_condition
SystemCondition m_condition
Timer to represent workload.
Definition: sample-show-progress.cc:103
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Time::S
@ S
second
Definition: nstime.h:115
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
anonymous_namespace{sample-show-progress.cc}::Hold::Event
void Event(void)
The Hold event.
Definition: sample-show-progress.cc:83
ns3::ShowProgress
Periodically print a status message indicating simulator progress.
Definition: show-progress.h:96
ns3::Time::MS
@ MS
millisecond
Definition: nstime.h:116