INET Framework for OMNeT++/OMNEST
inet::ThruputMeteringChannel Class Reference

A cDatarateChannel extended with throughput calculation. More...

#include <ThruputMeteringChannel.h>

Inheritance diagram for inet::ThruputMeteringChannel:

Public Member Functions

 ThruputMeteringChannel (const char *name=nullptr)
 Constructor. More...
 
virtual ~ThruputMeteringChannel ()
 Copy constructor. More...
 
virtual void initialize () override
 Assignment. More...
 
virtual cChannel::Result processMessage (cMessage *msg, const SendOptions &options, simtime_t t) override
 Adds statistics and live display to the channel. More...
 

Protected Member Functions

virtual void beginNewInterval (simtime_t now)
 
virtual void refreshDisplay () const override
 

Protected Attributes

bool displayAsTooltip
 
const char * fmt
 
unsigned int batchSize
 
simtime_t maxInterval
 
long numPackets
 
double numBits
 
simtime_t intvlStartTime
 
simtime_t intvlLastPkTime
 
unsigned long intvlNumPackets
 
unsigned long intvlNumBits
 
double currentBitPerSec
 
double currentPkPerSec
 

Detailed Description

A cDatarateChannel extended with throughput calculation.

Values get displayed on the link, using the connection's "t=" display string tag.

The display can be customized with the "format" attribute. In the format string, the following characters will get expanded:

  • 'N': number of packets
  • 'V': volume (in bytes)
  • 'p': current packet/sec
  • 'b': current bandwidth
  • 'u': current channel utilization (%)
  • 'P': average packet/sec on [0,now)
  • 'B': average bandwidth on [0,now)
  • 'U': average channel utilization (%) on [0,now) Other characters are copied verbatim.

"Current" actually means the last measurement interval, which is 10 packets or 0.1s, whichever comes first.

PROBLEM: display only gets updated if there's traffic! (For example, a high pk/sec value might stay displayed even when the link becomes idle!)

Constructor & Destructor Documentation

◆ ThruputMeteringChannel()

inet::ThruputMeteringChannel::ThruputMeteringChannel ( const char *  name = nullptr)
explicit

Constructor.

14  : cDatarateChannel(name)
15 {
16  displayAsTooltip = false;
17  fmt = nullptr;
18  batchSize = 10; // packets
19  maxInterval = 0.1; // seconds
20 
21  numPackets = 0;
22  numBits = 0;
23 
26 
28 }

◆ ~ThruputMeteringChannel()

inet::ThruputMeteringChannel::~ThruputMeteringChannel ( )
virtual

Copy constructor.

Destructor.

31 {
32 }

Member Function Documentation

◆ beginNewInterval()

void inet::ThruputMeteringChannel::beginNewInterval ( simtime_t  now)
protectedvirtual
75 {
76  simtime_t duration = now - intvlStartTime;
77 
78  // record measurements
79  currentBitPerSec = intvlNumBits / duration;
80  currentPkPerSec = intvlNumPackets / duration;
81 
82  // restart counters
83  intvlStartTime = now;
85 }

Referenced by processMessage().

◆ initialize()

void inet::ThruputMeteringChannel::initialize ( )
overridevirtual

Assignment.

Creates and returns an exact copy of this object. See cObject for more details. Add parameters and initialize the stat variables

45 {
46  cDatarateChannel::initialize();
47  displayAsTooltip = par("displayAsTooltip");
48  fmt = par("thruputDisplayFormat");
49 }

◆ processMessage()

cChannel::Result inet::ThruputMeteringChannel::processMessage ( cMessage *  msg,
const SendOptions &  options,
simtime_t  t 
)
overridevirtual

Adds statistics and live display to the channel.

52 {
53  cChannel::Result result = cDatarateChannel::processMessage(msg, options, t);
54 
55  cPacket *pkt = dynamic_cast<cPacket *>(msg);
56  // TODO handle disabled state (show with different style?/color? or print "disabled"?)
57  if (!pkt || !fmt || *fmt == 0 || result.discard)
58  return result;
59 
60  // count packets and bits
61  numPackets++;
62  numBits += pkt->getBitLength();
63 
64  // packet should be counted to new interval
67 
69  intvlNumBits += pkt->getBitLength();
70  intvlLastPkTime = t;
71  return result;
72 }

◆ refreshDisplay()

