satellite-stats-frame-load-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/callback.h>
26 #include <ns3/data-collection-object.h>
27 #include <ns3/enum.h>
28 #include <ns3/fatal-error.h>
29 #include <ns3/log.h>
30 #include <ns3/multi-file-aggregator.h>
31 #include <ns3/node-container.h>
32 #include <ns3/satellite-beam-helper.h>
33 #include <ns3/satellite-beam-scheduler.h>
34 #include <ns3/satellite-frame-symbol-load-probe.h>
35 #include <ns3/satellite-frame-user-load-probe.h>
36 #include <ns3/satellite-helper.h>
37 #include <ns3/satellite-ncc.h>
38 #include <ns3/scalar-collector.h>
39 #include <ns3/string.h>
40 
41 #include <utility>
42 
43 NS_LOG_COMPONENT_DEFINE("SatStatsFrameLoadHelper");
44 
45 namespace ns3
46 {
47 
48 NS_OBJECT_ENSURE_REGISTERED(SatStatsFrameLoadHelper);
49 
50 std::string // static
52 {
53  switch (unitType)
54  {
56  return "UNIT_SYMBOLS";
58  return "UNIT_USERS";
59  default:
60  NS_FATAL_ERROR("SatStatsFrameLoadHelper - Invalid unit type");
61  break;
62  }
63 
64  NS_FATAL_ERROR("SatStatsFrameLoadHelper - Invalid unit type");
65  return "";
66 }
67 
68 SatStatsFrameLoadHelper::SatStatsFrameLoadHelper(Ptr<const SatHelper> satHelper)
69  : SatStatsHelper(satHelper),
70  m_unitType(SatStatsFrameLoadHelper::UNIT_SYMBOLS),
71  m_shortLabel(""),
72  m_longLabel(""),
73  m_objectTraceSourceName(""),
74  m_probeTraceSourceName("")
75 {
76  NS_LOG_FUNCTION(this << satHelper);
77 }
78 
80 {
81  NS_LOG_FUNCTION(this);
82 }
83 
84 TypeId // static
86 {
87  static TypeId tid = TypeId("ns3::SatStatsFrameLoadHelper").SetParent<SatStatsHelper>();
88  return tid;
89 }
90 
91 void
93 {
94  NS_LOG_FUNCTION(this << GetUnitTypeName(unitType));
95  m_unitType = unitType;
96 
97  // Update unit-specific attributes.
99  {
100  m_shortLabel = "allocated_symbol_ratio";
101  m_longLabel = "Number of allocated symbols over total number of symbols";
102  m_objectTraceSourceName = "FrameLoadTrace";
103  m_probeTraceSourceName = "Output";
104  }
105  else if (unitType == SatStatsFrameLoadHelper::UNIT_USERS)
106  {
107  m_shortLabel = "allocated_users";
108  m_longLabel = "Number of scheduled users";
109  m_objectTraceSourceName = "FrameUtLoadTrace";
110  m_probeTraceSourceName = "Output";
111  }
112  else
113  {
114  NS_FATAL_ERROR("SatStatsFrameLoadHelper - Invalid unit type");
115  }
116 }
117 
120 {
121  return m_unitType;
122 }
123 
124 void
126 {
127  NS_LOG_FUNCTION(this);
128 
130  {
131  NS_FATAL_ERROR(GetOutputTypeName(GetOutputType())
132  << " is not a valid output type for this statistics.");
133  }
134 
137  {
138  NS_FATAL_ERROR(GetIdentifierTypeName(GetIdentifierType())
139  << " is not a valid identifier type for this statistics.");
140  }
141 
142  // Setup aggregator.
143  m_aggregator = CreateAggregator("ns3::MultiFileAggregator",
144  "OutputFileName",
145  StringValue(GetOutputFileName()),
146  "MultiFileMode",
147  BooleanValue(false),
148  "EnableContextPrinting",
149  BooleanValue(true),
150  "GeneralHeading",
151  StringValue(GetIdentifierHeading(m_shortLabel)));
152 
153  // Setup probes.
154  Ptr<SatBeamHelper> beamHelper = GetSatHelper()->GetBeamHelper();
155  NS_ASSERT(beamHelper != nullptr);
156  Ptr<SatNcc> ncc = beamHelper->GetNcc();
157  NS_ASSERT(ncc != nullptr);
158  std::list<std::pair<uint32_t, uint32_t>> beams = beamHelper->GetBeams();
159  bool ret = false;
160 
161  for (std::list<std::pair<uint32_t, uint32_t>>::const_iterator it = beams.begin();
162  it != beams.end();
163  ++it)
164  {
166  {
167  ret = SetupProbe<SatFrameSymbolLoadProbe>(
168  ncc->GetBeamScheduler(it->first, it->second),
169  GetIdentifierForBeam(it->first, it->second),
171  }
172  else
173  {
174  NS_ASSERT_MSG(m_unitType == SatStatsFrameLoadHelper::UNIT_USERS, "Invalid unit type");
175  ret =
176  SetupProbe<SatFrameUserLoadProbe>(ncc->GetBeamScheduler(it->first, it->second),
177  GetIdentifierForBeam(it->first, it->second),
179  }
180 
181  if (ret)
182  {
183  NS_LOG_INFO(this << " successfully connected"
184  << " with beam " << it->second);
185  }
186  else
187  {
188  NS_LOG_WARN(this << " unable to connect to beam " << it->second);
189  }
190 
191  } // end of `for (it: beams)`
192 
193 } // end of `void DoInstall ();`
194 
195 std::string
197 {
198  switch (GetIdentifierType())
199  {
201  return "% global frame_number " + dataLabel;
202 
204  return "% gw_id frame_number " + dataLabel;
205 
207  return "% beam_id frame_number " + dataLabel;
208 
209  default:
210  NS_FATAL_ERROR("SatStatsFrameLoadHelper - Invalid identifier type");
211  break;
212  }
213  return "";
214 }
215 
216 void
218  uint32_t frameId,
219  double loadRatio)
220 {
221  // NS_LOG_FUNCTION (this << context << frameId << loadRatio);
222 
223  // Get the right collector for this frame ID and identifier.
224  Ptr<ScalarCollector> collector = GetCollector(frameId, context);
225  NS_ASSERT_MSG(collector != nullptr,
226  "Unable to get/create collector"
227  << " for frame ID " << frameId << " and beam " << context);
228 
229  // Pass the sample to the collector.
230  collector->TraceSinkDouble(0, loadRatio);
231 }
232 
233 void
235  uint32_t frameId,
236  uint32_t utCount)
237 {
238  // NS_LOG_FUNCTION (this << context << frameId << utCount);
239 
240  // Get the right collector for this frame ID and identifier.
241  Ptr<ScalarCollector> collector = GetCollector(frameId, context);
242  NS_ASSERT_MSG(collector != nullptr,
243  "Unable to get/create collector"
244  << " for frame ID " << frameId << " and identifier " << context);
245 
246  // Pass the sample to the collector.
247  collector->TraceSinkUinteger32(0, utCount);
248 }
249 
250 Ptr<ScalarCollector>
251 SatStatsFrameLoadHelper::GetCollector(uint32_t frameId, std::string identifier)
252 {
253  // NS_LOG_FUNCTION (this << frameId);
254 
255  // convert context to number
256  std::stringstream ss(identifier);
257  uint32_t identifierNum;
258  if (!(ss >> identifierNum))
259  {
260  NS_FATAL_ERROR("Cannot convert '" << identifier << "' to number");
261  }
262 
263  std::map<uint32_t, CollectorMap>::iterator it = m_collectors.find(frameId);
264  if (it == m_collectors.end())
265  {
266  // Newly discovered frame ID
267  uint32_t n = 0;
268  CollectorMap collectorMap;
269  collectorMap.SetType("ns3::ScalarCollector");
271  {
272  collectorMap.SetAttribute("InputDataType",
273  EnumValue(ScalarCollector::INPUT_DATA_TYPE_DOUBLE));
274  collectorMap.SetAttribute("OutputType",
275  EnumValue(ScalarCollector::OUTPUT_TYPE_AVERAGE_PER_SAMPLE));
276  }
277  else
278  {
279  NS_ASSERT_MSG(m_unitType == SatStatsFrameLoadHelper::UNIT_USERS, "Invalid unit type");
280  collectorMap.SetAttribute("InputDataType",
281  EnumValue(ScalarCollector::INPUT_DATA_TYPE_UINTEGER));
282  collectorMap.SetAttribute("OutputType", EnumValue(ScalarCollector::OUTPUT_TYPE_SUM));
283  }
284 
285  /*
286  * Create a new set of collectors. Its name consists of two integers:
287  * - the first is the identifier ID (beam ID, GW ID, or simply zero for
288  * global);
289  * - the second is the frame ID.
290  */
291  switch (GetIdentifierType())
292  {
294  std::ostringstream name;
295  name << "0 " << frameId;
296  collectorMap.SetAttribute("Name", StringValue(name.str()));
297  collectorMap.Create(0);
298  n++;
299  break;
300  }
301 
303  NodeContainer gws = GetSatHelper()->GetBeamHelper()->GetGwNodes();
304  for (NodeContainer::Iterator it = gws.Begin(); it != gws.End(); ++it)
305  {
306  const uint32_t gwId = GetGwId(*it);
307  std::ostringstream name;
308  name << gwId << " " << frameId;
309  collectorMap.SetAttribute("Name", StringValue(name.str()));
310  collectorMap.Create(gwId);
311  n++;
312  }
313  break;
314  }
315 
317  std::list<std::pair<uint32_t, uint32_t>> beams =
318  GetSatHelper()->GetBeamHelper()->GetBeams();
319  for (std::list<std::pair<uint32_t, uint32_t>>::const_iterator it = beams.begin();
320  it != beams.end();
321  ++it)
322  {
323  const uint32_t satId = (it->first);
324  const uint32_t beamId = (it->second);
325  std::ostringstream name;
326  name << satId << "-" << beamId << " " << frameId;
327  collectorMap.SetAttribute("Name", StringValue(name.str()));
328  collectorMap.Create(SatConstVariables::MAX_BEAMS_PER_SATELLITE * (satId + 1) +
329  beamId);
330  n++;
331  }
332  break;
333  }
334 
335  default:
336  NS_FATAL_ERROR("SatStatsFrameLoadHelper - Invalid identifier type");
337  break;
338  }
339 
340  collectorMap.ConnectToAggregator("Output", m_aggregator, &MultiFileAggregator::Write1d);
341  NS_LOG_INFO(this << " created " << n << " instance(s)"
342  << " of " << collectorMap.GetType().GetName() << " for "
344 
345  std::pair<std::map<uint32_t, CollectorMap>::iterator, bool> ret;
346  ret = m_collectors.insert(std::make_pair(frameId, collectorMap));
347  NS_ASSERT(ret.second);
348  it = ret.first;
349 
350  } // end of `if (it == m_collectors.end ())`
351 
352  NS_ASSERT(it != m_collectors.end());
353 
354  // Find the collector with the right identifier.
355  Ptr<DataCollectionObject> collector = it->second.Get(identifierNum);
356  NS_ASSERT_MSG(collector != nullptr, "Unable to find collector with identifier " << identifier);
357  Ptr<ScalarCollector> c = collector->GetObject<ScalarCollector>();
358  return c;
359 
360 } // `Ptr<ScalarCollector> GetCollector (uint32_t, std::string)`
361 
362 // IN SYMBOL UNIT /////////////////////////////////////////////////////////////
363 
364 NS_OBJECT_ENSURE_REGISTERED(SatStatsFrameSymbolLoadHelper);
365 
367  : SatStatsFrameLoadHelper(satHelper)
368 {
369  NS_LOG_FUNCTION(this << satHelper);
371 }
372 
374 {
375  NS_LOG_FUNCTION(this);
376 }
377 
378 TypeId // static
380 {
381  static TypeId tid =
382  TypeId("ns3::SatStatsFrameSymbolLoadHelper").SetParent<SatStatsFrameLoadHelper>();
383  return tid;
384 }
385 
386 // IN USER UNIT ///////////////////////////////////////////////////////////////
387 
388 NS_OBJECT_ENSURE_REGISTERED(SatStatsFrameUserLoadHelper);
389 
391  : SatStatsFrameLoadHelper(satHelper)
392 {
393  NS_LOG_FUNCTION(this << satHelper);
395 }
396 
398 {
399  NS_LOG_FUNCTION(this);
400 }
401 
402 TypeId // static
404 {
405  static TypeId tid =
406  TypeId("ns3::SatStatsFrameUserLoadHelper").SetParent<SatStatsFrameLoadHelper>();
407  return tid;
408 }
409 
410 } // end of namespace ns3
Helper for frame load statistics.
static TypeId GetTypeId()
inherited from ObjectBase base class
std::map< uint32_t, CollectorMap > m_collectors
Two-dimensional map of collectors, indexed by the frame ID and then by the identifier.
Ptr< ScalarCollector > GetCollector(uint32_t frameId, std::string identifier)
static std::string GetUnitTypeName(UnitType_t unitType)
void FrameSymbolLoadCallback(std::string context, uint32_t frameId, double loadRatio)
std::string GetIdentifierHeading(std::string dataLabel) const
void DoInstall()
Install the probes, collectors, and aggregators necessary to produce the statistics output.
void FrameUserLoadCallback(std::string context, uint32_t frameId, uint32_t utCount)
Ptr< DataCollectionObject > m_aggregator
The aggregator created by this helper.
@ UNIT_USERS
Number of scheduled users (i.e., UT).
@ UNIT_SYMBOLS
Number of allocated symbols over total number of symbols in the frame.
SatStatsFrameLoadHelper(Ptr< const SatHelper > satHelper)
Helper for frame symbol load statistics.
static TypeId GetTypeId()
inherited from ObjectBase base class
SatStatsFrameSymbolLoadHelper(Ptr< const SatHelper > satHelper)
Helper for frame user load statistics.
static TypeId GetTypeId()
inherited from ObjectBase base class
SatStatsFrameUserLoadHelper(Ptr< const SatHelper > satHelper)
Parent abstract class of all satellite statistics helpers.
uint32_t GetIdentifierForBeam(uint32_t satId, uint32_t beamId) const
Ptr< const SatHelper > GetSatHelper() const
IdentifierType_t GetIdentifierType() const
static std::string GetOutputTypeName(OutputType_t outputType)
static std::string GetIdentifierTypeName(IdentifierType_t identifierType)
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.
OutputType_t GetOutputType() const
uint32_t GetGwId(Ptr< Node > gwNode) const
constexpr uint32_t MAX_BEAMS_PER_SATELLITE
Maximum number of beams per satellite.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.