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 | /* |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, 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 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 31 | namespace nfd::cs { |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 32 | |
| 33 | class Cs; |
| 34 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 35 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 36 | * \brief Represents a CS replacement policy. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 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 | { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 45 | BOOST_ASSERT(!policyName.empty()); |
| 46 | auto r = getRegistry().insert_or_assign(policyName, [] { return make_unique<P>(); }); |
| 47 | BOOST_VERIFY(r.second); |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 50 | /** |
| 51 | * \brief Returns 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 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 57 | /** |
| 58 | * \brief Returns a list of available policy names. |
Junxiao Shi | 7ee00fb | 2016-12-04 21:23:00 +0000 | [diff] [blame] | 59 | */ |
| 60 | static std::set<std::string> |
| 61 | getPolicyNames(); |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 62 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 63 | public: |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 64 | virtual |
Junxiao Shi | 2af9ca3 | 2016-08-25 21:51:02 +0000 | [diff] [blame] | 65 | ~Policy() = default; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 66 | |
| 67 | const std::string& |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 68 | getName() const noexcept |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 69 | { |
| 70 | return m_policyName; |
| 71 | } |
| 72 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 73 | /** |
| 74 | * \brief Returns a pointer to the associated CS instance. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 75 | */ |
| 76 | Cs* |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 77 | getCs() const noexcept |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 78 | { |
| 79 | return m_cs; |
| 80 | } |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 81 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 82 | /** |
| 83 | * \brief Sets the associated CS instance. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 84 | */ |
| 85 | void |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 86 | setCs(Cs* cs) noexcept |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 87 | { |
| 88 | m_cs = cs; |
| 89 | } |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 90 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 91 | /** |
| 92 | * \brief Gets hard limit (in number of entries). |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 93 | */ |
| 94 | size_t |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 95 | getLimit() const noexcept |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 96 | { |
| 97 | return m_limit; |
| 98 | } |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 99 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 100 | /** \brief Sets hard limit (in number of entries). |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 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 | |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 109 | public: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 110 | /** \brief A reference to a CS entry. |
| 111 | * \note `operator<` of EntryRef compares the Data name enclosed in the Entry. |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 112 | */ |
| 113 | using EntryRef = Table::const_iterator; |
| 114 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 115 | /** \brief %Signal emitted when an entry is being evicted. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 116 | * |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 117 | * 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] | 118 | * CS should connect to this signal and erase the entry upon signal emission. |
| 119 | */ |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 120 | signal::Signal<Policy, EntryRef> beforeEvict; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 121 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 122 | /** \brief Invoked by CS after a new entry is inserted. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 123 | * \post cs.size() <= getLimit() |
| 124 | * |
| 125 | * The policy may evict entries if necessary. |
| 126 | * During this process, \p i might be evicted. |
| 127 | */ |
| 128 | void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 129 | afterInsert(EntryRef i); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 130 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 131 | /** \brief Invoked by CS after an existing entry is refreshed by same Data. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 132 | * |
| 133 | * The policy may witness this refresh to make better eviction decisions in the future. |
| 134 | */ |
| 135 | void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 136 | afterRefresh(EntryRef i); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 137 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 138 | /** \brief Invoked by CS before an entry is erased due to management command. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 139 | * \warning CS must not invoke this method if an entry is erased due to eviction. |
| 140 | */ |
| 141 | void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 142 | beforeErase(EntryRef i); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 143 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 144 | /** \brief Invoked by CS before an entry is used to match a lookup. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 145 | * |
| 146 | * The policy may witness this usage to make better eviction decisions in the future. |
| 147 | */ |
| 148 | void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 149 | beforeUse(EntryRef i); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 150 | |
| 151 | protected: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 152 | /** \brief Invoked after a new entry is created in CS. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 153 | * |
| 154 | * When overridden in a subclass, a policy implementation should decide whether to accept \p i. |
| 155 | * If \p i is accepted, it should be inserted into a cleanup index. |
| 156 | * Otherwise, \p beforeEvict signal should be emitted with \p i to inform CS to erase the entry. |
| 157 | * A policy implementation may decide to evict other entries by emitting \p beforeEvict signal, |
| 158 | * in order to keep CS size under limit. |
| 159 | */ |
| 160 | virtual void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 161 | doAfterInsert(EntryRef i) = 0; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 162 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 163 | /** \brief Invoked after an existing entry is refreshed by same Data. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 164 | * |
| 165 | * When overridden in a subclass, a policy implementation may witness this operation |
| 166 | * and adjust its cleanup index. |
| 167 | */ |
| 168 | virtual void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 169 | doAfterRefresh(EntryRef i) = 0; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 170 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 171 | /** \brief Invoked before an entry is erased due to management command. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 172 | * \note This will not be invoked for an entry being evicted by policy. |
| 173 | * |
| 174 | * When overridden in a subclass, a policy implementation should erase \p i |
| 175 | * from its cleanup index without emitted \p afterErase signal. |
| 176 | */ |
| 177 | virtual void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 178 | doBeforeErase(EntryRef i) = 0; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 179 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 180 | /** \brief Invoked before an entry is used to match a lookup. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 181 | * |
| 182 | * When overridden in a subclass, a policy implementation may witness this operation |
| 183 | * and adjust its cleanup index. |
| 184 | */ |
| 185 | virtual void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 186 | doBeforeUse(EntryRef i) = 0; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 187 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 188 | /** \brief Evicts zero or more entries. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 189 | * \post CS size does not exceed hard limit |
| 190 | */ |
| 191 | virtual void |
| 192 | evictEntries() = 0; |
| 193 | |
| 194 | protected: |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 195 | explicit |
| 196 | Policy(std::string_view policyName); |
| 197 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 198 | DECLARE_SIGNAL_EMIT(beforeEvict) |
| 199 | |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 200 | private: // registry |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 201 | using CreateFunc = std::function<unique_ptr<Policy>()>; |
| 202 | using Registry = std::map<std::string, CreateFunc>; // indexed by policy name |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 203 | |
| 204 | static Registry& |
| 205 | getRegistry(); |
| 206 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 207 | private: |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 208 | const std::string m_policyName; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 209 | size_t m_limit; |
| 210 | Cs* m_cs; |
| 211 | }; |
| 212 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 213 | } // namespace nfd::cs |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 214 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 215 | /** \brief Registers a CS policy. |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 216 | * \param P a subclass of nfd::cs::Policy |
| 217 | */ |
| 218 | #define NFD_REGISTER_CS_POLICY(P) \ |
| 219 | static class NfdAuto ## P ## CsPolicyRegistrationClass \ |
| 220 | { \ |
| 221 | public: \ |
| 222 | NfdAuto ## P ## CsPolicyRegistrationClass() \ |
| 223 | { \ |
| 224 | ::nfd::cs::Policy::registerPolicy<P>(); \ |
| 225 | } \ |
| 226 | } g_nfdAuto ## P ## CsPolicyRegistrationVariable |
| 227 | |
Junxiao Shi | 2af9ca3 | 2016-08-25 21:51:02 +0000 | [diff] [blame] | 228 | #endif // NFD_DAEMON_TABLE_CS_POLICY_HPP |