blob: ca0d755c9a05e558c718fbb89adb81eb4a2b94e1 [file] [log] [blame]
Alexander Afanasyev048ae422012-08-17 17:33:02 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 University of California, Los Angeles
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 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21#include "ndn-pit-queue.h"
22
23#include "ns3/ndn-face.h"
24#include "ns3/ndn-pit-entry.h"
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -070025#include "ns3/log.h"
Alexander Afanasyevc23a3e32012-08-28 00:16:23 -070026#include "ns3/simulator.h"
Alexander Afanasyev048ae422012-08-17 17:33:02 -070027
28#include "ns3/assert.h"
29
30using namespace std;
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -070031using namespace boost;
Alexander Afanasyev048ae422012-08-17 17:33:02 -070032
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -070033NS_LOG_COMPONENT_DEFINE ("ndn.PitQueue");
34
Alexander Afanasyev048ae422012-08-17 17:33:02 -070035namespace ns3 {
36namespace ndn {
37
Alexander Afanasyev187cba22012-08-28 11:16:52 -070038const double MIN_WEIGHT = 0.1;
Alexander Afanasyev98c16e02012-08-28 00:02:02 -070039
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070040PitQueue::PitQueue ()
Alexander Afanasyev372cbab2012-08-22 09:43:53 -070041 // : m_maxQueueSize (20)
42 : m_lastQueue (m_queues.end ())
Alexander Afanasyev048ae422012-08-17 17:33:02 -070043{
44}
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070045
Alexander Afanasyev372cbab2012-08-22 09:43:53 -070046// void
47// PitQueue::SetMaxQueueSize (uint32_t size)
48// {
49// m_maxQueueSize = size;
50// }
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070051
Alexander Afanasyev372cbab2012-08-22 09:43:53 -070052// uint32_t
53// PitQueue::GetMaxQueueSize () const
54// {
55// return m_maxQueueSize;
56// }
Alexander Afanasyev048ae422012-08-17 17:33:02 -070057
58
59bool
60PitQueue::Enqueue (Ptr<Face> inFace,
Alexander Afanasyev98c16e02012-08-28 00:02:02 -070061 Ptr<pit::Entry> pitEntry,
62 double updatedWeight)
Alexander Afanasyev048ae422012-08-17 17:33:02 -070063{
Alexander Afanasyev187cba22012-08-28 11:16:52 -070064 if (updatedWeight < 0) updatedWeight = 0.5;
Alexander Afanasyev98c16e02012-08-28 00:02:02 -070065 if (updatedWeight < MIN_WEIGHT) updatedWeight = MIN_WEIGHT;
66
Alexander Afanasyeved449cc2012-08-21 11:10:33 -070067 PerInFaceQueue::iterator queue = m_queues.find (inFace);
68 if (queue == m_queues.end ())
69 {
Alexander Afanasyev98c16e02012-08-28 00:02:02 -070070 pair<PerInFaceQueue::iterator, bool> itemPair =
71 m_queues.insert (make_pair (inFace, boost::make_shared< WeightedQueue > (make_tuple (Queue (), updatedWeight, 0))));
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -070072 m_lastQueue = m_queues.end (); // for some reason end() iterator is invalidated when new item is inserted
Alexander Afanasyeved449cc2012-08-21 11:10:33 -070073 NS_ASSERT (itemPair.second == true);
74
75 queue = itemPair.first;
76 }
Alexander Afanasyevff033952012-08-23 15:45:52 -070077
Alexander Afanasyev98c16e02012-08-28 00:02:02 -070078 if ((inFace->GetLimits ().GetMaxLimit () == 0 && queue->second->get<0> ().size () > 100) ||
79 (inFace->GetLimits ().GetMaxLimit () != 0 && queue->second->get<0> ().size () >= 0.5 * inFace->GetLimits ().GetMaxLimit ()))
Alexander Afanasyevff033952012-08-23 15:45:52 -070080 {
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070081 return false;
Alexander Afanasyevff033952012-08-23 15:45:52 -070082 }
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070083
Alexander Afanasyev98c16e02012-08-28 00:02:02 -070084 Queue::iterator itemIterator = queue->second->get<0> ().insert (queue->second->get<0> ().end (), pitEntry);
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -070085
86 shared_ptr<fw::PitQueueTag> tag = pitEntry->GetFwTag<fw::PitQueueTag> ();
87 if (tag == shared_ptr<fw::PitQueueTag> ())
88 {
89 tag = make_shared<fw::PitQueueTag> ();
90 pitEntry->AddFwTag (tag);
91 }
92 tag->InsertQueue (queue->second, itemIterator);
Alexander Afanasyev98c16e02012-08-28 00:02:02 -070093
94 UpdateWeightedRounds ();
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070095 return true;
Alexander Afanasyev048ae422012-08-17 17:33:02 -070096}
97
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -070098
Alexander Afanasyev048ae422012-08-17 17:33:02 -070099Ptr<pit::Entry>
100PitQueue::Pop ()
101{
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700102 PerInFaceQueue::iterator queue = m_lastQueue;
103
Alexander Afanasyevc23a3e32012-08-28 00:16:23 -0700104 if (queue != m_queues.end () &&
Alexander Afanasyev187cba22012-08-28 11:16:52 -0700105 m_serviceCounter >= queue->second->get<2> ())
Alexander Afanasyevc23a3e32012-08-28 00:16:23 -0700106 {
Alexander Afanasyev187cba22012-08-28 11:16:52 -0700107 queue ++; // actually implement weighted round robin...
108 m_serviceCounter = 0;
Alexander Afanasyevc23a3e32012-08-28 00:16:23 -0700109 }
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700110
111 while (queue != m_queues.end () && queue->second->get<0> ().size () == 0) // advance iterator
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700112 {
113 queue ++;
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700114 m_serviceCounter = 0;
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700115 }
116
117 if (queue == m_queues.end ())
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700118 {
119 queue = m_queues.begin (); // circle to the beginning
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700120 m_serviceCounter = 0;
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700121 }
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700122
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700123 while (queue != m_queues.end () && queue->second->get<0> ().size () == 0) // advance iterator
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700124 {
125 queue ++;
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700126 m_serviceCounter = 0;
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700127 }
128
Alexander Afanasyev187cba22012-08-28 11:16:52 -0700129 m_serviceCounter ++;
130
131 // if (Simulator::GetContext () == 4)
Alexander Afanasyevc23a3e32012-08-28 00:16:23 -0700132 // {
Alexander Afanasyev187cba22012-08-28 11:16:52 -0700133 // cout << "====== " << Simulator::Now ().ToDouble (Time::S) << "s " << *queue->first << endl;
134 // cout << " " << m_serviceCounter << " / " << queue->second->get<2> () << "\n";
Alexander Afanasyevc23a3e32012-08-28 00:16:23 -0700135 // for (PerInFaceQueue::const_iterator somequeue = m_queues.begin ();
136 // somequeue != m_queues.end ();
137 // somequeue ++)
138 // {
139 // if (somequeue == queue) cout << "*";
140 // cout << somequeue->second->get<0> ().size () << " ";
141 // }
142 // cout << endl;
143 // }
144
Alexander Afanasyev187cba22012-08-28 11:16:52 -0700145 if (queue == m_queues.end () || queue->second->get<0> ().size () == 0)
146 return 0;
147
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700148 NS_ASSERT_MSG (queue->second->get<0> ().size () != 0, "Logic error");
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700149
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700150 Ptr<pit::Entry> entry = *queue->second->get<0> ().begin ();
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700151 shared_ptr<fw::PitQueueTag> tag = entry->GetFwTag<fw::PitQueueTag> ();
152 NS_ASSERT (tag != shared_ptr<fw::PitQueueTag> ());
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700153
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700154#ifdef NS3_LOG_ENABLE
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700155 size_t queueSize = queue->second->get<0> ().size ();
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700156#endif
157 tag->RemoveFromQueue (queue->second);
158#ifdef NS3_LOG_ENABLE
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700159 NS_ASSERT_MSG (queue->second->get<0> ().size () == queueSize-1, "Queue size should be reduced by one");
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700160#endif
Alexander Afanasyev187cba22012-08-28 11:16:52 -0700161
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700162 m_lastQueue = queue;
163 return entry;
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700164}
165
166void
167PitQueue::Remove (Ptr<Face> face)
168{
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700169 if (m_lastQueue->first == face)
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700170 {
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700171 m_lastQueue++;
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700172 }
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700173
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700174 PerInFaceQueue::iterator queue = m_queues.find (face);
175 if (queue == m_queues.end ())
176 return;
177
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700178 for (Queue::iterator pitEntry = queue->second->get<0> ().begin ();
179 pitEntry != queue->second->get<0> ().end ();
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700180 pitEntry ++)
181 {
182 shared_ptr<fw::PitQueueTag> tag = (*pitEntry)->GetFwTag<fw::PitQueueTag> ();
183 NS_ASSERT (tag != shared_ptr<fw::PitQueueTag> ());
184
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700185 tag->RemoveFromQueuesExcept (queue->second);
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700186 }
187
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700188 NS_ASSERT_MSG (queue->second->get<0> ().size () == 0, "Queue size should be 0 by now");
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700189 m_queues.erase (queue);
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700190}
191
192void
193PitQueue::Remove (Ptr<pit::Entry> entry)
194{
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700195 shared_ptr<fw::PitQueueTag> tag = entry->GetFwTag<fw::PitQueueTag> ();
196 if (tag == shared_ptr<fw::PitQueueTag> ())
197 return;
198
199 tag->RemoveFromAllQueues ();
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700200}
201
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700202bool
203PitQueue::IsEmpty () const
204{
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700205 bool isEmpty = true;
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700206
207 for (PerInFaceQueue::const_iterator queue = m_queues.begin ();
208 queue != m_queues.end ();
209 queue ++)
210 {
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700211 isEmpty &= (queue->second->get<0> ().size () == 0);
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700212 }
213
214 return isEmpty;
215}
216
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700217bool
218PitQueue::IsEmpty (Ptr<Face> inFace) const
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700219{
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700220 PerInFaceQueue::const_iterator queue = m_queues.find (inFace);
221 if (queue == m_queues.end ())
222 return true;
223
224 return queue->second->get<0> ().size () == 0;
225}
226
227
228void
229PitQueue::UpdateWeightedRounds ()
230{
231 double minWeight = 100.0;
232 for (PerInFaceQueue::const_iterator queue = m_queues.begin ();
233 queue != m_queues.end ();
234 queue ++)
235 {
236 if (queue->second->get<1> () < minWeight)
237 minWeight = queue->second->get<1> ();
238 }
239
240 for (PerInFaceQueue::const_iterator queue = m_queues.begin ();
241 queue != m_queues.end ();
242 queue ++)
243 {
Alexander Afanasyev187cba22012-08-28 11:16:52 -0700244 queue->second->get<2> () = static_cast<uint32_t>(queue->second->get<1> () / minWeight);
245 if (queue->second->get<2> () < 1)
246 queue->second->get<2> () = 1;
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700247 }
Alexander Afanasyev187cba22012-08-28 11:16:52 -0700248
249 // if (m_queues.size () > 1)
250 // {
251 // cout << "Node " << Simulator::GetContext () << " (min = " << minWeight << "): ";
252 // for (PerInFaceQueue::const_iterator queue = m_queues.begin ();
253 // queue != m_queues.end ();
254 // queue ++)
255 // {
256 // cout << queue->second->get<1> () << "/" << queue->second->get<2> () << " ";
257 // }
258 // cout << endl;
259 // }
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700260}
261
262
263////////////////////////////////////////////////////////////////////////////////
264
265void
266fw::PitQueueTag::InsertQueue (boost::shared_ptr<PitQueue::WeightedQueue> queue, PitQueue::Queue::iterator iterator)
267{
268 // std::cerr << "size before: " << m_items.size () << " item: " << (*iterator)->GetPrefix ();
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700269 pair<MapOfItems::iterator, bool> item = m_items.insert (make_pair (queue, iterator));
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700270 // std::cerr << " and after: " << m_items.size () << std::endl;
271 NS_ASSERT_MSG (item.second == true, "Should be a new tag for PIT entry, but something is wrong");
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700272}
273
274void
275fw::PitQueueTag::RemoveFromAllQueues ()
276{
277 for (MapOfItems::iterator item = m_items.begin ();
278 item != m_items.end ();
279 item ++)
280 {
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700281 item->first->get<0> ().erase (item->second);
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700282 }
283 m_items.clear ();
284}
285
286void
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700287fw::PitQueueTag::RemoveFromQueue (boost::shared_ptr<PitQueue::WeightedQueue> queue)
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700288{
289 MapOfItems::iterator item = m_items.find (queue);
290 if (item == m_items.end ())
291 return;
292
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700293 item->first->get<0> ().erase (item->second);
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700294 m_items.erase (item);
295}
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700296
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700297void
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700298fw::PitQueueTag::RemoveFromQueuesExcept (boost::shared_ptr<PitQueue::WeightedQueue> queue)
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700299{
300 for (MapOfItems::iterator item = m_items.begin ();
301 item != m_items.end (); )
302 {
303 if (item->first == queue)
304 {
305 item ++;
306 continue;
307 }
308
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700309 item->first->get<0> ().erase (item->second);
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700310
311 MapOfItems::iterator itemToDelete = item;
312 item ++;
313 m_items.erase (itemToDelete);
314 }
315}
316
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700317bool
318fw::PitQueueTag::IsLastOneInQueues () const
319{
320 bool lastOne = true;
321
322 for (MapOfItems::const_iterator item = m_items.begin ();
323 item != m_items.end ();
324 item ++)
325 {
326 lastOne &= (item->first->get<0> ().size () == 1);
327 }
328
329 return lastOne;
330}
331
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700332
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700333} // namespace ndn
334} // namespace ns3