OLD | NEW |
(Empty) | |
| 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2016 Universita' degli Studi di Napoli Federico II |
| 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 * Authors: Pasquale Imputato <p.imputato@gmail.com> |
| 19 * Stefano Avallone <stefano.avallone@unina.it> |
| 20 * |
| 21 * This code is a port of the dynamic queue limits library implemented |
| 22 * in the Linux kernel by |
| 23 * Author: Tom Herbert <therbert@google.com> |
| 24 */ |
| 25 |
| 26 #include "ns3/log.h" |
| 27 #include "ns3/uinteger.h" |
| 28 #include "ns3/simulator.h" |
| 29 #include "ns3/string.h" |
| 30 #include "dynamic-queue-limits.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 NS_LOG_COMPONENT_DEFINE ("DynamicQueueLimits"); |
| 40 |
| 41 NS_OBJECT_ENSURE_REGISTERED (DynamicQueueLimits); |
| 42 |
| 43 TypeId |
| 44 DynamicQueueLimits::GetTypeId (void) |
| 45 { |
| 46 static TypeId tid = TypeId ("ns3::DynamicQueueLimits") |
| 47 .SetParent<Object> () |
| 48 .SetParent<QueueLimits> () |
| 49 .SetGroupName ("Network") |
| 50 .AddConstructor<DynamicQueueLimits> () |
| 51 .AddAttribute ("HoldTime", |
| 52 "The DQL algorithm hold time", |
| 53 StringValue ("4ms"), // 1/HZ |
| 54 MakeTimeAccessor (&DynamicQueueLimits::m_slackHoldTime), |
| 55 MakeTimeChecker ()) |
| 56 .AddAttribute ("MaxLimit", |
| 57 "Maximum limit", |
| 58 UintegerValue (DQL_MAX_LIMIT), |
| 59 MakeUintegerAccessor (&DynamicQueueLimits::m_maxLimit), |
| 60 MakeUintegerChecker<uint32_t> (0, DQL_MAX_LIMIT)) |
| 61 .AddAttribute ("MinLimit", |
| 62 "Minimum limit", |
| 63 UintegerValue (0), |
| 64 MakeUintegerAccessor (&DynamicQueueLimits::m_minLimit), |
| 65 MakeUintegerChecker<uint32_t> ()) |
| 66 .AddTraceSource ("Limit", |
| 67 "Limit value calculated by DQL", |
| 68 MakeTraceSourceAccessor (&DynamicQueueLimits::m_limit), |
| 69 "ns3::TracedValueCallback::Uint32") |
| 70 ; |
| 71 return tid; |
| 72 } |
| 73 |
| 74 DynamicQueueLimits::DynamicQueueLimits () |
| 75 { |
| 76 NS_LOG_FUNCTION (this); |
| 77 Reset (); |
| 78 } |
| 79 |
| 80 DynamicQueueLimits::~DynamicQueueLimits () |
| 81 { |
| 82 NS_LOG_FUNCTION (this); |
| 83 } |
| 84 |
| 85 void |
| 86 DynamicQueueLimits::Reset () |
| 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; |
| 97 m_lowestSlack = UINTMAX; |
| 98 m_slackStartTime = Simulator::Now (); |
| 99 } |
| 100 |
| 101 void |
| 102 DynamicQueueLimits::Completed (uint32_t count) |
| 103 { |
| 104 NS_LOG_FUNCTION (this << count); |
| 105 uint32_t inprogress, prevInprogress, limit; |
| 106 uint32_t ovlimit, completed, numQueued; |
| 107 bool allPrevCompleted; |
| 108 |
| 109 numQueued = m_numQueued; |
| 110 |
| 111 // Can't complete more than what's in queue |
| 112 NS_ASSERT (count <= numQueued - m_numCompleted); |
| 113 |
| 114 completed = m_numCompleted + count; |
| 115 limit = m_limit; |
| 116 ovlimit = Posdiff (numQueued - m_numCompleted, limit); |
| 117 inprogress = numQueued - completed; |
| 118 prevInprogress = m_prevNumQueued - m_numCompleted; |
| 119 allPrevCompleted = ((int32_t)(completed - m_prevNumQueued)) >= 0; |
| 120 |
| 121 if ((ovlimit && !inprogress) || (m_prevOvlimit && allPrevCompleted)) |
| 122 { |
| 123 NS_LOG_DEBUG ("Queue starved, increase limit"); |
| 124 /* |
| 125 * Queue considered starved if: |
| 126 * - The queue was over-limit in the last interval, |
| 127 * and there is no more data in the queue. |
| 128 * OR |
| 129 * - The queue was over-limit in the previous interval and |
| 130 * when enqueuing it was possible that all queued data |
| 131 * had been consumed. This covers the case when queue |
| 132 * may have becomes starved between completion processing |
| 133 * running and next time enqueue was scheduled. |
| 134 * |
| 135 * When queue is starved increase the limit by the amount |
| 136 * of bytes both sent and completed in the last interval, |
| 137 * plus any previous over-limit. |
| 138 */ |
| 139 limit += Posdiff (completed, m_prevNumQueued) + m_prevOvlimit; |
| 140 m_slackStartTime = Simulator::Now (); |
| 141 m_lowestSlack = UINTMAX; |
| 142 } |
| 143 else if (inprogress && prevInprogress && !allPrevCompleted) |
| 144 { |
| 145 NS_LOG_DEBUG ("Queue not starved, check decrease limit"); |
| 146 /* |
| 147 * Queue was not starved, check if the limit can be decreased. |
| 148 * A decrease is only considered if the queue has been busy in |
| 149 * the whole interval (the check above). |
| 150 * |
| 151 * If there is slack, the amount of execess data queued above |
| 152 * the the amount needed to prevent starvation, the queue limit |
| 153 * can be decreased. To avoid hysteresis we consider the |
| 154 * minimum amount of slack found over several iterations of the |
| 155 * completion routine. |
| 156 */ |
| 157 uint32_t slack, slackLastObjs; |
| 158 |
| 159 /* |
| 160 * Slack is the maximum of |
| 161 * - The queue limit plus previous over-limit minus twice |
| 162 * the number of objects completed. Note that two times |
| 163 * number of completed bytes is a basis for an upper bound |
| 164 * of the limit. |
| 165 * - Portion of objects in the last queuing operation that |
| 166 * was not part of non-zero previous over-limit. That is |
| 167 * "round down" by non-overlimit portion of the last |
| 168 * queueing operation. |
| 169 */ |
| 170 slack = Posdiff (limit + m_prevOvlimit, 2 * (completed - m_numCompleted)); |
| 171 slackLastObjs = m_prevOvlimit ? Posdiff (m_prevLastObjCnt, m_prevOvlimit)
: 0; |
| 172 |
| 173 slack = std::max (slack, slackLastObjs); |
| 174 |
| 175 if (slack < m_lowestSlack) |
| 176 { |
| 177 m_lowestSlack = slack; |
| 178 } |
| 179 |
| 180 if (Simulator::Now () > (m_slackStartTime + m_slackHoldTime)) |
| 181 { |
| 182 limit = Posdiff (limit, m_lowestSlack); |
| 183 m_slackStartTime = Simulator::Now (); |
| 184 m_lowestSlack = UINTMAX; |
| 185 } |
| 186 } |
| 187 |
| 188 // Enforce bounds on limit |
| 189 limit = std::min ((uint32_t)std::max (limit, m_minLimit), m_maxLimit); |
| 190 |
| 191 if (limit != m_limit) |
| 192 { |
| 193 NS_LOG_DEBUG ("Update limit"); |
| 194 m_limit = limit; |
| 195 ovlimit = 0; |
| 196 } |
| 197 |
| 198 m_adjLimit = limit + completed; |
| 199 m_prevOvlimit = ovlimit; |
| 200 m_prevLastObjCnt = m_lastObjCnt; |
| 201 m_numCompleted = completed; |
| 202 m_prevNumQueued = numQueued; |
| 203 } |
| 204 |
| 205 int32_t |
| 206 DynamicQueueLimits::Available () const |
| 207 { |
| 208 NS_LOG_FUNCTION (this); |
| 209 return (m_adjLimit - m_numQueued); |
| 210 } |
| 211 |
| 212 void |
| 213 DynamicQueueLimits::Queued (uint32_t count) |
| 214 { |
| 215 NS_LOG_FUNCTION (this << count); |
| 216 NS_ASSERT (count <= DQL_MAX_OBJECT); |
| 217 |
| 218 m_lastObjCnt = count; |
| 219 m_numQueued += count; |
| 220 } |
| 221 |
| 222 int32_t |
| 223 DynamicQueueLimits::Posdiff (int32_t a, int32_t b) |
| 224 { |
| 225 NS_LOG_FUNCTION (this << a << b); |
| 226 return std::max ((a - b), 0); |
| 227 } |
| 228 |
| 229 } // namespace ns3 |
OLD | NEW |