A Discrete-Event Network Simulator
API
ofswitch13-stats-calculator.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 University of Campinas (Unicamp)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Luciano Jerez Chaves <ljerezchaves@gmail.com>
18  */
19 
21 
22 #include <ns3/config.h>
23 #include <ns3/log.h>
24 #include <ns3/simulator.h>
25 
26 #include <iomanip>
27 #include <iostream>
28 #include <numeric>
29 
30 using namespace std;
31 
32 namespace ns3
33 {
34 
35 NS_LOG_COMPONENT_DEFINE("OFSwitch13StatsCalculator");
36 NS_OBJECT_ENSURE_REGISTERED(OFSwitch13StatsCalculator);
37 
38 OFSwitch13StatsCalculator::OFSwitch13StatsCalculator()
39  : m_device(nullptr),
40  m_wrapper(nullptr),
41  m_lastUpdate(Simulator::Now()),
42  m_ewmaBufferEntries(0.0),
43  m_ewmaCpuLoad(0.0),
44  m_ewmaGroupEntries(0.0),
45  m_ewmaMeterEntries(0.0),
46  m_ewmaPipelineDelay(0.0),
47  m_ewmaSumFlowEntries(0.0),
48  m_bytes(0),
49  m_lastFlowMods(0),
50  m_lastGroupMods(0),
51  m_lastMeterMods(0),
52  m_lastPacketsIn(0),
53  m_lastPacketsOut(0),
54  m_loadDrops(0),
55  m_meterDrops(0),
56  m_tableDrops(0),
57  m_packets(0)
58 {
59  NS_LOG_FUNCTION(this);
60 }
61 
63 {
64  NS_LOG_FUNCTION(this);
65 }
66 
67 TypeId
69 {
70  static TypeId tid =
71  TypeId("ns3::OFSwitch13StatsCalculator")
72  .SetParent<Object>()
73  .SetGroupName("OFSwitch13")
74  .AddConstructor<OFSwitch13StatsCalculator>()
75  .AddAttribute(
76  "EwmaAlpha",
77  "The EWMA alpha parameter for averaging statistics.",
78  DoubleValue(0.2),
80  MakeDoubleChecker<double>(0.0, 1.0))
81  .AddAttribute(
82  "DumpTimeout",
83  "The interval to update and dump switch statistics.",
84  TimeValue(Seconds(1)),
87  .AddAttribute(
88  "OutputFilename",
89  "Filename for dumping switch statistics.",
91  StringValue("ofswitch_stats.log"),
94  .AddAttribute(
95  "FlowTableDetails",
96  "Dump individual pipeline flow table statistics.",
97  BooleanValue(false),
100  return tid;
101 }
102 
103 void
105 {
106  NS_LOG_FUNCTION(this << device);
107 
108  // Save switch device pointer.
109  m_device = device;
110 
111  // Print the header line.
112  *m_wrapper->GetStream() << boolalpha << right << fixed << setprecision(3)
113  << " " << setw(8) << "TimeSec"
114  << " " << setw(12) << "LoaKbps"
115  << " " << setw(7) << "LoaUsag"
116  << " " << setw(7) << "Packets"
117  << " " << setw(7) << "DlyUsec"
118  << " " << setw(7) << "LoaDrps"
119  << " " << setw(7) << "MetDrps"
120  << " " << setw(7) << "TabDrps"
121  << " " << setw(7) << "FloMods"
122  << " " << setw(7) << "MetMods"
123  << " " << setw(7) << "GroMods"
124  << " " << setw(7) << "PktsIn"
125  << " " << setw(7) << "PktsOut"
126  << " " << setw(7) << "FloEntr"
127  << " " << setw(7) << "FloUsag"
128  << " " << setw(7) << "MetEntr"
129  << " " << setw(7) << "MetUsag"
130  << " " << setw(7) << "GroEntr"
131  << " " << setw(7) << "GroUsag"
132  << " " << setw(7) << "BufPkts"
133  << " " << setw(7) << "BufUsag";
134 
135  if (m_details)
136  {
137  for (size_t i = 0; i < m_device->GetNPipelineTables(); i++)
138  {
139  std::string field1 = "T" + to_string(i) + "Entr";
140  std::string field2 = "T" + to_string(i) + "Usag";
141  *m_wrapper->GetStream()
142  << " " << setw(7) << field1 << " " << setw(7) << field2;
143  }
144  }
145 
146  *m_wrapper->GetStream() << std::endl;
147 
148  // Hook sinks.
149  device->TraceConnectWithoutContext(
150  "DatapathTimeout",
153  device->TraceConnectWithoutContext(
154  "OverloadDrop",
157  device->TraceConnectWithoutContext(
158  "MeterDrop",
161  device->TraceConnectWithoutContext(
162  "TableDrop",
165  device->TraceConnectWithoutContext(
166  "PipelinePacket",
169 
170  m_ewmaFlowEntries.resize(device->GetNPipelineTables(), 0.0);
171 }
172 
173 uint32_t
175 {
176  return std::round(m_ewmaBufferEntries);
177 }
178 
179 DataRate
181 {
182  return DataRate(std::round(m_ewmaCpuLoad));
183 }
184 
185 uint32_t
187 {
188  return std::round(m_ewmaFlowEntries.at(tableId));
189 }
190 
191 uint32_t
193 {
194  return std::round(m_ewmaGroupEntries);
195 }
196 
197 uint32_t
199 {
200  return std::round(m_ewmaMeterEntries);
201 }
202 
203 Time
205 {
206  return Time(m_ewmaPipelineDelay);
207 }
208 
209 uint32_t
211 {
212  return std::round(m_ewmaSumFlowEntries);
213 }
214 
215 uint32_t
217 {
218  if (m_device->GetBufferSize() == 0)
219  {
220  return 0;
221  }
222  return std::round(static_cast<double>(GetEwmaBufferEntries()) * 100 /
223  static_cast<double>(m_device->GetBufferSize()));
224 }
225 
226 uint32_t
228 {
229  if (m_device->GetCpuCapacity().GetBitRate() == 0)
230  {
231  return 0;
232  }
233  return std::round(
234  static_cast<double>(GetEwmaCpuLoad().GetBitRate()) * 100 /
235  static_cast<double>(m_device->GetCpuCapacity().GetBitRate()));
236 }
237 
238 uint32_t
240 {
241  if (m_device->GetFlowTableSize(tableId) == 0)
242  {
243  return 0;
244  }
245  return std::round(static_cast<double>(GetEwmaFlowTableEntries(tableId)) *
246  100 /
247  static_cast<double>(m_device->GetFlowTableSize(tableId)));
248 }
249 
250 uint32_t
252 {
253  if (m_device->GetGroupTableSize() == 0)
254  {
255  return 0;
256  }
257  return std::round(static_cast<double>(GetEwmaGroupTableEntries()) * 100 /
258  static_cast<double>(m_device->GetGroupTableSize()));
259 }
260 
261 uint32_t
263 {
264  if (m_device->GetMeterTableSize() == 0)
265  {
266  return 0;
267  }
268  return std::round(static_cast<double>(GetEwmaMeterTableEntries()) * 100 /
269  static_cast<double>(m_device->GetMeterTableSize()));
270 }
271 
272 uint32_t
274 {
275  uint32_t sumSize = 0;
276  for (size_t i = 0; i < m_device->GetNPipelineTables(); i++)
277  {
278  if (m_device->GetFlowTableEntries(i))
279  {
280  sumSize += m_device->GetFlowTableSize(i);
281  }
282  }
283 
284  if (sumSize == 0)
285  {
286  return 0;
287  }
288  return std::round(static_cast<double>(GetEwmaSumFlowEntries()) * 100 /
289  static_cast<double>(sumSize));
290 }
291 
292 void
294 {
295  NS_LOG_FUNCTION(this);
296 
297  m_device = nullptr;
298  m_wrapper = nullptr;
299 }
300 
301 void
303 {
304  NS_LOG_FUNCTION(this);
305 
306  // Open output file.
307  m_wrapper = Create<OutputStreamWrapper>(m_filename, std::ios::out);
308 
309  // Scheduling first update and dump.
312  this);
313 
314  // Chain up.
316 }
317 
318 void
321 {
322  NS_LOG_FUNCTION(this);
323 
324  NS_ASSERT_MSG(m_device == device, "Invalid device pointer.");
325  m_ewmaBufferEntries = m_alpha * m_device->GetBufferEntries() +
327  m_ewmaCpuLoad = m_alpha * m_device->GetCpuLoad().GetBitRate() +
328  (1 - m_alpha) * m_ewmaCpuLoad;
329  m_ewmaSumFlowEntries = m_alpha * m_device->GetSumFlowEntries() +
331  m_ewmaGroupEntries = m_alpha * m_device->GetGroupTableEntries() +
332  (1 - m_alpha) * m_ewmaGroupEntries;
333  m_ewmaMeterEntries = m_alpha * m_device->GetMeterTableEntries() +
334  (1 - m_alpha) * m_ewmaMeterEntries;
335  m_ewmaPipelineDelay = m_alpha * m_device->GetPipelineDelay().GetDouble() +
337 
338  for (size_t i = 0; i < m_device->GetNPipelineTables(); i++)
339  {
340  m_ewmaFlowEntries.at(i) = m_alpha * m_device->GetFlowTableEntries(i) +
341  (1 - m_alpha) * m_ewmaFlowEntries.at(i);
342  }
343 }
344 
345 void
347 {
348  NS_LOG_FUNCTION(this << packet);
349 
350  m_loadDrops++;
351 }
352 
353 void
355  uint32_t meterId)
356 {
357  NS_LOG_FUNCTION(this << packet << meterId);
358 
359  m_meterDrops++;
360 }
361 
362 void
364  uint8_t tableId)
365 {
366  NS_LOG_FUNCTION(this << packet << +tableId);
367 
368  m_tableDrops++;
369 }
370 
371 void
373 {
374  NS_LOG_FUNCTION(this << packet);
375 
376  m_bytes += packet->GetSize();
377  m_packets++;
378 }
379 
380 void
382 {
383  NS_LOG_FUNCTION(this);
384 
385  // Collect statistics from switch device.
386  uint64_t flowMods = m_device->GetFlowModCounter();
387  uint64_t groupMods = m_device->GetGroupModCounter();
388  uint64_t meterMods = m_device->GetMeterModCounter();
389  uint64_t packetsIn = m_device->GetPacketInCounter();
390  uint64_t packetsOut = m_device->GetPacketOutCounter();
391 
392  // We don't use the EWMA CPU load here. Instead, we use the number of
393  // bytes transmitted since the last dump operation to get a precise average
394  // CPU load.
395  double elapSeconds = (Simulator::Now() - m_lastUpdate).GetSeconds();
396  uint64_t cpuLoad = m_bytes * 8 / elapSeconds;
397  uint64_t cpuCapy = m_device->GetCpuCapacity().GetBitRate();
398  uint32_t cpuUsage = 0;
399  if (cpuCapy)
400  {
401  cpuUsage = std::round(static_cast<double>(cpuLoad) * 100 /
402  static_cast<double>(cpuCapy));
403  }
404 
405  // Print statistics to file.
406  *m_wrapper->GetStream()
407  << " " << setw(8) << Simulator::Now().GetSeconds() << " " << setw(12)
408  << static_cast<double>(cpuLoad) / 1000 << " " << setw(7) << cpuUsage
409  << " " << setw(7) << m_packets << " " << setw(7)
410  << GetEwmaPipelineDelay().GetMicroSeconds() << " " << setw(7)
411  << m_loadDrops << " " << setw(7) << m_meterDrops << " " << setw(7)
412  << m_tableDrops << " " << setw(7) << flowMods - m_lastFlowMods << " "
413  << setw(7) << meterMods - m_lastMeterMods << " " << setw(7)
414  << groupMods - m_lastGroupMods << " " << setw(7)
415  << packetsIn - m_lastPacketsIn << " " << setw(7)
416  << packetsOut - m_lastPacketsOut << " " << setw(7)
417  << GetEwmaSumFlowEntries() << " " << setw(7)
418  << GetAvgActFlowTableUsage() << " " << setw(7)
419  << GetEwmaMeterTableEntries() << " " << setw(7)
420  << GetAvgMeterTableUsage() << " " << setw(7)
421  << GetEwmaGroupTableEntries() << " " << setw(7)
422  << GetAvgGroupTableUsage() << " " << setw(7) << GetEwmaBufferEntries()
423  << " " << setw(7) << GetAvgBufferUsage();
424 
425  if (m_details)
426  {
427  for (size_t i = 0; i < m_device->GetNPipelineTables(); i++)
428  {
429  *m_wrapper->GetStream()
430  << " " << setw(7) << GetEwmaFlowTableEntries(i) << " "
431  << setw(7) << GetAvgFlowTableUsage(i);
432  }
433  }
434 
435  *m_wrapper->GetStream() << std::endl;
436 
437  // Update internal counters.
438  m_bytes = 0;
439  m_lastFlowMods = flowMods;
440  m_lastGroupMods = groupMods;
441  m_lastMeterMods = meterMods;
442  m_lastPacketsIn = packetsIn;
443  m_lastPacketsOut = packetsOut;
444  m_loadDrops = 0;
445  m_meterDrops = 0;
446  m_tableDrops = 0;
447  m_packets = 0;
448 
449  // Scheduling next update.
453  this);
454 }
455 
456 } // Namespace ns3
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Class for representing data rates.
Definition: data-rate.h:90
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
This class monitors a single OpenFlow switch device to collect statistics and periodically write them...
std::string m_filename
Output file name.
void NotifyConstructionCompleted() override
OpenFlow switch device.
uint32_t GetAvgCpuUsage() const
OpenFlow switch device.
void DumpStatistics()
Read statistics from switch, update internal counters, and dump data into output file.
uint32_t GetEwmaFlowTableEntries(uint8_t tableId) const
~OFSwitch13StatsCalculator() override
Default destructor.
static TypeId GetTypeId()
Register this type.
void NotifyTableDrop(Ptr< const Packet > packet, uint8_t tableId)
Notify when an unmatched packet is dropped by a flow table without a table-miss entry.
Ptr< OFSwitch13Device > m_device
OpenFlow switch device.
uint32_t GetAvgGroupTableUsage() const
OpenFlow switch device.
uint32_t GetAvgFlowTableUsage(uint8_t tableId) const
OpenFlow switch device.
uint32_t GetAvgBufferUsage() const
OpenFlow switch device.
Ptr< OutputStreamWrapper > m_wrapper
Output file wrapper.
uint32_t GetAvgActFlowTableUsage() const
OpenFlow switch device.
void NotifyOverloadDrop(Ptr< const Packet > packet)
Notify when a packet is dropped due to pipeline load.
uint32_t GetAvgMeterTableUsage() const
OpenFlow switch device.
void HookSinks(Ptr< OFSwitch13Device > device)
Hook switch device trace sources to internal stats calculator trace sinks.
void NotifyMeterDrop(Ptr< const Packet > packet, uint32_t meterId)
Notify when a packet is dropped by a meter band.
void NotifyPipelinePacket(Ptr< const Packet > packet)
Notify when a packet is sent to pipeline.
void DoDispose() override
Destructor implementation.
void NotifyDatapathTimeout(Ptr< const OFSwitch13Device > device)
Notify when a datapath timeout operation is completed.
virtual void NotifyConstructionCompleted()
Notifier called once the ObjectBase is fully constructed.
Definition: object-base.cc:75
A base class which provides memory management and object aggregation.
Definition: object.h:89
std::ostream * GetStream()
Return a pointer to an ostream previously set in the wrapper.
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:863
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
Control the scheduling of simulation events.
Definition: simulator.h:68
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:568
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:199
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:402
int64_t GetMicroSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:412
AttributeValue implementation for Time.
Definition: nstime.h:1423
a unique identifier for an interface.
Definition: type-id.h:60
@ ATTR_GET
The attribute can be read.
Definition: type-id.h:65
@ ATTR_CONSTRUCT
The attribute can be written at construction-time.
Definition: type-id.h:67
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: boolean.h:86
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition: boolean.cc:124
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: double.h:43
Ptr< const AttributeChecker > MakeStringChecker()
Definition: string.cc:30
Ptr< const AttributeAccessor > MakeStringAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: string.h:57
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: nstime.h:1424
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:328
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:296
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:848
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:707
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:535