blob: fb599907507e89118ce47d4d78baad25d91b8388 [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 /** \brief gets cs
75 */
76 Cs*
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000077 getCs() const
78 {
79 return m_cs;
80 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050081
82 /** \brief sets cs
83 */
84 void
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000085 setCs(Cs* cs)
86 {
87 m_cs = cs;
88 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050089
90 /** \brief gets hard limit (in number of entries)
91 */
92 size_t
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000093 getLimit() const
94 {
95 return m_limit;
96 }
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050097
98 /** \brief sets hard limit (in number of entries)
99 * \post getLimit() == nMaxEntries
100 * \post cs.size() <= getLimit()
101 *
102 * The policy may evict entries if necessary.
103 */
104 void
105 setLimit(size_t nMaxEntries);
106
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600107public:
108 /** \brief a reference to an CS entry
109 * \note operator< of EntryRef compares the Data name enclosed in the Entry.
110 */
111 using EntryRef = Table::const_iterator;
112
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500113 /** \brief emits when an entry is being evicted
114 *
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600115 * A policy implementation should emit this signal to cause CS to erase an entry from its index.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500116 * CS should connect to this signal and erase the entry upon signal emission.
117 */
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600118 signal::Signal<Policy, EntryRef> beforeEvict;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500119
120 /** \brief invoked by CS after a new entry is inserted
121 * \post cs.size() <= getLimit()
122 *
123 * The policy may evict entries if necessary.
124 * During this process, \p i might be evicted.
125 */
126 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600127 afterInsert(EntryRef i);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500128
129 /** \brief invoked by CS after an existing entry is refreshed by same Data
130 *
131 * The policy may witness this refresh to make better eviction decisions in the future.
132 */
133 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600134 afterRefresh(EntryRef i);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500135
136 /** \brief invoked by CS before an entry is erased due to management command
137 * \warning CS must not invoke this method if an entry is erased due to eviction.
138 */
139 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600140 beforeErase(EntryRef i);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500141
142 /** \brief invoked by CS before an entry is used to match a lookup
143 *
144 * The policy may witness this usage to make better eviction decisions in the future.
145 */
146 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600147 beforeUse(EntryRef i);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500148
149protected:
150 /** \brief invoked after a new entry is created in CS
151 *
152 * When overridden in a subclass, a policy implementation should decide whether to accept \p i.
153 * If \p i is accepted, it should be inserted into a cleanup index.
154 * Otherwise, \p beforeEvict signal should be emitted with \p i to inform CS to erase the entry.
155 * A policy implementation may decide to evict other entries by emitting \p beforeEvict signal,
156 * in order to keep CS size under limit.
157 */
158 virtual void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600159 doAfterInsert(EntryRef i) = 0;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500160
161 /** \brief invoked after an existing entry is refreshed by same Data
162 *
163 * When overridden in a subclass, a policy implementation may witness this operation
164 * and adjust its cleanup index.
165 */
166 virtual void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600167 doAfterRefresh(EntryRef i) = 0;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500168
169 /** \brief invoked before an entry is erased due to management command
170 * \note This will not be invoked for an entry being evicted by policy.
171 *
172 * When overridden in a subclass, a policy implementation should erase \p i
173 * from its cleanup index without emitted \p afterErase signal.
174 */
175 virtual void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600176 doBeforeErase(EntryRef i) = 0;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500177
178 /** \brief invoked before an entry is used to match a lookup
179 *
180 * When overridden in a subclass, a policy implementation may witness this operation
181 * and adjust its cleanup index.
182 */
183 virtual void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600184 doBeforeUse(EntryRef i) = 0;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500185
186 /** \brief evicts zero or more entries
187 * \post CS size does not exceed hard limit
188 */
189 virtual void
190 evictEntries() = 0;
191
192protected:
193 DECLARE_SIGNAL_EMIT(beforeEvict)
194
Junxiao Shi52555b02016-11-25 21:39:06 +0000195private: // registry
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600196 using CreateFunc = std::function<unique_ptr<Policy>()>;
197 using Registry = std::map<std::string, CreateFunc>; // indexed by policy name
Junxiao Shi52555b02016-11-25 21:39:06 +0000198
199 static Registry&
200 getRegistry();
201
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500202private:
203 std::string m_policyName;
204 size_t m_limit;
205 Cs* m_cs;
206};
207
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500208} // namespace cs
209} // namespace nfd
210
Junxiao Shi52555b02016-11-25 21:39:06 +0000211/** \brief registers a CS policy
212 * \param P a subclass of nfd::cs::Policy
213 */
214#define NFD_REGISTER_CS_POLICY(P) \
215static class NfdAuto ## P ## CsPolicyRegistrationClass \
216{ \
217public: \
218 NfdAuto ## P ## CsPolicyRegistrationClass() \
219 { \
220 ::nfd::cs::Policy::registerPolicy<P>(); \
221 } \
222} g_nfdAuto ## P ## CsPolicyRegistrationVariable
223
Junxiao Shi2af9ca32016-08-25 21:51:02 +0000224#endif // NFD_DAEMON_TABLE_CS_POLICY_HPP