Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 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" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 27 | #include "common/global.hpp" |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 28 | |
| 29 | namespace nfd { |
| 30 | namespace fw { |
| 31 | namespace asf { |
| 32 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 33 | NFD_LOG_INIT(AsfMeasurements); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 34 | |
| 35 | const RttStats::Rtt RttStats::RTT_TIMEOUT(-1.0); |
| 36 | const RttStats::Rtt RttStats::RTT_NO_MEASUREMENT(0.0); |
| 37 | const double RttStats::ALPHA = 0.125; |
| 38 | |
| 39 | RttStats::RttStats() |
| 40 | : m_srtt(RTT_NO_MEASUREMENT) |
| 41 | , m_rtt(RTT_NO_MEASUREMENT) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | RttStats::addRttMeasurement(RttEstimator::Duration& durationRtt) |
| 47 | { |
| 48 | m_rtt = static_cast<RttStats::Rtt>(durationRtt.count()); |
| 49 | |
| 50 | m_rttEstimator.addMeasurement(durationRtt); |
| 51 | |
| 52 | m_srtt = computeSrtt(m_srtt, m_rtt); |
| 53 | } |
| 54 | |
| 55 | RttStats::Rtt |
| 56 | RttStats::computeSrtt(Rtt previousSrtt, Rtt currentRtt) |
| 57 | { |
| 58 | if (previousSrtt == RTT_NO_MEASUREMENT) { |
| 59 | return currentRtt; |
| 60 | } |
| 61 | |
| 62 | return Rtt(ALPHA * currentRtt + (1 - ALPHA) * previousSrtt); |
| 63 | } |
| 64 | |
| 65 | //////////////////////////////////////////////////////////////////////////////// |
| 66 | //////////////////////////////////////////////////////////////////////////////// |
| 67 | |
| 68 | FaceInfo::FaceInfo() |
| 69 | : m_isTimeoutScheduled(false) |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 70 | , m_nSilentTimeouts(0) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 71 | { |
| 72 | } |
| 73 | |
| 74 | FaceInfo::~FaceInfo() |
| 75 | { |
| 76 | cancelTimeoutEvent(); |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 77 | m_measurementExpirationId.cancel(); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 81 | FaceInfo::setTimeoutEvent(const scheduler::EventId& id, const Name& interestName) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 82 | { |
| 83 | if (!m_isTimeoutScheduled) { |
| 84 | m_timeoutEventId = id; |
| 85 | m_isTimeoutScheduled = true; |
| 86 | m_lastInterestName = interestName; |
| 87 | } |
| 88 | else { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 89 | NDN_THROW(FaceInfo::Error("Tried to schedule a timeout for a face that already has a timeout scheduled")); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | FaceInfo::cancelTimeoutEvent() |
| 95 | { |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 96 | m_timeoutEventId.cancel(); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 97 | m_isTimeoutScheduled = false; |
| 98 | } |
| 99 | |
| 100 | void |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 101 | FaceInfo::cancelTimeoutEvent(const Name& prefix) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 102 | { |
| 103 | if (isTimeoutScheduled() && doesNameMatchLastInterest(prefix)) { |
| 104 | cancelTimeoutEvent(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | bool |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 109 | FaceInfo::doesNameMatchLastInterest(const Name& name) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 110 | { |
| 111 | return m_lastInterestName.isPrefixOf(name); |
| 112 | } |
| 113 | |
| 114 | void |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 115 | FaceInfo::recordRtt(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 116 | { |
| 117 | // Calculate RTT |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 118 | auto outRecord = pitEntry->getOutRecord(inFace, 0); |
Ashlesh Gawande | d3ac777 | 2016-11-06 00:53:05 -0600 | [diff] [blame] | 119 | |
| 120 | if (outRecord == pitEntry->out_end()) { // no out-record |
| 121 | NFD_LOG_TRACE(pitEntry->getInterest() << " dataFrom inFace=" << inFace.getId() << " no-out-record"); |
| 122 | return; |
| 123 | } |
| 124 | |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 125 | time::steady_clock::Duration steadyRtt = time::steady_clock::now() - outRecord->getLastRenewed(); |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 126 | auto durationRtt = time::duration_cast<RttEstimator::Duration>(steadyRtt); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 127 | |
| 128 | m_rttStats.addRttMeasurement(durationRtt); |
| 129 | |
| 130 | NFD_LOG_TRACE("Recording RTT for FaceId: " << inFace.getId() |
| 131 | << " RTT: " << m_rttStats.getRtt() |
| 132 | << " SRTT: " << m_rttStats.getSrtt()); |
| 133 | } |
| 134 | |
| 135 | void |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 136 | FaceInfo::recordTimeout(const Name& interestName) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 137 | { |
| 138 | m_rttStats.recordTimeout(); |
| 139 | cancelTimeoutEvent(interestName); |
| 140 | } |
| 141 | |
| 142 | //////////////////////////////////////////////////////////////////////////////// |
| 143 | //////////////////////////////////////////////////////////////////////////////// |
| 144 | |
| 145 | NamespaceInfo::NamespaceInfo() |
| 146 | : m_isProbingDue(false) |
| 147 | , m_hasFirstProbeBeenScheduled(false) |
| 148 | { |
| 149 | } |
| 150 | |
| 151 | FaceInfo* |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 152 | NamespaceInfo::getFaceInfo(const fib::Entry&, FaceId faceId) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 153 | { |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 154 | auto it = m_fit.find(faceId); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 155 | if (it != m_fit.end()) { |
| 156 | return &it->second; |
| 157 | } |
| 158 | else { |
| 159 | return nullptr; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | FaceInfo& |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 164 | NamespaceInfo::getOrCreateFaceInfo(const fib::Entry&, FaceId faceId) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 165 | { |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 166 | auto it = m_fit.find(faceId); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 167 | FaceInfo* info = nullptr; |
| 168 | |
| 169 | if (it == m_fit.end()) { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 170 | const auto& pair = m_fit.emplace(faceId, FaceInfo()); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 171 | info = &pair.first->second; |
| 172 | |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 173 | extendFaceInfoLifetime(*info, faceId); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 174 | } |
| 175 | else { |
| 176 | info = &it->second; |
| 177 | } |
| 178 | |
| 179 | return *info; |
| 180 | } |
| 181 | |
| 182 | void |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 183 | NamespaceInfo::expireFaceInfo(FaceId faceId) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 184 | { |
| 185 | m_fit.erase(faceId); |
| 186 | } |
| 187 | |
| 188 | void |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 189 | NamespaceInfo::extendFaceInfoLifetime(FaceInfo& info, FaceId faceId) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 190 | { |
| 191 | // Cancel previous expiration |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 192 | info.getMeasurementExpirationEventId().cancel(); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 193 | |
| 194 | // Refresh measurement |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 195 | auto id = getScheduler().schedule(AsfMeasurements::MEASUREMENTS_LIFETIME, |
| 196 | [=] { expireFaceInfo(faceId); }); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 197 | info.setMeasurementExpirationEventId(id); |
| 198 | } |
| 199 | |
| 200 | //////////////////////////////////////////////////////////////////////////////// |
| 201 | //////////////////////////////////////////////////////////////////////////////// |
| 202 | |
| 203 | constexpr time::microseconds AsfMeasurements::MEASUREMENTS_LIFETIME; |
| 204 | |
| 205 | AsfMeasurements::AsfMeasurements(MeasurementsAccessor& measurements) |
| 206 | : m_measurements(measurements) |
| 207 | { |
| 208 | } |
| 209 | |
| 210 | FaceInfo* |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 211 | AsfMeasurements::getFaceInfo(const fib::Entry& fibEntry, const Interest& interest, FaceId faceId) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 212 | { |
| 213 | NamespaceInfo& info = getOrCreateNamespaceInfo(fibEntry, interest); |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 214 | return info.getFaceInfo(fibEntry, faceId); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | FaceInfo& |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 218 | AsfMeasurements::getOrCreateFaceInfo(const fib::Entry& fibEntry, const Interest& interest, |
| 219 | FaceId faceId) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 220 | { |
| 221 | NamespaceInfo& info = getOrCreateNamespaceInfo(fibEntry, interest); |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 222 | return info.getOrCreateFaceInfo(fibEntry, faceId); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 225 | NamespaceInfo* |
| 226 | AsfMeasurements::getNamespaceInfo(const Name& prefix) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 227 | { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 228 | measurements::Entry* me = m_measurements.findLongestPrefixMatch(prefix); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 229 | if (me == nullptr) { |
| 230 | return nullptr; |
| 231 | } |
| 232 | |
| 233 | // Set or update entry lifetime |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 234 | extendLifetime(*me); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 235 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 236 | NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>().first; |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 237 | BOOST_ASSERT(info != nullptr); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 238 | return info; |
| 239 | } |
| 240 | |
| 241 | NamespaceInfo& |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 242 | AsfMeasurements::getOrCreateNamespaceInfo(const fib::Entry& fibEntry, const Interest& interest) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 243 | { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 244 | measurements::Entry* me = m_measurements.get(fibEntry); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 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 |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 257 | extendLifetime(*me); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 258 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 259 | NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>().first; |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 260 | BOOST_ASSERT(info != nullptr); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 261 | return *info; |
| 262 | } |
| 263 | |
| 264 | void |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 265 | AsfMeasurements::extendLifetime(measurements::Entry& me) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 266 | { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 267 | m_measurements.extendLifetime(me, MEASUREMENTS_LIFETIME); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | } // namespace asf |
| 271 | } // namespace fw |
| 272 | } // namespace nfd |