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 | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, 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 | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 28 | #include "common/global.hpp" |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 29 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 30 | namespace nfd::cs::priority_fifo { |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 31 | |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 32 | NFD_REGISTER_CS_POLICY(PriorityFifoPolicy); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 33 | |
| 34 | PriorityFifoPolicy::PriorityFifoPolicy() |
| 35 | : Policy(POLICY_NAME) |
| 36 | { |
| 37 | } |
| 38 | |
Minsheng Zhang | 9c903e0 | 2015-10-03 18:16:05 -0500 | [diff] [blame] | 39 | PriorityFifoPolicy::~PriorityFifoPolicy() |
| 40 | { |
| 41 | for (auto entryInfoMapPair : m_entryInfoMap) { |
| 42 | delete entryInfoMapPair.second; |
| 43 | } |
| 44 | } |
| 45 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 46 | void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 47 | PriorityFifoPolicy::doAfterInsert(EntryRef i) |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 48 | { |
| 49 | this->attachQueue(i); |
| 50 | this->evictEntries(); |
| 51 | } |
| 52 | |
| 53 | void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 54 | PriorityFifoPolicy::doAfterRefresh(EntryRef i) |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 55 | { |
| 56 | this->detachQueue(i); |
| 57 | this->attachQueue(i); |
| 58 | } |
| 59 | |
| 60 | void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 61 | PriorityFifoPolicy::doBeforeErase(EntryRef i) |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 62 | { |
| 63 | this->detachQueue(i); |
| 64 | } |
| 65 | |
| 66 | void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 67 | PriorityFifoPolicy::doBeforeUse(EntryRef i) |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 68 | { |
| 69 | BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end()); |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | PriorityFifoPolicy::evictEntries() |
| 74 | { |
| 75 | BOOST_ASSERT(this->getCs() != nullptr); |
| 76 | |
| 77 | while (this->getCs()->size() > this->getLimit()) { |
| 78 | this->evictOne(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | PriorityFifoPolicy::evictOne() |
| 84 | { |
| 85 | BOOST_ASSERT(!m_queues[QUEUE_UNSOLICITED].empty() || |
| 86 | !m_queues[QUEUE_STALE].empty() || |
| 87 | !m_queues[QUEUE_FIFO].empty()); |
| 88 | |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 89 | EntryRef i; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 90 | if (!m_queues[QUEUE_UNSOLICITED].empty()) { |
| 91 | i = m_queues[QUEUE_UNSOLICITED].front(); |
| 92 | } |
| 93 | else if (!m_queues[QUEUE_STALE].empty()) { |
| 94 | i = m_queues[QUEUE_STALE].front(); |
| 95 | } |
| 96 | else if (!m_queues[QUEUE_FIFO].empty()) { |
| 97 | i = m_queues[QUEUE_FIFO].front(); |
| 98 | } |
| 99 | |
| 100 | this->detachQueue(i); |
| 101 | this->emitSignal(beforeEvict, i); |
| 102 | } |
| 103 | |
| 104 | void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 105 | PriorityFifoPolicy::attachQueue(EntryRef i) |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 106 | { |
| 107 | BOOST_ASSERT(m_entryInfoMap.find(i) == m_entryInfoMap.end()); |
| 108 | |
| 109 | EntryInfo* entryInfo = new EntryInfo(); |
| 110 | if (i->isUnsolicited()) { |
| 111 | entryInfo->queueType = QUEUE_UNSOLICITED; |
| 112 | } |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 113 | else if (!i->isFresh()) { |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 114 | entryInfo->queueType = QUEUE_STALE; |
| 115 | } |
| 116 | else { |
| 117 | entryInfo->queueType = QUEUE_FIFO; |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 118 | entryInfo->moveStaleEventId = getScheduler().schedule(i->getData().getFreshnessPeriod(), |
| 119 | [=] { moveToStaleQueue(i); }); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | Queue& queue = m_queues[entryInfo->queueType]; |
| 123 | entryInfo->queueIt = queue.insert(queue.end(), i); |
| 124 | m_entryInfoMap[i] = entryInfo; |
| 125 | } |
| 126 | |
| 127 | void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 128 | PriorityFifoPolicy::detachQueue(EntryRef i) |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 129 | { |
| 130 | BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end()); |
| 131 | |
| 132 | EntryInfo* entryInfo = m_entryInfoMap[i]; |
| 133 | if (entryInfo->queueType == QUEUE_FIFO) { |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 134 | entryInfo->moveStaleEventId.cancel(); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | m_queues[entryInfo->queueType].erase(entryInfo->queueIt); |
| 138 | m_entryInfoMap.erase(i); |
Minsheng Zhang | 9c903e0 | 2015-10-03 18:16:05 -0500 | [diff] [blame] | 139 | delete entryInfo; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 143 | PriorityFifoPolicy::moveToStaleQueue(EntryRef i) |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 144 | { |
| 145 | BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end()); |
| 146 | |
| 147 | EntryInfo* entryInfo = m_entryInfoMap[i]; |
| 148 | BOOST_ASSERT(entryInfo->queueType == QUEUE_FIFO); |
| 149 | |
| 150 | m_queues[QUEUE_FIFO].erase(entryInfo->queueIt); |
| 151 | |
| 152 | entryInfo->queueType = QUEUE_STALE; |
| 153 | Queue& queue = m_queues[QUEUE_STALE]; |
| 154 | entryInfo->queueIt = queue.insert(queue.end(), i); |
| 155 | m_entryInfoMap[i] = entryInfo; |
| 156 | } |
| 157 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 158 | } // namespace nfd::cs::priority_fifo |