Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 2af9ca3 | 2016-08-25 21:51:02 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 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 | |
| 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 | |
| 32 | namespace nfd { |
| 33 | namespace cs { |
| 34 | |
| 35 | class Cs; |
| 36 | |
| 37 | /** \brief represents a CS replacement policy |
| 38 | */ |
| 39 | class Policy : noncopyable |
| 40 | { |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 41 | public: // registry |
| 42 | template<typename P> |
| 43 | static void |
Junxiao Shi | 7ee00fb | 2016-12-04 21:23:00 +0000 | [diff] [blame] | 44 | registerPolicy(const std::string& policyName = P::POLICY_NAME) |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 45 | { |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 46 | Registry& registry = getRegistry(); |
Junxiao Shi | 7ee00fb | 2016-12-04 21:23:00 +0000 | [diff] [blame] | 47 | BOOST_ASSERT(registry.count(policyName) == 0); |
| 48 | registry[policyName] = [] { return make_unique<P>(); }; |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Junxiao Shi | 7ee00fb | 2016-12-04 21:23:00 +0000 | [diff] [blame] | 51 | /** \return a cs::Policy identified by \p policyName, |
| 52 | * or nullptr if \p policyName is unknown |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 53 | */ |
| 54 | static unique_ptr<Policy> |
Junxiao Shi | 7ee00fb | 2016-12-04 21:23:00 +0000 | [diff] [blame] | 55 | create(const std::string& policyName); |
| 56 | |
| 57 | /** \return a list of available policy names |
| 58 | */ |
| 59 | static std::set<std::string> |
| 60 | getPolicyNames(); |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 61 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 62 | public: |
| 63 | explicit |
| 64 | Policy(const std::string& policyName); |
| 65 | |
| 66 | virtual |
Junxiao Shi | 2af9ca3 | 2016-08-25 21:51:02 +0000 | [diff] [blame] | 67 | ~Policy() = default; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 68 | |
| 69 | const std::string& |
| 70 | getName() const; |
| 71 | |
| 72 | public: |
| 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 | |
| 133 | protected: |
| 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 | |
| 176 | protected: |
| 177 | DECLARE_SIGNAL_EMIT(beforeEvict) |
| 178 | |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 179 | private: // registry |
| 180 | typedef std::function<unique_ptr<Policy>()> CreateFunc; |
Junxiao Shi | 7ee00fb | 2016-12-04 21:23:00 +0000 | [diff] [blame] | 181 | typedef std::map<std::string, CreateFunc> Registry; // indexed by policy name |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 182 | |
| 183 | static Registry& |
| 184 | getRegistry(); |
| 185 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 186 | private: |
| 187 | std::string m_policyName; |
| 188 | size_t m_limit; |
| 189 | Cs* m_cs; |
| 190 | }; |
| 191 | |
| 192 | inline const std::string& |
| 193 | Policy::getName() const |
| 194 | { |
| 195 | return m_policyName; |
| 196 | } |
| 197 | |
| 198 | inline Cs* |
| 199 | Policy::getCs() const |
| 200 | { |
| 201 | return m_cs; |
| 202 | } |
| 203 | |
| 204 | inline void |
| 205 | Policy::setCs(Cs *cs) |
| 206 | { |
| 207 | m_cs = cs; |
| 208 | } |
| 209 | |
| 210 | inline size_t |
| 211 | Policy::getLimit() const |
| 212 | { |
| 213 | return m_limit; |
| 214 | } |
| 215 | |
| 216 | } // namespace cs |
| 217 | } // namespace nfd |
| 218 | |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 219 | /** \brief registers a CS policy |
| 220 | * \param P a subclass of nfd::cs::Policy |
| 221 | */ |
| 222 | #define NFD_REGISTER_CS_POLICY(P) \ |
| 223 | static class NfdAuto ## P ## CsPolicyRegistrationClass \ |
| 224 | { \ |
| 225 | public: \ |
| 226 | NfdAuto ## P ## CsPolicyRegistrationClass() \ |
| 227 | { \ |
| 228 | ::nfd::cs::Policy::registerPolicy<P>(); \ |
| 229 | } \ |
| 230 | } g_nfdAuto ## P ## CsPolicyRegistrationVariable |
| 231 | |
Junxiao Shi | 2af9ca3 | 2016-08-25 21:51:02 +0000 | [diff] [blame] | 232 | #endif // NFD_DAEMON_TABLE_CS_POLICY_HPP |