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 
50 class Timestamp
51 {
52 public:
54  Timestamp () :
55  last (0),
56  diff (0)
57  {
58  stamp ();
59  }
60 
62  void stamp ()
63  {
64  time_t seconds = time (NULL);
65  diff = seconds - last;
66  last = seconds;
67  }
68 
73  std::string string ()
74  {
75  std::string now = ctime ( &last );
76  now.resize (now.length () - 1); // trim trailing newline
77  return now;
78  }
79 
80  time_t last;
81  time_t diff;
83 }; // class Timestamp
84 
85 
92 class Hold : public SimpleRefCount<Hold>
93 {
94 public:
95 
103  Hold (Time wait, Time interval)
104  {
105  m_wait = wait;
106  m_interval = interval;
107 
108  m_rng = CreateObject<ExponentialRandomVariable> ();
109  m_rng->SetAttribute ("Mean", DoubleValue (m_wait.GetSeconds ()));
110  }
111 
118  : m_rng (rng)
119  {}
120 
122  void Event (void)
123  {
124  double delta = m_rng->GetValue ();
125  Time delay = Seconds (delta);
126  NS_LOG_LOGIC ("event delay: " << delay);
127 
128  Simulator::Schedule (delay, &Hold::Event, this);
129 
130  // Switch work load every 10 * m_interval of simulation time
131  int64x64_t ratio = (Simulator::Now () / m_interval) / 10;
132  bool even = (ratio.GetHigh () % 2);
133  Time work = m_wait * (even ? 3 : 1);
134  m_condition.TimedWait ( work.GetNanoSeconds () );
135 
136  }
137 
138 private:
147 
148 }; // class HOLD
149 
150 } // unnamed namespace
151 
152 
153 int
154 main (int argc, char ** argv)
155 {
156  Time stop = Seconds (100);
157  Time interval = Seconds (10);
158  Time wait = MilliSeconds (10);
159  bool verbose = false;
160 
162  cmd.AddValue ("stop", "Simulation duration in virtual time.", stop);
163  cmd.AddValue ("interval", "Approximate reporting interval, in wall clock time.", interval);
164  cmd.AddValue ("wait", "Wallclock time to burn on each event.", wait);
165  cmd.AddValue ("verbose", "Turn on verbose progress message.", verbose);
166  cmd.Parse (argc, argv);
167 
168  std::cout << "\n"
169  << cmd.GetName () << ":\n"
170  << "\n"
171  << "verbose progress message: " << (verbose ? "on\n" : "off\n")
172  << "target reporting interval: " << interval.As (Time::S) << "\n"
173  << "average event sleep time: " << wait.As (Time::MS) << "\n"
174  << "total simulation run time: " << stop.As (Time::S)
175  << std::endl;
176 
177  Ptr<Hold> h = Create<Hold> (wait, interval);
178  h->Event ();
179 
180  Simulator::Stop (stop);
181  ShowProgress spinner (interval);
182  spinner.SetVerbose (verbose);
183 
184  Timestamp ts;
185  ts.stamp ();
186 
187  std::cout << std::endl;
188  std::cout << "Start wall clock: " << ts.string ()
189  << " (" << ts.last << ")"
190  << std::endl;
191 
192  Simulator::Run ();
193 
194  ts.stamp ();
195  std::cout << "Elapsed wall clock: " << ts.diff << "s" << std::endl;
196 
197 
199 
200 }
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Periodically print a status message indicating simulator progress.
Definition: show-progress.h:69
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:170
#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:1078
cmd
Definition: second.py:35
Utility class to record the difference between to wall-clock times.
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.
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1390
Parse command-line arguments.
Definition: command-line.h:212
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:134
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:373
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:193
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
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:178
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1070
std::string string()
Get the last time stamp as a string.
void stamp()
Record the current wall-clock time and delta since the last stamp().
second
Definition: nstime.h:114
time_t diff
Difference between the two previous time stamps.
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:115
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