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 
173  {
176  };
177 
185  void SetMode (QueueBase::QueueMode mode);
186 
194  QueueBase::QueueMode GetMode (void) const;
195 
203  void SetMaxPackets (uint32_t maxPackets);
204 
210  uint32_t GetMaxPackets (void) const;
211 
219  void SetMaxBytes (uint32_t maxBytes);
220 
226  uint32_t GetMaxBytes (void) const;
227 
235  void SetMaxSize (QueueSize size);
236 
240  QueueSize GetMaxSize (void) const;
241 
242 #if 0
243  // average calculation requires keeping around
244  // a buffer with the date of arrival of past received packets
245  // which are within the average window
246  // so, it is quite costly to do it all the time.
247  // Hence, it is disabled by default and must be explicitly
248  // enabled with this method which specifies the size
249  // of the average window in time units.
250  void EnableRunningAverage (Time averageWindow);
251  void DisableRunningAverage (void);
252  // average
253  double GetQueueSizeAverage (void);
254  double GetReceivedBytesPerSecondAverage (void);
255  double GetReceivedPacketsPerSecondAverage (void);
256  double GetDroppedBytesPerSecondAverage (void);
257  double GetDroppedPacketsPerSecondAverage (void);
258  // variance
259  double GetQueueSizeVariance (void);
260  double GetReceivedBytesPerSecondVariance (void);
261  double GetReceivedPacketsPerSecondVariance (void);
262  double GetDroppedBytesPerSecondVariance (void);
263  double GetDroppedPacketsPerSecondVariance (void);
264 #endif
265 
266 private:
277 
278  uint32_t m_maxPackets;
279  uint32_t m_maxBytes;
281 
283  template <typename Item>
284  friend class Queue;
285 };
286 
287 
311 template <typename Item>
312 class Queue : public QueueBase
313 {
314 public:
319  static TypeId GetTypeId (void);
320 
321  Queue ();
322  virtual ~Queue ();
323 
329  virtual bool Enqueue (Ptr<Item> item) = 0;
330 
336  virtual Ptr<Item> Dequeue (void) = 0;
337 
343  virtual Ptr<Item> Remove (void) = 0;
344 
350  virtual Ptr<const Item> Peek (void) const = 0;
351 
355  void Flush (void);
356 
357 protected:
358 
360  typedef typename std::list<Ptr<Item> >::const_iterator ConstIterator;
361 
376  ConstIterator Head (void) const;
377 
392  ConstIterator Tail (void) const;
393 
400  bool DoEnqueue (ConstIterator pos, Ptr<Item> item);
401 
407  Ptr<Item> DoDequeue (ConstIterator pos);
408 
414  Ptr<Item> DoRemove (ConstIterator pos);
415 
421  Ptr<const Item> DoPeek (ConstIterator pos) const;
422 
431  void DropBeforeEnqueue (Ptr<Item> item);
432 
441  void DropAfterDequeue (Ptr<Item> item);
442 
443 private:
444  std::list<Ptr<Item> > m_packets;
446 
457 };
458 
459 
464 template <typename Item>
465 TypeId
467 {
468  std::string name = GetTypeParamName<Queue<Item> > ();
469  static TypeId tid = TypeId (("ns3::Queue<" + name + ">").c_str ())
470  .SetParent<QueueBase> ()
471  .SetGroupName ("Network")
472  .AddTraceSource ("Enqueue", "Enqueue a packet in the queue.",
474  "ns3::" + name + "::TracedCallback")
475  .AddTraceSource ("Dequeue", "Dequeue a packet from the queue.",
477  "ns3::" + name + "::TracedCallback")
478  .AddTraceSource ("Drop", "Drop a packet (for whatever reason).",
480  "ns3::" + name + "::TracedCallback")
481  .AddTraceSource ("DropBeforeEnqueue", "Drop a packet before enqueue.",
483  "ns3::" + name + "::TracedCallback")
484  .AddTraceSource ("DropAfterDequeue", "Drop a packet after dequeue.",
486  "ns3::" + name + "::TracedCallback")
487  ;
488  return tid;
489 }
490 
491 template <typename Item>
493  : NS_LOG_TEMPLATE_DEFINE ("Queue")
494 {
495 }
496 
497 template <typename Item>
499 {
500 }
501 
502 template <typename Item>
503 bool
505 {
506  NS_LOG_FUNCTION (this << item);
507 
508  if (GetCurrentSize () + item > GetMaxSize ())
509  {
510  NS_LOG_LOGIC ("Queue full -- dropping pkt");
511  DropBeforeEnqueue (item);
512  return false;
513  }
514 
515  m_packets.insert (pos, item);
516 
517  uint32_t size = item->GetSize ();
518  m_nBytes += size;
519  m_nTotalReceivedBytes += size;
520 
521  m_nPackets++;
522  m_nTotalReceivedPackets++;
523 
524  NS_LOG_LOGIC ("m_traceEnqueue (p)");
525  m_traceEnqueue (item);
526 
527  return true;
528 }
529 
530 template <typename Item>
531 Ptr<Item>
533 {
534  NS_LOG_FUNCTION (this);
535 
536  if (m_nPackets.Get () == 0)
537  {
538  NS_LOG_LOGIC ("Queue empty");
539  return 0;
540  }
541 
542  Ptr<Item> item = *pos;
543  m_packets.erase (pos);
544 
545  if (item != 0)
546  {
547  NS_ASSERT (m_nBytes.Get () >= item->GetSize ());
548  NS_ASSERT (m_nPackets.Get () > 0);
549 
550  m_nBytes -= item->GetSize ();
551  m_nPackets--;
552 
553  NS_LOG_LOGIC ("m_traceDequeue (p)");
554  m_traceDequeue (item);
555  }
556  return item;
557 }
558 
559 template <typename Item>
560 Ptr<Item>
562 {
563  NS_LOG_FUNCTION (this);
564 
565  if (m_nPackets.Get () == 0)
566  {
567  NS_LOG_LOGIC ("Queue empty");
568  return 0;
569  }
570 
571  Ptr<Item> item = *pos;
572  m_packets.erase (pos);
573 
574  if (item != 0)
575  {
576  NS_ASSERT (m_nBytes.Get () >= item->GetSize ());
577  NS_ASSERT (m_nPackets.Get () > 0);
578 
579  m_nBytes -= item->GetSize ();
580  m_nPackets--;
581 
582  // packets are first dequeued and then dropped
583  NS_LOG_LOGIC ("m_traceDequeue (p)");
584  m_traceDequeue (item);
585 
586  DropAfterDequeue (item);
587  }
588  return item;
589 }
590 
591 template <typename Item>
592 void
594 {
595  NS_LOG_FUNCTION (this);
596  while (!IsEmpty ())
597  {
598  Remove ();
599  }
600 }
601 
602 template <typename Item>
605 {
606  NS_LOG_FUNCTION (this);
607 
608  if (m_nPackets.Get () == 0)
609  {
610  NS_LOG_LOGIC ("Queue empty");
611  return 0;
612  }
613 
614  return *pos;
615 }
616 
617 template <typename Item>
619 {
620  return m_packets.cbegin ();
621 }
622 
623 template <typename Item>
625 {
626  return m_packets.cend ();
627 }
628 
629 template <typename Item>
630 void
632 {
633  NS_LOG_FUNCTION (this << item);
634 
635  m_nTotalDroppedPackets++;
636  m_nTotalDroppedPacketsBeforeEnqueue++;
637  m_nTotalDroppedBytes += item->GetSize ();
638  m_nTotalDroppedBytesBeforeEnqueue += item->GetSize ();
639 
640  NS_LOG_LOGIC ("m_traceDropBeforeEnqueue (p)");
641  m_traceDrop (item);
642  m_traceDropBeforeEnqueue (item);
643 }
644 
645 template <typename Item>
646 void
648 {
649  NS_LOG_FUNCTION (this << item);
650 
651  m_nTotalDroppedPackets++;
652  m_nTotalDroppedPacketsAfterDequeue++;
653  m_nTotalDroppedBytes += item->GetSize ();
654  m_nTotalDroppedBytesAfterDequeue += item->GetSize ();
655 
656  NS_LOG_LOGIC ("m_traceDropAfterDequeue (p)");
657  m_traceDrop (item);
658  m_traceDropAfterDequeue (item);
659 }
660 
661 } // namespace ns3
662 
663 #endif /* QUEUE_H */
uint32_t m_nTotalDroppedPacketsBeforeEnqueue
Total dropped packets before enqueue.
Definition: queue.h:275
uint32_t m_nTotalReceivedPackets
Total received packets.
Definition: queue.h:270
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
uint32_t m_nTotalDroppedPackets
Total dropped packets.
Definition: queue.h:274
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
ConstIterator Head(void) const
Get a const iterator which refers to the first item in the queue.
Definition: queue.h:618
TracedCallback< Ptr< const Item > > m_traceDequeue
Traced callback: fired when a packet is dequeued.
Definition: queue.h:450
Class for representing queue sizes.
Definition: queue-size.h:94
uint32_t m_maxBytes
max bytes in the queue
Definition: queue.h:279
NS_LOG_TEMPLATE_DECLARE
the log component
Definition: queue.h:445
Forward calls to a chain of Callback.
void Flush(void)
Flush the queue.
Definition: queue.h:593
uint32_t m_nTotalDroppedBytes
Total dropped bytes.
Definition: queue.h:271
#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:276
uint32_t m_nTotalDroppedBytesAfterDequeue
Total dropped bytes after dequeue.
Definition: queue.h:273
Ptr< Item > DoRemove(ConstIterator pos)
Pull the item to drop from the queue.
Definition: queue.h:561
uint32_t GetTotalDroppedPacketsBeforeEnqueue(void) const
Definition: queue.cc:201
TracedCallback< Ptr< const Item > > m_traceDrop
Traced callback: fired when a packet is dropped.
Definition: queue.h:452
QueueBase::QueueMode GetMode(void) const
Get the operating mode of this device.
Definition: queue.cc:250
TracedValue< uint32_t > m_nPackets
Number of packets in the queue.
Definition: queue.h:269
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:104
QueueSize m_maxSize
max queue size
Definition: queue.h:280
Use number of packets for maximum queue size.
Definition: queue.h:174
virtual ~QueueBase()
Definition: queue.cc:98
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:466
TracedCallback< Ptr< const Item > > m_traceDropAfterDequeue
Traced callback: fired when a packet is dropped after dequeue.
Definition: queue.h:456
uint32_t GetNBytes(void) const
Definition: queue.cc:129
uint32_t m_maxPackets
max packets in the queue
Definition: queue.h:278
void SetMode(QueueBase::QueueMode mode)
Set the operating mode of this device.
Definition: queue.cc:231
uint32_t m_nTotalDroppedBytesBeforeEnqueue
Total dropped bytes before enqueue.
Definition: queue.h:272
#define NS_LOG_TEMPLATE_DEFINE(name)
Initialize a reference to a Log component.
Definition: log.h:235
QueueSize GetCurrentSize(void) const
Definition: queue.cc:137
TracedCallback< Ptr< const Item > > m_traceEnqueue
Traced callback: fired when a packet is enqueued.
Definition: queue.h:448
void SetMaxPackets(uint32_t maxPackets)
Set the maximum amount of packets that can be stored in this queue.
Definition: queue.cc:257
void DropAfterDequeue(Ptr< Item > item)
Drop a packet after dequeue.
Definition: queue.h:647
uint32_t GetMaxPackets(void) const
Definition: queue.cc:270
ConstIterator Tail(void) const
Get a const iterator which indicates past-the-last item in the queue.
Definition: queue.h:624
uint32_t GetTotalDroppedBytes(void) const
Definition: queue.cc:169
uint32_t m_nTotalReceivedBytes
Total received bytes.
Definition: queue.h:268
void ResetStatistics(void)
Resets the counts for dropped packets, dropped bytes, received packets, and received bytes...
Definition: queue.cc:217
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.
virtual Ptr< Item > Remove(void)=0
Remove an item from the Queue (each subclass defines the position), counting it as dropped...
uint32_t GetTotalDroppedBytesAfterDequeue(void) const
Definition: queue.cc:185
Queue()
Definition: queue.h:492
void SetMaxBytes(uint32_t maxBytes)
Set the maximum amount of bytes that can be stored in this queue.
Definition: queue.cc:277
Use number of bytes for maximum queue size.
Definition: queue.h:175
TracedCallback< Ptr< const Item > > m_traceDropBeforeEnqueue
Traced callback: fired when a packet is dropped before enqueue.
Definition: queue.h:454
uint32_t GetTotalReceivedPackets(void) const
Definition: queue.cc:161
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
uint32_t GetMaxBytes(void) const
Definition: queue.cc:290
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...
Ptr< const Item > DoPeek(ConstIterator pos) const
Peek the front item in the queue.
Definition: queue.h:604
void DropBeforeEnqueue(Ptr< Item > item)
Drop a packet before enqueue.
Definition: queue.h:631
bool IsEmpty(void) const
Definition: queue.cc:113
uint32_t GetNPackets(void) const
Definition: queue.cc:121
uint32_t GetTotalDroppedPacketsAfterDequeue(void) const
Definition: queue.cc:209
uint32_t GetTotalDroppedPackets(void) const
Definition: queue.cc:193
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
void SetMaxSize(QueueSize size)
Set the maximum size of this queue.
Definition: queue.cc:297
QueueMode
Enumeration of the modes supported in the class.
Definition: queue.h:172
Ptr< Item > DoDequeue(ConstIterator pos)
Pull the item to dequeue from the queue.
Definition: queue.h:532
a unique identifier for an interface.
Definition: type-id.h:58
uint32_t GetTotalReceivedBytes(void) const
Definition: queue.cc:153
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:914
bool DoEnqueue(ConstIterator pos, Ptr< Item > item)
Push an item in the queue.
Definition: queue.h:504
std::list< Ptr< Item > >::const_iterator ConstIterator
Const iterator.
Definition: queue.h:360
std::list< Ptr< Item > > m_packets
the items in the queue
Definition: queue.h:444
QueueSize GetMaxSize(void) const
Definition: queue.cc:323
static TypeId GetTypeId(void)
Get the type ID.
Definition: queue.cc:33
uint32_t GetTotalDroppedBytesBeforeEnqueue(void) const
Definition: queue.cc:177
virtual ~Queue()
Definition: queue.h:498
TracedValue< uint32_t > m_nBytes
Number of bytes in the queue.
Definition: queue.h:267