Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2021, Regents of the University of California, |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [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. |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 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 | #include "dead-nonce-list.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 27 | #include "common/city-hash.hpp" |
| 28 | #include "common/global.hpp" |
| 29 | #include "common/logger.hpp" |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 30 | |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 31 | namespace nfd { |
| 32 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 33 | NFD_LOG_INIT(DeadNonceList); |
| 34 | |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 35 | const time::nanoseconds DeadNonceList::DEFAULT_LIFETIME; |
| 36 | const time::nanoseconds DeadNonceList::MIN_LIFETIME; |
| 37 | const size_t DeadNonceList::INITIAL_CAPACITY; |
| 38 | const size_t DeadNonceList::MIN_CAPACITY; |
| 39 | const size_t DeadNonceList::MAX_CAPACITY; |
| 40 | const DeadNonceList::Entry DeadNonceList::MARK; |
| 41 | const size_t DeadNonceList::EXPECTED_MARK_COUNT; |
| 42 | const double DeadNonceList::CAPACITY_UP; |
| 43 | const double DeadNonceList::CAPACITY_DOWN; |
| 44 | const size_t DeadNonceList::EVICT_LIMIT; |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 45 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 46 | DeadNonceList::DeadNonceList(time::nanoseconds lifetime) |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 47 | : m_lifetime(lifetime) |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 48 | , m_capacity(INITIAL_CAPACITY) |
| 49 | , m_markInterval(m_lifetime / EXPECTED_MARK_COUNT) |
| 50 | , m_adjustCapacityInterval(m_lifetime) |
| 51 | { |
| 52 | if (m_lifetime < MIN_LIFETIME) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 53 | NDN_THROW(std::invalid_argument("lifetime is less than MIN_LIFETIME")); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | for (size_t i = 0; i < EXPECTED_MARK_COUNT; ++i) { |
| 57 | m_queue.push_back(MARK); |
| 58 | } |
| 59 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 60 | m_markEvent = getScheduler().schedule(m_markInterval, [this] { mark(); }); |
| 61 | m_adjustCapacityEvent = getScheduler().schedule(m_adjustCapacityInterval, [this] { adjustCapacity(); }); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 62 | |
Junxiao Shi | 3a8b1b5 | 2014-11-08 21:26:07 -0700 | [diff] [blame] | 63 | BOOST_ASSERT_MSG(DEFAULT_LIFETIME >= MIN_LIFETIME, "DEFAULT_LIFETIME is too small"); |
| 64 | static_assert(INITIAL_CAPACITY >= MIN_CAPACITY, "INITIAL_CAPACITY is too small"); |
| 65 | static_assert(INITIAL_CAPACITY <= MAX_CAPACITY, "INITIAL_CAPACITY is too large"); |
| 66 | BOOST_ASSERT_MSG(static_cast<size_t>(MIN_CAPACITY * CAPACITY_UP) > MIN_CAPACITY, |
| 67 | "CAPACITY_UP must be able to increase from MIN_CAPACITY"); |
| 68 | BOOST_ASSERT_MSG(static_cast<size_t>(MAX_CAPACITY * CAPACITY_DOWN) < MAX_CAPACITY, |
| 69 | "CAPACITY_DOWN must be able to decrease from MAX_CAPACITY"); |
| 70 | BOOST_ASSERT_MSG(CAPACITY_UP > 1.0, "CAPACITY_UP must adjust up"); |
| 71 | BOOST_ASSERT_MSG(CAPACITY_DOWN < 1.0, "CAPACITY_DOWN must adjust down"); |
| 72 | static_assert(EVICT_LIMIT >= 1, "EVICT_LIMIT must be at least 1"); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | size_t |
| 76 | DeadNonceList::size() const |
| 77 | { |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 78 | return m_queue.size() - countMarks(); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | bool |
Davide Pesavento | 51cf75c | 2020-03-11 22:21:13 -0400 | [diff] [blame] | 82 | DeadNonceList::has(const Name& name, Interest::Nonce nonce) const |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 83 | { |
| 84 | Entry entry = DeadNonceList::makeEntry(name, nonce); |
| 85 | return m_ht.find(entry) != m_ht.end(); |
| 86 | } |
| 87 | |
| 88 | void |
Davide Pesavento | 51cf75c | 2020-03-11 22:21:13 -0400 | [diff] [blame] | 89 | DeadNonceList::add(const Name& name, Interest::Nonce nonce) |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 90 | { |
Junxiao Shi | 3a8b1b5 | 2014-11-08 21:26:07 -0700 | [diff] [blame] | 91 | Entry entry = DeadNonceList::makeEntry(name, nonce); |
Varun Patil | 67ad70f | 2021-06-04 10:04:00 -0700 | [diff] [blame] | 92 | const auto iter = m_ht.find(entry); |
Davide Pesavento | e08cc4c | 2021-06-08 18:22:46 -0400 | [diff] [blame^] | 93 | bool isDuplicate = iter != m_ht.end(); |
| 94 | |
| 95 | NFD_LOG_TRACE("adding " << (isDuplicate ? "duplicate " : "") << name << " nonce=" << nonce); |
| 96 | |
| 97 | if (isDuplicate) { |
| 98 | m_queue.relocate(m_queue.end(), m_index.project<Queue>(iter)); |
Varun Patil | 67ad70f | 2021-06-04 10:04:00 -0700 | [diff] [blame] | 99 | } |
| 100 | else { |
| 101 | m_queue.push_back(entry); |
| 102 | evictEntries(); |
| 103 | } |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | DeadNonceList::Entry |
Davide Pesavento | 51cf75c | 2020-03-11 22:21:13 -0400 | [diff] [blame] | 107 | DeadNonceList::makeEntry(const Name& name, Interest::Nonce nonce) |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 108 | { |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 109 | const auto& nameWire = name.wireEncode(); |
Davide Pesavento | 51cf75c | 2020-03-11 22:21:13 -0400 | [diff] [blame] | 110 | uint32_t n; |
| 111 | std::memcpy(&n, nonce.data(), sizeof(n)); |
| 112 | return CityHash64WithSeed(reinterpret_cast<const char*>(nameWire.wire()), nameWire.size(), n); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | size_t |
| 116 | DeadNonceList::countMarks() const |
| 117 | { |
| 118 | return m_ht.count(MARK); |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | DeadNonceList::mark() |
| 123 | { |
| 124 | m_queue.push_back(MARK); |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 125 | size_t nMarks = countMarks(); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 126 | m_actualMarkCounts.insert(nMarks); |
| 127 | |
Davide Pesavento | 0e97a97 | 2015-02-22 03:50:21 +0100 | [diff] [blame] | 128 | NFD_LOG_TRACE("mark nMarks=" << nMarks); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 129 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 130 | m_markEvent = getScheduler().schedule(m_markInterval, [this] { mark(); }); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void |
| 134 | DeadNonceList::adjustCapacity() |
| 135 | { |
Davide Pesavento | e08cc4c | 2021-06-08 18:22:46 -0400 | [diff] [blame^] | 136 | auto oldCapacity = m_capacity; |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 137 | auto equalRange = m_actualMarkCounts.equal_range(EXPECTED_MARK_COUNT); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 138 | if (equalRange.second == m_actualMarkCounts.begin()) { |
| 139 | // all counts are above expected count, adjust down |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 140 | m_capacity = std::max(MIN_CAPACITY, static_cast<size_t>(m_capacity * CAPACITY_DOWN)); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 141 | } |
| 142 | else if (equalRange.first == m_actualMarkCounts.end()) { |
| 143 | // all counts are below expected count, adjust up |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 144 | m_capacity = std::min(MAX_CAPACITY, static_cast<size_t>(m_capacity * CAPACITY_UP)); |
Davide Pesavento | e08cc4c | 2021-06-08 18:22:46 -0400 | [diff] [blame^] | 145 | } |
| 146 | |
| 147 | if (m_capacity != oldCapacity) { |
| 148 | NFD_LOG_TRACE("adjusting capacity " << oldCapacity << " -> " << m_capacity); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | m_actualMarkCounts.clear(); |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 152 | evictEntries(); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 153 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 154 | m_adjustCapacityEvent = getScheduler().schedule(m_adjustCapacityInterval, [this] { adjustCapacity(); }); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void |
| 158 | DeadNonceList::evictEntries() |
| 159 | { |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 160 | if (m_queue.size() <= m_capacity) // not over capacity |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 161 | return; |
| 162 | |
Davide Pesavento | d412393 | 2021-05-28 18:48:37 -0400 | [diff] [blame] | 163 | auto nEvict = std::min(m_queue.size() - m_capacity, EVICT_LIMIT); |
Davide Pesavento | e08cc4c | 2021-06-08 18:22:46 -0400 | [diff] [blame^] | 164 | for (size_t i = 0; i < nEvict; i++) { |
| 165 | m_queue.pop_front(); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 166 | } |
| 167 | BOOST_ASSERT(m_queue.size() >= m_capacity); |
Davide Pesavento | e08cc4c | 2021-06-08 18:22:46 -0400 | [diff] [blame^] | 168 | |
| 169 | NFD_LOG_TRACE("evicted=" << nEvict << " size=" << size() << " capacity=" << m_capacity); |
Junxiao Shi | 0e42c57 | 2014-10-05 20:33:48 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | } // namespace nfd |