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 <string>
26#include <thread>
27
28using namespace ns3;
29
30NS_LOG_COMPONENT_DEFINE("SampleShowProgress");
31
32namespace
33{
34
35/**
36 * Execute a function periodically,
37 * which takes more or less time to run.
38 *
39 * Inspired by PHOLD.
40 */
41class Hold : public SimpleRefCount<Hold>
42{
43 public:
44 /**
45 * Create a Hold with mean inter-event time \pname{wait},
46 * changing workload every \pname{interval}.
47 * @param wait The mean inter-event time.
48 * @param interval How often to change work load. This
49 * should be an order of magnitude larger than \pname{wait}.
50 */
51 Hold(Time wait, Time interval)
52 {
53 m_wait = wait;
54 m_interval = interval;
55
57 m_rng->SetAttribute("Mean", DoubleValue(m_wait.GetSeconds()));
58 }
59
60 /**
61 * Create a hold with a specified random number generator for the
62 * \pname{wait} time. The RNG value will be interpreted as seconds.
63 * @param rng The random variable generator to use for the inter-event time.
64 */
66 : m_rng(rng)
67 {
68 }
69
70 /** The Hold event. */
71 void Event()
72 {
73 double delta = m_rng->GetValue();
74 Time delay = Seconds(delta);
75 NS_LOG_LOGIC("event delay: " << delay);
76
77 Simulator::Schedule(delay, &Hold::Event, this);
78
79 // Switch work load every 10 * m_interval of simulation time
80 int64x64_t ratio = (Simulator::Now() / m_interval) / 10;
81 bool even = (ratio.GetHigh() % 2);
82 Time work = m_wait * (even ? 3 : 1);
83 std::this_thread::sleep_for(std::chrono::nanoseconds(work.GetNanoSeconds()));
84 }
85
86 private:
87 /** The random number generator for the interval between events. */
89 /** Mean inter-event time. */
91 /** Time between switching workloads. */
93
94 // end of class HOLD
95};
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}
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.
Definition ptr.h:70
Periodically print a status message indicating simulator progress.
void SetVerbose(bool verbose)
Set verbose mode to print real and virtual time intervals.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:580
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:191
static void Run()
Run the simulation.
Definition simulator.cc:161
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:169
Simulation virtual time values and global simulation resolution.
Definition nstime.h:96
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:414
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:408
@ MS
millisecond
Definition nstime.h:108
@ S
second
Definition nstime.h:107
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:194
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:274
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:454
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1381
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1398
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool verbose