-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpid-queue-disc.cc
323 lines (258 loc) · 12.9 KB
/
pid-queue-disc.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) October 2021 : R. Olusegun Alli-Oke
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: R. Olusegun Alli-Oke <[email protected]>, <[email protected]>
*/
//qDisc size is same as internal queue size because qDisc has only one internal queue. So, "this->GetCurrentSize();" (via qDisc class) is equivalent to "this->GetInternalQueue (0)-> GetCurrentSize(); " (via Queuebase).
#include <fstream> //file streams
#include <iostream> //standard i/o streams
#include "ns3/log.h"
#include "ns3/enum.h"
#include "ns3/uinteger.h"
#include "ns3/double.h"
#include "ns3/simulator.h"
#include "ns3/abort.h"
#include "pid-queue-disc.h" //also for ->GetNetDeviceQueueInterface ()
#include "ns3/drop-tail-queue.h"
#include "ns3/net-device-queue-interface.h" //for ->GetTxQueue
#include "ns3/trace-helper.h"
#include "ns3/pointer.h"
namespace ns3
{
NS_LOG_COMPONENT_DEFINE ("PidQueueDisc");
NS_OBJECT_ENSURE_REGISTERED (PidQueueDisc);
TypeId PidQueueDisc::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::PidQueueDisc")
.SetParent<QueueDisc> ()
.SetGroupName ("TrafficControl")
.AddConstructor<PidQueueDisc> ()
.AddAttribute ("a",
"Value of a",
DoubleValue (1.0),
MakeDoubleAccessor (&PidQueueDisc::m_a),
MakeDoubleChecker<double> ())
.AddAttribute ("b",
"Value of b",
DoubleValue (0.0),
MakeDoubleAccessor (&PidQueueDisc::m_b),
MakeDoubleChecker<double> ())
.AddAttribute ("c",
"Value of c",
DoubleValue (0.0),
MakeDoubleAccessor (&PidQueueDisc::m_c),
MakeDoubleChecker<double> ())
.AddAttribute ("d",
"Value of d",
DoubleValue (0.0),
MakeDoubleAccessor (&PidQueueDisc::m_d),
MakeDoubleChecker<double> ())
.AddAttribute ("e",
"Value of e",
DoubleValue (0.0),
MakeDoubleAccessor (&PidQueueDisc::m_e),
MakeDoubleChecker<double> ())
.AddAttribute ("u0",
"Value of u0",
DoubleValue (0.0),
MakeDoubleAccessor (&PidQueueDisc::m_u0),
MakeDoubleChecker<double> ())
.AddAttribute ("Sstep",
"Start time to step-change in desired queue-length",
TimeValue (Seconds (0.0)),
MakeTimeAccessor (&PidQueueDisc::m_sStep),
MakeTimeChecker ())
.AddAttribute ("Supdate",
"Start time to calculate drop probability",
TimeValue (Seconds (0.0)),
MakeTimeAccessor (&PidQueueDisc::m_sUpdate),
MakeTimeChecker ())
.AddAttribute ("Tupdate",
"Time period to calculate drop probability",
TimeValue (Seconds (0.0)),
MakeTimeAccessor (&PidQueueDisc::m_tUpdate),
MakeTimeChecker ())
.AddAttribute ("MaxSize",
"The maximum number of packets accepted by this queue disc",
QueueSizeValue (QueueSize ("1p")),
MakeQueueSizeAccessor (&PidQueueDisc::m_queueLimit),
MakeQueueSizeChecker ())
.AddAttribute ("QueueSizeReferenceEQ",
"Equilibrium queue size used to approximate Model",
QueueSizeValue (QueueSize ("1p")),
MakeQueueSizeAccessor (&PidQueueDisc::m_QsizeRefEQ),
MakeQueueSizeChecker ())
.AddAttribute ("QueueSizeReferenceDQ",
"Desired queue size",
QueueSizeValue (QueueSize ("1p")),
MakeQueueSizeAccessor (&PidQueueDisc::m_QsizeRefDQ),
MakeQueueSizeChecker ())
;
return tid;
}
// ------------------------------------------------- KEY METHODS----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------
// KEY METHOD 0: CONSTRUCTOR ( step a)
PidQueueDisc::PidQueueDisc ()
: QueueDisc (QueueDiscSizePolicy::SINGLE_INTERNAL_QUEUE)
{
NS_LOG_FUNCTION (this);
m_uv = CreateObject<UniformRandomVariable> ();
//int64_t strmm = 123456; m_uv->SetStream (strmm); // globally done via "RngSeedManager::SetSeed(1)" in pid-script.cc
std::cout << "\t\trng stream: " << m_uv->GetStream () << "\n" << std::endl;
}
// KEY METHOD 1: Check Configuration (step b)
bool PidQueueDisc::CheckConfig (void)
{
NS_LOG_FUNCTION (this);
if (GetNQueueDiscClasses () > 0)
{
NS_LOG_ERROR ("PidQueueDisc cannot have classes");
return false;
}
if (GetNPacketFilters () > 0)
{
NS_LOG_ERROR ("PidQueueDisc cannot have packet filters");
return false;
}
if (GetNInternalQueues () == 0)
{
AddInternalQueue (CreateObjectWithAttributes<DropTailQueue<QueueDiscItem> > ("MaxSize", QueueSizeValue (m_queueLimit)));
}
if (GetNInternalQueues () != 1)
{
NS_LOG_ERROR ("PidQueueDisc needs 1 internal queue");
return false;
}
return true;
}
// KEY METHOD 2: Intialize Parameters of PID (step c)
void PidQueueDisc::InitializeParams (void)
{
// Initially queue is empty so variables are initialize to zero.
m_dropProb = -1; // ensures that there is no drop when in open loop.
m_dropProb1 = 0; //since it is linearized controller (ie du0).
m_dropProb2 = 0;
m_ErrQsize1 = 0;
m_ErrQsize2 = 0;
m_QsizeRef = m_QsizeRefEQ;
std::cout << "linearization equilibrium queue-length (q0): " << m_QsizeRef.GetValue() << std::endl;
m_rtrsEvent1 = Simulator::Schedule (m_sStep, &PidQueueDisc::QSizeRefUpdate, this); //effects step-change from linearization equlibrium point (q0) to desired reference queue-length (qref)
m_rtrsEvent2 = Simulator::Schedule (m_sUpdate, &PidQueueDisc::CalculateP, this); //starts the computation of drop-ratio method (CalculateP) i.e. initiates the closed-loop control
}
// KEY METHOD 3: DoEnqueue (step d)
bool PidQueueDisc::DoEnqueue (Ptr<QueueDiscItem> item)
{
NS_LOG_FUNCTION (this << item);
QueueSize nQueued = this->GetCurrentSize();
bool bv =DropEarly (item);
if (nQueued == m_queueLimit)
{
//Drops due to queue limit: reactive --> drops packet if qdisc-queue size exceeds queue limit
DropBeforeEnqueue (item, FORCED_DROP);
return false;
}
else if (bv)
{
//Early probability drop: proactive --> calls the DropEarly sub-method --> which uses m_dropProb from the CalculateP sub-method
DropBeforeEnqueue (item, UNFORCED_DROP);
return false;
}
//No Drop
bool retval = GetInternalQueue (0)->Enqueue (item);
//If Queue::Enqueue fails, QueueDisc::DropBeforeEnqueue is called by the internal queue because QueueDisc::AddInternalQueue sets the trace callback
NS_LOG_LOGIC ("\t bytesInQueue " << GetInternalQueue (0)->GetNBytes ());
NS_LOG_LOGIC ("\t packetsInQueue " << GetInternalQueue (0)->GetNPackets ());
return retval;
}
// KEY METHOD 4: DoDequeue (step e)
Ptr<QueueDiscItem> PidQueueDisc::DoDequeue ()
{
NS_LOG_FUNCTION (this);
if (GetInternalQueue (0)->IsEmpty ())
{
NS_LOG_LOGIC ("Queue empty");
return 0;
}
Ptr<QueueDiscItem> item = GetInternalQueue (0)->Dequeue ();
return item;
}
// KEY MWTHOD 5: DoPeek (Omitted, optional)
// KEY METHOD 6: Dispose (step ?)
void PidQueueDisc::DoDispose (void)
{
NS_LOG_FUNCTION (this);
m_uv = 0;
Simulator::Remove (m_rtrsEvent1);
Simulator::Remove (m_rtrsEvent2);
QueueDisc::DoDispose ();
}
// KEY METHOD 7: DECONSTRUCTOR (step f)
PidQueueDisc::~PidQueueDisc ()
{
NS_LOG_FUNCTION (this);
}
// ------------------------------------------------- SUB-METHODS----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------
//SUB-METHODS for the KEY METHODS
bool PidQueueDisc::DropEarly (Ptr<QueueDiscItem> item)
{
NS_LOG_FUNCTION (this << item);
double p = m_dropProb;
double v = m_uv->GetValue ();
//DropEarly is bypassed if queue (disc) has less than a couple of packets.OMITTED.
//DropEarly is bypassed if DropProbability p is less than the random variable v \in [0, 1]
if (p < v)
{
//if (p < 0) { std::cout << "No Drop: " << "\t" << Simulator::Now ().GetSeconds () << "\t" << p << std::endl;} // checking to make sure that there is no drop in open-loop mode
return false; //dont drop packet
}
return true; //drop packet
}
void PidQueueDisc::CalculateP ()
{
NS_LOG_FUNCTION (this);
double pp;
//QueueSize nQueued = this->GetCurrentSize(); //current queue size (packets) of qdisc (internal) queue
Ptr<NetDeviceQueueInterface> qti = this->GetNetDeviceQueueInterface () ;
Ptr<NetDevice> ndd = qti->GetObject<NetDevice>();
PointerValue ptrV; ndd->GetAttribute ("TxQueue", ptrV);
Ptr<Queue<Packet>> txQueue = ptrV.Get<Queue<Packet>>();
m_Qsize = txQueue->GetCurrentSize(); //current queue size (packets) of netdevice (external) queue
m_ErrQsize0 = double(m_QsizeRef.GetValue()) - double(m_Qsize.GetValue()); //current error
//PID ( tustin (integral term) + backward difference (differential term) ) ; (see equation 15)
m_dropProb0 = (m_a * m_dropProb1) + (m_b * m_dropProb2) + ( m_c * m_ErrQsize0 ) + ( m_d * m_ErrQsize1 ) + ( m_e * m_ErrQsize2 ); //unsaturated drop probability, m_dropProb0
m_dropProb2 = m_dropProb1; m_dropProb1 = m_dropProb0; // store current and previous unsaturated drop probabilities .....m_dropProb0 needs to be stored before adding m_u0, see below
m_ErrQsize2 = m_ErrQsize1; m_ErrQsize1 = m_ErrQsize0; // store current and previous errors
m_dropProb0 = m_u0 + m_dropProb0; // (see equation 18); m_u0 needs to be added, see Figure 5
pp = (m_dropProb0 <= 0) ? 0 : m_dropProb0; m_dropProb = (pp <= 1) ? pp : 1; //saturated drop probability, m_dropProb . Saturation comes after equation 18 and not before it, see Figure 5
m_rtrsEvent2 = Simulator::Schedule (m_tUpdate, &PidQueueDisc::CalculateP, this);
//preferably after scheduling an m_tUpdate of m_rtrsEvent2; worst case, we miss some data points, doesnt affect computation of control action m_dropProb
AsciiTraceHelper asciiTraceHelper; Ptr<OutputStreamWrapper> stream1b = asciiTraceHelper.CreateFileStream ("nsplots/Jplots/dprobp.dat",std::ios::app);
*stream1b->GetStream () << Simulator::Now ().GetSeconds () << "\t" << m_dropProb0 << "\t" << m_dropProb << "\t" << m_uv->GetValue() << std::endl;
}
void PidQueueDisc::QSizeRefUpdate ()
{
NS_LOG_FUNCTION (this);
m_QsizeRef = m_QsizeRefDQ;
std::cout << "desired set-point reference queue-length (qref): " << m_QsizeRef.GetValue() << "\n" << std::endl;
}
void PidQueueDisc::PWMUpdate ()
{
NS_LOG_FUNCTION (this);
}
} //namespace ns3