blob: f50f85451335971ab2f268ad15b442d716a463ea [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/*
3 * Copyright (c) 2014-2019, 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
36/** \brief represents a CS replacement policy
37 */
38class Policy : noncopyable
39{
Junxiao Shi52555b02016-11-25 21:39:06 +000040public: // registry
41 template<typename P>
42 static void
Junxiao Shi7ee00fb2016-12-04 21:23:00 +000043 registerPolicy(const std::string& policyName = P::POLICY_NAME)
Junxiao Shi52555b02016-11-25 21:39:06 +000044 {
Junxiao Shi52555b02016-11-25 21:39:06 +000045 Registry& registry = getRegistry();
Junxiao Shi7ee00fb2016-12-04 21:23:00 +000046 BOOST_ASSERT(registry.count(policyName) == 0);
47 registry[policyName] = [] { return make_unique<P>(); };
Junxiao Shi52555b02016-11-25 21:39:06 +000048 }
49
Junxiao Shi7ee00fb2016-12-04 21:23:00 +000050 /** \return a cs::Policy identified by \p policyName,
51 * or nullptr if \p policyName is unknown
Junxiao Shi52555b02016-11-25 21:39:06 +000052 */
53 static unique_ptr<Policy>
Junxiao Shi7ee00fb2016-12-04 21:23:00 +000054 create(const std::string& policyName);
55
56 /** \return a list of available policy names
57 */
58 static std::set<std::string>
59 getPolicyNames();
Junxiao Shi52555b02016-11-25 21:39:06 +000060
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050061public:
62 explicit
63 Policy(const std::string& policyName);
64
65 virtual
Junxiao Shi2af9ca32016-08-25 21:51:02 +000066 ~Policy() = default;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050067
68 const std::string&
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000069 getName() const
70 {
71 return m_policyName;
72 }
73
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050074
75public:
76 /** \brief gets cs
77 */
78 Cs*
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000079 getCs() const
80 {
81 return m_cs;
82 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050083
84 /** \brief sets cs
85 */
86 void
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000087 setCs(Cs* cs)
88 {
89 m_cs = cs;
90 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050091
92 /** \brief gets hard limit (in number of entries)
93 */
94 size_t
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000095 getLimit() const
96 {
97 return m_limit;
98 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050099
100 /** \brief sets hard limit (in number of entries)
101 * \post getLimit() == nMaxEntries
102 * \post cs.size() <= getLimit()
103 *
104 * The policy may evict entries if necessary.
105 */
106 void
107 setLimit(size_t nMaxEntries);
108
109 /** \brief emits when an entry is being evicted
110 *
111 * A policy implementation should emit this signal to cause CS to erase the entry from its index.
112 * CS should connect to this signal and erase the entry upon signal emission.
113 */
114 signal::Signal<Policy, iterator> beforeEvict;
115
116 /** \brief invoked by CS after a new entry is inserted
117 * \post cs.size() <= getLimit()
118 *
119 * The policy may evict entries if necessary.
120 * During this process, \p i might be evicted.
121 */
122 void
123 afterInsert(iterator i);
124
125 /** \brief invoked by CS after an existing entry is refreshed by same Data
126 *
127 * The policy may witness this refresh to make better eviction decisions in the future.
128 */
129 void
130 afterRefresh(iterator i);
131
132 /** \brief invoked by CS before an entry is erased due to management command
133 * \warning CS must not invoke this method if an entry is erased due to eviction.
134 */
135 void
136 beforeErase(iterator i);
137
138 /** \brief invoked by CS before an entry is used to match a lookup
139 *
140 * The policy may witness this usage to make better eviction decisions in the future.
141 */
142 void
143 beforeUse(iterator i);
144
145protected:
146 /** \brief invoked after a new entry is created in CS
147 *
148 * When overridden in a subclass, a policy implementation should decide whether to accept \p i.
149 * If \p i is accepted, it should be inserted into a cleanup index.
150 * Otherwise, \p beforeEvict signal should be emitted with \p i to inform CS to erase the entry.
151 * A policy implementation may decide to evict other entries by emitting \p beforeEvict signal,
152 * in order to keep CS size under limit.
153 */
154 virtual void
155 doAfterInsert(iterator i) = 0;
156
157 /** \brief invoked after an existing entry is refreshed by same Data
158 *
159 * When overridden in a subclass, a policy implementation may witness this operation
160 * and adjust its cleanup index.
161 */
162 virtual void
163 doAfterRefresh(iterator i) = 0;
164
165 /** \brief invoked before an entry is erased due to management command
166 * \note This will not be invoked for an entry being evicted by policy.
167 *
168 * When overridden in a subclass, a policy implementation should erase \p i
169 * from its cleanup index without emitted \p afterErase signal.
170 */
171 virtual void
172 doBeforeErase(iterator i) = 0;
173
174 /** \brief invoked before an entry is used to match a lookup
175 *
176 * When overridden in a subclass, a policy implementation may witness this operation
177 * and adjust its cleanup index.
178 */
179 virtual void
180 doBeforeUse(iterator i) = 0;
181
182 /** \brief evicts zero or more entries
183 * \post CS size does not exceed hard limit
184 */
185 virtual void
186 evictEntries() = 0;
187
188protected:
189 DECLARE_SIGNAL_EMIT(beforeEvict)
190
Junxiao Shi52555b02016-11-25 21:39:06 +0000191private: // registry
192 typedef std::function<unique_ptr<Policy>()> CreateFunc;
Junxiao Shi7ee00fb2016-12-04 21:23:00 +0000193 typedef std::map<std::string, CreateFunc> Registry; // indexed by policy name
Junxiao Shi52555b02016-11-25 21:39:06 +0000194
195 static Registry&
196 getRegistry();
197
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500198private:
199 std::string m_policyName;
200 size_t m_limit;
201 Cs* m_cs;
202};
203
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500204} // namespace cs
205} // namespace nfd
206
Junxiao Shi52555b02016-11-25 21:39:06 +0000207/** \brief registers a CS policy
208 * \param P a subclass of nfd::cs::Policy
209 */
210#define NFD_REGISTER_CS_POLICY(P) \
211static class NfdAuto ## P ## CsPolicyRegistrationClass \
212{ \
213public: \
214 NfdAuto ## P ## CsPolicyRegistrationClass() \
215 { \
216 ::nfd::cs::Policy::registerPolicy<P>(); \
217 } \
218} g_nfdAuto ## P ## CsPolicyRegistrationVariable
219
Junxiao Shi2af9ca32016-08-25 21:51:02 +0000220#endif // NFD_DAEMON_TABLE_CS_POLICY_HPP