blob: ed0da6ef2912216272c6d854cfeaa9c294fb7337 [file] [log] [blame]
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoe4b22382018-06-10 14:37:24 -04002/*
Davide Pesaventoae430302023-05-11 01:42:46 -04003 * Copyright (c) 2014-2023, Regents of the University of California,
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -05004 * 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 Pesavento2cae8ca2019-04-18 20:48:05 -040028#include "common/global.hpp"
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050029
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040030namespace nfd::cs::priority_fifo {
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050031
Junxiao Shi52555b02016-11-25 21:39:06 +000032NFD_REGISTER_CS_POLICY(PriorityFifoPolicy);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050033
34PriorityFifoPolicy::PriorityFifoPolicy()
35 : Policy(POLICY_NAME)
36{
37}
38
Minsheng Zhang9c903e02015-10-03 18:16:05 -050039PriorityFifoPolicy::~PriorityFifoPolicy()
40{
41 for (auto entryInfoMapPair : m_entryInfoMap) {
42 delete entryInfoMapPair.second;
43 }
44}
45
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050046void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060047PriorityFifoPolicy::doAfterInsert(EntryRef i)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050048{
49 this->attachQueue(i);
50 this->evictEntries();
51}
52
53void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060054PriorityFifoPolicy::doAfterRefresh(EntryRef i)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050055{
56 this->detachQueue(i);
57 this->attachQueue(i);
58}
59
60void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060061PriorityFifoPolicy::doBeforeErase(EntryRef i)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050062{
63 this->detachQueue(i);
64}
65
66void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060067PriorityFifoPolicy::doBeforeUse(EntryRef i)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050068{
69 BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end());
70}
71
72void
73PriorityFifoPolicy::evictEntries()
74{
75 BOOST_ASSERT(this->getCs() != nullptr);
76
77 while (this->getCs()->size() > this->getLimit()) {
78 this->evictOne();
79 }
80}
81
82void
83PriorityFifoPolicy::evictOne()
84{
85 BOOST_ASSERT(!m_queues[QUEUE_UNSOLICITED].empty() ||
86 !m_queues[QUEUE_STALE].empty() ||
87 !m_queues[QUEUE_FIFO].empty());
88
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060089 EntryRef i;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050090 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
104void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600105PriorityFifoPolicy::attachQueue(EntryRef i)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500106{
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 Shi5e4a02f2019-07-15 11:59:18 +0000113 else if (!i->isFresh()) {
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500114 entryInfo->queueType = QUEUE_STALE;
115 }
116 else {
117 entryInfo->queueType = QUEUE_FIFO;
Davide Pesavento3dade002019-03-19 11:29:56 -0600118 entryInfo->moveStaleEventId = getScheduler().schedule(i->getData().getFreshnessPeriod(),
119 [=] { moveToStaleQueue(i); });
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500120 }
121
122 Queue& queue = m_queues[entryInfo->queueType];
123 entryInfo->queueIt = queue.insert(queue.end(), i);
124 m_entryInfoMap[i] = entryInfo;
125}
126
127void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600128PriorityFifoPolicy::detachQueue(EntryRef i)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500129{
130 BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end());
131
132 EntryInfo* entryInfo = m_entryInfoMap[i];
133 if (entryInfo->queueType == QUEUE_FIFO) {
Davide Pesavento3dade002019-03-19 11:29:56 -0600134 entryInfo->moveStaleEventId.cancel();
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500135 }
136
137 m_queues[entryInfo->queueType].erase(entryInfo->queueIt);
138 m_entryInfoMap.erase(i);
Minsheng Zhang9c903e02015-10-03 18:16:05 -0500139 delete entryInfo;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500140}
141
142void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600143PriorityFifoPolicy::moveToStaleQueue(EntryRef i)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500144{
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 Pesaventoe422f9e2022-06-03 01:30:23 -0400158} // namespace nfd::cs::priority_fifo