NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
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 
118  if (outRecord == pitEntry->out_end()) { // no out-record
119  NFD_LOG_TRACE(pitEntry->getInterest() << " dataFrom inFace=" << inFace.getId() << " no-out-record");
120  return;
121  }
122 
123  time::steady_clock::Duration steadyRtt = time::steady_clock::now() - outRecord->getLastRenewed();
124  RttEstimator::Duration durationRtt = time::duration_cast<RttEstimator::Duration>(steadyRtt);
125 
126  m_rttStats.addRttMeasurement(durationRtt);
127 
128  NFD_LOG_TRACE("Recording RTT for FaceId: " << inFace.getId()
129  << " RTT: " << m_rttStats.getRtt()
130  << " SRTT: " << m_rttStats.getSrtt());
131 }
132 
133 void
134 FaceInfo::recordTimeout(const Name& interestName)
135 {
136  m_rttStats.recordTimeout();
137  cancelTimeoutEvent(interestName);
138 }
139 
142 
144  : m_isProbingDue(false)
145  , m_hasFirstProbeBeenScheduled(false)
146 {
147 }
148 
149 FaceInfo*
151 {
152  FaceInfoTable::iterator it = m_fit.find(face.getId());
153 
154  if (it != m_fit.end()) {
155  return &it->second;
156  }
157  else {
158  return nullptr;
159  }
160 }
161 
162 FaceInfo&
164 {
165  FaceInfoTable::iterator it = m_fit.find(face.getId());
166 
167  FaceInfo* info = nullptr;
168 
169  if (it == m_fit.end()) {
170  const auto& pair = m_fit.insert(std::make_pair(face.getId(), FaceInfo()));
171  info = &pair.first->second;
172 
173  extendFaceInfoLifetime(*info, face);
174  }
175  else {
176  info = &it->second;
177  }
178 
179  return *info;
180 }
181 
182 void
184 {
185  m_fit.erase(faceId);
186 }
187 
188 void
190 {
191  // Cancel previous expiration
193 
194  // Refresh measurement
196  bind(&NamespaceInfo::expireFaceInfo, this, face.getId()));
197 
199 }
200 
203 
204 constexpr time::microseconds AsfMeasurements::MEASUREMENTS_LIFETIME;
205 
207  : m_measurements(measurements)
208 {
209 }
210 
211 FaceInfo*
212 AsfMeasurements::getFaceInfo(const fib::Entry& fibEntry, const Interest& interest, const Face& face)
213 {
214  NamespaceInfo& info = getOrCreateNamespaceInfo(fibEntry, interest);
215  return info.getFaceInfo(fibEntry, face);
216 }
217 
218 FaceInfo&
219 AsfMeasurements::getOrCreateFaceInfo(const fib::Entry& fibEntry, const Interest& interest, const Face& face)
220 {
221  NamespaceInfo& info = getOrCreateNamespaceInfo(fibEntry, interest);
222  return info.getOrCreateFaceInfo(fibEntry, face);
223 }
224 
227 {
228  measurements::Entry* me = m_measurements.findLongestPrefixMatch(prefix);
229  if (me == nullptr) {
230  return nullptr;
231  }
232 
233  // Set or update entry lifetime
234  extendLifetime(*me);
235 
236  NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>().first;
237  BOOST_ASSERT(info != nullptr);
238  return info;
239 }
240 
243 {
244  measurements::Entry* me = m_measurements.get(fibEntry);
245 
246  // If the FIB entry is not under the strategy's namespace, find a part of the prefix
247  // that falls under the strategy's namespace
248  for (size_t prefixLen = fibEntry.getPrefix().size() + 1;
249  me == nullptr && prefixLen <= interest.getName().size(); ++prefixLen) {
250  me = m_measurements.get(interest.getName().getPrefix(prefixLen));
251  }
252 
253  // Either the FIB entry or the Interest's name must be under this strategy's namespace
254  BOOST_ASSERT(me != nullptr);
255 
256  // Set or update entry lifetime
257  extendLifetime(*me);
258 
259  NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>().first;
260  BOOST_ASSERT(info != nullptr);
261  return *info;
262 }
263 
264 void
265 AsfMeasurements::extendLifetime(measurements::Entry& me)
266 {
267  m_measurements.extendLifetime(me, MEASUREMENTS_LIFETIME);
268 }
269 
270 } // namespace asf
271 } // namespace fw
272 } // namespace nfd
time::microseconds Duration
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix (PartialName) of the name, containing first nComponents components.
Definition: name.hpp:241
const Name & getPrefix() const
Definition: fib-entry.hpp:58
FaceInfo & getOrCreateFaceInfo(const fib::Entry &fibEntry, const Face &face)
FaceInfo & getOrCreateFaceInfo(const fib::Entry &fibEntry, const Interest &interest, const Face &face)
void recordRtt(const shared_ptr< pit::Entry > &pitEntry, const Face &inFace)
void setMeasurementExpirationEventId(const scheduler::EventId &id)
generalization of a network interface
Definition: face.hpp:67
void extendFaceInfoLifetime(FaceInfo &info, const Face &face)
void cancel(const EventId &eventId)
cancel a scheduled event
Definition: scheduler.cpp:53
represents a FIB entry
Definition: fib-entry.hpp:51
void addRttMeasurement(RttEstimator::Duration &durationRtt)
static time_point now() noexcept
Definition: time.cpp:79
const scheduler::EventId & getMeasurementExpirationEventId()
AsfMeasurements(MeasurementsAccessor &measurements)
represents an Interest packet
Definition: interest.hpp:42
stores stategy information about each face in this namespace
void extendLifetime(Entry &entry, const time::nanoseconds &lifetime)
extend lifetime of an entry
Entry * findLongestPrefixMatch(const Name &name, const EntryPredicate &pred=AnyEntry()) const
perform a longest prefix match for name
static const Rtt RTT_TIMEOUT
void cancelTimeoutEvent(const Name &prefix)
represents a Measurements entry
Table::const_iterator iterator
Definition: cs-internal.hpp:41
std::shared_ptr< ns3::EventId > EventId
Definition: scheduler.hpp:39
FaceInfo * getFaceInfo(const fib::Entry &fibEntry, const Face &face)
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:54
void addMeasurement(Duration measure)
FaceInfo * getFaceInfo(const fib::Entry &fibEntry, const Interest &interest, const Face &face)
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
FaceId getId() const
Definition: face.hpp:229
NamespaceInfo & getOrCreateNamespaceInfo(const fib::Entry &fibEntry, const Interest &interest)
static const Rtt RTT_NO_MEASUREMENT
static constexpr time::microseconds MEASUREMENTS_LIFETIME
Name abstraction to represent an absolute name.
Definition: name.hpp:46
void setTimeoutEvent(const scheduler::EventId &id, const Name &interestName)
bool isPrefixOf(const Name &name) const
Check if the N components of this name are the same as the first N components of the given name...
Definition: name.cpp:308
std::pair< T *, bool > insertStrategyInfo(A &&... args)
insert a StrategyInfo item
Strategy information for each face in a namespace.
size_t size() const
Get the number of components.
Definition: name.hpp:400
void expireFaceInfo(nfd::face::FaceId faceId)
allows Strategy to access portion of Measurements table under its namespace
NamespaceInfo * getNamespaceInfo(const Name &prefix)
EventId schedule(const time::nanoseconds &after, const Scheduler::Event &event)
schedule an event
Definition: scheduler.cpp:47
Entry * get(const Name &name)
find or insert a Measurements entry for name
void recordTimeout(const Name &interestName)
uint64_t FaceId
identifies a face
Definition: face.hpp:39
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
time::duration< double, boost::micro > Rtt
const Name & getName() const
Definition: interest.hpp:215