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 | /** \brief gets cs |
| 75 | */ |
| 76 | Cs* |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 77 | getCs() const |
| 78 | { |
| 79 | return m_cs; |
| 80 | } |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 81 | |
| 82 | /** \brief sets cs |
| 83 | */ |
| 84 | void |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 85 | setCs(Cs* cs) |
| 86 | { |
| 87 | m_cs = cs; |
| 88 | } |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 89 | |
| 90 | /** \brief gets hard limit (in number of entries) |
| 91 | */ |
| 92 | size_t |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 93 | getLimit() const |
| 94 | { |
| 95 | return m_limit; |
| 96 | } |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 97 | |
| 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 Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame^] | 107 | public: |
| 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 Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 113 | /** \brief emits when an entry is being evicted |
| 114 | * |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame^] | 115 | * A policy implementation should emit this signal to cause CS to erase an entry from its index. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 116 | * CS should connect to this signal and erase the entry upon signal emission. |
| 117 | */ |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame^] | 118 | signal::Signal<Policy, EntryRef> beforeEvict; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 119 | |
| 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 Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame^] | 127 | afterInsert(EntryRef i); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 128 | |
| 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 Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame^] | 134 | afterRefresh(EntryRef i); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 135 | |
| 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 Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame^] | 140 | beforeErase(EntryRef i); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 141 | |
| 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 Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame^] | 147 | beforeUse(EntryRef i); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 148 | |
| 149 | protected: |
| 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 Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame^] | 159 | doAfterInsert(EntryRef i) = 0; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 160 | |
| 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 Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame^] | 167 | doAfterRefresh(EntryRef i) = 0; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 168 | |
| 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 Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame^] | 176 | doBeforeErase(EntryRef i) = 0; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 177 | |
| 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 Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame^] | 184 | doBeforeUse(EntryRef i) = 0; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 185 | |
| 186 | /** \brief evicts zero or more entries |
| 187 | * \post CS size does not exceed hard limit |
| 188 | */ |
| 189 | virtual void |
| 190 | evictEntries() = 0; |
| 191 | |
| 192 | protected: |
| 193 | DECLARE_SIGNAL_EMIT(beforeEvict) |
| 194 | |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 195 | private: // registry |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame^] | 196 | using CreateFunc = std::function<unique_ptr<Policy>()>; |
| 197 | using Registry = std::map<std::string, CreateFunc>; // indexed by policy name |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 198 | |
| 199 | static Registry& |
| 200 | getRegistry(); |
| 201 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 202 | private: |
| 203 | std::string m_policyName; |
| 204 | size_t m_limit; |
| 205 | Cs* m_cs; |
| 206 | }; |
| 207 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 208 | } // namespace cs |
| 209 | } // namespace nfd |
| 210 | |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 211 | /** \brief registers a CS policy |
| 212 | * \param P a subclass of nfd::cs::Policy |
| 213 | */ |
| 214 | #define NFD_REGISTER_CS_POLICY(P) \ |
| 215 | static class NfdAuto ## P ## CsPolicyRegistrationClass \ |
| 216 | { \ |
| 217 | public: \ |
| 218 | NfdAuto ## P ## CsPolicyRegistrationClass() \ |
| 219 | { \ |
| 220 | ::nfd::cs::Policy::registerPolicy<P>(); \ |
| 221 | } \ |
| 222 | } g_nfdAuto ## P ## CsPolicyRegistrationVariable |
| 223 | |
Junxiao Shi | 2af9ca3 | 2016-08-25 21:51:02 +0000 | [diff] [blame] | 224 | #endif // NFD_DAEMON_TABLE_CS_POLICY_HPP |