blob: af3cc1ed6fd9ecea4df4f8aba4ca6967c2030351 [file] [log] [blame]
Junxiao Shi65d00722014-02-17 10:50:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi3f9234b2017-12-07 17:41:50 +00002/*
Davide Pesavento50a6af32019-02-21 00:04:40 -05003 * Copyright (c) 2014-2019, Regents of the University of California,
Junxiao Shi767cb332015-01-08 09:35:49 -07004 * Arizona Board of Regents,
5 * Colorado State University,
Junxiao Shi35353962015-01-08 09:13:47 -07006 * University Pierre & Marie Curie, Sorbonne University,
Junxiao Shi767cb332015-01-08 09:35:49 -07007 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -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/>.
Junxiao Shi19838042014-06-21 00:34:01 -070024 */
Junxiao Shi65d00722014-02-17 10:50:20 -070025
26#include "measurements.hpp"
HangZhangc85a23c2014-03-01 15:55:55 +080027#include "name-tree.hpp"
Junxiao Shi65d00722014-02-17 10:50:20 -070028#include "pit-entry.hpp"
HangZhangc85a23c2014-03-01 15:55:55 +080029#include "fib-entry.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040030#include "common/global.hpp"
Junxiao Shi65d00722014-02-17 10:50:20 -070031
32namespace nfd {
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000033namespace measurements {
Junxiao Shib30c7b02015-01-07 15:45:54 -070034
HangZhangc85a23c2014-03-01 15:55:55 +080035Measurements::Measurements(NameTree& nameTree)
36 : m_nameTree(nameTree)
Junxiao Shi65d00722014-02-17 10:50:20 -070037{
38}
39
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000040Entry&
Junxiao Shie368d992014-12-02 23:44:31 -070041Measurements::get(name_tree::Entry& nte)
Junxiao Shi65d00722014-02-17 10:50:20 -070042{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000043 Entry* entry = nte.getMeasurementsEntry();
44 if (entry != nullptr) {
45 return *entry;
46 }
Junxiao Shi19838042014-06-21 00:34:01 -070047
Junxiao Shie3cf2852016-08-09 03:50:56 +000048 nte.setMeasurementsEntry(make_unique<Entry>(nte.getName()));
Junxiao Shi19838042014-06-21 00:34:01 -070049 ++m_nItems;
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000050 entry = nte.getMeasurementsEntry();
Junxiao Shi19838042014-06-21 00:34:01 -070051
52 entry->m_expiry = time::steady_clock::now() + getInitialLifetime();
Davide Pesavento3dade002019-03-19 11:29:56 -060053 entry->m_cleanup = getScheduler().schedule(getInitialLifetime(), [=] { cleanup(*entry); });
Junxiao Shi19838042014-06-21 00:34:01 -070054
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000055 return *entry;
Junxiao Shi65d00722014-02-17 10:50:20 -070056}
57
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000058Entry&
HangZhangcb4fc832014-03-11 16:57:11 +080059Measurements::get(const Name& name)
60{
Junxiao Shi057d1492018-03-20 17:14:18 +000061 name_tree::Entry& nte = m_nameTree.lookup(name, std::min(name.size(), getMaxDepth()));
Junxiao Shi7f358432016-08-11 17:06:33 +000062 return this->get(nte);
HangZhangcb4fc832014-03-11 16:57:11 +080063}
64
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000065Entry&
Junxiao Shi65d00722014-02-17 10:50:20 -070066Measurements::get(const fib::Entry& fibEntry)
67{
Junxiao Shi057d1492018-03-20 17:14:18 +000068 name_tree::Entry& nte = m_nameTree.lookup(fibEntry);
Junxiao Shi7f358432016-08-11 17:06:33 +000069 return this->get(nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070070}
71
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000072Entry&
Junxiao Shi65d00722014-02-17 10:50:20 -070073Measurements::get(const pit::Entry& pitEntry)
74{
Junxiao Shi057d1492018-03-20 17:14:18 +000075 name_tree::Entry& nte = m_nameTree.lookup(pitEntry);
Junxiao Shi7f358432016-08-11 17:06:33 +000076 return this->get(nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070077}
78
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000079Entry*
Junxiao Shib30c7b02015-01-07 15:45:54 -070080Measurements::getParent(const Entry& child)
Junxiao Shi65d00722014-02-17 10:50:20 -070081{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000082 if (child.getName().empty()) { // the root entry
Junxiao Shie368d992014-12-02 23:44:31 -070083 return nullptr;
Junxiao Shi65d00722014-02-17 10:50:20 -070084 }
85
Junxiao Shi7f358432016-08-11 17:06:33 +000086 name_tree::Entry* nteChild = m_nameTree.getEntry(child);
Junxiao Shib660b4c2016-08-06 20:47:44 +000087 name_tree::Entry* nte = nteChild->getParent();
Junxiao Shie368d992014-12-02 23:44:31 -070088 BOOST_ASSERT(nte != nullptr);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000089 return &this->get(*nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070090}
91
Junxiao Shi767cb332015-01-08 09:35:49 -070092template<typename K>
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000093Entry*
Junxiao Shi811c0102016-08-10 04:12:45 +000094Measurements::findLongestPrefixMatchImpl(const K& key, const EntryPredicate& pred) const
Junxiao Shi65d00722014-02-17 10:50:20 -070095{
Junxiao Shi811c0102016-08-10 04:12:45 +000096 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 });
Junxiao Shi767cb332015-01-08 09:35:49 -0700101 if (match != nullptr) {
102 return match->getMeasurementsEntry();
Junxiao Shi65d00722014-02-17 10:50:20 -0700103 }
Junxiao Shie368d992014-12-02 23:44:31 -0700104 return nullptr;
HangZhangc85a23c2014-03-01 15:55:55 +0800105}
Junxiao Shi65d00722014-02-17 10:50:20 -0700106
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000107Entry*
Junxiao Shi811c0102016-08-10 04:12:45 +0000108Measurements::findLongestPrefixMatch(const Name& name, const EntryPredicate& pred) const
Junxiao Shi767cb332015-01-08 09:35:49 -0700109{
Junxiao Shi3f9234b2017-12-07 17:41:50 +0000110 return this->findLongestPrefixMatchImpl(name.getPrefix(NameTree::getMaxDepth()), pred);
Junxiao Shi767cb332015-01-08 09:35:49 -0700111}
112
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000113Entry*
Junxiao Shi811c0102016-08-10 04:12:45 +0000114Measurements::findLongestPrefixMatch(const pit::Entry& pitEntry, const EntryPredicate& pred) const
Junxiao Shi767cb332015-01-08 09:35:49 -0700115{
Junxiao Shi3f9234b2017-12-07 17:41:50 +0000116 return this->findLongestPrefixMatch(pitEntry.getName(), pred);
Junxiao Shi767cb332015-01-08 09:35:49 -0700117}
118
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000119Entry*
HangZhangc85a23c2014-03-01 15:55:55 +0800120Measurements::findExactMatch(const Name& name) const
121{
Junxiao Shif2420fc2016-08-11 13:18:21 +0000122 const name_tree::Entry* nte = m_nameTree.findExactMatch(name);
123 return nte == nullptr ? nullptr : nte->getMeasurementsEntry();
Junxiao Shi65d00722014-02-17 10:50:20 -0700124}
125
126void
Junxiao Shib184e532016-05-26 18:09:57 +0000127Measurements::extendLifetime(Entry& entry, const time::nanoseconds& lifetime)
Junxiao Shi65d00722014-02-17 10:50:20 -0700128{
Davide Pesavento1c2c6c42016-08-16 12:36:06 +0000129 BOOST_ASSERT(m_nameTree.getEntry(entry) != nullptr);
Junxiao Shiee5a4442014-07-27 17:13:43 -0700130
Davide Pesavento50a6af32019-02-21 00:04:40 -0500131 auto expiry = time::steady_clock::now() + lifetime;
Junxiao Shie368d992014-12-02 23:44:31 -0700132 if (entry.m_expiry >= expiry) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700133 // has longer lifetime, not extending
134 return;
135 }
136
Davide Pesavento3dade002019-03-19 11:29:56 -0600137 entry.m_cleanup.cancel();
Junxiao Shie368d992014-12-02 23:44:31 -0700138 entry.m_expiry = expiry;
Davide Pesavento3dade002019-03-19 11:29:56 -0600139 entry.m_cleanup = getScheduler().schedule(lifetime, [&] { cleanup(entry); });
HangZhangc85a23c2014-03-01 15:55:55 +0800140}
141
142void
Junxiao Shib30c7b02015-01-07 15:45:54 -0700143Measurements::cleanup(Entry& entry)
HangZhangc85a23c2014-03-01 15:55:55 +0800144{
Junxiao Shi7f358432016-08-11 17:06:33 +0000145 name_tree::Entry* nte = m_nameTree.getEntry(entry);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000146 BOOST_ASSERT(nte != nullptr);
147
148 nte->setMeasurementsEntry(nullptr);
Junxiao Shi7f358432016-08-11 17:06:33 +0000149 m_nameTree.eraseIfEmpty(nte);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000150 --m_nItems;
Junxiao Shi65d00722014-02-17 10:50:20 -0700151}
152
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000153} // namespace measurements
Junxiao Shi65d00722014-02-17 10:50:20 -0700154} // namespace nfd