Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 25 | #include "ns3/log.h" |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 26 | |
| 27 | #include "ns3/assert.h" |
| 28 | |
| 29 | using namespace std; |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 30 | using namespace boost; |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 31 | |
Alexander Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 32 | NS_LOG_COMPONENT_DEFINE ("ndn.PitQueue"); |
| 33 | |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 34 | namespace ns3 { |
| 35 | namespace ndn { |
| 36 | |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 37 | const double MIN_WEIGHT = 0.01; |
| 38 | |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 39 | PitQueue::PitQueue () |
Alexander Afanasyev | 372cbab | 2012-08-22 09:43:53 -0700 | [diff] [blame] | 40 | // : m_maxQueueSize (20) |
| 41 | : m_lastQueue (m_queues.end ()) |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 42 | { |
| 43 | } |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 44 | |
Alexander Afanasyev | 372cbab | 2012-08-22 09:43:53 -0700 | [diff] [blame] | 45 | // void |
| 46 | // PitQueue::SetMaxQueueSize (uint32_t size) |
| 47 | // { |
| 48 | // m_maxQueueSize = size; |
| 49 | // } |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 50 | |
Alexander Afanasyev | 372cbab | 2012-08-22 09:43:53 -0700 | [diff] [blame] | 51 | // uint32_t |
| 52 | // PitQueue::GetMaxQueueSize () const |
| 53 | // { |
| 54 | // return m_maxQueueSize; |
| 55 | // } |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 56 | |
| 57 | |
| 58 | bool |
| 59 | PitQueue::Enqueue (Ptr<Face> inFace, |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 60 | Ptr<pit::Entry> pitEntry, |
| 61 | double updatedWeight) |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 62 | { |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 63 | if (updatedWeight < MIN_WEIGHT) updatedWeight = MIN_WEIGHT; |
| 64 | |
Alexander Afanasyev | ed449cc | 2012-08-21 11:10:33 -0700 | [diff] [blame] | 65 | PerInFaceQueue::iterator queue = m_queues.find (inFace); |
| 66 | if (queue == m_queues.end ()) |
| 67 | { |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 68 | pair<PerInFaceQueue::iterator, bool> itemPair = |
| 69 | m_queues.insert (make_pair (inFace, boost::make_shared< WeightedQueue > (make_tuple (Queue (), updatedWeight, 0)))); |
Alexander Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 70 | m_lastQueue = m_queues.end (); // for some reason end() iterator is invalidated when new item is inserted |
Alexander Afanasyev | ed449cc | 2012-08-21 11:10:33 -0700 | [diff] [blame] | 71 | NS_ASSERT (itemPair.second == true); |
| 72 | |
| 73 | queue = itemPair.first; |
| 74 | } |
Alexander Afanasyev | ff03395 | 2012-08-23 15:45:52 -0700 | [diff] [blame] | 75 | |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 76 | if ((inFace->GetLimits ().GetMaxLimit () == 0 && queue->second->get<0> ().size () > 100) || |
| 77 | (inFace->GetLimits ().GetMaxLimit () != 0 && queue->second->get<0> ().size () >= 0.5 * inFace->GetLimits ().GetMaxLimit ())) |
Alexander Afanasyev | ff03395 | 2012-08-23 15:45:52 -0700 | [diff] [blame] | 78 | { |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 79 | return false; |
Alexander Afanasyev | ff03395 | 2012-08-23 15:45:52 -0700 | [diff] [blame] | 80 | } |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 81 | |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 82 | Queue::iterator itemIterator = queue->second->get<0> ().insert (queue->second->get<0> ().end (), pitEntry); |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 83 | |
| 84 | shared_ptr<fw::PitQueueTag> tag = pitEntry->GetFwTag<fw::PitQueueTag> (); |
| 85 | if (tag == shared_ptr<fw::PitQueueTag> ()) |
| 86 | { |
| 87 | tag = make_shared<fw::PitQueueTag> (); |
| 88 | pitEntry->AddFwTag (tag); |
| 89 | } |
| 90 | tag->InsertQueue (queue->second, itemIterator); |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 91 | |
| 92 | UpdateWeightedRounds (); |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 93 | return true; |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 96 | |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 97 | Ptr<pit::Entry> |
| 98 | PitQueue::Pop () |
| 99 | { |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 100 | PerInFaceQueue::iterator queue = m_lastQueue; |
| 101 | |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 102 | // if (queue != m_queues.end () && |
| 103 | // true) |
| 104 | // { |
| 105 | // queue ++; // actually implement round robin... |
| 106 | // } |
| 107 | |
| 108 | while (queue != m_queues.end () && queue->second->get<0> ().size () == 0) // advance iterator |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 109 | { |
| 110 | queue ++; |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 111 | m_serviceCounter = 0; |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | if (queue == m_queues.end ()) |
Alexander Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 115 | { |
| 116 | queue = m_queues.begin (); // circle to the beginning |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 117 | m_serviceCounter = 0; |
Alexander Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 118 | } |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 119 | |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 120 | while (queue != m_queues.end () && queue->second->get<0> ().size () == 0) // advance iterator |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 121 | { |
| 122 | queue ++; |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 123 | m_serviceCounter = 0; |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | if (queue == m_queues.end ()) // e.g., begin () == end () |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 127 | return 0; |
| 128 | |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 129 | NS_ASSERT_MSG (queue->second->get<0> ().size () != 0, "Logic error"); |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 130 | |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 131 | Ptr<pit::Entry> entry = *queue->second->get<0> ().begin (); |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 132 | shared_ptr<fw::PitQueueTag> tag = entry->GetFwTag<fw::PitQueueTag> (); |
| 133 | NS_ASSERT (tag != shared_ptr<fw::PitQueueTag> ()); |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 134 | |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 135 | #ifdef NS3_LOG_ENABLE |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 136 | size_t queueSize = queue->second->get<0> ().size (); |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 137 | #endif |
| 138 | tag->RemoveFromQueue (queue->second); |
| 139 | #ifdef NS3_LOG_ENABLE |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 140 | NS_ASSERT_MSG (queue->second->get<0> ().size () == queueSize-1, "Queue size should be reduced by one"); |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 141 | #endif |
| 142 | |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 143 | m_lastQueue = queue; |
| 144 | return entry; |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void |
| 148 | PitQueue::Remove (Ptr<Face> face) |
| 149 | { |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 150 | if (m_lastQueue->first == face) |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 151 | { |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 152 | m_lastQueue++; |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 153 | } |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 154 | |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 155 | PerInFaceQueue::iterator queue = m_queues.find (face); |
| 156 | if (queue == m_queues.end ()) |
| 157 | return; |
| 158 | |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 159 | for (Queue::iterator pitEntry = queue->second->get<0> ().begin (); |
| 160 | pitEntry != queue->second->get<0> ().end (); |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 161 | pitEntry ++) |
| 162 | { |
| 163 | shared_ptr<fw::PitQueueTag> tag = (*pitEntry)->GetFwTag<fw::PitQueueTag> (); |
| 164 | NS_ASSERT (tag != shared_ptr<fw::PitQueueTag> ()); |
| 165 | |
Alexander Afanasyev | 6f101fc | 2012-08-23 16:34:34 -0700 | [diff] [blame] | 166 | tag->RemoveFromQueuesExcept (queue->second); |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 169 | NS_ASSERT_MSG (queue->second->get<0> ().size () == 0, "Queue size should be 0 by now"); |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 170 | m_queues.erase (queue); |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void |
| 174 | PitQueue::Remove (Ptr<pit::Entry> entry) |
| 175 | { |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 176 | shared_ptr<fw::PitQueueTag> tag = entry->GetFwTag<fw::PitQueueTag> (); |
| 177 | if (tag == shared_ptr<fw::PitQueueTag> ()) |
| 178 | return; |
| 179 | |
| 180 | tag->RemoveFromAllQueues (); |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 183 | bool |
| 184 | PitQueue::IsEmpty () const |
| 185 | { |
Alexander Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 186 | bool isEmpty = true; |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 187 | |
| 188 | for (PerInFaceQueue::const_iterator queue = m_queues.begin (); |
| 189 | queue != m_queues.end (); |
| 190 | queue ++) |
| 191 | { |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 192 | isEmpty &= (queue->second->get<0> ().size () == 0); |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | return isEmpty; |
| 196 | } |
| 197 | |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 198 | bool |
| 199 | PitQueue::IsEmpty (Ptr<Face> inFace) const |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 200 | { |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 201 | PerInFaceQueue::const_iterator queue = m_queues.find (inFace); |
| 202 | if (queue == m_queues.end ()) |
| 203 | return true; |
| 204 | |
| 205 | return queue->second->get<0> ().size () == 0; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | void |
| 210 | PitQueue::UpdateWeightedRounds () |
| 211 | { |
| 212 | double minWeight = 100.0; |
| 213 | for (PerInFaceQueue::const_iterator queue = m_queues.begin (); |
| 214 | queue != m_queues.end (); |
| 215 | queue ++) |
| 216 | { |
| 217 | if (queue->second->get<1> () < minWeight) |
| 218 | minWeight = queue->second->get<1> (); |
| 219 | } |
| 220 | |
| 221 | for (PerInFaceQueue::const_iterator queue = m_queues.begin (); |
| 222 | queue != m_queues.end (); |
| 223 | queue ++) |
| 224 | { |
| 225 | queue->second->get<2> () = static_cast<uint32_t>((queue->second->get<1> () + 0.5) / minWeight); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | |
| 230 | //////////////////////////////////////////////////////////////////////////////// |
| 231 | |
| 232 | void |
| 233 | fw::PitQueueTag::InsertQueue (boost::shared_ptr<PitQueue::WeightedQueue> queue, PitQueue::Queue::iterator iterator) |
| 234 | { |
| 235 | // std::cerr << "size before: " << m_items.size () << " item: " << (*iterator)->GetPrefix (); |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 236 | pair<MapOfItems::iterator, bool> item = m_items.insert (make_pair (queue, iterator)); |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 237 | // std::cerr << " and after: " << m_items.size () << std::endl; |
| 238 | NS_ASSERT_MSG (item.second == true, "Should be a new tag for PIT entry, but something is wrong"); |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void |
| 242 | fw::PitQueueTag::RemoveFromAllQueues () |
| 243 | { |
| 244 | for (MapOfItems::iterator item = m_items.begin (); |
| 245 | item != m_items.end (); |
| 246 | item ++) |
| 247 | { |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 248 | item->first->get<0> ().erase (item->second); |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 249 | } |
| 250 | m_items.clear (); |
| 251 | } |
| 252 | |
| 253 | void |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 254 | fw::PitQueueTag::RemoveFromQueue (boost::shared_ptr<PitQueue::WeightedQueue> queue) |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 255 | { |
| 256 | MapOfItems::iterator item = m_items.find (queue); |
| 257 | if (item == m_items.end ()) |
| 258 | return; |
| 259 | |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 260 | item->first->get<0> ().erase (item->second); |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 261 | m_items.erase (item); |
| 262 | } |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 263 | |
Alexander Afanasyev | 6f101fc | 2012-08-23 16:34:34 -0700 | [diff] [blame] | 264 | void |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 265 | fw::PitQueueTag::RemoveFromQueuesExcept (boost::shared_ptr<PitQueue::WeightedQueue> queue) |
Alexander Afanasyev | 6f101fc | 2012-08-23 16:34:34 -0700 | [diff] [blame] | 266 | { |
| 267 | for (MapOfItems::iterator item = m_items.begin (); |
| 268 | item != m_items.end (); ) |
| 269 | { |
| 270 | if (item->first == queue) |
| 271 | { |
| 272 | item ++; |
| 273 | continue; |
| 274 | } |
| 275 | |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 276 | item->first->get<0> ().erase (item->second); |
Alexander Afanasyev | 6f101fc | 2012-08-23 16:34:34 -0700 | [diff] [blame] | 277 | |
| 278 | MapOfItems::iterator itemToDelete = item; |
| 279 | item ++; |
| 280 | m_items.erase (itemToDelete); |
| 281 | } |
| 282 | } |
| 283 | |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame^] | 284 | bool |
| 285 | fw::PitQueueTag::IsLastOneInQueues () const |
| 286 | { |
| 287 | bool lastOne = true; |
| 288 | |
| 289 | for (MapOfItems::const_iterator item = m_items.begin (); |
| 290 | item != m_items.end (); |
| 291 | item ++) |
| 292 | { |
| 293 | lastOne &= (item->first->get<0> ().size () == 1); |
| 294 | } |
| 295 | |
| 296 | return lastOne; |
| 297 | } |
| 298 | |
Alexander Afanasyev | 6f101fc | 2012-08-23 16:34:34 -0700 | [diff] [blame] | 299 | |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 300 | } // namespace ndn |
| 301 | } // namespace ns3 |