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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Gustavo Carneiro <gjc@inescporto.pt>
7 * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
8 */
9
10/**
11 * \file
12 * \ingroup core-examples
13 * \ingroup logging
14 * Example program that demonstrates ShowProgress.
15 */
16
17/**
18 * Example program that demonstrates ShowProgress.
19 *
20 */
21
22#include "ns3/core-module.h"
23
24#include <chrono>
25#include <iomanip>
26#include <string>
27#include <thread>
28
29using namespace ns3;
30
31NS_LOG_COMPONENT_DEFINE("SampleShowProgress");
32
33namespace
34{
35
36/**
37 * Execute a function periodically,
38 * which takes more or less time to run.
39 *
40 * Inspired by PHOLD.
41 */
42class Hold : public SimpleRefCount<Hold>
43{
44 public:
45 /**
46 * Create a Hold with mean inter-event time \pname{wait},
47 * changing workload every \pname{interval}.
48 * \param wait The mean inter-event time.
49 * \param interval How often to change work load. This
50 * should be an order of magnitude larger than \pname{wait}.
51 */
52 Hold(Time wait, Time interval)
53 {
54 m_wait = wait;
55 m_interval = interval;
56
58 m_rng->SetAttribute("Mean", DoubleValue(m_wait.GetSeconds()));
59 }
60
61 /**
62 * Create a hold with a specified random number generator for the
63 * \pname{wait} time. The RNG value will be interpreted as seconds.
64 * \param rng The random variable generator to use for the inter-event time.
65 */
67 : m_rng(rng)
68 {
69 }
70
71 /** The Hold event. */
72 void Event()
73 {
74 double delta = m_rng->GetValue();
75 Time delay = Seconds(delta);
76 NS_LOG_LOGIC("event delay: " << delay);
77
78 Simulator::Schedule(delay, &Hold::Event, this);
79
80 // Switch work load every 10 * m_interval of simulation time
81 int64x64_t ratio = (Simulator::Now() / m_interval) / 10;
82 bool even = (ratio.GetHigh() % 2);
83 Time work = m_wait * (even ? 3 : 1);
84 std::this_thread::sleep_for(std::chrono::nanoseconds(work.GetNanoSeconds()));
85 }
86
87 private:
88 /** The random number generator for the interval between events. */
90 /** Mean inter-event time. */
92 /** Time between switching workloads. */
94
95}; // class HOLD
96
97} // unnamed namespace
98
99int
100main(int argc, char** argv)
101{
102 Time stop = Seconds(100);
103 Time interval = Seconds(10);
104 Time wait = MilliSeconds(10);
105 bool verbose = false;
106
107 CommandLine cmd(__FILE__);
108 cmd.AddValue("stop", "Simulation duration in virtual time.", stop);
109 cmd.AddValue("interval", "Approximate reporting interval, in wall clock time.", interval);
110 cmd.AddValue("wait", "Wallclock time to burn on each event.", wait);
111 cmd.AddValue("verbose", "Turn on verbose progress message.", verbose);
112 cmd.Parse(argc, argv);
113
114 std::cout << "\n"
115 << cmd.GetName() << ":\n"
116 << "\n"
117 << "verbose progress message: " << (verbose ? "on\n" : "off\n")
118 << "target reporting interval: " << interval.As(Time::S) << "\n"
119 << "average event sleep time: " << wait.As(Time::MS) << "\n"
120 << "total simulation run time: " << stop.As(Time::S) << std::endl;
121
122 Ptr<Hold> h = Create<Hold>(wait, interval);
123 h->Event();
124
125 Simulator::Stop(stop);
126 ShowProgress spinner(interval);
127 spinner.SetVerbose(verbose);
128
131
132 return 0;
133}
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.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
Smart pointer class similar to boost::intrusive_ptr.
Periodically print a status message indicating simulator progress.
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:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:407
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:404
@ MS
millisecond
Definition nstime.h:106
@ S
second
Definition nstime.h:105
High precision numerical type, implementing Q64.64 fixed precision.
int64_t GetHigh() const
Get the integer portion.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool verbose