blob: 4405e66ce87594433dbdc1cc0e3d0cc07e2f4aaa [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 Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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 +000032const std::string PriorityFifoPolicy::POLICY_NAME = "priority_fifo";
33NFD_REGISTER_CS_POLICY(PriorityFifoPolicy);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050034
35PriorityFifoPolicy::PriorityFifoPolicy()
36 : Policy(POLICY_NAME)
37{
38}
39
Minsheng Zhang9c903e02015-10-03 18:16:05 -050040PriorityFifoPolicy::~PriorityFifoPolicy()
41{
42 for (auto entryInfoMapPair : m_entryInfoMap) {
43 delete entryInfoMapPair.second;
44 }
45}
46
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050047void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060048PriorityFifoPolicy::doAfterInsert(EntryRef i)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050049{
50 this->attachQueue(i);
51 this->evictEntries();
52}
53
54void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060055PriorityFifoPolicy::doAfterRefresh(EntryRef i)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050056{
57 this->detachQueue(i);
58 this->attachQueue(i);
59}
60
61void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060062PriorityFifoPolicy::doBeforeErase(EntryRef i)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050063{
64 this->detachQueue(i);
65}
66
67void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060068PriorityFifoPolicy::doBeforeUse(EntryRef i)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050069{
70 BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end());
71}
72
73void
74PriorityFifoPolicy::evictEntries()
75{
76 BOOST_ASSERT(this->getCs() != nullptr);
77
78 while (this->getCs()->size() > this->getLimit()) {
79 this->evictOne();
80 }
81}
82
83void
84PriorityFifoPolicy::evictOne()
85{
86 BOOST_ASSERT(!m_queues[QUEUE_UNSOLICITED].empty() ||
87 !m_queues[QUEUE_STALE].empty() ||
88 !m_queues[QUEUE_FIFO].empty());
89
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060090 EntryRef i;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050091 if (!m_queues[QUEUE_UNSOLICITED].empty()) {
92 i = m_queues[QUEUE_UNSOLICITED].front();
93 }
94 else if (!m_queues[QUEUE_STALE].empty()) {
95 i = m_queues[QUEUE_STALE].front();
96 }
97 else if (!m_queues[QUEUE_FIFO].empty()) {
98 i = m_queues[QUEUE_FIFO].front();
99 }
100
101 this->detachQueue(i);
102 this->emitSignal(beforeEvict, i);
103}
104
105void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600106PriorityFifoPolicy::attachQueue(EntryRef i)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500107{
108 BOOST_ASSERT(m_entryInfoMap.find(i) == m_entryInfoMap.end());
109
110 EntryInfo* entryInfo = new EntryInfo();
111 if (i->isUnsolicited()) {
112 entryInfo->queueType = QUEUE_UNSOLICITED;
113 }
Junxiao Shi5e4a02f2019-07-15 11:59:18 +0000114 else if (!i->isFresh()) {
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500115 entryInfo->queueType = QUEUE_STALE;
116 }
117 else {
118 entryInfo->queueType = QUEUE_FIFO;
Davide Pesavento3dade002019-03-19 11:29:56 -0600119 entryInfo->moveStaleEventId = getScheduler().schedule(i->getData().getFreshnessPeriod(),
120 [=] { moveToStaleQueue(i); });
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500121 }
122
123 Queue& queue = m_queues[entryInfo->queueType];
124 entryInfo->queueIt = queue.insert(queue.end(), i);
125 m_entryInfoMap[i] = entryInfo;
126}
127
128void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600129PriorityFifoPolicy::detachQueue(EntryRef i)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500130{
131 BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end());
132
133 EntryInfo* entryInfo = m_entryInfoMap[i];
134 if (entryInfo->queueType == QUEUE_FIFO) {
Davide Pesavento3dade002019-03-19 11:29:56 -0600135 entryInfo->moveStaleEventId.cancel();
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500136 }
137
138 m_queues[entryInfo->queueType].erase(entryInfo->queueIt);
139 m_entryInfoMap.erase(i);
Minsheng Zhang9c903e02015-10-03 18:16:05 -0500140 delete entryInfo;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500141}
142
143void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600144PriorityFifoPolicy::moveToStaleQueue(EntryRef i)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500145{
146 BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end());
147
148 EntryInfo* entryInfo = m_entryInfoMap[i];
149 BOOST_ASSERT(entryInfo->queueType == QUEUE_FIFO);
150
151 m_queues[QUEUE_FIFO].erase(entryInfo->queueIt);
152
153 entryInfo->queueType = QUEUE_STALE;
154 Queue& queue = m_queues[QUEUE_STALE];
155 entryInfo->queueIt = queue.insert(queue.end(), i);
156 m_entryInfoMap[i] = entryInfo;
157}
158
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400159} // namespace nfd::cs::priority_fifo