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-topology.h>
38 #include <ns3/satellite-ut-mac.h>
39 #include <ns3/scalar-collector.h>
40 #include <ns3/simulator.h>
41 #include <ns3/singleton.h>
42 #include <ns3/string.h>
43 #include <ns3/unit-conversion-collector.h>
44 
45 #include <map>
46 #include <sstream>
47 #include <string>
48 #include <utility>
49 
50 NS_LOG_COMPONENT_DEFINE("SatStatsResourcesGrantedHelper");
51 
52 namespace ns3
53 {
54 
55 NS_OBJECT_ENSURE_REGISTERED(SatStatsResourcesGrantedHelper);
56 
58  : SatStatsHelper(satHelper)
59 {
60  NS_LOG_FUNCTION(this << satHelper);
61 }
62 
64 {
65  NS_LOG_FUNCTION(this);
66 }
67 
68 TypeId // static
70 {
71  static TypeId tid = TypeId("ns3::SatStatsResourcesGrantedHelper").SetParent<SatStatsHelper>();
72  return tid;
73 }
74 
75 void
77 {
78  NS_LOG_FUNCTION(this);
79 
80  switch (GetOutputType())
81  {
83  NS_FATAL_ERROR(GetOutputTypeName(GetOutputType())
84  << " is not a valid output type for this statistics.");
85  break;
86 
88  // Setup aggregator.
89  m_aggregator = CreateAggregator("ns3::MultiFileAggregator",
90  "OutputFileName",
91  StringValue(GetOutputFileName()),
92  "MultiFileMode",
93  BooleanValue(false),
94  "EnableContextPrinting",
95  BooleanValue(true),
96  "GeneralHeading",
97  StringValue(GetIdentifierHeading("resources_bytes")));
98 
99  // Setup collectors.
100  m_terminalCollectors.SetType("ns3::ScalarCollector");
101  m_terminalCollectors.SetAttribute("InputDataType",
102  EnumValue(ScalarCollector::INPUT_DATA_TYPE_UINTEGER));
103  m_terminalCollectors.SetAttribute(
104  "OutputType",
105  EnumValue(ScalarCollector::OUTPUT_TYPE_AVERAGE_PER_SAMPLE));
107  m_terminalCollectors.ConnectToAggregator("Output",
108  m_aggregator,
109  &MultiFileAggregator::Write1d);
110 
111  // Setup a probe in each UT MAC.
112  NodeContainer uts = Singleton<SatTopology>::Get()->GetUtNodes();
113  for (NodeContainer::Iterator it = uts.Begin(); it != uts.End(); ++it)
114  {
115  InstallProbe(*it, &ScalarCollector::TraceSinkUinteger32);
116  }
117 
118  break;
119  }
120 
122  // Setup aggregator.
123  m_aggregator = CreateAggregator("ns3::MultiFileAggregator",
124  "OutputFileName",
125  StringValue(GetOutputFileName()),
126  "GeneralHeading",
127  StringValue(GetTimeHeading("resources_bytes")));
128 
129  // Setup collectors.
130  m_terminalCollectors.SetType("ns3::UnitConversionCollector");
131  m_terminalCollectors.SetAttribute("ConversionType",
132  EnumValue(UnitConversionCollector::TRANSPARENT));
134  m_terminalCollectors.ConnectToAggregator("OutputTimeValue",
135  m_aggregator,
136  &MultiFileAggregator::Write2d);
137 
138  // Setup a probe in each UT MAC.
139  NodeContainer uts = Singleton<SatTopology>::Get()->GetUtNodes();
140  for (NodeContainer::Iterator it = uts.Begin(); it != uts.End(); ++it)
141  {
142  InstallProbe(*it, &UnitConversionCollector::TraceSinkUinteger32);
143  }
144 
145  break;
146  }
147 
151  // Setup aggregator.
152  m_aggregator = CreateAggregator("ns3::MultiFileAggregator",
153  "OutputFileName",
154  StringValue(GetOutputFileName()),
155  "GeneralHeading",
156  StringValue(GetDistributionHeading("resources_bytes")));
157 
158  // Setup collectors.
159  m_terminalCollectors.SetType("ns3::DistributionCollector");
160  DistributionCollector::OutputType_t outputType =
161  DistributionCollector::OUTPUT_TYPE_HISTOGRAM;
163  {
164  outputType = DistributionCollector::OUTPUT_TYPE_PROBABILITY;
165  }
167  {
168  outputType = DistributionCollector::OUTPUT_TYPE_CUMULATIVE;
169  }
170  m_terminalCollectors.SetAttribute("OutputType", EnumValue(outputType));
172  m_terminalCollectors.ConnectToAggregator("Output",
173  m_aggregator,
174  &MultiFileAggregator::Write2d);
175  m_terminalCollectors.ConnectToAggregator("OutputString",
176  m_aggregator,
177  &MultiFileAggregator::AddContextHeading);
178  m_terminalCollectors.ConnectToAggregator("Warning",
179  m_aggregator,
180  &MultiFileAggregator::EnableContextWarning);
181 
182  // Setup a probe in each UT MAC.
183  NodeContainer uts = Singleton<SatTopology>::Get()->GetUtNodes();
184  for (NodeContainer::Iterator it = uts.Begin(); it != uts.End(); ++it)
185  {
186  InstallProbe(*it, &DistributionCollector::TraceSinkUinteger32);
187  }
188 
189  break;
190  }
191 
194  NS_FATAL_ERROR(GetOutputTypeName(GetOutputType())
195  << " is not a valid output type for this statistics.");
196  break;
197 
199  // Setup aggregator.
200  m_aggregator = CreateAggregator("ns3::MagisterGnuplotAggregator",
201  "OutputPath",
202  StringValue(GetOutputPath()),
203  "OutputFileName",
204  StringValue(GetName()));
205  Ptr<MagisterGnuplotAggregator> plotAggregator =
206  m_aggregator->GetObject<MagisterGnuplotAggregator>();
207  NS_ASSERT(plotAggregator != nullptr);
208  // plot->SetTitle ("");
209  plotAggregator->SetLegend("Time (in seconds)", "Resources granted (in bytes)");
210  plotAggregator->Set2dDatasetDefaultStyle(Gnuplot2dDataset::LINES_POINTS);
211 
212  // Setup collectors.
213  m_terminalCollectors.SetType("ns3::UnitConversionCollector");
214  m_terminalCollectors.SetAttribute("ConversionType",
215  EnumValue(UnitConversionCollector::TRANSPARENT));
217  for (CollectorMap::Iterator it = m_terminalCollectors.Begin();
218  it != m_terminalCollectors.End();
219  ++it)
220  {
221  const std::string context = it->second->GetName();
222  plotAggregator->Add2dDataset(context, context);
223  }
224  m_terminalCollectors.ConnectToAggregator("OutputTimeValue",
225  m_aggregator,
226  &MagisterGnuplotAggregator::Write2d);
227 
228  // Setup a probe in each UT MAC.
229  NodeContainer uts = Singleton<SatTopology>::Get()->GetUtNodes();
230  for (NodeContainer::Iterator it = uts.Begin(); it != uts.End(); ++it)
231  {
232  InstallProbe(*it, &UnitConversionCollector::TraceSinkUinteger32);
233  }
234 
235  break;
236  }
237 
241  // Setup aggregator.
242  m_aggregator = CreateAggregator("ns3::MagisterGnuplotAggregator",
243  "OutputPath",
244  StringValue(GetOutputPath()),
245  "OutputFileName",
246  StringValue(GetName()));
247  Ptr<MagisterGnuplotAggregator> plotAggregator =
248  m_aggregator->GetObject<MagisterGnuplotAggregator>();
249  NS_ASSERT(plotAggregator != nullptr);
250  // plot->SetTitle ("");
251  plotAggregator->SetLegend("Resources granted (in bytes)", "Frequency");
252  plotAggregator->Set2dDatasetDefaultStyle(Gnuplot2dDataset::LINES);
253 
254  // Setup collectors.
255  m_terminalCollectors.SetType("ns3::DistributionCollector");
256  DistributionCollector::OutputType_t outputType =
257  DistributionCollector::OUTPUT_TYPE_HISTOGRAM;
259  {
260  outputType = DistributionCollector::OUTPUT_TYPE_PROBABILITY;
261  }
263  {
264  outputType = DistributionCollector::OUTPUT_TYPE_CUMULATIVE;
265  }
266  m_terminalCollectors.SetAttribute("OutputType", EnumValue(outputType));
268  for (CollectorMap::Iterator it = m_terminalCollectors.Begin();
269  it != m_terminalCollectors.End();
270  ++it)
271  {
272  const std::string context = it->second->GetName();
273  plotAggregator->Add2dDataset(context, context);
274  }
275  m_terminalCollectors.ConnectToAggregator("Output",
276  m_aggregator,
277  &MagisterGnuplotAggregator::Write2d);
278 
279  // Setup a probe in each UT MAC.
280  NodeContainer uts = Singleton<SatTopology>::Get()->GetUtNodes();
281  for (NodeContainer::Iterator it = uts.Begin(); it != uts.End(); ++it)
282  {
283  InstallProbe(*it, &DistributionCollector::TraceSinkUinteger32);
284  }
285 
286  break;
287  }
288 
289  default:
290  NS_FATAL_ERROR("SatStatsResourcesGrantedHelper - Invalid output type");
291  break;
292  }
293 
294 } // end of `void DoInstall ();`
295 
296 template <typename R, typename C, typename P>
297 void
298 SatStatsResourcesGrantedHelper::InstallProbe(Ptr<Node> utNode, R (C::*collectorTraceSink)(P, P))
299 {
300  NS_LOG_FUNCTION(this << utNode);
301 
302  const int32_t utId = GetUtId(utNode);
303  NS_ASSERT_MSG(utId > 0, "Node " << utNode->GetId() << " is not a valid UT");
304  const uint32_t identifier = GetIdentifierForUt(utNode);
305 
306  // Create the probe.
307  std::ostringstream probeName;
308  probeName << utId;
309  Ptr<BytesProbe> probe = CreateObject<BytesProbe>();
310  probe->SetName(probeName.str());
311 
312  Ptr<NetDevice> dev = GetUtSatNetDevice(utNode);
313  Ptr<SatNetDevice> satDev = dev->GetObject<SatNetDevice>();
314  NS_ASSERT(satDev != nullptr);
315  Ptr<SatMac> satMac = satDev->GetMac();
316  NS_ASSERT(satMac != nullptr);
317  Ptr<SatUtMac> satUtMac = satMac->GetObject<SatUtMac>();
318  NS_ASSERT(satUtMac != nullptr);
319 
320  // Connect the object to the probe.
321  if (probe->ConnectByObject("DaResourcesTrace", satUtMac))
322  {
323  // Connect the probe to the right collector.
324  if (m_terminalCollectors.ConnectWithProbe(probe->GetObject<Probe>(),
325  "Output",
326  identifier,
327  collectorTraceSink))
328  {
329  NS_LOG_INFO(this << " created probe " << probeName.str() << ", connected to collector "
330  << identifier);
331  m_probes.insert(
332  std::make_pair(probe->GetObject<Probe>(), std::make_pair(utNode, identifier)));
333  }
334  else
335  {
336  NS_LOG_WARN(this << " unable to connect probe " << probeName.str() << " to collector "
337  << identifier);
338  }
339  }
340  else
341  {
342  NS_FATAL_ERROR("Error connecting to DaResourcesTrace trace source of SatUtMac"
343  << " at node ID " << utNode->GetId() << " device #" << satDev->GetIfIndex());
344  }
345 
346 } // end of `void InstallProbe (Ptr<Node>, R (C::*) (P, P));`
347 
348 template <typename R, typename C, typename P>
349 void
351 {
352  NS_LOG_FUNCTION(this);
353 
354  std::map<Ptr<Probe>, std::pair<Ptr<Node>, uint32_t>>::iterator it;
355 
356  for (it = m_probes.begin(); it != m_probes.end(); it++)
357  {
358  Ptr<Probe> probe = it->first;
359  Ptr<Node> node = it->second.first;
360  uint32_t identifier = it->second.second;
361  R (C::*collectorTraceSink)(P, P);
362 
363  switch (GetOutputType())
364  {
366  NS_FATAL_ERROR(GetOutputTypeName(GetOutputType())
367  << " is not a valid output type for this statistics.");
368  break;
369 
371  collectorTraceSink = &ScalarCollector::TraceSinkUinteger32;
372  break;
373  }
374 
376  collectorTraceSink = &UnitConversionCollector::TraceSinkUinteger32;
377  break;
378  }
379 
383  collectorTraceSink = &DistributionCollector::TraceSinkUinteger32;
384  break;
385  }
386 
388  NS_FATAL_ERROR(GetOutputTypeName(GetOutputType())
389  << " is not a valid output type for this statistics.");
390  break;
391 
393  collectorTraceSink = &UnitConversionCollector::TraceSinkUinteger32;
394  break;
395  }
396 
400  collectorTraceSink = &DistributionCollector::TraceSinkUinteger32;
401  break;
402  }
403 
404  default:
405  NS_FATAL_ERROR("SatStatsResourcesGrantedHelper - Invalid output type");
406  break;
407  }
408 
409  if (!m_terminalCollectors.DisconnectWithProbe(probe->GetObject<Probe>(),
410  "Output",
411  identifier,
412  collectorTraceSink))
413  {
414  NS_FATAL_ERROR("Error disconnecting trace file on handover");
415  }
416 
417  identifier = GetIdentifierForUtUser(node);
418 
419  if (!m_terminalCollectors.ConnectWithProbe(probe->GetObject<Probe>(),
420  "Output",
421  identifier,
422  collectorTraceSink))
423  {
424  NS_FATAL_ERROR("Error connecting trace file on handover");
425  }
426 
427  it->second.second = identifier;
428  }
429 } // end of `void UpdateIdentifierOnProbes ();`
430 
431 } // end of namespace ns3
SatNetDevice to be utilized in the UT and GW nodes.
Parent abstract class of all satellite statistics helpers.
uint32_t GetIdentifierForUtUser(Ptr< Node > utUserNode) 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
std::map< Ptr< Probe >, std::pair< Ptr< Node >, uint32_t > > m_probes
Maintains a list of probes created by this helper.
void InstallProbe(Ptr< Node > utNode, R(C::*collectorTraceSink)(P, P))
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)
void UpdateIdentifierOnProbes()
Change identifier used on probes, when handovers occur.
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.