A Discrete-Event Network Simulator
API
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 
28 #include <iomanip>
29 
30 #include "event-id.h"
31 #include "log.h"
32 #include "nstime.h"
33 #include "simulator.h"
34 #include "singleton.h"
35 
36 #include "show-progress.h"
37 
38 namespace ns3 {
39 
40 NS_LOG_COMPONENT_DEFINE ("ShowProgress");
41 
42 ShowProgress::ShowProgress (const Time interval /* = Seconds (1.0) */,
43  std::ostream & os /* = std::cout */)
44  : m_timer (),
45  m_interval (interval),
46  m_vtime (Seconds (1.0)),
47  m_event (),
48  m_eventCount (0),
49  m_printer (DefaultTimePrinter),
50  m_os (&os),
51  m_verbose (false),
52  m_repCount (0)
53 {
54  NS_LOG_FUNCTION (this << interval);
55  Start ();
56 }
57 
58 void
60 {
61  NS_LOG_FUNCTION (this << interval);
62  const int64x64_t ratio = interval / m_interval;
63  m_interval = interval;
64  m_vtime = m_vtime * ratio;
66  Start ();
67 
68 } // ShowProgress::SetInterval
69 
70 void
72 {
73  NS_LOG_FUNCTION (this << lp);
74  m_printer = lp;
75 }
76 
77 void
79 {
80  NS_LOG_FUNCTION (this << verbose);
82 }
83 
84 void
85 ShowProgress::SetStream (std::ostream & os)
86 {
87  m_os = &os;
88 }
89 
90 void
92 {
93  NS_LOG_FUNCTION (this);
95  m_timer.Start ();
96 
97 } // ShowProgress::Start
98 
99 void
101 {
102  // Get elapsed wall clock time
103  Time elapsed = MilliSeconds (m_timer.End ());
104  NS_LOG_FUNCTION (this << elapsed);
105 
106  // Don't do anything unless the elapsed time is positive.
107  if (elapsed <= Time (0))
108  {
109  m_vtime = m_vtime * MAXGAIN;
110  Start ();
111  return;
112  }
113 
114  // Speed: how fast are we compared to real time
115  const int64x64_t speed = m_vtime / elapsed;
116 
117  // Ratio: how much real time did we use,
118  // compared to reporting interval target
119  const int64x64_t ratio = elapsed / m_interval;
120 
121  // Elapsed event count
122  uint64_t events = Simulator::GetEventCount ();
123  uint64_t nEvents = events - m_eventCount;
124  m_eventCount = events;
125 
176  if (ratio > HYSTERESIS)
177  {
178  m_vtime = m_vtime / std::min (ratio, MAXGAIN);
179  }
180  else if (ratio < 1.0 / HYSTERESIS)
181  {
182  m_vtime = m_vtime / std::max (ratio, 1.0 / MAXGAIN);
183  }
184 
185  // Save stream state
186  auto precision = m_os->precision ();
187  auto flags = m_os->flags ();
188 
189  m_os->setf (std::ios::fixed, std:: ios::floatfield);
190 
191  if (m_verbose)
192  {
193  (*m_os) << m_repCount
194  << " [del: " << elapsed.As(Time::S)
195  << "/ int: " << m_interval.As(Time::S)
196  << " = rat: " << ratio
197  << (ratio > HYSTERESIS ? " up" :
198  (ratio < 1.0 / HYSTERESIS ? " dn" : " --"))
199  << ", vt: " << m_vtime.As(Time::S) << "] ";
200  }
201  m_repCount++;
202 
203  // Print the current time
204  (*m_printer)(*m_os);
205 
206  (*m_os) << " ("
207  << std::setprecision (3) << std::setw (8) << speed.GetDouble () << "x real time) "
208  << nEvents << " events processed"
209  << std::endl
210  << std::flush;
211 
212  // Restore stream state
213  m_os->precision (precision);
214  m_os->flags (flags);
215 
216  // And do it again
217  Start ();
218 
219 } // ShowProgress::Feedback
220 
221 
222 } // namespace ns3
bool m_verbose
Verbose mode flag.
EventId m_event
The next progress event.
void DefaultTimePrinter(std::ostream &os)
Default Time printer.
Definition: time-printer.cc:39
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
ShowProgress(const Time interval=Seconds(1.0), std::ostream &os=std::cout)
Constructor.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
ns3::Singleton declaration and template implementation.
ns3::ShowProgress declaration.
#define min(a, b)
Definition: 80211b.c:42
SystemWallClockMs m_timer
Wallclock timer.
High precision numerical type, implementing Q64.64 fixed precision.
Definition: int64x64-128.h:45
Time m_interval
The target update interval, in wallclock time.
#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
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event&#39;s associated function will not be invoked when it expires...
Definition: simulator.cc:290
ns3::Simulator declaration.
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.
void SetStream(std::ostream &os)
Set the output stream to show progress on.
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1389
#define max(a, b)
Definition: 80211b.c:43
std::ostream * m_os
The output stream to use.
const int64x64_t MAXGAIN
Maximum growth factor.
const int64x64_t HYSTERESIS
Hysteresis factor.
void Start(void)
Start a measure.
void SetInterval(const Time interval)
Set the target update interval, in wallclock time.
Declaration of classes ns3::Time and ns3::TimeWithUnit, and the TimeValue implementation classes...
uint64_t m_eventCount
Simulator event count.
void(* TimePrinter)(std::ostream &os)
Function signature for features requiring a time formatter, such as logging or ShowProgress.
Definition: time-printer.h:43
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void Start(void)
Start the progress timer.
void SetTimePrinter(TimePrinter lp)
Set the TimePrinter function to be used to prepend progress messages with the simulation time...
static uint64_t GetEventCount(void)
Get the number of events executed.
Definition: simulator.cc:328
TimePrinter m_printer
The TimePrinter to use.
Time m_vtime
The virtual time interval.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1062
double GetDouble(void) const
Get this value as a double.
Definition: int64x64-128.h:203
second
Definition: nstime.h:114
void Feedback(void)
Show execution progress.
uint64_t m_repCount
Count of progress lines printed.
Debug message logging.
int64_t End(void)
Stop measuring the time since Start() was called.
ns3::EventId declarations.
bool verbose