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