blob: 7a5f9b723fa9b33db38220bbbeaf065ec21d856e [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 Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040032namespace nfd::measurements {
Junxiao Shib30c7b02015-01-07 15:45:54 -070033
HangZhangc85a23c2014-03-01 15:55:55 +080034Measurements::Measurements(NameTree& nameTree)
35 : m_nameTree(nameTree)
Junxiao Shi65d00722014-02-17 10:50:20 -070036{
37}
38
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000039Entry&
Junxiao Shie368d992014-12-02 23:44:31 -070040Measurements::get(name_tree::Entry& nte)
Junxiao Shi65d00722014-02-17 10:50:20 -070041{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000042 Entry* entry = nte.getMeasurementsEntry();
43 if (entry != nullptr) {
44 return *entry;
45 }
Junxiao Shi19838042014-06-21 00:34:01 -070046
Junxiao Shie3cf2852016-08-09 03:50:56 +000047 nte.setMeasurementsEntry(make_unique<Entry>(nte.getName()));
Junxiao Shi19838042014-06-21 00:34:01 -070048 ++m_nItems;
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000049 entry = nte.getMeasurementsEntry();
Junxiao Shi19838042014-06-21 00:34:01 -070050
51 entry->m_expiry = time::steady_clock::now() + getInitialLifetime();
Davide Pesavento3dade002019-03-19 11:29:56 -060052 entry->m_cleanup = getScheduler().schedule(getInitialLifetime(), [=] { cleanup(*entry); });
Junxiao Shi19838042014-06-21 00:34:01 -070053
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000054 return *entry;
Junxiao Shi65d00722014-02-17 10:50:20 -070055}
56
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000057Entry&
HangZhangcb4fc832014-03-11 16:57:11 +080058Measurements::get(const Name& name)
59{
Junxiao Shi057d1492018-03-20 17:14:18 +000060 name_tree::Entry& nte = m_nameTree.lookup(name, std::min(name.size(), getMaxDepth()));
Junxiao Shi7f358432016-08-11 17:06:33 +000061 return this->get(nte);
HangZhangcb4fc832014-03-11 16:57:11 +080062}
63
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000064Entry&
Junxiao Shi65d00722014-02-17 10:50:20 -070065Measurements::get(const fib::Entry& fibEntry)
66{
Junxiao Shi057d1492018-03-20 17:14:18 +000067 name_tree::Entry& nte = m_nameTree.lookup(fibEntry);
Junxiao Shi7f358432016-08-11 17:06:33 +000068 return this->get(nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070069}
70
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000071Entry&
Junxiao Shi65d00722014-02-17 10:50:20 -070072Measurements::get(const pit::Entry& pitEntry)
73{
Junxiao Shi057d1492018-03-20 17:14:18 +000074 name_tree::Entry& nte = m_nameTree.lookup(pitEntry);
Junxiao Shi7f358432016-08-11 17:06:33 +000075 return this->get(nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070076}
77
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000078Entry*
Junxiao Shib30c7b02015-01-07 15:45:54 -070079Measurements::getParent(const Entry& child)
Junxiao Shi65d00722014-02-17 10:50:20 -070080{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000081 if (child.getName().empty()) { // the root entry
Junxiao Shie368d992014-12-02 23:44:31 -070082 return nullptr;
Junxiao Shi65d00722014-02-17 10:50:20 -070083 }
84
Junxiao Shi7f358432016-08-11 17:06:33 +000085 name_tree::Entry* nteChild = m_nameTree.getEntry(child);
Junxiao Shib660b4c2016-08-06 20:47:44 +000086 name_tree::Entry* nte = nteChild->getParent();
Junxiao Shie368d992014-12-02 23:44:31 -070087 BOOST_ASSERT(nte != nullptr);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000088 return &this->get(*nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070089}
90
Junxiao Shi767cb332015-01-08 09:35:49 -070091template<typename K>
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000092Entry*
Junxiao Shi811c0102016-08-10 04:12:45 +000093Measurements::findLongestPrefixMatchImpl(const K& key, const EntryPredicate& pred) const
Junxiao Shi65d00722014-02-17 10:50:20 -070094{
Junxiao Shi811c0102016-08-10 04:12:45 +000095 name_tree::Entry* match = m_nameTree.findLongestPrefixMatch(key,
96 [&pred] (const name_tree::Entry& nte) {
97 const Entry* entry = nte.getMeasurementsEntry();
98 return entry != nullptr && pred(*entry);
99 });
Junxiao Shi767cb332015-01-08 09:35:49 -0700100 if (match != nullptr) {
101 return match->getMeasurementsEntry();
Junxiao Shi65d00722014-02-17 10:50:20 -0700102 }
Junxiao Shie368d992014-12-02 23:44:31 -0700103 return nullptr;
HangZhangc85a23c2014-03-01 15:55:55 +0800104}
Junxiao Shi65d00722014-02-17 10:50:20 -0700105
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000106Entry*
Junxiao Shi811c0102016-08-10 04:12:45 +0000107Measurements::findLongestPrefixMatch(const Name& name, const EntryPredicate& pred) const
Junxiao Shi767cb332015-01-08 09:35:49 -0700108{
Junxiao Shi3f9234b2017-12-07 17:41:50 +0000109 return this->findLongestPrefixMatchImpl(name.getPrefix(NameTree::getMaxDepth()), pred);
Junxiao Shi767cb332015-01-08 09:35:49 -0700110}
111
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000112Entry*
Junxiao Shi811c0102016-08-10 04:12:45 +0000113Measurements::findLongestPrefixMatch(const pit::Entry& pitEntry, const EntryPredicate& pred) const
Junxiao Shi767cb332015-01-08 09:35:49 -0700114{
Junxiao Shi3f9234b2017-12-07 17:41:50 +0000115 return this->findLongestPrefixMatch(pitEntry.getName(), pred);
Junxiao Shi767cb332015-01-08 09:35:49 -0700116}
117
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000118Entry*
HangZhangc85a23c2014-03-01 15:55:55 +0800119Measurements::findExactMatch(const Name& name) const
120{
Junxiao Shif2420fc2016-08-11 13:18:21 +0000121 const name_tree::Entry* nte = m_nameTree.findExactMatch(name);
122 return nte == nullptr ? nullptr : nte->getMeasurementsEntry();
Junxiao Shi65d00722014-02-17 10:50:20 -0700123}
124
125void
Junxiao Shib184e532016-05-26 18:09:57 +0000126Measurements::extendLifetime(Entry& entry, const time::nanoseconds& lifetime)
Junxiao Shi65d00722014-02-17 10:50:20 -0700127{
Davide Pesavento1c2c6c42016-08-16 12:36:06 +0000128 BOOST_ASSERT(m_nameTree.getEntry(entry) != nullptr);
Junxiao Shiee5a4442014-07-27 17:13:43 -0700129
Davide Pesavento50a6af32019-02-21 00:04:40 -0500130 auto expiry = time::steady_clock::now() + lifetime;
Junxiao Shie368d992014-12-02 23:44:31 -0700131 if (entry.m_expiry >= expiry) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700132 // has longer lifetime, not extending
133 return;
134 }
135
Davide Pesavento3dade002019-03-19 11:29:56 -0600136 entry.m_cleanup.cancel();
Junxiao Shie368d992014-12-02 23:44:31 -0700137 entry.m_expiry = expiry;
Davide Pesavento3dade002019-03-19 11:29:56 -0600138 entry.m_cleanup = getScheduler().schedule(lifetime, [&] { cleanup(entry); });
HangZhangc85a23c2014-03-01 15:55:55 +0800139}
140
141void
Junxiao Shib30c7b02015-01-07 15:45:54 -0700142Measurements::cleanup(Entry& entry)
HangZhangc85a23c2014-03-01 15:55:55 +0800143{
Junxiao Shi7f358432016-08-11 17:06:33 +0000144 name_tree::Entry* nte = m_nameTree.getEntry(entry);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000145 BOOST_ASSERT(nte != nullptr);
146
147 nte->setMeasurementsEntry(nullptr);
Junxiao Shi7f358432016-08-11 17:06:33 +0000148 m_nameTree.eraseIfEmpty(nte);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000149 --m_nItems;
Junxiao Shi65d00722014-02-17 10:50:20 -0700150}
151
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400152} // namespace nfd::measurements