A Discrete-Event Network Simulator
API
queue.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 University of Washington
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 
19 // The queue base class has a limit on its size, in terms of number of
20 // packets or number of bytes depending on the operating mode.
21 // The base class implements tracing and basic statistics calculations.
22 
23 #ifndef QUEUE_H
24 #define QUEUE_H
25 
26 #include "ns3/packet.h"
27 #include "ns3/object.h"
28 #include "ns3/traced-callback.h"
29 #include "ns3/traced-value.h"
30 #include "ns3/unused.h"
31 #include "ns3/log.h"
32 #include "ns3/queue-size.h"
33 #include <string>
34 #include <sstream>
35 #include <list>
36 
37 namespace ns3 {
38 
51 class QueueBase : public Object
52 {
53 public:
58  static TypeId GetTypeId (void);
59 
60  QueueBase ();
61  virtual ~QueueBase ();
62 
82  static void AppendItemTypeIfNotPresent (std::string& typeId, const std::string& itemType);
83 
87  bool IsEmpty (void) const;
88 
92  uint32_t GetNPackets (void) const;
93 
97  uint32_t GetNBytes (void) const;
98 
103  QueueSize GetCurrentSize (void) const;
104 
110  uint32_t GetTotalReceivedBytes (void) const;
111 
117  uint32_t GetTotalReceivedPackets (void) const;
118 
124  uint32_t GetTotalDroppedBytes (void) const;
125 
131  uint32_t GetTotalDroppedBytesBeforeEnqueue (void) const;
132 
138  uint32_t GetTotalDroppedBytesAfterDequeue (void) const;
139 
145  uint32_t GetTotalDroppedPackets (void) const;
146 
152  uint32_t GetTotalDroppedPacketsBeforeEnqueue (void) const;
153 
159  uint32_t GetTotalDroppedPacketsAfterDequeue (void) const;
160 
165  void ResetStatistics (void);
166 
174  void SetMaxSize (QueueSize size);
175 
179  QueueSize GetMaxSize (void) const;
180 
181 #if 0
182  // average calculation requires keeping around
183  // a buffer with the date of arrival of past received packets
184  // which are within the average window
185  // so, it is quite costly to do it all the time.
186  // Hence, it is disabled by default and must be explicitly
187  // enabled with this method which specifies the size
188  // of the average window in time units.
189  void EnableRunningAverage (Time averageWindow);
190  void DisableRunningAverage (void);
191  // average
192  double GetQueueSizeAverage (void);
193  double GetReceivedBytesPerSecondAverage (void);
194  double GetReceivedPacketsPerSecondAverage (void);
195  double GetDroppedBytesPerSecondAverage (void);
196  double GetDroppedPacketsPerSecondAverage (void);
197  // variance
198  double GetQueueSizeVariance (void);
199  double GetReceivedBytesPerSecondVariance (void);
200  double GetReceivedPacketsPerSecondVariance (void);
201  double GetDroppedBytesPerSecondVariance (void);
202  double GetDroppedPacketsPerSecondVariance (void);
203 #endif
204 
205 private:
216 
218 
220  template <typename Item>
221  friend class Queue;
222 };
223 
224 
248 template <typename Item>
249 class Queue : public QueueBase
250 {
251 public:
256  static TypeId GetTypeId (void);
257 
258  Queue ();
259  virtual ~Queue ();
260 
266  virtual bool Enqueue (Ptr<Item> item) = 0;
267 
273  virtual Ptr<Item> Dequeue (void) = 0;
274 
280  virtual Ptr<Item> Remove (void) = 0;
281 
287  virtual Ptr<const Item> Peek (void) const = 0;
288 
292  void Flush (void);
293 
294 protected:
295 
297  typedef typename std::list<Ptr<Item> >::const_iterator ConstIterator;
298 
313  ConstIterator Head (void) const;
314 
329  ConstIterator Tail (void) const;
330 
337  bool DoEnqueue (ConstIterator pos, Ptr<Item> item);
338 
345 
352 
359 
368  void DropBeforeEnqueue (Ptr<Item> item);
369 
378  void DropAfterDequeue (Ptr<Item> item);
379 
380 private:
381  std::list<Ptr<Item> > m_packets;
383 
394 };
395 
396 
401 template <typename Item>
402 TypeId
404 {
405  std::string name = GetTypeParamName<Queue<Item> > ();
406  static TypeId tid = TypeId (("ns3::Queue<" + name + ">").c_str ())
407  .SetParent<QueueBase> ()
408  .SetGroupName ("Network")
409  .AddTraceSource ("Enqueue", "Enqueue a packet in the queue.",
411  "ns3::" + name + "::TracedCallback")
412  .AddTraceSource ("Dequeue", "Dequeue a packet from the queue.",
414  "ns3::" + name + "::TracedCallback")
415  .AddTraceSource ("Drop", "Drop a packet (for whatever reason).",
417  "ns3::" + name + "::TracedCallback")
418  .AddTraceSource ("DropBeforeEnqueue", "Drop a packet before enqueue.",
420  "ns3::" + name + "::TracedCallback")
421  .AddTraceSource ("DropAfterDequeue", "Drop a packet after dequeue.",
423  "ns3::" + name + "::TracedCallback")
424  ;
425  return tid;
426 }
427 
428 template <typename Item>
430  : NS_LOG_TEMPLATE_DEFINE ("Queue")
431 {
432 }
433 
434 template <typename Item>
436 {
437 }
438 
439 template <typename Item>
440 bool
442 {
443  NS_LOG_FUNCTION (this << item);
444 
445  if (GetCurrentSize () + item > GetMaxSize ())
446  {
447  NS_LOG_LOGIC ("Queue full -- dropping pkt");
448  DropBeforeEnqueue (item);
449  return false;
450  }
451 
452  m_packets.insert (pos, item);
453 
454  uint32_t size = item->GetSize ();
455  m_nBytes += size;
456  m_nTotalReceivedBytes += size;
457 
458  m_nPackets++;
459  m_nTotalReceivedPackets++;
460 
461  NS_LOG_LOGIC ("m_traceEnqueue (p)");
462  m_traceEnqueue (item);
463 
464  return true;
465 }
466 
467 template <typename Item>
468 Ptr<Item>
470 {
471  NS_LOG_FUNCTION (this);
472 
473  if (m_nPackets.Get () == 0)
474  {
475  NS_LOG_LOGIC ("Queue empty");
476  return 0;
477  }
478 
479  Ptr<Item> item = *pos;
480  m_packets.erase (pos);
481 
482  if (item != 0)
483  {
484  NS_ASSERT (m_nBytes.Get () >= item->GetSize ());
485  NS_ASSERT (m_nPackets.Get () > 0);
486 
487  m_nBytes -= item->GetSize ();
488  m_nPackets--;
489 
490  NS_LOG_LOGIC ("m_traceDequeue (p)");
491  m_traceDequeue (item);
492  }
493  return item;
494 }
495 
496 template <typename Item>
497 Ptr<Item>
499 {
500  NS_LOG_FUNCTION (this);
501 
502  if (m_nPackets.Get () == 0)
503  {
504  NS_LOG_LOGIC ("Queue empty");
505  return 0;
506  }
507 
508  Ptr<Item> item = *pos;
509  m_packets.erase (pos);
510 
511  if (item != 0)
512  {
513  NS_ASSERT (m_nBytes.Get () >= item->GetSize ());
514  NS_ASSERT (m_nPackets.Get () > 0);
515 
516  m_nBytes -= item->GetSize ();
517  m_nPackets--;
518 
519  // packets are first dequeued and then dropped
520  NS_LOG_LOGIC ("m_traceDequeue (p)");
521  m_traceDequeue (item);
522 
523  DropAfterDequeue (item);
524  }
525  return item;
526 }
527 
528 template <typename Item>
529 void
531 {
532  NS_LOG_FUNCTION (this);
533  while (!IsEmpty ())
534  {
535  Remove ();
536  }
537 }
538 
539 template <typename Item>
542 {
543  NS_LOG_FUNCTION (this);
544 
545  if (m_nPackets.Get () == 0)
546  {
547  NS_LOG_LOGIC ("Queue empty");
548  return 0;
549  }
550 
551  return *pos;
552 }
553 
554 template <typename Item>
556 {
557  return m_packets.cbegin ();
558 }
559 
560 template <typename Item>
562 {
563  return m_packets.cend ();
564 }
565 
566 template <typename Item>
567 void
569 {
570  NS_LOG_FUNCTION (this << item);
571 
572  m_nTotalDroppedPackets++;
573  m_nTotalDroppedPacketsBeforeEnqueue++;
574  m_nTotalDroppedBytes += item->GetSize ();
575  m_nTotalDroppedBytesBeforeEnqueue += item->GetSize ();
576 
577  NS_LOG_LOGIC ("m_traceDropBeforeEnqueue (p)");
578  m_traceDrop (item);
579  m_traceDropBeforeEnqueue (item);
580 }
581 
582 template <typename Item>
583 void
585 {
586  NS_LOG_FUNCTION (this << item);
587 
588  m_nTotalDroppedPackets++;
589  m_nTotalDroppedPacketsAfterDequeue++;
590  m_nTotalDroppedBytes += item->GetSize ();
591  m_nTotalDroppedBytesAfterDequeue += item->GetSize ();
592 
593  NS_LOG_LOGIC ("m_traceDropAfterDequeue (p)");
594  m_traceDrop (item);
595  m_traceDropAfterDequeue (item);
596 }
597 
598 } // namespace ns3
599 
600 #endif /* QUEUE_H */
uint32_t m_nTotalDroppedPacketsBeforeEnqueue
Total dropped packets before enqueue.
Definition: queue.h:214
uint32_t m_nTotalReceivedPackets
Total received packets.
Definition: queue.h:209
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
uint32_t m_nTotalDroppedPackets
Total dropped packets.
Definition: queue.h:213
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
TracedCallback< Ptr< const Item > > m_traceDequeue
Traced callback: fired when a packet is dequeued.
Definition: queue.h:387
Class for representing queue sizes.
Definition: queue-size.h:94
QueueSize GetMaxSize(void) const
Definition: queue.cc:221
uint32_t GetTotalDroppedPacketsBeforeEnqueue(void) const
Definition: queue.cc:174
uint32_t GetTotalDroppedBytes(void) const
Definition: queue.cc:142
uint32_t GetTotalReceivedPackets(void) const
Definition: queue.cc:134
NS_LOG_TEMPLATE_DECLARE
the log component
Definition: queue.h:382
Forward calls to a chain of Callback.
void Flush(void)
Flush the queue.
Definition: queue.h:530
uint32_t m_nTotalDroppedBytes
Total dropped bytes.
Definition: queue.h:210
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
uint32_t m_nTotalDroppedPacketsAfterDequeue
Total dropped packets after dequeue.
Definition: queue.h:215
uint32_t GetTotalDroppedBytesBeforeEnqueue(void) const
Definition: queue.cc:150
uint32_t m_nTotalDroppedBytesAfterDequeue
Total dropped bytes after dequeue.
Definition: queue.h:212
Ptr< Item > DoRemove(ConstIterator pos)
Pull the item to drop from the queue.
Definition: queue.h:498
TracedCallback< Ptr< const Item > > m_traceDrop
Traced callback: fired when a packet is dropped.
Definition: queue.h:389
TracedValue< uint32_t > m_nPackets
Number of packets in the queue.
Definition: queue.h:208
static void AppendItemTypeIfNotPresent(std::string &typeId, const std::string &itemType)
Append the item type to the provided type ID if the latter does not end with &#39;>&#39;. ...
Definition: queue.cc:77
QueueSize m_maxSize
max queue size
Definition: queue.h:217
virtual ~QueueBase()
Definition: queue.cc:71
uint32_t GetTotalDroppedBytesAfterDequeue(void) const
Definition: queue.cc:158
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Template class for packet Queues.
static TypeId GetTypeId(void)
Get the type ID.
Definition: queue.h:403
TracedCallback< Ptr< const Item > > m_traceDropAfterDequeue
Traced callback: fired when a packet is dropped after dequeue.
Definition: queue.h:393
uint32_t GetNBytes(void) const
Definition: queue.cc:102
uint32_t m_nTotalDroppedBytesBeforeEnqueue
Total dropped bytes before enqueue.
Definition: queue.h:211
#define NS_LOG_TEMPLATE_DEFINE(name)
Initialize a reference to a Log component.
Definition: log.h:236
TracedCallback< Ptr< const Item > > m_traceEnqueue
Traced callback: fired when a packet is enqueued.
Definition: queue.h:385
void DropAfterDequeue(Ptr< Item > item)
Drop a packet after dequeue.
Definition: queue.h:584
ConstIterator Tail(void) const
Get a const iterator which indicates past-the-last item in the queue.
Definition: queue.h:561
uint32_t m_nTotalReceivedBytes
Total received bytes.
Definition: queue.h:207
void ResetStatistics(void)
Resets the counts for dropped packets, dropped bytes, received packets, and received bytes...
Definition: queue.cc:190
virtual bool Enqueue(Ptr< Item > item)=0
Place an item into the Queue (each subclass defines the position)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ConstIterator Head(void) const
Get a const iterator which refers to the first item in the queue.
Definition: queue.h:555
virtual Ptr< Item > Remove(void)=0
Remove an item from the Queue (each subclass defines the position), counting it as dropped...
Queue()
Definition: queue.h:429
uint32_t GetTotalDroppedPacketsAfterDequeue(void) const
Definition: queue.cc:182
TracedCallback< Ptr< const Item > > m_traceDropBeforeEnqueue
Traced callback: fired when a packet is dropped before enqueue.
Definition: queue.h:391
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
Ptr< const Item > DoPeek(ConstIterator pos) const
Peek the front item in the queue.
Definition: queue.h:541
Abstract base class for packet Queues.
Definition: queue.h:51
virtual Ptr< Item > Dequeue(void)=0
Remove an item from the Queue (each subclass defines the position), counting it as dequeued...
uint32_t GetTotalDroppedPackets(void) const
Definition: queue.cc:166
void DropBeforeEnqueue(Ptr< Item > item)
Drop a packet before enqueue.
Definition: queue.h:568
virtual Ptr< const Item > Peek(void) const =0
Get a copy of an item in the queue (each subclass defines the position) without removing it...
A base class which provides memory management and object aggregation.
Definition: object.h:87
uint32_t GetTotalReceivedBytes(void) const
Definition: queue.cc:126
void SetMaxSize(QueueSize size)
Set the maximum size of this queue.
Definition: queue.cc:204
Ptr< Item > DoDequeue(ConstIterator pos)
Pull the item to dequeue from the queue.
Definition: queue.h:469
QueueSize GetCurrentSize(void) const
Definition: queue.cc:110
uint32_t GetNPackets(void) const
Definition: queue.cc:94
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
bool DoEnqueue(ConstIterator pos, Ptr< Item > item)
Push an item in the queue.
Definition: queue.h:441
std::list< Ptr< Item > >::const_iterator ConstIterator
Const iterator.
Definition: queue.h:297
std::list< Ptr< Item > > m_packets
the items in the queue
Definition: queue.h:381
static TypeId GetTypeId(void)
Get the type ID.
Definition: queue.cc:33
bool IsEmpty(void) const
Definition: queue.cc:86
virtual ~Queue()
Definition: queue.h:435
TracedValue< uint32_t > m_nBytes
Number of bytes in the queue.
Definition: queue.h:206