void inet::ThruputMeteringChannel::refreshDisplay ( ) const
overrideprotectedvirtual
88 {
89  // produce label, based on format string
90  char buf[200];
91  char *p = buf;
92  simtime_t tt = getTransmissionFinishTime();
93  if (tt == 0)
94  tt = simTime();
95  double bps = (tt == 0) ? 0 : numBits / tt;
96  double bytes;
97  for (const char *fp = fmt; *fp && buf + 200 - p > 20; fp++) {
98  switch (*fp) {
99  case 'N': // number of packets
100  p += sprintf(p, "%ld", numPackets);
101  break;
102 
103  case 'V': // volume (in bytes)
104  bytes = floor(numBits / 8);
105  if (bytes < 1024)
106  p += sprintf(p, "%gB", bytes);
107  else if (bytes < 1024 * 1024)
108  p += sprintf(p, "%.3gKiB", bytes / 1024);
109  else
110  p += sprintf(p, "%.3gMiB", bytes / 1024 / 1024);
111  break;
112 
113  case 'p': // current packet/sec
114  p += sprintf(p, "%.3gpps", currentPkPerSec);
115  break;
116 
117  case 'b': // current bandwidth
118  if (currentBitPerSec < 1000000)
119  p += sprintf(p, "%.3gk", currentBitPerSec / 1000);
120  else
121  p += sprintf(p, "%.3gM", currentBitPerSec / 1000000);
122  break;
123 
124  case 'u': // current channel utilization (%)
125  if (getDatarate() == 0)
126  p += sprintf(p, "n/a");
127  else
128  p += sprintf(p, "%.3g%%", currentBitPerSec / getDatarate() * 100.0);
129  break;
130 
131  case 'P': // average packet/sec on [0,now)
132  p += sprintf(p, "%.3gpps", tt == 0 ? 0 : numPackets / tt);
133  break;
134 
135  case 'B': // average bandwidth on [0,now)
136  if (bps < 1000000)
137  p += sprintf(p, "%.3gk", bps / 1000);
138  else
139  p += sprintf(p, "%.3gM", bps / 1000000);
140  break;
141 
142  case 'U': // average channel utilization (%) on [0,now)
143  if (getDatarate() == 0)
144  p += sprintf(p, "n/a");
145  else
146  p += sprintf(p, "%.3g%%", bps / getDatarate() * 100.0);
147  break;
148 
149  default:
150  *p++ = *fp;
151  break;
152  }
153  }
154  *p = '\0';
155 
156  // display label
157  auto srcGate = getSourceGate();
158  if (srcGate && srcGate->getChannel())
159  srcGate->getDisplayString().setTagArg(displayAsTooltip ? "tt" : "t", 0, buf);
160 }

Member Data Documentation

◆ batchSize

unsigned int inet::ThruputMeteringChannel::batchSize
protected

◆ currentBitPerSec

double inet::ThruputMeteringChannel::currentBitPerSec
protected

◆ currentPkPerSec

double inet::ThruputMeteringChannel::currentPkPerSec
protected

◆ displayAsTooltip

bool inet::ThruputMeteringChannel::displayAsTooltip
protected

◆ fmt

const char* inet::ThruputMeteringChannel::fmt
protected

◆ intvlLastPkTime

simtime_t inet::ThruputMeteringChannel::intvlLastPkTime
protected

◆ intvlNumBits

unsigned long inet::ThruputMeteringChannel::intvlNumBits
protected

◆ intvlNumPackets

unsigned long inet::ThruputMeteringChannel::intvlNumPackets
protected

◆ intvlStartTime

simtime_t inet::ThruputMeteringChannel::intvlStartTime
protected

◆ maxInterval

simtime_t inet::ThruputMeteringChannel::maxInterval
protected

◆ numBits

double inet::ThruputMeteringChannel::numBits
protected

◆ numPackets

long inet::ThruputMeteringChannel::numPackets
protected

The documentation for this class was generated from the following files:
inet::units::units::bps
compose< b, pow< s, -1 > > bps
Definition: Units.h:1169
inet::ThruputMeteringChannel::numBits
double numBits
Definition: ThruputMeteringChannel.h:51
inet::ThruputMeteringChannel::currentPkPerSec
double currentPkPerSec
Definition: ThruputMeteringChannel.h:61
inet::ThruputMeteringChannel::intvlLastPkTime
simtime_t intvlLastPkTime
Definition: ThruputMeteringChannel.h:55
inet::ThruputMeteringChannel::numPackets
long numPackets
Definition: ThruputMeteringChannel.h:50
inet::ThruputMeteringChannel::fmt
const char * fmt
Definition: ThruputMeteringChannel.h:43
inet::ThruputMeteringChannel::intvlStartTime
simtime_t intvlStartTime
Definition: ThruputMeteringChannel.h:54
inet::ThruputMeteringChannel::batchSize
unsigned int batchSize
Definition: ThruputMeteringChannel.h:44
inet::ThruputMeteringChannel::maxInterval
simtime_t maxInterval
Definition: ThruputMeteringChannel.h:45
inet::ThruputMeteringChannel::beginNewInterval
virtual void beginNewInterval(simtime_t now)
Definition: ThruputMeteringChannel.cc:74
inet::ThruputMeteringChannel::intvlNumBits
unsigned long intvlNumBits
Definition: ThruputMeteringChannel.h:57
inet::ThruputMeteringChannel::intvlNumPackets
unsigned long intvlNumPackets
Definition: ThruputMeteringChannel.h:56
inet::ThruputMeteringChannel::displayAsTooltip
bool displayAsTooltip
Definition: ThruputMeteringChannel.h:42
inet::ThruputMeteringChannel::currentBitPerSec
double currentBitPerSec
Definition: ThruputMeteringChannel.h:60