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
41using namespace ns3;
42
43NS_LOG_COMPONENT_DEFINE ("SampleShowProgress");
44
45namespace {
46
53class Hold : public SimpleRefCount<Hold>
54{
55public:
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
99private:
108
109}; // class HOLD
110
111} // unnamed namespace
112
113
114int
115main (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 ();
146 Simulator::Destroy ();
147
148}
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.
SystemCondition m_condition
Timer to represent workload.
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:229
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
Periodically print a status message indicating simulator progress.
Definition: show-progress.h:96
void SetVerbose(bool verbose)
Set verbose mode to print real and virtual time intervals.
A template-based reference counting class.
A class which provides a relatively platform-independent conditional-wait thread synchronization prim...
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
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:432
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:391
High precision numerical type, implementing Q64.64 fixed precision.
int64_t GetHigh(void) const
Get the integer portion.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1252
Every class exported by the ns3 library is enclosed in the ns3 namespace.
rng
Random number generator.
cmd
Definition: second.py:35
bool verbose