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 "ns3/queue-item.h"
34 #include <string>
35 #include <sstream>
36 #include <list>
37 
38 namespace ns3 {
39 
52 class QueueBase : public Object
53 {
54 public:
59  static TypeId GetTypeId (void);
60 
61  QueueBase ();
62  virtual ~QueueBase ();
63 
83  static void AppendItemTypeIfNotPresent (std::string& typeId, const std::string& itemType);
84 
88  bool IsEmpty (void) const;
89 
93  uint32_t GetNPackets (void) const;
94 
98  uint32_t GetNBytes (void) const;
99 
104  QueueSize GetCurrentSize (void) const;
105 
111  uint32_t GetTotalReceivedBytes (void) const;
112 
118  uint32_t GetTotalReceivedPackets (void) const;
119 
125  uint32_t GetTotalDroppedBytes (void) const;
126 
132  uint32_t GetTotalDroppedBytesBeforeEnqueue (void) const;
133 
139  uint32_t GetTotalDroppedBytesAfterDequeue (void) const;
140 
146  uint32_t GetTotalDroppedPackets (void) const;
147 
153  uint32_t GetTotalDroppedPacketsBeforeEnqueue (void) const;
154 
160  uint32_t GetTotalDroppedPacketsAfterDequeue (void) const;
161 
166  void ResetStatistics (void);
167 
175  void SetMaxSize (QueueSize size);
176 
180  QueueSize GetMaxSize (void) const;
181 
182 #if 0
183  // average calculation requires keeping around
184  // a buffer with the date of arrival of past received packets
185  // which are within the average window
186  // so, it is quite costly to do it all the time.
187  // Hence, it is disabled by default and must be explicitly
188  // enabled with this method which specifies the size
189  // of the average window in time units.
190  void EnableRunningAverage (Time averageWindow);
191  void DisableRunningAverage (void);
192  // average
193  double GetQueueSizeAverage (void);
194  double GetReceivedBytesPerSecondAverage (void);
195  double GetReceivedPacketsPerSecondAverage (void);
196  double GetDroppedBytesPerSecondAverage (void);
197  double GetDroppedPacketsPerSecondAverage (void);
198  // variance
199  double GetQueueSizeVariance (void);
200  double GetReceivedBytesPerSecondVariance (void);
201  double GetReceivedPacketsPerSecondVariance (void);
202  double GetDroppedBytesPerSecondVariance (void);
203  double GetDroppedPacketsPerSecondVariance (void);
204 #endif
205 
206 private:
217 
219 
221  template <typename Item>
222  friend class Queue;
223 };
224 
225 
252 template <typename Item>
253 class Queue : public QueueBase
254 {
255 public:
260  static TypeId GetTypeId (void);
261 
262  Queue ();
263  virtual ~Queue ();
264 
270  virtual bool Enqueue (Ptr<Item> item) = 0;
271 
277  virtual Ptr<Item> Dequeue (void) = 0;
278 
284  virtual Ptr<Item> Remove (void) = 0;
285 
291  virtual Ptr<const Item> Peek (void) const = 0;
292 
298  void Flush (void);
299 
301  typedef Item ItemType;
302 
303 protected:
304 
306  typedef typename std::list<Ptr<Item> >::const_iterator ConstIterator;
308  typedef typename std::list<Ptr<Item> >::iterator Iterator;
309 
324  ConstIterator begin (void) const;
325 
340  Iterator begin (void);
341 
356  ConstIterator end (void) const;
357 
372  Iterator end (void);
373 
381 
389  bool DoEnqueue (ConstIterator pos, Ptr<Item> item, Iterator& ret);
390 
397 
404 
411 
421 
431 
432  void DoDispose (void) override;
433 
434 private:
435  std::list<Ptr<Item> > m_packets;
437 
448 };
449 
450 
455 template <typename Item>
456 TypeId
458 {
459  std::string name = GetTypeParamName<Queue<Item> > ();
460  static TypeId tid = TypeId (("ns3::Queue<" + name + ">").c_str ())
461  .SetParent<QueueBase> ()
462  .SetGroupName ("Network")
463  .AddTraceSource ("Enqueue", "Enqueue a packet in the queue.",
465  "ns3::" + name + "::TracedCallback")
466  .AddTraceSource ("Dequeue", "Dequeue a packet from the queue.",
468  "ns3::" + name + "::TracedCallback")
469  .AddTraceSource ("Drop", "Drop a packet (for whatever reason).",
471  "ns3::" + name + "::TracedCallback")
472  .AddTraceSource ("DropBeforeEnqueue", "Drop a packet before enqueue.",
474  "ns3::" + name + "::TracedCallback")
475  .AddTraceSource ("DropAfterDequeue", "Drop a packet after dequeue.",
477  "ns3::" + name + "::TracedCallback")
478  ;
479  return tid;
480 }
481 
482 template <typename Item>
484  : NS_LOG_TEMPLATE_DEFINE ("Queue")
485 {
486 }
487 
488 template <typename Item>
490 {
491 }
492 
493 template <typename Item>
494 bool
496 {
497  Iterator ret;
498  return DoEnqueue (pos, item, ret);
499 }
500 
501 template <typename Item>
502 bool
504 {
505  NS_LOG_FUNCTION (this << item);
506 
507  if (GetCurrentSize () + item > GetMaxSize ())
508  {
509  NS_LOG_LOGIC ("Queue full -- dropping pkt");
510  DropBeforeEnqueue (item);
511  return false;
512  }
513 
514  ret = m_packets.insert (pos, item);
515 
516  uint32_t size = item->GetSize ();
517  m_nBytes += size;
518  m_nTotalReceivedBytes += size;
519 
520  m_nPackets++;
521  m_nTotalReceivedPackets++;
522 
523  NS_LOG_LOGIC ("m_traceEnqueue (p)");
524  m_traceEnqueue (item);
525 
526  return true;
527 }
528 
529 template <typename Item>
530 Ptr<Item>
532 {
533  NS_LOG_FUNCTION (this);
534 
535  if (m_nPackets.Get () == 0)
536  {
537  NS_LOG_LOGIC ("Queue empty");
538  return 0;
539  }
540 
541  Ptr<Item> item = *pos;
542  m_packets.erase (pos);
543 
544  if (item != 0)
545  {
546  NS_ASSERT (m_nBytes.Get () >= item->GetSize ());
547  NS_ASSERT (m_nPackets.Get () > 0);
548 
549  m_nBytes -= item->GetSize ();
550  m_nPackets--;
551 
552  NS_LOG_LOGIC ("m_traceDequeue (p)");
553  m_traceDequeue (item);
554  }
555  return item;
556 }
557 
558 template <typename Item>
559 Ptr<Item>
561 {
562  NS_LOG_FUNCTION (this);
563 
564  if (m_nPackets.Get () == 0)
565  {
566  NS_LOG_LOGIC ("Queue empty");
567  return 0;
568  }
569 
570  Ptr<Item> item = *pos;
571  m_packets.erase (pos);
572 
573  if (item != 0)
574  {
575  NS_ASSERT (m_nBytes.Get () >= item->GetSize ());
576  NS_ASSERT (m_nPackets.Get () > 0);
577 
578  m_nBytes -= item->GetSize ();
579  m_nPackets--;
580 
581  // packets are first dequeued and then dropped
582  NS_LOG_LOGIC ("m_traceDequeue (p)");
583  m_traceDequeue (item);
584 
585  DropAfterDequeue (item);
586  }
587  return item;
588 }
589 
590 template <typename Item>
591 void
593 {
594  NS_LOG_FUNCTION (this);
595  while (!IsEmpty ())
596  {
597  Remove ();
598  }
599 }
600 
601 template <typename Item>
602 void
604 {
605  NS_LOG_FUNCTION (this);
606  m_packets.clear ();
608 }
609 
610 template <typename Item>
613 {
614  NS_LOG_FUNCTION (this);
615 
616  if (m_nPackets.Get () == 0)
617  {
618  NS_LOG_LOGIC ("Queue empty");
619  return 0;
620  }
621 
622  return *pos;
623 }
624 
625 template <typename Item>
627 {
628  return m_packets.cbegin ();
629 }
630 
631 template <typename Item>
633 {
634  return m_packets.begin ();
635 }
636 
637 template <typename Item>
639 {
640  return m_packets.cend ();
641 }
642 
643 template <typename Item>
645 {
646  return m_packets.end ();
647 }
648 
649 template <typename Item>
650 void
652 {
653  NS_LOG_FUNCTION (this << item);
654 
655  m_nTotalDroppedPackets++;
656  m_nTotalDroppedPacketsBeforeEnqueue++;
657  m_nTotalDroppedBytes += item->GetSize ();
658  m_nTotalDroppedBytesBeforeEnqueue += item->GetSize ();
659 
660  NS_LOG_LOGIC ("m_traceDropBeforeEnqueue (p)");
661  m_traceDrop (item);
662  m_traceDropBeforeEnqueue (item);
663 }
664 
665 template <typename Item>
666 void
668 {
669  NS_LOG_FUNCTION (this << item);
670 
671  m_nTotalDroppedPackets++;
672  m_nTotalDroppedPacketsAfterDequeue++;
673  m_nTotalDroppedBytes += item->GetSize ();
674  m_nTotalDroppedBytesAfterDequeue += item->GetSize ();
675 
676  NS_LOG_LOGIC ("m_traceDropAfterDequeue (p)");
677  m_traceDrop (item);
678  m_traceDropAfterDequeue (item);
679 }
680 
681 // The following explicit template instantiation declarations prevent all the
682 // translation units including this header file to implicitly instantiate the
683 // Queue<Packet> class and the Queue<QueueDiscItem> class. The unique instances
684 // of these classes are explicitly created through the macros
685 // NS_OBJECT_TEMPLATE_CLASS_DEFINE (Queue,Packet) and
686 // NS_OBJECT_TEMPLATE_CLASS_DEFINE (Queue,QueueDiscItem), which are included in queue.cc
687 extern template class Queue<Packet>;
688 extern template class Queue<QueueDiscItem>;
689 
690 } // namespace ns3
691 
692 #endif /* QUEUE_H */
A base class which provides memory management and object aggregation.
Definition: object.h:88
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
Introspection did not find any typical Config paths.
Abstract base class for packet Queues.
Definition: queue.h:53
uint32_t GetTotalDroppedPacketsBeforeEnqueue(void) const
Definition: queue.cc:170
uint32_t GetTotalDroppedBytes(void) const
Definition: queue.cc:138
uint32_t m_nTotalDroppedBytesBeforeEnqueue
Total dropped bytes before enqueue.
Definition: queue.h:212
uint32_t GetTotalDroppedPackets(void) const
Definition: queue.cc:162
void ResetStatistics(void)
Resets the counts for dropped packets, dropped bytes, received packets, and received bytes.
Definition: queue.cc:186
TracedValue< uint32_t > m_nPackets
Number of packets in the queue.
Definition: queue.h:209
bool IsEmpty(void) const
Definition: queue.cc:82
uint32_t m_nTotalDroppedPacketsAfterDequeue
Total dropped packets after dequeue.
Definition: queue.h:216
uint32_t m_nTotalReceivedPackets
Total received packets.
Definition: queue.h:210
uint32_t GetNPackets(void) const
Definition: queue.cc:90
uint32_t GetTotalDroppedBytesAfterDequeue(void) const
Definition: queue.cc:154
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 '>'.
Definition: queue.cc:73
virtual ~QueueBase()
Definition: queue.cc:67
uint32_t m_nTotalDroppedBytes
Total dropped bytes.
Definition: queue.h:211
uint32_t m_nTotalDroppedPacketsBeforeEnqueue
Total dropped packets before enqueue.
Definition: queue.h:215
uint32_t GetTotalDroppedBytesBeforeEnqueue(void) const
Definition: queue.cc:146
QueueSize m_maxSize
max queue size
Definition: queue.h:218
uint32_t m_nTotalDroppedPackets
Total dropped packets.
Definition: queue.h:214
uint32_t GetTotalReceivedPackets(void) const
Definition: queue.cc:130
void SetMaxSize(QueueSize size)
Set the maximum size of this queue.
Definition: queue.cc:200
uint32_t GetTotalReceivedBytes(void) const
Definition: queue.cc:122
TracedValue< uint32_t > m_nBytes
Number of bytes in the queue.
Definition: queue.h:207
QueueSize GetCurrentSize(void) const
Definition: queue.cc:106
uint32_t m_nTotalDroppedBytesAfterDequeue
Total dropped bytes after dequeue.
Definition: queue.h:213
uint32_t m_nTotalReceivedBytes
Total received bytes.
Definition: queue.h:208
uint32_t GetTotalDroppedPacketsAfterDequeue(void) const
Definition: queue.cc:178
static TypeId GetTypeId(void)
Get the type ID.
Definition: queue.cc:34
uint32_t GetNBytes(void) const
Definition: queue.cc:98
QueueSize GetMaxSize(void) const
Definition: queue.cc:217
Template class for packet Queues.
Definition: queue.h:254
void DropAfterDequeue(Ptr< Item > item)
Drop a packet after dequeue.
Definition: queue.h:667
void Flush(void)
Flush the queue by calling Remove() on each item enqueued.
Definition: queue.h:592
Item ItemType
Define ItemType as the type of the stored elements.
Definition: queue.h:301
void DropBeforeEnqueue(Ptr< Item > item)
Drop a packet before enqueue.
Definition: queue.h:651
std::list< Ptr< Item > >::iterator Iterator
Iterator.
Definition: queue.h:308
virtual Ptr< Item > Remove(void)=0
Remove an item from the Queue (each subclass defines the position), counting it and tracing it as bot...
TracedCallback< Ptr< const Item > > m_traceEnqueue
Traced callback: fired when a packet is enqueued.
Definition: queue.h:439
std::list< Ptr< Item > > m_packets
the items in the queue
Definition: queue.h:435
Ptr< Item > DoDequeue(ConstIterator pos)
Pull the item to dequeue from the queue.
Definition: queue.h:531
Iterator end(void)
Get an iterator which indicates past-the-last item in the queue.
Definition: queue.h:644
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.
TracedCallback< Ptr< const Item > > m_traceDropAfterDequeue
Traced callback: fired when a packet is dropped after dequeue.
Definition: queue.h:447
TracedCallback< Ptr< const Item > > m_traceDropBeforeEnqueue
Traced callback: fired when a packet is dropped before enqueue.
Definition: queue.h:445
NS_LOG_TEMPLATE_DECLARE
the log component
Definition: queue.h:436
virtual Ptr< Item > Dequeue(void)=0
Remove an item from the Queue (each subclass defines the position), counting it and tracing it as deq...
Queue()
Definition: queue.h:483
Iterator begin(void)
Get an iterator which refers to the first item in the queue.
Definition: queue.h:632
void DoDispose(void) override
Destructor implementation.
Definition: queue.h:603
bool DoEnqueue(ConstIterator pos, Ptr< Item > item)
Push an item in the queue.
Definition: queue.h:495
TracedCallback< Ptr< const Item > > m_traceDequeue
Traced callback: fired when a packet is dequeued.
Definition: queue.h:441
bool DoEnqueue(ConstIterator pos, Ptr< Item > item, Iterator &ret)
Push an item in the queue.
Definition: queue.h:503
virtual bool Enqueue(Ptr< Item > item)=0
Place an item into the Queue (each subclass defines the position)
TracedCallback< Ptr< const Item > > m_traceDrop
Traced callback: fired when a packet is dropped.
Definition: queue.h:443
Ptr< Item > DoRemove(ConstIterator pos)
Pull the item to drop from the queue.
Definition: queue.h:560
Ptr< const Item > DoPeek(ConstIterator pos) const
Peek the front item in the queue.
Definition: queue.h:612
std::list< Ptr< Item > >::const_iterator ConstIterator
Const iterator.
Definition: queue.h:306
ConstIterator begin(void) const
Get a const iterator which refers to the first item in the queue.
Definition: queue.h:626
ConstIterator end(void) const
Get a const iterator which indicates past-the-last item in the queue.
Definition: queue.h:638
static TypeId GetTypeId(void)
Get the type ID.
Definition: queue.h:457
virtual ~Queue()
Definition: queue.h:489
Class for representing queue sizes.
Definition: queue-size.h:95
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
#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
#define NS_LOG_TEMPLATE_DEFINE(name)
Initialize a reference to a Log component.
Definition: log.h:239
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.