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 
47 class Timestamp
48 {
49  public:
50  Timestamp () :
51  last(0),
52  diff(0)
53  {
54  stamp ();
55  }
56 
57  void stamp () {
58  time_t seconds = time (NULL);
59  diff = seconds - last;
60  last = seconds;
61  }
62 
63  std::string string () {
64  std::string now = ctime ( &last );
65  now.resize(now.length () - 1); // trim trailing newline
66  return now;
67  }
68 
69  time_t last;
70  time_t diff;
71 
72 }; // class Timestamp
73 
74 
78 class Hold : public SimpleRefCount<Hold>
79 {
80 public:
81 
82  Hold (Time wait, Time interval)
83  {
84  m_wait = wait;
85  m_interval = interval;
86 
87  m_rng = CreateObject<ExponentialRandomVariable> ();
88  m_rng->SetAttribute ("Mean", DoubleValue (wait.GetSeconds ()));
89  }
90 
92  : m_rng (rng)
93  {
94  }
95 
96  void Event (void)
97  {
98  double delta = m_rng->GetValue ();
99  Time delay = Seconds (delta);
100  NS_LOG_LOGIC ("event delay: " << delay);
101 
102  Simulator::Schedule (delay, &Hold::Event, this);
103 
104  // Switch work load every 10 * m_interval of simulation time
105  int64x64_t ratio = (Simulator::Now () / m_interval) / 10;
106  bool even = (ratio.GetHigh () % 2);
107  Time work = m_wait * (even ? 3 : 1);
108  m_condition.TimedWait ( work.GetNanoSeconds () );
109 
110  }
111 
112 private:
117 
118 }; // class HOLD
119 
120 } // unnamed namespace
121 
122 
123 int
124 main (int argc, char ** argv)
125 {
126  Time stop = Seconds (100);
127  Time interval = Seconds (1);
128  Time wait = MilliSeconds (10);
129  bool verbose = false;
130 
132  cmd.AddValue ("stop", "Simulation duration in virtual time.", stop);
133  cmd.AddValue ("interval", "Approximate reporting interval, in wall clock time.", interval);
134  cmd.AddValue ("wait", "Wallclock time to burn on each event.", wait);
135  cmd.AddValue ("verbose", "Turn on verbose progress message.", verbose);
136  cmd.Parse (argc, argv);
137 
138  std::cout << "\n"
139  << cmd.GetName () << ":\n"
140  << "\n"
141  << "verbose progress message: " << (verbose ? "on\n" : "off\n")
142  << "target reporting interval: " << interval.As (Time::S) << "\n"
143  << "average event sleep time: " << wait.As (Time::MS) << "\n"
144  << "total simulation run time: " << stop.As (Time::S)
145  << std::endl;
146 
147  Ptr<Hold> h = Create<Hold> (wait, interval);
148  h->Event ();
149 
150  Simulator::Stop (stop);
151  ShowProgress spinner (interval);
152  spinner.SetVerbose (verbose);
153 
154  Timestamp ts;
155  ts.stamp ();
156 
157  std::cout << std::endl;
158  std::cout << "Start wall clock: " << ts.string ()
159  << " (" << ts.last << ")"
160  << std::endl;
161 
162  Simulator::Run ();
163 
164  ts.stamp ();
165  std::cout << "Elapsed wall clock: " << ts.diff << "s" << std::endl;
166 
167 
169 
170 }
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
High precision numerical type, implementing Q64.64 fixed precision.
Definition: int64x64-128.h:45
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:355
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:204
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1070
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:388
void SetVerbose(bool verbose)
Set verbose mode to print real and virtual time intervals.
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1389
Parse command-line arguments.
Definition: command-line.h:213
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:134
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:367
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:193
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
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:1062
second
Definition: nstime.h:114
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:219