blob: 6149fd6bb7d12b25f9d8acbc44e09818b89bb461 [file] [log] [blame]
Junxiao Shi0e42c572014-10-05 20:33:48 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa3148082018-04-12 18:21:54 -04002/*
Davide Pesaventodeb54272022-03-11 18:51:05 -05003 * Copyright (c) 2014-2022, Regents of the University of California,
Davide Pesaventoa3148082018-04-12 18:21:54 -04004 * 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 Shi0e42c572014-10-05 20:33:48 -070010 *
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 Pesavento2cae8ca2019-04-18 20:48:05 -040027#include "common/city-hash.hpp"
28#include "common/global.hpp"
29#include "common/logger.hpp"
Junxiao Shi0e42c572014-10-05 20:33:48 -070030
Junxiao Shi0e42c572014-10-05 20:33:48 -070031namespace nfd {
32
Davide Pesaventoa3148082018-04-12 18:21:54 -040033NFD_LOG_INIT(DeadNonceList);
34
Davide Pesavento50a6af32019-02-21 00:04:40 -050035DeadNonceList::DeadNonceList(time::nanoseconds lifetime)
Junxiao Shi0e42c572014-10-05 20:33:48 -070036 : m_lifetime(lifetime)
Junxiao Shi0e42c572014-10-05 20:33:48 -070037 , m_capacity(INITIAL_CAPACITY)
38 , m_markInterval(m_lifetime / EXPECTED_MARK_COUNT)
39 , m_adjustCapacityInterval(m_lifetime)
40{
41 if (m_lifetime < MIN_LIFETIME) {
Davide Pesavento19779d82019-02-14 13:40:04 -050042 NDN_THROW(std::invalid_argument("lifetime is less than MIN_LIFETIME"));
Junxiao Shi0e42c572014-10-05 20:33:48 -070043 }
44
45 for (size_t i = 0; i < EXPECTED_MARK_COUNT; ++i) {
46 m_queue.push_back(MARK);
47 }
48
Davide Pesavento3dade002019-03-19 11:29:56 -060049 m_markEvent = getScheduler().schedule(m_markInterval, [this] { mark(); });
50 m_adjustCapacityEvent = getScheduler().schedule(m_adjustCapacityInterval, [this] { adjustCapacity(); });
Junxiao Shi0e42c572014-10-05 20:33:48 -070051
Junxiao Shi3a8b1b52014-11-08 21:26:07 -070052 BOOST_ASSERT_MSG(DEFAULT_LIFETIME >= MIN_LIFETIME, "DEFAULT_LIFETIME is too small");
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040053 static_assert(INITIAL_CAPACITY >= MIN_CAPACITY);
54 static_assert(INITIAL_CAPACITY <= MAX_CAPACITY);
Junxiao Shi3a8b1b52014-11-08 21:26:07 -070055 BOOST_ASSERT_MSG(static_cast<size_t>(MIN_CAPACITY * CAPACITY_UP) > MIN_CAPACITY,
56 "CAPACITY_UP must be able to increase from MIN_CAPACITY");
57 BOOST_ASSERT_MSG(static_cast<size_t>(MAX_CAPACITY * CAPACITY_DOWN) < MAX_CAPACITY,
58 "CAPACITY_DOWN must be able to decrease from MAX_CAPACITY");
59 BOOST_ASSERT_MSG(CAPACITY_UP > 1.0, "CAPACITY_UP must adjust up");
60 BOOST_ASSERT_MSG(CAPACITY_DOWN < 1.0, "CAPACITY_DOWN must adjust down");
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040061 static_assert(EVICT_LIMIT >= 1);
Junxiao Shi0e42c572014-10-05 20:33:48 -070062}
63
64size_t
65DeadNonceList::size() const
66{
Davide Pesaventod4123932021-05-28 18:48:37 -040067 return m_queue.size() - countMarks();
Junxiao Shi0e42c572014-10-05 20:33:48 -070068}
69
70bool
Davide Pesavento51cf75c2020-03-11 22:21:13 -040071DeadNonceList::has(const Name& name, Interest::Nonce nonce) const
Junxiao Shi0e42c572014-10-05 20:33:48 -070072{
73 Entry entry = DeadNonceList::makeEntry(name, nonce);
74 return m_ht.find(entry) != m_ht.end();
75}
76
77void
Davide Pesavento51cf75c2020-03-11 22:21:13 -040078DeadNonceList::add(const Name& name, Interest::Nonce nonce)
Junxiao Shi0e42c572014-10-05 20:33:48 -070079{
Junxiao Shi3a8b1b52014-11-08 21:26:07 -070080 Entry entry = DeadNonceList::makeEntry(name, nonce);
Varun Patil67ad70f2021-06-04 10:04:00 -070081 const auto iter = m_ht.find(entry);
Davide Pesaventoe08cc4c2021-06-08 18:22:46 -040082 bool isDuplicate = iter != m_ht.end();
83
84 NFD_LOG_TRACE("adding " << (isDuplicate ? "duplicate " : "") << name << " nonce=" << nonce);
85
86 if (isDuplicate) {
87 m_queue.relocate(m_queue.end(), m_index.project<Queue>(iter));
Varun Patil67ad70f2021-06-04 10:04:00 -070088 }
89 else {
90 m_queue.push_back(entry);
91 evictEntries();
92 }
Junxiao Shi0e42c572014-10-05 20:33:48 -070093}
94
95DeadNonceList::Entry
Davide Pesavento51cf75c2020-03-11 22:21:13 -040096DeadNonceList::makeEntry(const Name& name, Interest::Nonce nonce)
Junxiao Shi0e42c572014-10-05 20:33:48 -070097{
Davide Pesaventod4123932021-05-28 18:48:37 -040098 const auto& nameWire = name.wireEncode();
Davide Pesavento51cf75c2020-03-11 22:21:13 -040099 uint32_t n;
100 std::memcpy(&n, nonce.data(), sizeof(n));
Davide Pesaventodeb54272022-03-11 18:51:05 -0500101 return CityHash64WithSeed(reinterpret_cast<const char*>(nameWire.data()), nameWire.size(), n);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700102}
103
104size_t
105DeadNonceList::countMarks() const
106{
107 return m_ht.count(MARK);
108}
109
110void
111DeadNonceList::mark()
112{
113 m_queue.push_back(MARK);
Davide Pesaventod4123932021-05-28 18:48:37 -0400114 size_t nMarks = countMarks();
Junxiao Shi0e42c572014-10-05 20:33:48 -0700115 m_actualMarkCounts.insert(nMarks);
116
Davide Pesavento0e97a972015-02-22 03:50:21 +0100117 NFD_LOG_TRACE("mark nMarks=" << nMarks);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700118
Davide Pesavento3dade002019-03-19 11:29:56 -0600119 m_markEvent = getScheduler().schedule(m_markInterval, [this] { mark(); });
Junxiao Shi0e42c572014-10-05 20:33:48 -0700120}
121
122void
123DeadNonceList::adjustCapacity()
124{
Davide Pesaventoe08cc4c2021-06-08 18:22:46 -0400125 auto oldCapacity = m_capacity;
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400126 auto equalRange = m_actualMarkCounts.equal_range(EXPECTED_MARK_COUNT);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700127 if (equalRange.second == m_actualMarkCounts.begin()) {
128 // all counts are above expected count, adjust down
Davide Pesavento50a6af32019-02-21 00:04:40 -0500129 m_capacity = std::max(MIN_CAPACITY, static_cast<size_t>(m_capacity * CAPACITY_DOWN));
Junxiao Shi0e42c572014-10-05 20:33:48 -0700130 }
131 else if (equalRange.first == m_actualMarkCounts.end()) {
132 // all counts are below expected count, adjust up
Davide Pesavento50a6af32019-02-21 00:04:40 -0500133 m_capacity = std::min(MAX_CAPACITY, static_cast<size_t>(m_capacity * CAPACITY_UP));
Davide Pesaventoe08cc4c2021-06-08 18:22:46 -0400134 }
135
136 if (m_capacity != oldCapacity) {
137 NFD_LOG_TRACE("adjusting capacity " << oldCapacity << " -> " << m_capacity);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700138 }
139
140 m_actualMarkCounts.clear();
Davide Pesaventod4123932021-05-28 18:48:37 -0400141 evictEntries();
Junxiao Shi0e42c572014-10-05 20:33:48 -0700142
Davide Pesavento3dade002019-03-19 11:29:56 -0600143 m_adjustCapacityEvent = getScheduler().schedule(m_adjustCapacityInterval, [this] { adjustCapacity(); });
Junxiao Shi0e42c572014-10-05 20:33:48 -0700144}
145
146void
147DeadNonceList::evictEntries()
148{
Davide Pesaventod4123932021-05-28 18:48:37 -0400149 if (m_queue.size() <= m_capacity) // not over capacity
Junxiao Shi0e42c572014-10-05 20:33:48 -0700150 return;
151
Davide Pesaventod4123932021-05-28 18:48:37 -0400152 auto nEvict = std::min(m_queue.size() - m_capacity, EVICT_LIMIT);
Davide Pesaventoe08cc4c2021-06-08 18:22:46 -0400153 for (size_t i = 0; i < nEvict; i++) {
154 m_queue.pop_front();
Junxiao Shi0e42c572014-10-05 20:33:48 -0700155 }
156 BOOST_ASSERT(m_queue.size() >= m_capacity);
Davide Pesaventoe08cc4c2021-06-08 18:22:46 -0400157
158 NFD_LOG_TRACE("evicted=" << nEvict << " size=" << size() << " capacity=" << m_capacity);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700159}
160
161} // namespace nfd