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