asf-measurements.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "asf-measurements.hpp"
27 
28 namespace nfd {
29 namespace fw {
30 namespace asf {
31 
32 NFD_LOG_INIT("AsfMeasurements");
33 
36 const double RttStats::ALPHA = 0.125;
37 
39  : m_srtt(RTT_NO_MEASUREMENT)
40  , m_rtt(RTT_NO_MEASUREMENT)
41 {
42 }
43 
44 void
46 {
47  m_rtt = static_cast<RttStats::Rtt>(durationRtt.count());
48 
49  m_rttEstimator.addMeasurement(durationRtt);
50 
51  m_srtt = computeSrtt(m_srtt, m_rtt);
52 }
53 
55 RttStats::computeSrtt(Rtt previousSrtt, Rtt currentRtt)
56 {
57  if (previousSrtt == RTT_NO_MEASUREMENT) {
58  return currentRtt;
59  }
60 
61  return Rtt(ALPHA * currentRtt + (1 - ALPHA) * previousSrtt);
62 }
63 
66 
68  : m_isTimeoutScheduled(false)
69 {
70 }
71 
73 {
75  scheduler::cancel(m_measurementExpirationId);
76 }
77 
78 void
79 FaceInfo::setTimeoutEvent(const scheduler::EventId& id, const Name& interestName)
80 {
81  if (!m_isTimeoutScheduled) {
82  m_timeoutEventId = id;
83  m_isTimeoutScheduled = true;
84  m_lastInterestName = interestName;
85  }
86  else {
87  BOOST_THROW_EXCEPTION(FaceInfo::Error("Tried to schedule a timeout for a face that already has a timeout scheduled."));
88  }
89 }
90 
91 void
92 FaceInfo::cancelTimeoutEvent()
93 {
94  scheduler::cancel(m_timeoutEventId);
95  m_isTimeoutScheduled = false;
96 }
97 
98 void
99 FaceInfo::cancelTimeoutEvent(const Name& prefix)
100 {
101  if (isTimeoutScheduled() && doesNameMatchLastInterest(prefix)) {
103  }
104 }
105 
106 bool
107 FaceInfo::doesNameMatchLastInterest(const Name& name)
108 {
109  return m_lastInterestName.isPrefixOf(name);
110 }
111 
112 void
113 FaceInfo::recordRtt(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace)
114 {
115  // Calculate RTT
116  pit::OutRecordCollection::const_iterator outRecord = pitEntry->getOutRecord(inFace);
117  time::steady_clock::Duration steadyRtt = time::steady_clock::now() - outRecord->getLastRenewed();
118  RttEstimator::Duration durationRtt = time::duration_cast<RttEstimator::Duration>(steadyRtt);
119 
120  m_rttStats.addRttMeasurement(durationRtt);
121 
122  NFD_LOG_TRACE("Recording RTT for FaceId: " << inFace.getId()
123  << " RTT: " << m_rttStats.getRtt()
124  << " SRTT: " << m_rttStats.getSrtt());
125 }
126 
127 void
128 FaceInfo::recordTimeout(const Name& interestName)
129 {
130  m_rttStats.recordTimeout();
131  cancelTimeoutEvent(interestName);
132 }
133 
136 
138  : m_isProbingDue(false)
139  , m_hasFirstProbeBeenScheduled(false)
140 {
141 }
142 
143 FaceInfo*
144 NamespaceInfo::getFaceInfo(const fib::Entry& fibEntry, const Face& face)
145 {
146  FaceInfoTable::iterator it = m_fit.find(face.getId());
147 
148  if (it != m_fit.end()) {
149  return &it->second;
150  }
151  else {
152  return nullptr;
153  }
154 }
155 
156 FaceInfo&
157 NamespaceInfo::getOrCreateFaceInfo(const fib::Entry& fibEntry, const Face& face)
158 {
159  FaceInfoTable::iterator it = m_fit.find(face.getId());
160 
161  FaceInfo* info = nullptr;
162 
163  if (it == m_fit.end()) {
164  const auto& pair = m_fit.insert(std::make_pair(face.getId(), FaceInfo()));
165  info = &pair.first->second;
166 
167  extendFaceInfoLifetime(*info, face);
168  }
169  else {
170  info = &it->second;
171  }
172 
173  return *info;
174 }
175 
176 void
178 {
179  m_fit.erase(faceId);
180 }
181 
182 void
184 {
185  // Cancel previous expiration
187 
188  // Refresh measurement
190  bind(&NamespaceInfo::expireFaceInfo, this, face.getId()));
191 
193 }
194 
197 
198 constexpr time::microseconds AsfMeasurements::MEASUREMENTS_LIFETIME;
199 
200 AsfMeasurements::AsfMeasurements(MeasurementsAccessor& measurements)
201  : m_measurements(measurements)
202 {
203 }
204 
205 FaceInfo*
206 AsfMeasurements::getFaceInfo(const fib::Entry& fibEntry, const Interest& interest, const Face& face)
207 {
208  NamespaceInfo& info = getOrCreateNamespaceInfo(fibEntry, interest);
209  return info.getFaceInfo(fibEntry, face);
210 }
211 
212 FaceInfo&
213 AsfMeasurements::getOrCreateFaceInfo(const fib::Entry& fibEntry, const Interest& interest, const Face& face)
214 {
215  NamespaceInfo& info = getOrCreateNamespaceInfo(fibEntry, interest);
216  return info.getOrCreateFaceInfo(fibEntry, face);
217 }
218 
221 {
222  measurements::Entry* me = m_measurements.findLongestPrefixMatch(prefix);
223  if (me == nullptr) {
224  return nullptr;
225  }
226 
227  // Set or update entry lifetime
228  extendLifetime(*me);
229 
230  NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>().first;
231  BOOST_ASSERT(info != nullptr);
232  return info;
233 }
234 
236 AsfMeasurements::getOrCreateNamespaceInfo(const fib::Entry& fibEntry, const Interest& interest)
237 {
238  measurements::Entry* me = m_measurements.get(fibEntry);
239 
240  // If the FIB entry is not under the strategy's namespace, find a part of the prefix
241  // that falls under the strategy's namespace
242  for (size_t prefixLen = fibEntry.getPrefix().size() + 1;
243  me == nullptr && prefixLen <= interest.getName().size(); ++prefixLen) {
244  me = m_measurements.get(interest.getName().getPrefix(prefixLen));
245  }
246 
247  // Either the FIB entry or the Interest's name must be under this strategy's namespace
248  BOOST_ASSERT(me != nullptr);
249 
250  // Set or update entry lifetime
251  extendLifetime(*me);
252 
253  NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>().first;
254  BOOST_ASSERT(info != nullptr);
255  return *info;
256 }
257 
258 void
259 AsfMeasurements::extendLifetime(measurements::Entry& me)
260 {
261  m_measurements.extendLifetime(me, MEASUREMENTS_LIFETIME);
262 }
263 
264 } // namespace asf
265 } // namespace fw
266 } // namespace nfd
const Name & getName() const
FaceInfo & getOrCreateFaceInfo(const fib::Entry &fibEntry, const Interest &interest, const Face &face)
represents a Measurements entry
void cancel(const EventId &eventId)
cancel a scheduled event
Definition: scheduler.cpp:53
time::microseconds Duration
NamespaceInfo * getNamespaceInfo(const Name &prefix)
FaceInfo * getFaceInfo(const fib::Entry &fibEntry, const Face &face)
represents a FIB entry
Definition: fib-entry.hpp:51
void extendFaceInfoLifetime(FaceInfo &info, const Face &face)
void addRttMeasurement(RttEstimator::Duration &durationRtt)
time::duration< double, boost::micro > Rtt
std::pair< T *, bool > insertStrategyInfo(A &&...args)
insert a StrategyInfo item
Table::const_iterator iterator
Definition: cs-internal.hpp:41
void cancelTimeoutEvent(const Name &prefix)
FaceInfo * getFaceInfo(const fib::Entry &fibEntry, const Interest &interest, const Face &face)
const Name & getPrefix() const
Definition: fib-entry.hpp:58
static constexpr time::microseconds MEASUREMENTS_LIFETIME
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
NamespaceInfo & getOrCreateNamespaceInfo(const fib::Entry &fibEntry, const Interest &interest)
const scheduler::EventId & getMeasurementExpirationEventId()
bool isTimeoutScheduled() const
void recordRtt(const shared_ptr< pit::Entry > &pitEntry, const Face &inFace)
void expireFaceInfo(nfd::face::FaceId faceId)
stores stategy information about each face in this namespace
void recordTimeout(const Name &interestName)
void setTimeoutEvent(const scheduler::EventId &id, const Name &interestName)
#define NFD_LOG_INIT(name)
Definition: logger.hpp:122
EventId schedule(const time::nanoseconds &after, const Scheduler::Event &event)
schedule an event
Definition: scheduler.cpp:47
static const Rtt RTT_TIMEOUT
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:160
uint64_t FaceId
identifies a face
Definition: face.hpp:39
AsfMeasurements(MeasurementsAccessor &measurements)
FaceInfo & getOrCreateFaceInfo(const fib::Entry &fibEntry, const Face &face)
void setMeasurementExpirationEventId(const scheduler::EventId &id)
void addMeasurement(Duration measure)
Strategy information for each face in a namespace.
static const Rtt RTT_NO_MEASUREMENT