A Discrete-Event Network Simulator
API
dynamic-queue-limits.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Universita' degli Studi di Napoli Federico II
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  * Authors: Pasquale Imputato <p.imputato@gmail.com>
18  * Stefano Avallone <stefano.avallone@unina.it>
19  *
20  * This code is a port of the dynamic queue limits library implemented
21  * in the Linux kernel by
22  * Author: Tom Herbert <therbert@google.com>
23  */
24 
25 #include "dynamic-queue-limits.h"
26 
27 #include "ns3/log.h"
28 #include "ns3/simulator.h"
29 #include "ns3/string.h"
30 #include "ns3/uinteger.h"
31 
32 // Set some static maximums
33 static const uint32_t UINTMAX = std::numeric_limits<uint32_t>::max();
34 static const uint32_t DQL_MAX_OBJECT = UINTMAX / 16;
35 static const uint32_t DQL_MAX_LIMIT = (UINTMAX / 2) - DQL_MAX_OBJECT;
36 
37 namespace ns3
38 {
39 
40 NS_LOG_COMPONENT_DEFINE("DynamicQueueLimits");
41 
42 NS_OBJECT_ENSURE_REGISTERED(DynamicQueueLimits);
43 
44 TypeId
46 {
47  static TypeId tid = TypeId("ns3::DynamicQueueLimits")
48  .SetParent<Object>()
49  .SetParent<QueueLimits>()
50  .SetGroupName("Network")
51  .AddConstructor<DynamicQueueLimits>()
52  .AddAttribute("HoldTime",
53  "The DQL algorithm hold time",
54  StringValue("1s"),
57  .AddAttribute("MaxLimit",
58  "Maximum limit",
61  MakeUintegerChecker<uint32_t>(0, DQL_MAX_LIMIT))
62  .AddAttribute("MinLimit",
63  "Minimum limit",
64  UintegerValue(0),
66  MakeUintegerChecker<uint32_t>())
67  .AddTraceSource("Limit",
68  "Limit value calculated by DQL",
70  "ns3::TracedValueCallback::Uint32");
71  return tid;
72 }
73 
75 {
76  NS_LOG_FUNCTION(this);
77  Reset();
78 }
79 
81 {
82  NS_LOG_FUNCTION(this);
83 }
84 
85 void
87 {
88  NS_LOG_FUNCTION(this);
89  // Reset all dynamic values
90  m_limit = 0;
91  m_numQueued = 0;
92  m_numCompleted = 0;
93  m_lastObjCnt = 0;
94  m_prevNumQueued = 0;
95  m_prevLastObjCnt = 0;
96  m_prevOvlimit = 0;
99 }
100 
101 void
103 {
104  NS_LOG_FUNCTION(this << count);
105  uint32_t inprogress;
106  uint32_t prevInprogress;
107  uint32_t limit;
108  uint32_t ovlimit;
109  uint32_t completed;
110  uint32_t numQueued;
111  bool allPrevCompleted;
112 
113  numQueued = m_numQueued;
114 
115  // Can't complete more than what's in queue
116  NS_ASSERT(count <= numQueued - m_numCompleted);
117 
118  completed = m_numCompleted + count;
119  limit = m_limit;
120  ovlimit = Posdiff(numQueued - m_numCompleted, limit);
121  inprogress = numQueued - completed;
122  prevInprogress = m_prevNumQueued - m_numCompleted;
123  allPrevCompleted = ((int32_t)(completed - m_prevNumQueued)) >= 0;
124 
125  if ((ovlimit && !inprogress) || (m_prevOvlimit && allPrevCompleted))
126  {
127  NS_LOG_DEBUG("Queue starved, increase limit");
128  /*
129  * Queue considered starved if:
130  * - The queue was over-limit in the last interval,
131  * and there is no more data in the queue.
132  * OR
133  * - The queue was over-limit in the previous interval and
134  * when enqueuing it was possible that all queued data
135  * had been consumed. This covers the case when queue
136  * may have becomes starved between completion processing
137  * running and next time enqueue was scheduled.
138  *
139  * When queue is starved increase the limit by the amount
140  * of bytes both sent and completed in the last interval,
141  * plus any previous over-limit.
142  */
143  limit += Posdiff(completed, m_prevNumQueued) + m_prevOvlimit;
146  }
147  else if (inprogress && prevInprogress && !allPrevCompleted)
148  {
149  NS_LOG_DEBUG("Queue not starved, check decrease limit");
150  /*
151  * Queue was not starved, check if the limit can be decreased.
152  * A decrease is only considered if the queue has been busy in
153  * the whole interval (the check above).
154  *
155  * If there is slack, the amount of execess data queued above
156  * the the amount needed to prevent starvation, the queue limit
157  * can be decreased. To avoid hysteresis we consider the
158  * minimum amount of slack found over several iterations of the
159  * completion routine.
160  */
161  uint32_t slack;
162  uint32_t slackLastObjs;
163 
164  /*
165  * Slack is the maximum of
166  * - The queue limit plus previous over-limit minus twice
167  * the number of objects completed. Note that two times
168  * number of completed bytes is a basis for an upper bound
169  * of the limit.
170  * - Portion of objects in the last queuing operation that
171  * was not part of non-zero previous over-limit. That is
172  * "round down" by non-overlimit portion of the last
173  * queueing operation.
174  */
175  slack = Posdiff(limit + m_prevOvlimit, 2 * (completed - m_numCompleted));
176  slackLastObjs = m_prevOvlimit ? Posdiff(m_prevLastObjCnt, m_prevOvlimit) : 0;
177 
178  slack = std::max(slack, slackLastObjs);
179 
180  if (slack < m_lowestSlack)
181  {
182  m_lowestSlack = slack;
183  }
184 
186  {
187  limit = Posdiff(limit, m_lowestSlack);
190  }
191  }
192 
193  // Enforce bounds on limit
194  limit = std::min((uint32_t)std::max(limit, m_minLimit), m_maxLimit);
195 
196  if (limit != m_limit)
197  {
198  NS_LOG_DEBUG("Update limit");
199  m_limit = limit;
200  ovlimit = 0;
201  }
202 
203  m_adjLimit = limit + completed;
204  m_prevOvlimit = ovlimit;
206  m_numCompleted = completed;
207  m_prevNumQueued = numQueued;
208 }
209 
210 int32_t
212 {
213  NS_LOG_FUNCTION(this);
214  return (m_adjLimit - m_numQueued);
215 }
216 
217 void
219 {
220  NS_LOG_FUNCTION(this << count);
221  NS_ASSERT(count <= DQL_MAX_OBJECT);
222 
223  m_lastObjCnt = count;
224  m_numQueued += count;
225 }
226 
227 int32_t
228 DynamicQueueLimits::Posdiff(int32_t a, int32_t b)
229 {
230  NS_LOG_FUNCTION(this << a << b);
231  return std::max((a - b), 0);
232 }
233 
234 } // namespace ns3
#define min(a, b)
Definition: 80211b.c:41
#define max(a, b)
Definition: 80211b.c:42
DynamicQueueLimits would be used in conjunction with a producer/consumer type queue (possibly a netde...
uint32_t m_adjLimit
limit + num_completed
void Reset() override
Reset queue limits state.
uint32_t m_numCompleted
Total ever completed.
Time m_slackStartTime
Time slacks seen.
uint32_t m_numQueued
Total ever queued.
uint32_t m_prevOvlimit
Previous over limit.
uint32_t m_minLimit
Minimum limit.
Time m_slackHoldTime
Time to measure slack.
uint32_t m_prevNumQueued
Previous queue total.
uint32_t m_lastObjCnt
Count at last queuing.
uint32_t m_prevLastObjCnt
Previous queuing cnt.
static TypeId GetTypeId()
Get the type ID.
void Completed(uint32_t count) override
Record number of completed bytes and recalculate the limit.
int32_t Posdiff(int32_t a, int32_t b)
Calculates the difference between the two operators and returns the number if positive,...
uint32_t m_lowestSlack
Lowest slack found.
int32_t Available() const override
Available is called from NotifyTransmittedBytes to calculate the number of bytes that can be passed a...
TracedValue< uint32_t > m_limit
Current limit.
void Queued(uint32_t count) override
Record the number of bytes queued.
uint32_t m_maxLimit
Max limit.
A base class which provides memory management and object aggregation.
Definition: object.h:89
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
Hold variables of type string.
Definition: string.h:56
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
Hold an unsigned integer type.
Definition: uinteger.h:45
static const uint32_t DQL_MAX_LIMIT
static const uint32_t UINTMAX
static const uint32_t DQL_MAX_OBJECT
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
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.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1414
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:533
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:46