Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2019, 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 | |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 29 | #include "cs-entry.hpp" |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
| 32 | namespace cs { |
| 33 | |
| 34 | class Cs; |
| 35 | |
| 36 | /** \brief represents a CS replacement policy |
| 37 | */ |
| 38 | class Policy : noncopyable |
| 39 | { |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 40 | public: // registry |
| 41 | template<typename P> |
| 42 | static void |
Junxiao Shi | 7ee00fb | 2016-12-04 21:23:00 +0000 | [diff] [blame] | 43 | registerPolicy(const std::string& policyName = P::POLICY_NAME) |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 44 | { |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 45 | Registry& registry = getRegistry(); |
Junxiao Shi | 7ee00fb | 2016-12-04 21:23:00 +0000 | [diff] [blame] | 46 | BOOST_ASSERT(registry.count(policyName) == 0); |
| 47 | registry[policyName] = [] { return make_unique<P>(); }; |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Junxiao Shi | 7ee00fb | 2016-12-04 21:23:00 +0000 | [diff] [blame] | 50 | /** \return a cs::Policy identified by \p policyName, |
| 51 | * or nullptr if \p policyName is unknown |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 52 | */ |
| 53 | static unique_ptr<Policy> |
Junxiao Shi | 7ee00fb | 2016-12-04 21:23:00 +0000 | [diff] [blame] | 54 | create(const std::string& policyName); |
| 55 | |
| 56 | /** \return a list of available policy names |
| 57 | */ |
| 58 | static std::set<std::string> |
| 59 | getPolicyNames(); |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 60 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 61 | public: |
| 62 | explicit |
| 63 | Policy(const std::string& policyName); |
| 64 | |
| 65 | virtual |
Junxiao Shi | 2af9ca3 | 2016-08-25 21:51:02 +0000 | [diff] [blame] | 66 | ~Policy() = default; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 67 | |
| 68 | const std::string& |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 69 | getName() const |
| 70 | { |
| 71 | return m_policyName; |
| 72 | } |
| 73 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 74 | |
| 75 | public: |
| 76 | /** \brief gets cs |
| 77 | */ |
| 78 | Cs* |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 79 | getCs() const |
| 80 | { |
| 81 | return m_cs; |
| 82 | } |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 83 | |
| 84 | /** \brief sets cs |
| 85 | */ |
| 86 | void |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 87 | setCs(Cs* cs) |
| 88 | { |
| 89 | m_cs = cs; |
| 90 | } |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 91 | |
| 92 | /** \brief gets hard limit (in number of entries) |
| 93 | */ |
| 94 | size_t |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 95 | getLimit() const |
| 96 | { |
| 97 | return m_limit; |
| 98 | } |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 99 | |
| 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 | |
| 145 | protected: |
| 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 | |
| 188 | protected: |
| 189 | DECLARE_SIGNAL_EMIT(beforeEvict) |
| 190 | |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 191 | private: // registry |
| 192 | typedef std::function<unique_ptr<Policy>()> CreateFunc; |
Junxiao Shi | 7ee00fb | 2016-12-04 21:23:00 +0000 | [diff] [blame] | 193 | typedef std::map<std::string, CreateFunc> Registry; // indexed by policy name |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 194 | |
| 195 | static Registry& |
| 196 | getRegistry(); |
| 197 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 198 | private: |
| 199 | std::string m_policyName; |
| 200 | size_t m_limit; |
| 201 | Cs* m_cs; |
| 202 | }; |
| 203 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 204 | } // namespace cs |
| 205 | } // namespace nfd |
| 206 | |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 207 | /** \brief registers a CS policy |
| 208 | * \param P a subclass of nfd::cs::Policy |
| 209 | */ |
| 210 | #define NFD_REGISTER_CS_POLICY(P) \ |
| 211 | static class NfdAuto ## P ## CsPolicyRegistrationClass \ |
| 212 | { \ |
| 213 | public: \ |
| 214 | NfdAuto ## P ## CsPolicyRegistrationClass() \ |
| 215 | { \ |
| 216 | ::nfd::cs::Policy::registerPolicy<P>(); \ |
| 217 | } \ |
| 218 | } g_nfdAuto ## P ## CsPolicyRegistrationVariable |
| 219 | |
Junxiao Shi | 2af9ca3 | 2016-08-25 21:51:02 +0000 | [diff] [blame] | 220 | #endif // NFD_DAEMON_TABLE_CS_POLICY_HPP |