asf-measurements.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, 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 #ifndef NFD_DAEMON_FW_ASF_MEASUREMENTS_HPP
27 #define NFD_DAEMON_FW_ASF_MEASUREMENTS_HPP
28 
29 #include "face/face-common.hpp"
30 #include "fw/strategy-info.hpp"
32 
33 #include <ndn-cxx/util/rtt-estimator.hpp>
34 
35 #include <unordered_map>
36 
37 namespace nfd::fw::asf {
38 
42 class FaceInfo
43 {
44 public:
45  explicit
46  FaceInfo(shared_ptr<const ndn::util::RttEstimator::Options> opts)
47  : m_rttEstimator(std::move(opts))
48  {
49  }
50 
51  bool
53  {
54  return !!m_timeoutEvent;
55  }
56 
57  time::nanoseconds
58  scheduleTimeout(const Name& interestName, ndn::scheduler::EventCallback cb);
59 
60  void
61  cancelTimeout(const Name& prefix);
62 
63  void
64  recordRtt(time::nanoseconds rtt)
65  {
66  m_lastRtt = rtt;
67  m_rttEstimator.addMeasurement(rtt);
68  }
69 
70  void
71  recordTimeout(const Name& interestName)
72  {
73  m_lastRtt = RTT_TIMEOUT;
74  cancelTimeout(interestName);
75  }
76 
77  time::nanoseconds
78  getLastRtt() const
79  {
80  return m_lastRtt;
81  }
82 
83  time::nanoseconds
84  getSrtt() const
85  {
86  return m_rttEstimator.getSmoothedRtt();
87  }
88 
89  size_t
90  getNTimeouts() const
91  {
92  return m_nTimeouts;
93  }
94 
95  void
96  setNTimeouts(size_t nTimeouts)
97  {
98  m_nTimeouts = nTimeouts;
99  }
100 
101 public:
102  static constexpr time::nanoseconds RTT_NO_MEASUREMENT = -1_ns;
103  static constexpr time::nanoseconds RTT_TIMEOUT = -2_ns;
104 
105 private:
106  ndn::util::RttEstimator m_rttEstimator;
107  time::nanoseconds m_lastRtt = RTT_NO_MEASUREMENT;
108  Name m_lastInterestName;
109  size_t m_nTimeouts = 0;
110 
111  // Timeout associated with measurement
112  ndn::scheduler::ScopedEventId m_measurementExpiration;
113  friend class NamespaceInfo;
114 
115  // RTO associated with Interest
116  ndn::scheduler::ScopedEventId m_timeoutEvent;
117 };
118 
121 
125 class NamespaceInfo final : public StrategyInfo
126 {
127 public:
128  static constexpr int
130  {
131  return 1030;
132  }
133 
134  explicit
135  NamespaceInfo(shared_ptr<const ndn::util::RttEstimator::Options> opts, time::milliseconds measurementLifetime)
136  : m_rttEstimatorOpts(std::move(opts))
137  , m_measurementLifetime(measurementLifetime)
138  {
139  }
140 
141  FaceInfo*
142  getFaceInfo(FaceId faceId);
143 
144  FaceInfo&
145  getOrCreateFaceInfo(FaceId faceId);
146 
147  void
148  extendFaceInfoLifetime(FaceInfo& info, FaceId faceId);
149 
150  bool
151  isProbingDue() const
152  {
153  return m_isProbingDue;
154  }
155 
156  void
158  {
159  m_isProbingDue = isProbingDue;
160  }
161 
162  bool
164  {
165  return m_isFirstProbeScheduled;
166  }
167 
168  void
169  setIsFirstProbeScheduled(bool isScheduled)
170  {
171  m_isFirstProbeScheduled = isScheduled;
172  }
173 
174 private:
175  std::unordered_map<FaceId, FaceInfo> m_fiMap;
176  shared_ptr<const ndn::util::RttEstimator::Options> m_rttEstimatorOpts;
177  time::milliseconds m_measurementLifetime;
178  bool m_isProbingDue = false;
179  bool m_isFirstProbeScheduled = false;
180 };
181 
184 
188 class AsfMeasurements : noncopyable
189 {
190 public:
191  explicit
192  AsfMeasurements(MeasurementsAccessor& measurements);
193 
194  FaceInfo*
195  getFaceInfo(const fib::Entry& fibEntry, const Name& interestName, FaceId faceId);
196 
197  FaceInfo&
198  getOrCreateFaceInfo(const fib::Entry& fibEntry, const Name& interestName, FaceId faceId);
199 
201  getNamespaceInfo(const Name& prefix);
202 
204  getOrCreateNamespaceInfo(const fib::Entry& fibEntry, const Name& prefix);
205 
206  void
207  setMeasurementsLifetime(time::milliseconds measurementsLifetime)
208  {
209  // Measurement lifetime should not expire as soon as it is configured
210  BOOST_ASSERT(measurementsLifetime > 0_ms);
211  m_measurementsLifetime = measurementsLifetime;
212  }
213 
214  time::milliseconds
216  {
217  return m_measurementsLifetime;
218  }
219 
220 private:
221  void
222  extendLifetime(measurements::Entry& me);
223 
224 public:
225  static constexpr time::milliseconds DEFAULT_MEASUREMENTS_LIFETIME = 5_min;
226 
227 private:
228  time::milliseconds m_measurementsLifetime = DEFAULT_MEASUREMENTS_LIFETIME;
229  MeasurementsAccessor& m_measurements;
230  shared_ptr<const ndn::util::RttEstimator::Options> m_rttEstimatorOpts;
231 };
232 
233 } // namespace nfd::fw::asf
234 
235 #endif // NFD_DAEMON_FW_ASF_MEASUREMENTS_HPP
Represents an entry in the FIB.
Definition: fib-entry.hpp:91
Contains arbitrary information placed by the forwarding strategy on table entries.
Helper class to retrieve and create strategy measurements.
NamespaceInfo & getOrCreateNamespaceInfo(const fib::Entry &fibEntry, const Name &prefix)
AsfMeasurements(MeasurementsAccessor &measurements)
static constexpr time::milliseconds DEFAULT_MEASUREMENTS_LIFETIME
void setMeasurementsLifetime(time::milliseconds measurementsLifetime)
time::milliseconds getMeasurementsLifetime() const
FaceInfo & getOrCreateFaceInfo(const fib::Entry &fibEntry, const Name &interestName, FaceId faceId)
FaceInfo * getFaceInfo(const fib::Entry &fibEntry, const Name &interestName, FaceId faceId)
NamespaceInfo * getNamespaceInfo(const Name &prefix)
Strategy information for each face in a namespace.
static constexpr time::nanoseconds RTT_TIMEOUT
void cancelTimeout(const Name &prefix)
time::nanoseconds scheduleTimeout(const Name &interestName, ndn::scheduler::EventCallback cb)
static constexpr time::nanoseconds RTT_NO_MEASUREMENT
time::nanoseconds getSrtt() const
void setNTimeouts(size_t nTimeouts)
time::nanoseconds getLastRtt() const
FaceInfo(shared_ptr< const ndn::util::RttEstimator::Options > opts)
void recordTimeout(const Name &interestName)
void recordRtt(time::nanoseconds rtt)
Stores strategy information about each face in this namespace.
void setIsFirstProbeScheduled(bool isScheduled)
FaceInfo * getFaceInfo(FaceId faceId)
static constexpr int getTypeId()
void extendFaceInfoLifetime(FaceInfo &info, FaceId faceId)
FaceInfo & getOrCreateFaceInfo(FaceId faceId)
NamespaceInfo(shared_ptr< const ndn::util::RttEstimator::Options > opts, time::milliseconds measurementLifetime)
void setIsProbingDue(bool isProbingDue)
Represents an entry in the Measurements table.
Allows fw::Strategy to access the portion of Measurements table under its namespace.
uint64_t FaceId
Identifies a face.
Definition: face-common.hpp:48