cs-policy-priority-fifo.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2023, Regents of the University of California,
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 
27 #include "cs.hpp"
28 #include "common/global.hpp"
29 
31 
33 
35  : Policy(POLICY_NAME)
36 {
37 }
38 
40 {
41  for (auto entryInfoMapPair : m_entryInfoMap) {
42  delete entryInfoMapPair.second;
43  }
44 }
45 
46 void
47 PriorityFifoPolicy::doAfterInsert(EntryRef i)
48 {
49  this->attachQueue(i);
50  this->evictEntries();
51 }
52 
53 void
54 PriorityFifoPolicy::doAfterRefresh(EntryRef i)
55 {
56  this->detachQueue(i);
57  this->attachQueue(i);
58 }
59 
60 void
61 PriorityFifoPolicy::doBeforeErase(EntryRef i)
62 {
63  this->detachQueue(i);
64 }
65 
66 void
67 PriorityFifoPolicy::doBeforeUse(EntryRef i)
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 
89  EntryRef i;
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
105 PriorityFifoPolicy::attachQueue(EntryRef i)
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  }
113  else if (!i->isFresh()) {
114  entryInfo->queueType = QUEUE_STALE;
115  }
116  else {
117  entryInfo->queueType = QUEUE_FIFO;
118  entryInfo->moveStaleEventId = getScheduler().schedule(i->getData().getFreshnessPeriod(),
119  [=] { moveToStaleQueue(i); });
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
128 PriorityFifoPolicy::detachQueue(EntryRef i)
129 {
130  BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end());
131 
132  EntryInfo* entryInfo = m_entryInfoMap[i];
133  if (entryInfo->queueType == QUEUE_FIFO) {
134  entryInfo->moveStaleEventId.cancel();
135  }
136 
137  m_queues[entryInfo->queueType].erase(entryInfo->queueIt);
138  m_entryInfoMap.erase(i);
139  delete entryInfo;
140 }
141 
142 void
143 PriorityFifoPolicy::moveToStaleQueue(EntryRef i)
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 
158 } // namespace nfd::cs::priority_fifo
Represents a CS replacement policy.
Definition: cs-policy.hpp:43
Cs * getCs() const noexcept
Returns a pointer to the associated CS instance.
Definition: cs-policy.hpp:81
Table::const_iterator EntryRef
A reference to a CS entry.
Definition: cs-policy.hpp:117
size_t getLimit() const noexcept
Gets hard limit (in number of entries).
Definition: cs-policy.hpp:99
signal::Signal< Policy, EntryRef > beforeEvict
Signal emitted when an entry is being evicted.
Definition: cs-policy.hpp:124
Priority First-In-First-Out (FIFO) replacement policy.
#define NFD_REGISTER_CS_POLICY(P)
Registers a CS policy.
Definition: cs-policy.hpp:222
std::list< Policy::EntryRef > Queue
ndn::Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
Definition: global.cpp:45