asf-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 "asf-measurements.hpp"
27 #include "common/global.hpp"
28 
29 namespace nfd {
30 namespace fw {
31 namespace asf {
32 
33 const time::nanoseconds FaceInfo::RTT_NO_MEASUREMENT{-1};
34 const time::nanoseconds FaceInfo::RTT_TIMEOUT{-2};
35 
36 time::nanoseconds
37 FaceInfo::scheduleTimeout(const Name& interestName, scheduler::EventCallback cb)
38 {
39  BOOST_ASSERT(!m_timeoutEvent);
40  m_lastInterestName = interestName;
41  m_timeoutEvent = getScheduler().schedule(m_rttEstimator.getEstimatedRto(), std::move(cb));
42  return m_rttEstimator.getEstimatedRto();
43 }
44 
45 void
46 FaceInfo::cancelTimeout(const Name& prefix)
47 {
48  if (m_lastInterestName.isPrefixOf(prefix)) {
49  m_timeoutEvent.cancel();
50  }
51 }
52 
55 
56 FaceInfo*
58 {
59  auto it = m_fiMap.find(faceId);
60  return it != m_fiMap.end() ? &it->second : nullptr;
61 }
62 
63 FaceInfo&
65 {
66  auto ret = m_fiMap.emplace(std::piecewise_construct,
67  std::forward_as_tuple(faceId),
68  std::forward_as_tuple(m_rttEstimatorOpts));
69  auto& faceInfo = ret.first->second;
70  if (ret.second) {
71  extendFaceInfoLifetime(faceInfo, faceId);
72  }
73  return faceInfo;
74 }
75 
76 void
78 {
79  info.m_measurementExpiration = getScheduler().schedule(AsfMeasurements::MEASUREMENTS_LIFETIME,
80  [=] { m_fiMap.erase(faceId); });
81 }
82 
85 
86 constexpr time::microseconds AsfMeasurements::MEASUREMENTS_LIFETIME;
87 
89  : m_measurements(measurements)
90  , m_rttEstimatorOpts(make_shared<ndn::util::RttEstimator::Options>())
91 {
92 }
93 
94 FaceInfo*
95 AsfMeasurements::getFaceInfo(const fib::Entry& fibEntry, const Interest& interest, FaceId faceId)
96 {
97  return getOrCreateNamespaceInfo(fibEntry, interest).getFaceInfo(faceId);
98 }
99 
100 FaceInfo&
101 AsfMeasurements::getOrCreateFaceInfo(const fib::Entry& fibEntry, const Interest& interest,
102  FaceId faceId)
103 {
104  return getOrCreateNamespaceInfo(fibEntry, interest).getOrCreateFaceInfo(faceId);
105 }
106 
109 {
110  measurements::Entry* me = m_measurements.findLongestPrefixMatch(prefix);
111  if (me == nullptr) {
112  return nullptr;
113  }
114 
115  // Set or update entry lifetime
116  extendLifetime(*me);
117 
118  NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>(m_rttEstimatorOpts).first;
119  BOOST_ASSERT(info != nullptr);
120  return info;
121 }
122 
124 AsfMeasurements::getOrCreateNamespaceInfo(const fib::Entry& fibEntry, const Interest& interest)
125 {
126  measurements::Entry* me = m_measurements.get(fibEntry);
127 
128  // If the FIB entry is not under the strategy's namespace, find a part of the prefix
129  // that falls under the strategy's namespace
130  for (size_t prefixLen = fibEntry.getPrefix().size() + 1;
131  me == nullptr && prefixLen <= interest.getName().size(); ++prefixLen) {
132  me = m_measurements.get(interest.getName().getPrefix(prefixLen));
133  }
134 
135  // Either the FIB entry or the Interest's name must be under this strategy's namespace
136  BOOST_ASSERT(me != nullptr);
137 
138  // Set or update entry lifetime
139  extendLifetime(*me);
140 
141  NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>(m_rttEstimatorOpts).first;
142  BOOST_ASSERT(info != nullptr);
143  return *info;
144 }
145 
146 void
147 AsfMeasurements::extendLifetime(measurements::Entry& me)
148 {
149  m_measurements.extendLifetime(me, MEASUREMENTS_LIFETIME);
150 }
151 
152 } // namespace asf
153 } // namespace fw
154 } // namespace nfd
void cancelTimeout(const Name &prefix)
FaceInfo * getFaceInfo(const fib::Entry &fibEntry, const Interest &interest, FaceId faceId)
Copyright (c) 2014-2017, Regents of the University of California, Arizona Board of Regents...
Definition: dns-srv.cpp:41
static const time::nanoseconds RTT_NO_MEASUREMENT
static const time::nanoseconds RTT_TIMEOUT
void extendLifetime(Entry &entry, const time::nanoseconds &lifetime)
extend lifetime of an entry
void extendFaceInfoLifetime(FaceInfo &info, FaceId faceId)
std::pair< T *, bool > insertStrategyInfo(A &&... args)
Insert a StrategyInfo item.
Represents a Measurements entry.
NamespaceInfo * getNamespaceInfo(const Name &prefix)
represents a FIB entry
Definition: fib-entry.hpp:53
FaceInfo & getOrCreateFaceInfo(const fib::Entry &fibEntry, const Interest &interest, FaceId faceId)
Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
Definition: global.cpp:45
Entry * findLongestPrefixMatch(const Name &name, const EntryPredicate &pred=AnyEntry()) const
perform a longest prefix match for name
static constexpr time::microseconds MEASUREMENTS_LIFETIME
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
time::nanoseconds scheduleTimeout(const Name &interestName, scheduler::EventCallback cb)
const Name & getPrefix() const
Definition: fib-entry.hpp:60
NamespaceInfo & getOrCreateNamespaceInfo(const fib::Entry &fibEntry, const Interest &interest)
allows Strategy to access portion of Measurements table under its namespace
FaceInfo & getOrCreateFaceInfo(FaceId faceId)
Stores strategy information about each face in this namespace.
AsfMeasurements(MeasurementsAccessor &measurements)
const Name & getName() const
uint64_t FaceId
Identifies a face.
Definition: face-common.hpp:44
Strategy information for each face in a namespace.
Entry * get(const Name &name)
find or insert a Measurements entry for name
FaceInfo * getFaceInfo(FaceId faceId)