Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "cs-policy-priority-fifo.hpp" |
| 27 | #include "cs.hpp" |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 28 | #include "daemon/global.hpp" |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 29 | |
| 30 | namespace nfd { |
| 31 | namespace cs { |
| 32 | namespace priority_fifo { |
| 33 | |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 34 | const std::string PriorityFifoPolicy::POLICY_NAME = "priority_fifo"; |
| 35 | NFD_REGISTER_CS_POLICY(PriorityFifoPolicy); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 36 | |
| 37 | PriorityFifoPolicy::PriorityFifoPolicy() |
| 38 | : Policy(POLICY_NAME) |
| 39 | { |
| 40 | } |
| 41 | |
Minsheng Zhang | 9c903e0 | 2015-10-03 18:16:05 -0500 | [diff] [blame] | 42 | PriorityFifoPolicy::~PriorityFifoPolicy() |
| 43 | { |
| 44 | for (auto entryInfoMapPair : m_entryInfoMap) { |
| 45 | delete entryInfoMapPair.second; |
| 46 | } |
| 47 | } |
| 48 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 49 | void |
| 50 | PriorityFifoPolicy::doAfterInsert(iterator i) |
| 51 | { |
| 52 | this->attachQueue(i); |
| 53 | this->evictEntries(); |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | PriorityFifoPolicy::doAfterRefresh(iterator i) |
| 58 | { |
| 59 | this->detachQueue(i); |
| 60 | this->attachQueue(i); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | PriorityFifoPolicy::doBeforeErase(iterator i) |
| 65 | { |
| 66 | this->detachQueue(i); |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | PriorityFifoPolicy::doBeforeUse(iterator i) |
| 71 | { |
| 72 | BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end()); |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | PriorityFifoPolicy::evictEntries() |
| 77 | { |
| 78 | BOOST_ASSERT(this->getCs() != nullptr); |
| 79 | |
| 80 | while (this->getCs()->size() > this->getLimit()) { |
| 81 | this->evictOne(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | PriorityFifoPolicy::evictOne() |
| 87 | { |
| 88 | BOOST_ASSERT(!m_queues[QUEUE_UNSOLICITED].empty() || |
| 89 | !m_queues[QUEUE_STALE].empty() || |
| 90 | !m_queues[QUEUE_FIFO].empty()); |
| 91 | |
| 92 | iterator i; |
| 93 | if (!m_queues[QUEUE_UNSOLICITED].empty()) { |
| 94 | i = m_queues[QUEUE_UNSOLICITED].front(); |
| 95 | } |
| 96 | else if (!m_queues[QUEUE_STALE].empty()) { |
| 97 | i = m_queues[QUEUE_STALE].front(); |
| 98 | } |
| 99 | else if (!m_queues[QUEUE_FIFO].empty()) { |
| 100 | i = m_queues[QUEUE_FIFO].front(); |
| 101 | } |
| 102 | |
| 103 | this->detachQueue(i); |
| 104 | this->emitSignal(beforeEvict, i); |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | PriorityFifoPolicy::attachQueue(iterator i) |
| 109 | { |
| 110 | BOOST_ASSERT(m_entryInfoMap.find(i) == m_entryInfoMap.end()); |
| 111 | |
| 112 | EntryInfo* entryInfo = new EntryInfo(); |
| 113 | if (i->isUnsolicited()) { |
| 114 | entryInfo->queueType = QUEUE_UNSOLICITED; |
| 115 | } |
| 116 | else if (i->isStale()) { |
| 117 | entryInfo->queueType = QUEUE_STALE; |
| 118 | } |
| 119 | else { |
| 120 | entryInfo->queueType = QUEUE_FIFO; |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 121 | entryInfo->moveStaleEventId = getScheduler().schedule(i->getData().getFreshnessPeriod(), |
| 122 | [=] { moveToStaleQueue(i); }); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | Queue& queue = m_queues[entryInfo->queueType]; |
| 126 | entryInfo->queueIt = queue.insert(queue.end(), i); |
| 127 | m_entryInfoMap[i] = entryInfo; |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | PriorityFifoPolicy::detachQueue(iterator i) |
| 132 | { |
| 133 | BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end()); |
| 134 | |
| 135 | EntryInfo* entryInfo = m_entryInfoMap[i]; |
| 136 | if (entryInfo->queueType == QUEUE_FIFO) { |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 137 | entryInfo->moveStaleEventId.cancel(); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | m_queues[entryInfo->queueType].erase(entryInfo->queueIt); |
| 141 | m_entryInfoMap.erase(i); |
Minsheng Zhang | 9c903e0 | 2015-10-03 18:16:05 -0500 | [diff] [blame] | 142 | delete entryInfo; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void |
| 146 | PriorityFifoPolicy::moveToStaleQueue(iterator i) |
| 147 | { |
| 148 | BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end()); |
| 149 | |
| 150 | EntryInfo* entryInfo = m_entryInfoMap[i]; |
| 151 | BOOST_ASSERT(entryInfo->queueType == QUEUE_FIFO); |
| 152 | |
| 153 | m_queues[QUEUE_FIFO].erase(entryInfo->queueIt); |
| 154 | |
| 155 | entryInfo->queueType = QUEUE_STALE; |
| 156 | Queue& queue = m_queues[QUEUE_STALE]; |
| 157 | entryInfo->queueIt = queue.insert(queue.end(), i); |
| 158 | m_entryInfoMap[i] = entryInfo; |
| 159 | } |
| 160 | |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 161 | } // namespace priority_fifo |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 162 | } // namespace cs |
| 163 | } // namespace nfd |