satellite-stats-resources-granted-helper.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014 Magister Solutions
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: Budiarto Herman <budiarto.herman@magister.fi>
19  *
20  */
21 
23 
24 #include <ns3/boolean.h>
25 #include <ns3/bytes-probe.h>
26 #include <ns3/data-collection-object.h>
27 #include <ns3/distribution-collector.h>
28 #include <ns3/enum.h>
29 #include <ns3/fatal-error.h>
30 #include <ns3/log.h>
31 #include <ns3/magister-gnuplot-aggregator.h>
32 #include <ns3/multi-file-aggregator.h>
33 #include <ns3/node-container.h>
34 #include <ns3/probe.h>
35 #include <ns3/satellite-helper.h>
36 #include <ns3/satellite-net-device.h>
37 #include <ns3/satellite-ut-mac.h>
38 #include <ns3/scalar-collector.h>
39 #include <ns3/simulator.h>
40 #include <ns3/string.h>
41 #include <ns3/unit-conversion-collector.h>
42 
43 NS_LOG_COMPONENT_DEFINE("SatStatsResourcesGrantedHelper");
44 
45 namespace ns3
46 {
47 
48 NS_OBJECT_ENSURE_REGISTERED(SatStatsResourcesGrantedHelper);
49 
51  : SatStatsHelper(satHelper)
52 {
53  NS_LOG_FUNCTION(this << satHelper);
54 }
55 
57 {
58  NS_LOG_FUNCTION(this);
59 }
60 
61 TypeId // static
63 {
64  static TypeId tid = TypeId("ns3::SatStatsResourcesGrantedHelper").SetParent<SatStatsHelper>();
65  return tid;
66 }
67 
68 void
70 {
71  NS_LOG_FUNCTION(this);
72 
73  switch (GetOutputType())
74  {
76  NS_FATAL_ERROR(GetOutputTypeName(GetOutputType())
77  << " is not a valid output type for this statistics.");
78  break;
79 
81  // Setup aggregator.
82  m_aggregator = CreateAggregator("ns3::MultiFileAggregator",
83  "OutputFileName",
84  StringValue(GetOutputFileName()),
85  "MultiFileMode",
86  BooleanValue(false),
87  "EnableContextPrinting",
88  BooleanValue(true),
89  "GeneralHeading",
90  StringValue(GetIdentifierHeading("resources_bytes")));
91 
92  // Setup collectors.
93  m_terminalCollectors.SetType("ns3::ScalarCollector");
94  m_terminalCollectors.SetAttribute("InputDataType",
95  EnumValue(ScalarCollector::INPUT_DATA_TYPE_UINTEGER));
96  m_terminalCollectors.SetAttribute(
97  "OutputType",
98  EnumValue(ScalarCollector::OUTPUT_TYPE_AVERAGE_PER_SAMPLE));
100  m_terminalCollectors.ConnectToAggregator("Output",
101  m_aggregator,
102  &MultiFileAggregator::Write1d);
103 
104  // Setup a probe in each UT MAC.
105  NodeContainer uts = GetSatHelper()->GetBeamHelper()->GetUtNodes();
106  for (NodeContainer::Iterator it = uts.Begin(); it != uts.End(); ++it)
107  {
108  InstallProbe(*it, &ScalarCollector::TraceSinkUinteger32);
109  }
110 
111  break;
112  }
113 
115  // Setup aggregator.
116  m_aggregator = CreateAggregator("ns3::MultiFileAggregator",
117  "OutputFileName",
118  StringValue(GetOutputFileName()),
119  "GeneralHeading",
120  StringValue(GetTimeHeading("resources_bytes")));
121 
122  // Setup collectors.
123  m_terminalCollectors.SetType("ns3::UnitConversionCollector");
124  m_terminalCollectors.SetAttribute("ConversionType",
125  EnumValue(UnitConversionCollector::TRANSPARENT));
127  m_terminalCollectors.ConnectToAggregator("OutputTimeValue",
128  m_aggregator,
129  &MultiFileAggregator::Write2d);
130 
131  // Setup a probe in each UT MAC.
132  NodeContainer uts = GetSatHelper()->GetBeamHelper()->GetUtNodes();
133  for (NodeContainer::Iterator it = uts.Begin(); it != uts.End(); ++it)
134  {
135  InstallProbe(*it, &UnitConversionCollector::TraceSinkUinteger32);
136  }
137 
138  break;
139  }
140 
144  // Setup aggregator.
145  m_aggregator = CreateAggregator("ns3::MultiFileAggregator",
146  "OutputFileName",
147  StringValue(GetOutputFileName()),
148  "GeneralHeading",
149  StringValue(GetDistributionHeading("resources_bytes")));
150 
151  // Setup collectors.
152  m_terminalCollectors.SetType("ns3::DistributionCollector");
153  DistributionCollector::OutputType_t outputType =
154  DistributionCollector::OUTPUT_TYPE_HISTOGRAM;
156  {
157  outputType = DistributionCollector::OUTPUT_TYPE_PROBABILITY;
158  }
160  {
161  outputType = DistributionCollector::OUTPUT_TYPE_CUMULATIVE;
162  }
163  m_terminalCollectors.SetAttribute("OutputType", EnumValue(outputType));
165  m_terminalCollectors.ConnectToAggregator("Output",
166  m_aggregator,
167  &MultiFileAggregator::Write2d);
168  m_terminalCollectors.ConnectToAggregator("OutputString",
169  m_aggregator,
170  &MultiFileAggregator::AddContextHeading);
171  m_terminalCollectors.ConnectToAggregator("Warning",
172  m_aggregator,
173  &MultiFileAggregator::EnableContextWarning);
174 
175  // Setup a probe in each UT MAC.
176  NodeContainer uts = GetSatHelper()->GetBeamHelper()->GetUtNodes();
177  for (NodeContainer::Iterator it = uts.Begin(); it != uts.End(); ++it)
178  {
179  InstallProbe(*it, &DistributionCollector::TraceSinkUinteger32);
180  }
181 
182  break;
183  }
184 
187  NS_FATAL_ERROR(GetOutputTypeName(GetOutputType())
188  << " is not a valid output type for this statistics.");
189  break;
190 
192  // Setup aggregator.
193  m_aggregator = CreateAggregator("ns3::MagisterGnuplotAggregator",
194  "OutputPath",
195  StringValue(GetOutputPath()),
196  "OutputFileName",
197  StringValue(GetName()));
198  Ptr<MagisterGnuplotAggregator> plotAggregator =
199  m_aggregator->GetObject<MagisterGnuplotAggregator>();
200  NS_ASSERT(plotAggregator != nullptr);
201  // plot->SetTitle ("");
202  plotAggregator->SetLegend("Time (in seconds)", "Resources granted (in bytes)");
203  plotAggregator->Set2dDatasetDefaultStyle(Gnuplot2dDataset::LINES_POINTS);
204 
205  // Setup collectors.
206  m_terminalCollectors.SetType("ns3::UnitConversionCollector");
207  m_terminalCollectors.SetAttribute("ConversionType",
208  EnumValue(UnitConversionCollector::TRANSPARENT));
210  for (CollectorMap::Iterator it = m_terminalCollectors.Begin();
211  it != m_terminalCollectors.End();
212  ++it)
213  {
214  const std::string context = it->second->GetName();
215  plotAggregator->Add2dDataset(context, context);
216  }
217  m_terminalCollectors.ConnectToAggregator("OutputTimeValue",
218  m_aggregator,
219  &MagisterGnuplotAggregator::Write2d);
220 
221  // Setup a probe in each UT MAC.
222  NodeContainer uts = GetSatHelper()->GetBeamHelper()->GetUtNodes();
223  for (NodeContainer::Iterator it = uts.Begin(); it != uts.End(); ++it)
224  {
225  InstallProbe(*it, &UnitConversionCollector::TraceSinkUinteger32);
226  }
227 
228  break;
229  }
230 
234  // Setup aggregator.
235  m_aggregator = CreateAggregator("ns3::MagisterGnuplotAggregator",
236  "OutputPath",
237  StringValue(GetOutputPath()),
238  "OutputFileName",
239  StringValue(GetName()));
240  Ptr<MagisterGnuplotAggregator> plotAggregator =
241  m_aggregator->GetObject<MagisterGnuplotAggregator>();
242  NS_ASSERT(plotAggregator != nullptr);
243  // plot->SetTitle ("");
244  plotAggregator->SetLegend("Resources granted (in bytes)", "Frequency");
245  plotAggregator->Set2dDatasetDefaultStyle(Gnuplot2dDataset::LINES);
246 
247  // Setup collectors.
248  m_terminalCollectors.SetType("ns3::DistributionCollector");
249  DistributionCollector::OutputType_t outputType =
250  DistributionCollector::OUTPUT_TYPE_HISTOGRAM;
252  {
253  outputType = DistributionCollector::OUTPUT_TYPE_PROBABILITY;
254  }
256  {
257  outputType = DistributionCollector::OUTPUT_TYPE_CUMULATIVE;
258  }
259  m_terminalCollectors.SetAttribute("OutputType", EnumValue(outputType));
261  for (CollectorMap::Iterator it = m_terminalCollectors.Begin();
262  it != m_terminalCollectors.End();
263  ++it)
264  {
265  const std::string context = it->second->GetName();
266  plotAggregator->Add2dDataset(context, context);
267  }
268  m_terminalCollectors.ConnectToAggregator("Output",
269  m_aggregator,
270  &MagisterGnuplotAggregator::Write2d);
271 
272  // Setup a probe in each UT MAC.
273  NodeContainer uts = GetSatHelper()->GetBeamHelper()->GetUtNodes();
274  for (NodeContainer::Iterator it = uts.Begin(); it != uts.End(); ++it)
275  {
276  InstallProbe(*it, &DistributionCollector::TraceSinkUinteger32);
277  }
278 
279  break;
280  }
281 
282  default:
283  NS_FATAL_ERROR("SatStatsResourcesGrantedHelper - Invalid output type");
284  break;
285  }
286 
287 } // end of `void DoInstall ();`
288 
289 template <typename R, typename C, typename P>
290 void
291 SatStatsResourcesGrantedHelper::InstallProbe(Ptr<Node> utNode, R (C::*collectorTraceSink)(P, P))
292 {
293  NS_LOG_FUNCTION(this << utNode);
294 
295  const int32_t utId = GetUtId(utNode);
296  NS_ASSERT_MSG(utId > 0, "Node " << utNode->GetId() << " is not a valid UT");
297  const uint32_t identifier = GetIdentifierForUt(utNode);
298 
299  // Create the probe.
300  std::ostringstream probeName;
301  probeName << utId;
302  Ptr<BytesProbe> probe = CreateObject<BytesProbe>();
303  probe->SetName(probeName.str());
304 
305  Ptr<NetDevice> dev = GetUtSatNetDevice(utNode);
306  Ptr<SatNetDevice> satDev = dev->GetObject<SatNetDevice>();
307  NS_ASSERT(satDev != nullptr);
308  Ptr<SatMac> satMac = satDev->GetMac();
309  NS_ASSERT(satMac != nullptr);
310  Ptr<SatUtMac> satUtMac = satMac->GetObject<SatUtMac>();
311  NS_ASSERT(satUtMac != nullptr);
312 
313  // Connect the object to the probe.
314  if (probe->ConnectByObject("DaResourcesTrace", satUtMac))
315  {
316  // Connect the probe to the right collector.
317  if (m_terminalCollectors.ConnectWithProbe(probe->GetObject<Probe>(),
318  "Output",
319  identifier,
320  collectorTraceSink))
321  {
322  NS_LOG_INFO(this << " created probe " << probeName.str() << ", connected to collector "
323  << identifier);
324  m_probes.push_back(probe->GetObject<Probe>());
325  }
326  else
327  {
328  NS_LOG_WARN(this << " unable to connect probe " << probeName.str() << " to collector "
329  << identifier);
330  }
331  }
332  else
333  {
334  NS_FATAL_ERROR("Error connecting to DaResourcesTrace trace source of SatUtMac"
335  << " at node ID " << utNode->GetId() << " device #" << satDev->GetIfIndex());
336  }
337 
338 } // end of `void InstallProbe (Ptr<Node>, R (C::*) (P, P));`
339 
340 } // end of namespace ns3
SatNetDevice to be utilized in the UT and GW nodes.
Parent abstract class of all satellite statistics helpers.
Ptr< const SatHelper > GetSatHelper() const
static std::string GetOutputTypeName(OutputType_t outputType)
virtual std::string GetIdentifierHeading(std::string dataLabel) const
virtual std::string GetOutputPath() const
Ptr< DataCollectionObject > CreateAggregator(std::string aggregatorTypeId, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue())
Create the aggregator according to the output type.
virtual std::string GetOutputFileName() const
Compute the path and file name where statistics output should be written to.
uint32_t GetUtId(Ptr< Node > utNode) const
uint32_t CreateCollectorPerIdentifier(CollectorMap &collectorMap) const
Create one collector instance for each identifier in the simulation.
static Ptr< NetDevice > GetUtSatNetDevice(Ptr< Node > utNode)
OutputType_t GetOutputType() const
uint32_t GetIdentifierForUt(Ptr< Node > utNode) const
std::string GetName() const
virtual std::string GetTimeHeading(std::string dataLabel) const
virtual std::string GetDistributionHeading(std::string dataLabel) const
void InstallProbe(Ptr< Node > utNode, R(C::*collectorTraceSink)(P, P))
std::list< Ptr< Probe > > m_probes
Maintains a list of probes created by this helper.
void DoInstall()
Install the probes, collectors, and aggregators necessary to produce the statistics output.
Ptr< DataCollectionObject > m_aggregator
The aggregator created by this helper.
SatStatsResourcesGrantedHelper(Ptr< const SatHelper > satHelper)
static TypeId GetTypeId()
inherited from ObjectBase base class
CollectorMap m_terminalCollectors
Maintains a list of collectors created by this helper.
UT specific Mac class for Sat Net Devices.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.