blob: 93e028e949e70834b63ec73f3303df93236631ae [file] [log] [blame]
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi2af9ca32016-08-25 21:51:02 +00003 * Copyright (c) 2014-2016, 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
29#include "cs-internal.hpp"
30#include "cs-entry-impl.hpp"
31
32namespace nfd {
33namespace cs {
34
35class Cs;
36
37/** \brief represents a CS replacement policy
38 */
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 {
Junxiao Shi52555b02016-11-25 21:39:06 +000046 Registry& registry = getRegistry();
Junxiao Shi7ee00fb2016-12-04 21:23:00 +000047 BOOST_ASSERT(registry.count(policyName) == 0);
48 registry[policyName] = [] { return make_unique<P>(); };
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:
63 explicit
64 Policy(const std::string& policyName);
65
66 virtual
Junxiao Shi2af9ca32016-08-25 21:51:02 +000067 ~Policy() = default;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050068
69 const std::string&
70 getName() const;
71
72public:
73 /** \brief gets cs
74 */
75 Cs*
76 getCs() const;
77
78 /** \brief sets cs
79 */
80 void
81 setCs(Cs *cs);
82
83 /** \brief gets hard limit (in number of entries)
84 */
85 size_t
86 getLimit() const;
87
88 /** \brief sets hard limit (in number of entries)
89 * \post getLimit() == nMaxEntries
90 * \post cs.size() <= getLimit()
91 *
92 * The policy may evict entries if necessary.
93 */
94 void
95 setLimit(size_t nMaxEntries);
96
97 /** \brief emits when an entry is being evicted
98 *
99 * A policy implementation should emit this signal to cause CS to erase the entry from its index.
100 * CS should connect to this signal and erase the entry upon signal emission.
101 */
102 signal::Signal<Policy, iterator> beforeEvict;
103
104 /** \brief invoked by CS after a new entry is inserted
105 * \post cs.size() <= getLimit()
106 *
107 * The policy may evict entries if necessary.
108 * During this process, \p i might be evicted.
109 */
110 void
111 afterInsert(iterator i);
112
113 /** \brief invoked by CS after an existing entry is refreshed by same Data
114 *
115 * The policy may witness this refresh to make better eviction decisions in the future.
116 */
117 void
118 afterRefresh(iterator i);
119
120 /** \brief invoked by CS before an entry is erased due to management command
121 * \warning CS must not invoke this method if an entry is erased due to eviction.
122 */
123 void
124 beforeErase(iterator i);
125
126 /** \brief invoked by CS before an entry is used to match a lookup
127 *
128 * The policy may witness this usage to make better eviction decisions in the future.
129 */
130 void
131 beforeUse(iterator i);
132
133protected:
134 /** \brief invoked after a new entry is created in CS
135 *
136 * When overridden in a subclass, a policy implementation should decide whether to accept \p i.
137 * If \p i is accepted, it should be inserted into a cleanup index.
138 * Otherwise, \p beforeEvict signal should be emitted with \p i to inform CS to erase the entry.
139 * A policy implementation may decide to evict other entries by emitting \p beforeEvict signal,
140 * in order to keep CS size under limit.
141 */
142 virtual void
143 doAfterInsert(iterator i) = 0;
144
145 /** \brief invoked after an existing entry is refreshed by same Data
146 *
147 * When overridden in a subclass, a policy implementation may witness this operation
148 * and adjust its cleanup index.
149 */
150 virtual void
151 doAfterRefresh(iterator i) = 0;
152
153 /** \brief invoked before an entry is erased due to management command
154 * \note This will not be invoked for an entry being evicted by policy.
155 *
156 * When overridden in a subclass, a policy implementation should erase \p i
157 * from its cleanup index without emitted \p afterErase signal.
158 */
159 virtual void
160 doBeforeErase(iterator i) = 0;
161
162 /** \brief invoked before an entry is used to match a lookup
163 *
164 * When overridden in a subclass, a policy implementation may witness this operation
165 * and adjust its cleanup index.
166 */
167 virtual void
168 doBeforeUse(iterator i) = 0;
169
170 /** \brief evicts zero or more entries
171 * \post CS size does not exceed hard limit
172 */
173 virtual void
174 evictEntries() = 0;
175
176protected:
177 DECLARE_SIGNAL_EMIT(beforeEvict)
178
Junxiao Shi52555b02016-11-25 21:39:06 +0000179private: // registry
180 typedef std::function<unique_ptr<Policy>()> CreateFunc;
Junxiao Shi7ee00fb2016-12-04 21:23:00 +0000181 typedef std::map<std::string, CreateFunc> Registry; // indexed by policy name
Junxiao Shi52555b02016-11-25 21:39:06 +0000182
183 static Registry&
184 getRegistry();
185
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500186private:
187 std::string m_policyName;
188 size_t m_limit;
189 Cs* m_cs;
190};
191
192inline const std::string&
193Policy::getName() const
194{
195 return m_policyName;
196}
197
198inline Cs*
199Policy::getCs() const
200{
201 return m_cs;
202}
203
204inline void
205Policy::setCs(Cs *cs)
206{
207 m_cs = cs;
208}
209
210inline size_t
211Policy::getLimit() const
212{
213 return m_limit;
214}
215
216} // namespace cs
217} // namespace nfd
218
Junxiao Shi52555b02016-11-25 21:39:06 +0000219/** \brief registers a CS policy
220 * \param P a subclass of nfd::cs::Policy
221 */
222#define NFD_REGISTER_CS_POLICY(P) \
223static class NfdAuto ## P ## CsPolicyRegistrationClass \
224{ \
225public: \
226 NfdAuto ## P ## CsPolicyRegistrationClass() \
227 { \
228 ::nfd::cs::Policy::registerPolicy<P>(); \
229 } \
230} g_nfdAuto ## P ## CsPolicyRegistrationVariable
231
Junxiao Shi2af9ca32016-08-25 21:51:02 +0000232#endif // NFD_DAEMON_TABLE_CS_POLICY_HPP