A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
gnuplot-helper-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013 University of Washington
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * This is based on double-probe-example.cc.
7 *
8 * Author: Mitch Watrous (watrous@u.washington.edu)
9 */
10
11/*
12 * This example is designed to show the main features of an
13 * ns3::GnuplotHelper.
14 */
15
16#include "ns3/core-module.h"
17#include "ns3/stats-module.h"
18
19#include <string>
20
21using namespace ns3;
22
23NS_LOG_COMPONENT_DEFINE("GnuplotHelperExample");
24
25/**
26 * This is our test object, an object that increments a counter according
27 * to a Poisson process, and exports the (integer-valued) count as a
28 * trace source.
29 */
30class Emitter : public Object
31{
32 public:
33 /**
34 * Register this type.
35 * \return The TypeId.
36 */
37 static TypeId GetTypeId();
39
40 private:
41 void DoInitialize() override;
42 /// Counts how many time this function is called.
43 void Count();
44
45 TracedValue<uint32_t> m_counter; //!< Simple counter
46 Ptr<ExponentialRandomVariable> m_var; //!< Random number generator.
47};
48
50
53{
54 static TypeId tid = TypeId("ns3::Emitter")
56 .SetGroupName("Stats")
57 .AddConstructor<Emitter>()
58 .AddTraceSource("Counter",
59 "sample counter",
61 "ns3::TracedValueCallback::Double");
62 return tid;
63}
64
66{
67 NS_LOG_FUNCTION(this);
68 m_counter = 0;
70}
71
72void
74{
75 NS_LOG_FUNCTION(this);
76 Simulator::Schedule(Seconds(m_var->GetValue()), &Emitter::Count, this);
77}
78
79void
81{
82 NS_LOG_FUNCTION(this);
83 NS_LOG_DEBUG("Counting at " << Simulator::Now().As(Time::S));
84 m_counter += 1.0;
85 Simulator::Schedule(Seconds(m_var->GetValue()), &Emitter::Count, this);
86}
87
88int
89main(int argc, char* argv[])
90{
91 CommandLine cmd(__FILE__);
92 cmd.Parse(argc, argv);
93
94 //
95 // This Emitter has a trace source object that will emit values at
96 // random times.
97 //
98
100 Names::Add("/Names/Emitter", emitter);
101
102 //
103 // This gnuplot helper will be used to produce output used to make
104 // gnuplot plots.
105 //
106
107 // Create the gnuplot helper.
108 GnuplotHelper plotHelper;
109
110 // Configure the plot. Arguments include file prefix, plot title,
111 // x-label, y-label, and output file type
112 plotHelper.ConfigurePlot("gnuplot-helper-example",
113 "Emitter Count vs. Time",
114 "Time (Seconds)",
115 "Emitter Count",
116 "png");
117
118 // Create a probe. Because the trace source we are interested in is
119 // of type uint32_t, we specify the type of probe to use by the first
120 // argument specifying its ns3 TypeId.
121 plotHelper.PlotProbe("ns3::Uinteger32Probe",
122 "/Names/Emitter/Counter",
123 "Output",
124 "Emitter Count",
126
127 // The Emitter object is not associated with an ns-3 node, so
128 // it won't get started automatically, so we need to do this ourselves
130
131 Simulator::Stop(Seconds(100.0));
134
135 return 0;
136}
This is our test object, an object that increments counters at various times and emits one of them as...
void DoInitialize() override
Initialize() implementation.
static TypeId GetTypeId()
Register this type.
TracedValue< double > m_counter
Sample counter, normally this would be integer type.
static TypeId GetTypeId()
Register this type.
void Count()
Counts how many time this function is called.
Ptr< ExponentialRandomVariable > m_var
Random number generator.
Parse command-line arguments.
Helper class used to make gnuplot plots.
void ConfigurePlot(const std::string &outputFileNameWithoutExtension, const std::string &title, const std::string &xLegend, const std::string &yLegend, const std::string &terminalType="png")
void PlotProbe(const std::string &typeId, const std::string &path, const std::string &probeTraceSource, const std::string &title, GnuplotAggregator::KeyLocation keyLocation=GnuplotAggregator::KEY_INSIDE)
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition names.cc:764
A base class which provides memory management and object aggregation.
Definition object.h:78
void Initialize()
Invoke DoInitialize on all Objects aggregated to this one.
Definition object.cc:203
Smart pointer class similar to boost::intrusive_ptr.
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
@ S
second
Definition nstime.h:105
Trace classes with value semantics.
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.