blob: 71dfcb376795523cd12d09a44a44c65882d0d2d0 [file] [log] [blame]
Junxiao Shi0e42c572014-10-05 20:33:48 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California,
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#include "dead-nonce-list.hpp"
27#include "core/city-hash.hpp"
28#include "core/logger.hpp"
29
30NFD_LOG_INIT("DeadNonceList");
31
32namespace nfd {
33
34const time::nanoseconds DeadNonceList::DEFAULT_LIFETIME = time::seconds(6);
35const time::nanoseconds DeadNonceList::MIN_LIFETIME = time::milliseconds(1);
36const size_t DeadNonceList::INITIAL_CAPACITY = (1 << 7);
37const size_t DeadNonceList::MIN_CAPACITY = (1 << 3);
38const size_t DeadNonceList::MAX_CAPACITY = (1 << 24);
39const 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;
43const size_t DeadNonceList::EVICT_LIMIT = (1 << 6);
44
45DeadNonceList::DeadNonceList(const time::nanoseconds& lifetime)
46 : 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) {
54 throw std::invalid_argument("lifetime is less than MIN_LIFETIME");
55 }
56
57 for (size_t i = 0; i < EXPECTED_MARK_COUNT; ++i) {
58 m_queue.push_back(MARK);
59 }
60
61 m_markEvent = scheduler::schedule(m_markInterval, bind(&DeadNonceList::mark, this));
62 m_adjustCapacityEvent = scheduler::schedule(m_adjustCapacityInterval,
63 bind(&DeadNonceList::adjustCapacity, this));
64}
65
66DeadNonceList::~DeadNonceList()
67{
68 scheduler::cancel(m_markEvent);
69 scheduler::cancel(m_adjustCapacityEvent);
70
71 BOOST_ASSERT(MIN_LIFETIME <= DEFAULT_LIFETIME);
72 BOOST_ASSERT(INITIAL_CAPACITY >= MIN_CAPACITY);
73 BOOST_ASSERT(INITIAL_CAPACITY <= MAX_CAPACITY);
74 BOOST_ASSERT(static_cast<size_t>(MIN_CAPACITY * CAPACITY_UP) > MIN_CAPACITY);
75 BOOST_ASSERT(static_cast<size_t>(MAX_CAPACITY * CAPACITY_DOWN) < MAX_CAPACITY);
76 BOOST_ASSERT(CAPACITY_UP > 1.0);
77 BOOST_ASSERT(CAPACITY_DOWN < 1.0);
78 BOOST_ASSERT(EVICT_LIMIT >= 1);
79}
80
81size_t
82DeadNonceList::size() const
83{
84 return m_queue.size() - this->countMarks();
85}
86
87bool
88DeadNonceList::has(const Name& name, uint32_t nonce) const
89{
90 Entry entry = DeadNonceList::makeEntry(name, nonce);
91 return m_ht.find(entry) != m_ht.end();
92}
93
94void
95DeadNonceList::add(const Name& name, uint32_t nonce)
96{
97 Entry entry = this->makeEntry(name, nonce);
98 m_queue.push_back(entry);
99
100 this->evictEntries();
101}
102
103DeadNonceList::Entry
104DeadNonceList::makeEntry(const Name& name, uint32_t nonce)
105{
106 Block nameWire = name.wireEncode();
107 return CityHash64WithSeed(reinterpret_cast<const char*>(nameWire.wire()), nameWire.size(),
108 static_cast<uint64_t>(nonce));
109}
110
111size_t
112DeadNonceList::countMarks() const
113{
114 return m_ht.count(MARK);
115}
116
117void
118DeadNonceList::mark()
119{
120 m_queue.push_back(MARK);
121 size_t nMarks = this->countMarks();
122 m_actualMarkCounts.insert(nMarks);
123
124 NFD_LOG_DEBUG("mark nMarks=" << nMarks);
125
126 scheduler::schedule(m_markInterval, bind(&DeadNonceList::mark, this));
127}
128
129void
130DeadNonceList::adjustCapacity()
131{
132 std::pair<std::multiset<size_t>::iterator, std::multiset<size_t>::iterator> equalRange =
133 m_actualMarkCounts.equal_range(EXPECTED_MARK_COUNT);
134
135 if (equalRange.second == m_actualMarkCounts.begin()) {
136 // all counts are above expected count, adjust down
137 m_capacity = std::max(MIN_CAPACITY,
138 static_cast<size_t>(m_capacity * CAPACITY_DOWN));
139 NFD_LOG_DEBUG("adjustCapacity DOWN capacity=" << m_capacity);
140 }
141 else if (equalRange.first == m_actualMarkCounts.end()) {
142 // all counts are below expected count, adjust up
143 m_capacity = std::min(MAX_CAPACITY,
144 static_cast<size_t>(m_capacity * CAPACITY_UP));
145 NFD_LOG_DEBUG("adjustCapacity UP capacity=" << m_capacity);
146 }
147
148 m_actualMarkCounts.clear();
149
150 this->evictEntries();
151
152 m_adjustCapacityEvent = scheduler::schedule(m_adjustCapacityInterval,
153 bind(&DeadNonceList::adjustCapacity, this));
154}
155
156void
157DeadNonceList::evictEntries()
158{
159 ssize_t nOverCapacity = m_queue.size() - m_capacity;
160 if (nOverCapacity <= 0) // not over capacity
161 return;
162
163 for (ssize_t nEvict = std::min<ssize_t>(nOverCapacity, EVICT_LIMIT); nEvict > 0; --nEvict) {
164 m_queue.erase(m_queue.begin());
165 }
166 BOOST_ASSERT(m_queue.size() >= m_capacity);
167}
168
169} // namespace nfd