Loading...
Searching...
No Matches
satellite-stats-frame-type-usage-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: Lauri Sormunen <lauri.sormunen@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/distribution-collector.h"
28#include "ns3/enum.h"
29#include "ns3/fatal-error.h"
30#include "ns3/log.h"
31#include "ns3/multi-file-aggregator.h"
32#include "ns3/node-container.h"
33#include "ns3/satellite-beam-helper.h"
34#include "ns3/satellite-beam-scheduler.h"
35#include "ns3/satellite-gw-mac.h"
36#include "ns3/satellite-helper.h"
37#include "ns3/satellite-ncc.h"
38#include "ns3/satellite-topology.h"
39#include "ns3/scalar-collector.h"
40#include "ns3/singleton.h"
41#include "ns3/string.h"
42
43#include <list>
44#include <map>
45#include <sstream>
46#include <string>
47#include <utility>
48
49NS_LOG_COMPONENT_DEFINE("SatStatsFrameTypeUsageHelper");
50
51namespace ns3
52{
53
54NS_OBJECT_ENSURE_REGISTERED(SatStatsFrameTypeUsageHelper);
55
56const std::map<SatEnums::SatBbFrameType_t, uint32_t> SatStatsFrameTypeUsageHelper::frameTypeIdMap =
61
63 : SatStatsHelper(satHelper),
64 m_usePercentage(false)
65{
66 NS_LOG_FUNCTION(this << satHelper);
67}
68
73
74TypeId // static
76{
77 static TypeId tid =
78 TypeId("ns3::SatStatsFrameTypeUsageHelper")
79 .SetParent<SatStatsHelper>()
80 .AddAttribute("Percentage",
81 "Use percentage of each frame type instead of sum per identifier.",
82 BooleanValue(false),
84 MakeBooleanChecker());
85 return tid;
86}
87
88void
90{
91 NS_LOG_FUNCTION(this);
92
94 {
95 NS_FATAL_ERROR(GetOutputTypeName(GetOutputType())
96 << " is not a valid output type for this statistics.");
97 }
98
101 {
103 << " is not a valid identifier type for this statistics.");
104 }
105
106 // Setup aggregator.
107 std::string dataLabel;
108 if (m_usePercentage)
109 {
110 dataLabel = "usage_percentage";
111 }
112 else
113 {
114 dataLabel = "usage_count";
115 }
116
117 m_aggregator = CreateAggregator("ns3::MultiFileAggregator",
118 "OutputFileName",
119 StringValue(GetOutputFileName()),
120 "MultiFileMode",
121 BooleanValue(false),
122 "EnableContextPrinting",
123 BooleanValue(true),
124 "GeneralHeading",
125 StringValue(GetIdentifierHeading(dataLabel)));
126
127 // For each frame type ID, create a CollectorMap instance
128 for (auto it : frameTypeIdMap)
129 {
130 uint32_t frameTypeId = it.second;
131 // Newly discovered waveform ID
132 NS_LOG_INFO(this << " Creating new collectors for frame type ID " << frameTypeId);
133
134 CollectorMap collectorMap;
135 ScalarCollector::OutputType_t opType;
136 if (m_usePercentage)
137 {
138 opType = ScalarCollector::OUTPUT_TYPE_AVERAGE_PER_SAMPLE;
139 }
140 else
141 {
142 opType = ScalarCollector::OUTPUT_TYPE_SUM;
143 }
144
145 collectorMap.SetType("ns3::ScalarCollector");
146 collectorMap.SetAttribute("OutputType", EnumValue(opType));
147 collectorMap.SetAttribute("InputDataType",
148 EnumValue(ScalarCollector::INPUT_DATA_TYPE_UINTEGER));
149 uint32_t n = 0;
150 /*
151 * Create a new set of collectors. Its name consists of two integers:
152 * - the first is the identifier ID (beam ID, GW ID, or simply zero for
153 * global);
154 * - the second is the frame ID.
155 */
156 switch (GetIdentifierType())
157 {
159 std::ostringstream name;
160 name << "0 " << frameTypeId;
161 collectorMap.SetAttribute("Name", StringValue(name.str()));
162 collectorMap.Create(0);
163 n++;
164 break;
165 }
166
168 NodeContainer gws = Singleton<SatTopology>::Get()->GetGwNodes();
169 for (NodeContainer::Iterator it = gws.Begin(); it != gws.End(); ++it)
170 {
171 const uint32_t gwId = GetGwId(*it);
172 std::ostringstream name;
173 name << gwId << " " << frameTypeId;
174 collectorMap.SetAttribute("Name", StringValue(name.str()));
175 collectorMap.Create(gwId);
176 n++;
177 }
178 break;
179 }
180
182 std::list<std::pair<uint32_t, uint32_t>> beams =
183 GetSatHelper()->GetBeamHelper()->GetBeams();
184 for (std::list<std::pair<uint32_t, uint32_t>>::const_iterator it = beams.begin();
185 it != beams.end();
186 ++it)
187 {
188 const uint32_t satId = (it->first);
189 const uint32_t beamId = (it->second);
190 std::ostringstream name;
191 name << satId << "-" << beamId << " " << frameTypeId;
192 collectorMap.SetAttribute("Name", StringValue(name.str()));
193 collectorMap.Create(SatConstVariables::MAX_BEAMS_PER_SATELLITE * (satId + 1) +
194 beamId);
195 n++;
196 }
197 break;
198 }
199
200 default:
201 NS_FATAL_ERROR("SatStatsWaveformUsageHelper - Invalid identifier type");
202 break;
203 }
204
205 collectorMap.ConnectToAggregator("Output", m_aggregator, &MultiFileAggregator::Write1d);
206
207 NS_LOG_INFO(this << " created " << n << " instance(s)" << " of "
208 << collectorMap.GetType().GetName() << " for "
210
211 std::pair<std::map<uint32_t, CollectorMap>::iterator, bool> ret;
212 ret = m_collectors.insert(std::make_pair(frameTypeId, collectorMap));
213 NS_ASSERT(ret.second);
214 }
215
216 // Create the callback for trace source
217 Callback<void, std::string, Ptr<SatBbFrame>> frameTypeUsageCallback =
219
220 // Connect SatGwMac of each beam by identifier (global, gw, beam) to callback
221 NodeContainer gwNodes = Singleton<SatTopology>::Get()->GetGwNodes();
222 for (auto node = gwNodes.Begin(); node != gwNodes.End(); node++)
223 {
224 for (uint32_t i = 0; i < (*node)->GetNDevices(); i++)
225 {
226 Ptr<SatNetDevice> dev = DynamicCast<SatNetDevice>((*node)->GetDevice(i));
227 if (dev == nullptr)
228 {
229 continue;
230 }
231 Ptr<SatGwMac> mac = DynamicCast<SatGwMac>(dev->GetMac());
232 if (mac == nullptr)
233 {
234 continue;
235 }
236
237 // Connect the trace source
238 uint32_t beamId = mac->GetBeamId();
239 uint32_t satId = mac->GetSatId();
240 std::ostringstream context;
241 context << GetIdentifierForBeam(satId, beamId);
242 const bool ret =
243 mac->TraceConnect("BBFrameTxTrace", context.str(), frameTypeUsageCallback);
244 NS_ASSERT_MSG(ret, "Error connecting to BBFrameTxTrace of beam " << beamId);
245 NS_LOG_INFO(this << " successfully connected" << " with beam " << beamId);
246 }
247 }
248
249} // end of `void DoInstall ();`
250
251std::string
253{
254 switch (GetIdentifierType())
255 {
257 return "% global frame_type " + dataLabel;
258
260 return "% gw_id frame_type " + dataLabel;
261
263 return "% beam_id frame_type " + dataLabel;
264
265 default:
266 NS_FATAL_ERROR("SatStatsFrameTypeUsageHelper - Invalid identifier type");
267 break;
268 }
269 return "";
270}
271
272// static
273uint32_t
275{
276 auto it = frameTypeIdMap.find(frameType);
277 NS_ASSERT(it != frameTypeIdMap.end());
278 return it->second;
279}
280
281void
282SatStatsFrameTypeUsageHelper::FrameTypeUsageCallback(std::string context, Ptr<SatBbFrame> bbFrame)
283{
285 bbFrame ? bbFrame->GetFrameType() : SatEnums::DUMMY_FRAME;
286 NS_LOG_FUNCTION(this << context << SatEnums::GetFrameTypeName(frameType));
287
288 // convert context to number
289 std::stringstream ss(context);
290 uint32_t identifier;
291 if (!(ss >> identifier))
292 {
293 NS_FATAL_ERROR("Cannot convert '" << context << "' to number");
294 }
295 uint32_t frameTypeId = GetFrameTypeId(frameType);
296
297 std::map<uint32_t, CollectorMap>::iterator it = m_collectors.find(frameTypeId);
298
299 NS_ASSERT(it != m_collectors.end());
300 if (!m_usePercentage)
301 {
302 // Find the collector with the right identifier.
303 Ptr<DataCollectionObject> collector = it->second.Get(identifier);
304 NS_ASSERT_MSG(collector != nullptr,
305 "Unable to find collector with identifier " << identifier);
306
307 Ptr<ScalarCollector> c = collector->GetObject<ScalarCollector>();
308 NS_ASSERT(c != nullptr);
309
310 // Pass the sample to the collector.
311 c->TraceSinkUinteger32(0, 1);
312 }
313 else
314 {
315 // Push 0 to all other frame type collectors of the beam/gw/global identifier
316 // and 1 to the collector of the right frame type
317 for (auto it : m_collectors)
318 {
319 Ptr<DataCollectionObject> collector = it.second.Get(identifier);
320 NS_ASSERT_MSG(collector != nullptr,
321 "Unable to find collector with identifier " << identifier);
322 Ptr<ScalarCollector> c = collector->GetObject<ScalarCollector>();
323 NS_ASSERT(c != nullptr);
324 if (it.first == frameTypeId)
325 {
326 c->TraceSinkUinteger32(0, 1);
327 }
328 else
329 {
330 c->TraceSinkUinteger32(0, 0);
331 }
332 }
333 }
334
335} // end of `void FrameTypeUsageCallback (std::string, uint32_t)`
336
337} // end of namespace ns3
SatBbFrameType_t
BB frame type used in DVB-S2 FWD link.
static std::string GetFrameTypeName(SatBbFrameType_t frameType)
SatStatsFrameTypeUsageHelper(Ptr< const SatHelper > satHelper)
Constructor.
std::string GetIdentifierHeading(std::string dataLabel) const
Get identifier header for file.
void FrameTypeUsageCallback(std::string context, Ptr< SatBbFrame > bbFrame)
static const std::map< SatEnums::SatBbFrameType_t, uint32_t > frameTypeIdMap
Mapping for frame type IDs to integers, in case that SatBbFrameType_t enums are assigned.
std::map< uint32_t, CollectorMap > m_collectors
Two-dimensional map of collectors, indexed first by the the frame type identifier and second by the g...
void DoInstall()
Install the probes, collectors, and aggregators necessary to produce the statistics output.
static uint32_t GetFrameTypeId(SatEnums::SatBbFrameType_t frameType)
Get frame type ID.
bool m_usePercentage
Flag for using percentage of the frame types in beam/in gw/globally instead of sum by type.
static TypeId GetTypeId()
inherited from ObjectBase base class
Ptr< DataCollectionObject > m_aggregator
The aggregator created by this helper.
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)
SatStatsHelper(Ptr< const SatHelper > satHelper)
Creates a new helper instance.
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.