blob: 9a4c6ed916f8eb118c4ade85c3c44eb5fec5edea [file] [log] [blame]
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5e4a02f2019-07-15 11:59:18 +00002/*
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -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#ifndef NFD_DAEMON_TABLE_CS_POLICY_HPP
27#define NFD_DAEMON_TABLE_CS_POLICY_HPP
28
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000029#include "cs-entry.hpp"
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050030
31namespace nfd {
32namespace cs {
33
34class Cs;
35
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040036/**
37 * \brief Represents a CS replacement policy
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050038 */
39class Policy : noncopyable
40{
Junxiao Shi52555b02016-11-25 21:39:06 +000041public: // registry
42 template<typename P>
43 static void
Junxiao Shi7ee00fb2016-12-04 21:23:00 +000044 registerPolicy(const std::string& policyName = P::POLICY_NAME)
Junxiao Shi52555b02016-11-25 21:39:06 +000045 {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040046 BOOST_ASSERT(!policyName.empty());
47 auto r = getRegistry().insert_or_assign(policyName, [] { return make_unique<P>(); });
48 BOOST_VERIFY(r.second);
Junxiao Shi52555b02016-11-25 21:39:06 +000049 }
50
Junxiao Shi7ee00fb2016-12-04 21:23:00 +000051 /** \return a cs::Policy identified by \p policyName,
52 * or nullptr if \p policyName is unknown
Junxiao Shi52555b02016-11-25 21:39:06 +000053 */
54 static unique_ptr<Policy>
Junxiao Shi7ee00fb2016-12-04 21:23:00 +000055 create(const std::string& policyName);
56
57 /** \return a list of available policy names
58 */
59 static std::set<std::string>
60 getPolicyNames();
Junxiao Shi52555b02016-11-25 21:39:06 +000061
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050062public:
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050063 virtual
Junxiao Shi2af9ca32016-08-25 21:51:02 +000064 ~Policy() = default;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050065
66 const std::string&
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000067 getName() const
68 {
69 return m_policyName;
70 }
71
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050072 /** \brief gets cs
73 */
74 Cs*
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000075 getCs() const
76 {
77 return m_cs;
78 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050079
80 /** \brief sets cs
81 */
82 void
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000083 setCs(Cs* cs)
84 {
85 m_cs = cs;
86 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050087
88 /** \brief gets hard limit (in number of entries)
89 */
90 size_t
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000091 getLimit() const
92 {
93 return m_limit;
94 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050095
96 /** \brief sets hard limit (in number of entries)
97 * \post getLimit() == nMaxEntries
98 * \post cs.size() <= getLimit()
99 *
100 * The policy may evict entries if necessary.
101 */
102 void
103 setLimit(size_t nMaxEntries);
104
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600105public:
106 /** \brief a reference to an CS entry
107 * \note operator< of EntryRef compares the Data name enclosed in the Entry.
108 */
109 using EntryRef = Table::const_iterator;
110
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500111 /** \brief emits when an entry is being evicted
112 *
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600113 * A policy implementation should emit this signal to cause CS to erase an entry from its index.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500114 * CS should connect to this signal and erase the entry upon signal emission.
115 */
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600116 signal::Signal<Policy, EntryRef> beforeEvict;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500117
118 /** \brief invoked by CS after a new entry is inserted
119 * \post cs.size() <= getLimit()
120 *
121 * The policy may evict entries if necessary.
122 * During this process, \p i might be evicted.
123 */
124 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600125 afterInsert(EntryRef i);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500126
127 /** \brief invoked by CS after an existing entry is refreshed by same Data
128 *
129 * The policy may witness this refresh to make better eviction decisions in the future.
130 */
131 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600132 afterRefresh(EntryRef i);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500133
134 /** \brief invoked by CS before an entry is erased due to management command
135 * \warning CS must not invoke this method if an entry is erased due to eviction.
136 */
137 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600138 beforeErase(EntryRef i);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500139
140 /** \brief invoked by CS before an entry is used to match a lookup
141 *
142 * The policy may witness this usage to make better eviction decisions in the future.
143 */
144 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600145 beforeUse(EntryRef i);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500146
147protected:
148 /** \brief invoked after a new entry is created in CS
149 *
150 * When overridden in a subclass, a policy implementation should decide whether to accept \p i.
151 * If \p i is accepted, it should be inserted into a cleanup index.
152 * Otherwise, \p beforeEvict signal should be emitted with \p i to inform CS to erase the entry.
153 * A policy implementation may decide to evict other entries by emitting \p beforeEvict signal,
154 * in order to keep CS size under limit.
155 */
156 virtual void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600157 doAfterInsert(EntryRef i) = 0;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500158
159 /** \brief invoked after an existing entry is refreshed by same Data
160 *
161 * When overridden in a subclass, a policy implementation may witness this operation
162 * and adjust its cleanup index.
163 */
164 virtual void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600165 doAfterRefresh(EntryRef i) = 0;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500166
167 /** \brief invoked before an entry is erased due to management command
168 * \note This will not be invoked for an entry being evicted by policy.
169 *
170 * When overridden in a subclass, a policy implementation should erase \p i
171 * from its cleanup index without emitted \p afterErase signal.
172 */
173 virtual void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600174 doBeforeErase(EntryRef i) = 0;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500175
176 /** \brief invoked before an entry is used to match a lookup
177 *
178 * When overridden in a subclass, a policy implementation may witness this operation
179 * and adjust its cleanup index.
180 */
181 virtual void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600182 doBeforeUse(EntryRef i) = 0;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500183
184 /** \brief evicts zero or more entries
185 * \post CS size does not exceed hard limit
186 */
187 virtual void
188 evictEntries() = 0;
189
190protected:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400191 explicit
192 Policy(std::string_view policyName);
193
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500194 DECLARE_SIGNAL_EMIT(beforeEvict)
195
Junxiao Shi52555b02016-11-25 21:39:06 +0000196private: // registry
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600197 using CreateFunc = std::function<unique_ptr<Policy>()>;
198 using Registry = std::map<std::string, CreateFunc>; // indexed by policy name
Junxiao Shi52555b02016-11-25 21:39:06 +0000199
200 static Registry&
201 getRegistry();
202
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500203private:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400204 const std::string m_policyName;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500205 size_t m_limit;
206 Cs* m_cs;
207};
208
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500209} // namespace cs
210} // namespace nfd
211
Junxiao Shi52555b02016-11-25 21:39:06 +0000212/** \brief registers a CS policy
213 * \param P a subclass of nfd::cs::Policy
214 */
215#define NFD_REGISTER_CS_POLICY(P) \
216static class NfdAuto ## P ## CsPolicyRegistrationClass \
217{ \
218public: \
219 NfdAuto ## P ## CsPolicyRegistrationClass() \
220 { \
221 ::nfd::cs::Policy::registerPolicy<P>(); \
222 } \
223} g_nfdAuto ## P ## CsPolicyRegistrationVariable
224
Junxiao Shi2af9ca32016-08-25 21:51:02 +0000225#endif // NFD_DAEMON_TABLE_CS_POLICY_HPP