blob: 720a41597737d1a2bf22d7286cdcfa1c043a50a4 [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 Pesavento19779d82019-02-14 13:40:04 -05003 * Copyright (c) 2014-2019, 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"
27#include "core/city-hash.hpp"
28#include "core/logger.hpp"
29
Junxiao Shi0e42c572014-10-05 20:33:48 -070030namespace nfd {
31
Davide Pesaventoa3148082018-04-12 18:21:54 -040032NFD_LOG_INIT(DeadNonceList);
33
Davide Pesaventoe4b22382018-06-10 14:37:24 -040034const time::nanoseconds DeadNonceList::DEFAULT_LIFETIME = 6_s;
35const time::nanoseconds DeadNonceList::MIN_LIFETIME = 1_ms;
Davide Pesavento50a6af32019-02-21 00:04:40 -050036const size_t DeadNonceList::INITIAL_CAPACITY = 1 << 7;
37const size_t DeadNonceList::MIN_CAPACITY = 1 << 3;
38const size_t DeadNonceList::MAX_CAPACITY = 1 << 24;
Junxiao Shi0e42c572014-10-05 20:33:48 -070039const DeadNonceList::Entry DeadNonceList::MARK = 0;
40const size_t DeadNonceList::EXPECTED_MARK_COUNT = 5;
41const double DeadNonceList::CAPACITY_UP = 1.2;
42const double DeadNonceList::CAPACITY_DOWN = 0.9;
Davide Pesavento50a6af32019-02-21 00:04:40 -050043const size_t DeadNonceList::EVICT_LIMIT = 1 << 6;
Junxiao Shi0e42c572014-10-05 20:33:48 -070044
Davide Pesavento50a6af32019-02-21 00:04:40 -050045DeadNonceList::DeadNonceList(time::nanoseconds lifetime)
Junxiao Shi0e42c572014-10-05 20:33:48 -070046 : m_lifetime(lifetime)
47 , m_queue(m_index.get<0>())
48 , m_ht(m_index.get<1>())
49 , m_capacity(INITIAL_CAPACITY)
50 , m_markInterval(m_lifetime / EXPECTED_MARK_COUNT)
51 , m_adjustCapacityInterval(m_lifetime)
52{
53 if (m_lifetime < MIN_LIFETIME) {
Davide Pesavento19779d82019-02-14 13:40:04 -050054 NDN_THROW(std::invalid_argument("lifetime is less than MIN_LIFETIME"));
Junxiao Shi0e42c572014-10-05 20:33:48 -070055 }
56
57 for (size_t i = 0; i < EXPECTED_MARK_COUNT; ++i) {
58 m_queue.push_back(MARK);
59 }
60
Davide Pesaventoe4b22382018-06-10 14:37:24 -040061 m_markEvent = scheduler::schedule(m_markInterval, [this] { mark(); });
62 m_adjustCapacityEvent = scheduler::schedule(m_adjustCapacityInterval, [this] { adjustCapacity(); });
Junxiao Shi0e42c572014-10-05 20:33:48 -070063}
64
65DeadNonceList::~DeadNonceList()
66{
67 scheduler::cancel(m_markEvent);
68 scheduler::cancel(m_adjustCapacityEvent);
69
Junxiao Shi3a8b1b52014-11-08 21:26:07 -070070 BOOST_ASSERT_MSG(DEFAULT_LIFETIME >= MIN_LIFETIME, "DEFAULT_LIFETIME is too small");
71 static_assert(INITIAL_CAPACITY >= MIN_CAPACITY, "INITIAL_CAPACITY is too small");
72 static_assert(INITIAL_CAPACITY <= MAX_CAPACITY, "INITIAL_CAPACITY is too large");
73 BOOST_ASSERT_MSG(static_cast<size_t>(MIN_CAPACITY * CAPACITY_UP) > MIN_CAPACITY,
74 "CAPACITY_UP must be able to increase from MIN_CAPACITY");
75 BOOST_ASSERT_MSG(static_cast<size_t>(MAX_CAPACITY * CAPACITY_DOWN) < MAX_CAPACITY,
76 "CAPACITY_DOWN must be able to decrease from MAX_CAPACITY");
77 BOOST_ASSERT_MSG(CAPACITY_UP > 1.0, "CAPACITY_UP must adjust up");
78 BOOST_ASSERT_MSG(CAPACITY_DOWN < 1.0, "CAPACITY_DOWN must adjust down");
79 static_assert(EVICT_LIMIT >= 1, "EVICT_LIMIT must be at least 1");
Junxiao Shi0e42c572014-10-05 20:33:48 -070080}
81
82size_t
83DeadNonceList::size() const
84{
85 return m_queue.size() - this->countMarks();
86}
87
88bool
89DeadNonceList::has(const Name& name, uint32_t nonce) const
90{
91 Entry entry = DeadNonceList::makeEntry(name, nonce);
92 return m_ht.find(entry) != m_ht.end();
93}
94
95void
96DeadNonceList::add(const Name& name, uint32_t nonce)
97{
Junxiao Shi3a8b1b52014-11-08 21:26:07 -070098 Entry entry = DeadNonceList::makeEntry(name, nonce);
Junxiao Shi0e42c572014-10-05 20:33:48 -070099 m_queue.push_back(entry);
100
101 this->evictEntries();
102}
103
104DeadNonceList::Entry
105DeadNonceList::makeEntry(const Name& name, uint32_t nonce)
106{
107 Block nameWire = name.wireEncode();
108 return CityHash64WithSeed(reinterpret_cast<const char*>(nameWire.wire()), nameWire.size(),
109 static_cast<uint64_t>(nonce));
110}
111
112size_t
113DeadNonceList::countMarks() const
114{
115 return m_ht.count(MARK);
116}
117
118void
119DeadNonceList::mark()
120{
121 m_queue.push_back(MARK);
122 size_t nMarks = this->countMarks();
123 m_actualMarkCounts.insert(nMarks);
124
Davide Pesavento0e97a972015-02-22 03:50:21 +0100125 NFD_LOG_TRACE("mark nMarks=" << nMarks);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700126
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400127 m_markEvent = scheduler::schedule(m_markInterval, [this] { mark(); });
Junxiao Shi0e42c572014-10-05 20:33:48 -0700128}
129
130void
131DeadNonceList::adjustCapacity()
132{
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400133 auto equalRange = m_actualMarkCounts.equal_range(EXPECTED_MARK_COUNT);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700134 if (equalRange.second == m_actualMarkCounts.begin()) {
135 // all counts are above expected count, adjust down
Davide Pesavento50a6af32019-02-21 00:04:40 -0500136 m_capacity = std::max(MIN_CAPACITY, static_cast<size_t>(m_capacity * CAPACITY_DOWN));
Davide Pesavento0e97a972015-02-22 03:50:21 +0100137 NFD_LOG_TRACE("adjustCapacity DOWN capacity=" << m_capacity);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700138 }
139 else if (equalRange.first == m_actualMarkCounts.end()) {
140 // all counts are below expected count, adjust up
Davide Pesavento50a6af32019-02-21 00:04:40 -0500141 m_capacity = std::min(MAX_CAPACITY, static_cast<size_t>(m_capacity * CAPACITY_UP));
Davide Pesavento0e97a972015-02-22 03:50:21 +0100142 NFD_LOG_TRACE("adjustCapacity UP capacity=" << m_capacity);
Junxiao Shi0e42c572014-10-05 20:33:48 -0700143 }
144
145 m_actualMarkCounts.clear();
Junxiao Shi0e42c572014-10-05 20:33:48 -0700146 this->evictEntries();
147
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400148 m_adjustCapacityEvent = scheduler::schedule(m_adjustCapacityInterval, [this] { adjustCapacity(); });
Junxiao Shi0e42c572014-10-05 20:33:48 -0700149}
150
151void
152DeadNonceList::evictEntries()
153{
154 ssize_t nOverCapacity = m_queue.size() - m_capacity;
155 if (nOverCapacity <= 0) // not over capacity
156 return;
157
158 for (ssize_t nEvict = std::min<ssize_t>(nOverCapacity, EVICT_LIMIT); nEvict > 0; --nEvict) {
159 m_queue.erase(m_queue.begin());
160 }
161 BOOST_ASSERT(m_queue.size() >= m_capacity);
162}
163
164} // namespace nfd