A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
sample-show-progress.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 Lawrence Livermore National Laboratory
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Gustavo Carneiro <gjc@inescporto.pt>
18 * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
19 */
20
21/**
22 * \file
23 * \ingroup core-examples
24 * \ingroup logging
25 * Example program that demonstrates ShowProgress.
26 */
27
28/**
29 * Example program that demonstrates ShowProgress.
30 *
31 */
32
33#include "ns3/core-module.h"
34
35#include <chrono>
36#include <iomanip>
37#include <string>
38#include <thread>
39
40using namespace ns3;
41
42NS_LOG_COMPONENT_DEFINE("SampleShowProgress");
43
44namespace
45{
46
47/**
48 * Execute a function periodically,
49 * which takes more or less time to run.
50 *
51 * Inspired by PHOLD.
52 */
53class Hold : public SimpleRefCount<Hold>
54{
55 public:
56 /**
57 * Create a Hold with mean inter-event time \pname{wait},
58 * changing workload every \pname{interval}.
59 * \param wait The mean inter-event time.
60 * \param interval How often to change work load. This
61 * should be an order of magnitude larger than \pname{wait}.
62 */
63 Hold(Time wait, Time interval)
64 {
65 m_wait = wait;
66 m_interval = interval;
67
68 m_rng = CreateObject<ExponentialRandomVariable>();
69 m_rng->SetAttribute("Mean", DoubleValue(m_wait.GetSeconds()));
70 }
71
72 /**
73 * Create a hold with a specified random number generator for the
74 * \pname{wait} time. The RNG value will be interpreted as seconds.
75 * \param rng The random variable generator to use for the inter-event time.
76 */
78 : m_rng(rng)
79 {
80 }
81
82 /** The Hold event. */
83 void Event()
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 std::this_thread::sleep_for(std::chrono::nanoseconds(work.GetNanoSeconds()));
96 }
97
98 private:
99 /** The random number generator for the interval between events. */
101 /** Mean inter-event time. */
103 /** Time between switching workloads. */
105
106}; // class HOLD
107
108} // unnamed namespace
109
110int
111main(int argc, char** argv)
112{
113 Time stop = Seconds(100);
114 Time interval = Seconds(10);
115 Time wait = MilliSeconds(10);
116 bool verbose = false;
117
118 CommandLine cmd(__FILE__);
119 cmd.AddValue("stop", "Simulation duration in virtual time.", stop);
120 cmd.AddValue("interval", "Approximate reporting interval, in wall clock time.", interval);
121 cmd.AddValue("wait", "Wallclock time to burn on each event.", wait);
122 cmd.AddValue("verbose", "Turn on verbose progress message.", verbose);
123 cmd.Parse(argc, argv);
124
125 std::cout << "\n"
126 << cmd.GetName() << ":\n"
127 << "\n"
128 << "verbose progress message: " << (verbose ? "on\n" : "off\n")
129 << "target reporting interval: " << interval.As(Time::S) << "\n"
130 << "average event sleep time: " << wait.As(Time::MS) << "\n"
131 << "total simulation run time: " << stop.As(Time::S) << std::endl;
132
133 Ptr<Hold> h = Create<Hold>(wait, interval);
134 h->Event();
135
136 Simulator::Stop(stop);
137 ShowProgress spinner(interval);
138 spinner.SetVerbose(verbose);
139
142
143 return 0;
144}
Execute a function periodically, which takes more or less time to run.
Time m_interval
Time between switching workloads.
Ptr< RandomVariableStream > m_rng
The random number generator for the interval between events.
Hold(Ptr< RandomVariableStream > rng)
Create a hold with a specified random number generator for the wait time.
Hold(Time wait, Time interval)
Create a Hold with mean inter-event time wait, changing workload every interval.
Parse command-line arguments.
Definition: command-line.h:232
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Periodically print a status message indicating simulator progress.
Definition: show-progress.h:94
void SetVerbose(bool verbose)
Set verbose mode to print real and virtual time intervals.
A template-based reference counting class.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:418
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:415
@ MS
millisecond
Definition: nstime.h:117
@ S
second
Definition: nstime.h:116
High precision numerical type, implementing Q64.64 fixed precision.
Definition: int64x64-128.h:56
int64_t GetHigh() const
Get the integer portion.
Definition: int64x64-128.h:257
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1331
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool verbose