A Discrete-Event Network Simulator
API
gnuplot-helper-example.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013 University of Washington
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  * This is based on double-probe-example.cc.
19  *
20  * Author: Mitch Watrous (watrous@u.washington.edu)
21  */
22 
23 /*
24  * This example is designed to show the main features of an
25  * ns3::GnuplotHelper.
26  */
27 
28 #include <string>
29 
30 #include "ns3/core-module.h"
31 #include "ns3/stats-module.h"
32 
33 using namespace ns3;
34 
35 NS_LOG_COMPONENT_DEFINE ("GnuplotHelperExample");
36 
37 //
38 // This is our test object, an object that increments a counter according
39 // to a Poisson process, and exports the (integer-valued) count as a
40 // trace source.
41 //
42 class Emitter : public Object
43 {
44 public:
49  static TypeId GetTypeId (void);
50  Emitter ();
51 private:
52  void DoInitialize (void);
53  void Count (void);
54 
55  TracedValue<uint32_t> m_counter;
57 };
58 
60 
61 TypeId
62 Emitter::GetTypeId (void)
63 {
64  static TypeId tid = TypeId ("ns3::Emitter")
65  .SetParent<Object> ()
66  .SetGroupName ("Stats")
67  .AddConstructor<Emitter> ()
68  .AddTraceSource ("Counter",
69  "sample counter",
71  "ns3::TracedValueCallback::Double")
72  ;
73  return tid;
74 }
75 
76 Emitter::Emitter (void)
77 {
78  NS_LOG_FUNCTION (this);
79  m_counter = 0;
80  m_var = CreateObject<ExponentialRandomVariable> ();
81 }
82 
83 void
85 {
86  NS_LOG_FUNCTION (this);
87  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this);
88 }
89 
90 void
91 Emitter::Count (void)
92 {
93  NS_LOG_FUNCTION (this);
94  NS_LOG_DEBUG ("Counting at " << Simulator::Now ().As (Time::S));
95  m_counter += 1.0;
96  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this);
97 }
98 
99 int main (int argc, char *argv[])
100 {
101  CommandLine cmd (__FILE__);
102  cmd.Parse (argc, argv);
103 
104  //
105  // This Emitter has a trace source object that will emit values at
106  // random times.
107  //
108 
109  Ptr<Emitter> emitter = CreateObject<Emitter> ();
110  Names::Add ("/Names/Emitter", emitter);
111 
112  //
113  // This gnuplot helper will be used to produce output used to make
114  // gnuplot plots.
115  //
116 
117  // Create the gnuplot helper.
118  GnuplotHelper plotHelper;
119 
120  // Configure the plot. Arguments include file prefix, plot title,
121  // x-label, y-label, and output file type
122  plotHelper.ConfigurePlot ("gnuplot-helper-example",
123  "Emitter Count vs. Time",
124  "Time (Seconds)",
125  "Emitter Count",
126  "png");
127 
128  // Create a probe. Because the trace source we are interested in is
129  // of type uint32_t, we specify the type of probe to use by the first
130  // argument specifying its ns3 TypeId.
131  plotHelper.PlotProbe ("ns3::Uinteger32Probe",
132  "/Names/Emitter/Counter",
133  "Output",
134  "Emitter Count",
136 
137  // The Emitter object is not associated with an ns-3 node, so
138  // it won't get started automatically, so we need to do this ourselves
140 
141  Simulator::Stop (Seconds (100.0));
142  Simulator::Run ();
144 
145  return 0;
146 }
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
ns3::Simulator::Now
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Emitter::Emitter
Emitter()
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:557
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
ns3::GnuplotHelper::PlotProbe
void PlotProbe(const std::string &typeId, const std::string &path, const std::string &probeTraceSource, const std::string &title, enum GnuplotAggregator::KeyLocation keyLocation=GnuplotAggregator::KEY_INSIDE)
Definition: gnuplot-helper.cc:105
ns3::MakeTraceSourceAccessor
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Definition: trace-source-accessor.h:202
ns3::Ptr< ExponentialRandomVariable >
Emitter
Definition: double-probe-example.cc:39
Emitter::DoInitialize
void DoInitialize(void)
Initialize() implementation.
ns3::Simulator::Stop
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
ns3::Object
A base class which provides memory management and object aggregation.
Definition: object.h:88
second.cmd
cmd
Definition: second.py:35
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
ns3::GnuplotHelper
Helper class used to make gnuplot plots.
Definition: gnuplot-helper.h:40
NS_LOG_DEBUG
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
ns3::GnuplotHelper::ConfigurePlot
void ConfigurePlot(const std::string &outputFileNameWithoutExtension, const std::string &title, const std::string &xLegend, const std::string &yLegend, const std::string &terminalType="png")
Definition: gnuplot-helper.cc:77
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::TracedValue< uint32_t >
ns3::Time::S
@ S
second
Definition: nstime.h:115
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition: log-macros-enabled.h:244
Emitter::Count
void Count(void)
Emitter::m_counter
TracedValue< double > m_counter
Definition: double-probe-example.cc:52
ns3::Object::Initialize
void Initialize(void)
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:183
Emitter::GetTypeId
static TypeId GetTypeId(void)
Register this type.
Definition: double-probe-example.cc:60
ns3::Names::Add
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition: names.cc:768
Emitter::GetTypeId
static TypeId GetTypeId(void)
Register this type.
ns3::GnuplotAggregator::KEY_INSIDE
@ KEY_INSIDE
Definition: gnuplot-aggregator.h:45