NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
measurements.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2019, 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 "measurements.hpp"
27 #include "name-tree.hpp"
28 #include "pit-entry.hpp"
29 #include "fib-entry.hpp"
30 #include "common/global.hpp"
31 
32 namespace nfd {
33 namespace measurements {
34 
36  : m_nameTree(nameTree)
37 {
38 }
39 
40 Entry&
42 {
43  Entry* entry = nte.getMeasurementsEntry();
44  if (entry != nullptr) {
45  return *entry;
46  }
47 
48  nte.setMeasurementsEntry(make_unique<Entry>(nte.getName()));
49  ++m_nItems;
50  entry = nte.getMeasurementsEntry();
51 
52  entry->m_expiry = time::steady_clock::now() + getInitialLifetime();
53  entry->m_cleanup = getScheduler().schedule(getInitialLifetime(), [=] { cleanup(*entry); });
54 
55  return *entry;
56 }
57 
58 Entry&
60 {
61  name_tree::Entry& nte = m_nameTree.lookup(name, std::min(name.size(), getMaxDepth()));
62  return this->get(nte);
63 }
64 
65 Entry&
66 Measurements::get(const fib::Entry& fibEntry)
67 {
68  name_tree::Entry& nte = m_nameTree.lookup(fibEntry);
69  return this->get(nte);
70 }
71 
72 Entry&
73 Measurements::get(const pit::Entry& pitEntry)
74 {
75  name_tree::Entry& nte = m_nameTree.lookup(pitEntry);
76  return this->get(nte);
77 }
78 
79 Entry*
81 {
82  if (child.getName().empty()) { // the root entry
83  return nullptr;
84  }
85 
86  name_tree::Entry* nteChild = m_nameTree.getEntry(child);
87  name_tree::Entry* nte = nteChild->getParent();
88  BOOST_ASSERT(nte != nullptr);
89  return &this->get(*nte);
90 }
91 
92 template<typename K>
93 Entry*
94 Measurements::findLongestPrefixMatchImpl(const K& key, const EntryPredicate& pred) const
95 {
96  name_tree::Entry* match = m_nameTree.findLongestPrefixMatch(key,
97  [&pred] (const name_tree::Entry& nte) {
98  const Entry* entry = nte.getMeasurementsEntry();
99  return entry != nullptr && pred(*entry);
100  });
101  if (match != nullptr) {
102  return match->getMeasurementsEntry();
103  }
104  return nullptr;
105 }
106 
107 Entry*
109 {
110  return this->findLongestPrefixMatchImpl(name.getPrefix(NameTree::getMaxDepth()), pred);
111 }
112 
113 Entry*
115 {
116  return this->findLongestPrefixMatch(pitEntry.getName(), pred);
117 }
118 
119 Entry*
121 {
122  const name_tree::Entry* nte = m_nameTree.findExactMatch(name);
123  return nte == nullptr ? nullptr : nte->getMeasurementsEntry();
124 }
125 
126 void
127 Measurements::extendLifetime(Entry& entry, const time::nanoseconds& lifetime)
128 {
129  BOOST_ASSERT(m_nameTree.getEntry(entry) != nullptr);
130 
131  auto expiry = time::steady_clock::now() + lifetime;
132  if (entry.m_expiry >= expiry) {
133  // has longer lifetime, not extending
134  return;
135  }
136 
137  entry.m_cleanup.cancel();
138  entry.m_expiry = expiry;
139  entry.m_cleanup = getScheduler().schedule(lifetime, [&] { cleanup(entry); });
140 }
141 
142 void
143 Measurements::cleanup(Entry& entry)
144 {
145  name_tree::Entry* nte = m_nameTree.getEntry(entry);
146  BOOST_ASSERT(nte != nullptr);
147 
148  nte->setMeasurementsEntry(nullptr);
149  m_nameTree.eraseIfEmpty(nte);
150  --m_nItems;
151 }
152 
153 } // namespace measurements
154 } // namespace nfd
name-tree.hpp
nfd::name_tree::Entry
An entry in the name tree.
Definition: name-tree-entry.hpp:42
global.hpp
nfd::name_tree::NameTree::getEntry
Entry * getEntry(const EntryT &tableEntry) const
Definition: name-tree.hpp:77
nfd::name_tree::NameTree
A common index structure for FIB, PIT, StrategyChoice, and Measurements.
Definition: name-tree.hpp:37
nfd::measurements::Measurements::Measurements
Measurements(NameTree &nameTree)
Definition: measurements.cpp:35
ndn::time::steady_clock::now
static time_point now() noexcept
Definition: time.cpp:80
ndn::Name::empty
NDN_CXX_NODISCARD bool empty() const
Checks if the name is empty, i.e.
Definition: name.hpp:145
nfd::name_tree::NameTree::eraseIfEmpty
size_t eraseIfEmpty(Entry *entry, bool canEraseAncestors=true)
Delete the entry if it is empty.
Definition: name-tree.cpp:123
ndn::detail::CancelHandle::cancel
void cancel() const
Cancel the operation.
Definition: cancel-handle.cpp:28
nfd::measurements::Measurements::get
Entry & get(const Name &name)
Find or insert an entry by name.
Definition: measurements.cpp:59
nfd::name_tree::NameTree::lookup
Entry & lookup(const Name &name, size_t prefixLen)
Find or insert an entry by name.
Definition: name-tree.cpp:44
nfd::measurements::Measurements::getMaxDepth
static constexpr size_t getMaxDepth()
maximum depth of a Measurements entry
Definition: measurements.hpp:88
nfd::measurements::EntryPredicate
std::function< bool(const Entry &)> EntryPredicate
A predicate that accepts or rejects an entry.
Definition: measurements.hpp:46
nfd::name_tree::NameTree::findExactMatch
Entry * findExactMatch(const Name &name, size_t prefixLen=std::numeric_limits< size_t >::max()) const
Exact match lookup.
Definition: name-tree.cpp:150
nfd::measurements::Measurements::findExactMatch
Entry * findExactMatch(const Name &name) const
Perform an exact match.
Definition: measurements.cpp:120
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
nfd::name_tree::Entry::getName
const Name & getName() const
Definition: name-tree-entry.hpp:47
nfd::measurements::Entry::getName
const Name & getName() const
Definition: measurements-entry.hpp:51
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
nfd::name_tree::Entry::setMeasurementsEntry
void setMeasurementsEntry(unique_ptr< measurements::Entry > measurementsEntry)
Definition: name-tree-entry.cpp:113
pit-entry.hpp
nfd::getScheduler
Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
Definition: global.cpp:70
nfd::measurements::Measurements::extendLifetime
void extendLifetime(Entry &entry, const time::nanoseconds &lifetime)
Extend lifetime of an entry.
Definition: measurements.cpp:127
nfd::pit::Entry
An Interest table entry.
Definition: pit-entry.hpp:59
nfd::pit::Entry::getName
const Name & getName() const
Definition: pit-entry.hpp:78
nfd::measurements::Measurements::findLongestPrefixMatch
Entry * findLongestPrefixMatch(const Name &name, const EntryPredicate &pred=AnyEntry()) const
Perform a longest prefix match for name.
Definition: measurements.cpp:108
nfd::fib::Entry
represents a FIB entry
Definition: fib-entry.hpp:54
nfd::name_tree::Entry::getMeasurementsEntry
measurements::Entry * getMeasurementsEntry() const
Definition: name-tree-entry.hpp:137
nfd::measurements::Measurements::getParent
Entry * getParent(const Entry &child)
Find or insert a parent entry.
Definition: measurements.cpp:80
nfd::name_tree::NameTree::getMaxDepth
static constexpr size_t getMaxDepth()
Maximum depth of the name tree.
Definition: name-tree.hpp:51
nfd::measurements::Entry
Represents a Measurements entry.
Definition: measurements-entry.hpp:42
ndn::name
Definition: name-component-types.hpp:33
fib-entry.hpp
nfd::name_tree::NameTree::findLongestPrefixMatch
Entry * findLongestPrefixMatch(const Name &name, const EntrySelector &entrySelector=AnyEntry()) const
Longest prefix matching.
Definition: name-tree.cpp:162
measurements.hpp
nfd::name_tree::Entry::getParent
Entry * getParent() const
Definition: name-tree-entry.hpp:56
nfd::measurements::Measurements::getInitialLifetime
static time::nanoseconds getInitialLifetime()
Definition: measurements.hpp:136