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 | |
Ernest McCracken | 1402fa1 | 2019-06-09 00:36:28 -0700 | [diff] [blame] | 35 | const time::nanoseconds RttStats::RTT_TIMEOUT(-1); |
| 36 | const time::nanoseconds RttStats::RTT_NO_MEASUREMENT(0); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 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 |
Ernest McCracken | 1402fa1 | 2019-06-09 00:36:28 -0700 | [diff] [blame] | 46 | RttStats::addRttMeasurement(time::nanoseconds durationRtt) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 47 | { |
Ernest McCracken | 1402fa1 | 2019-06-09 00:36:28 -0700 | [diff] [blame] | 48 | m_rtt = durationRtt; |
| 49 | m_rttEstimator.addMeasurement(durationRtt, 1); |
| 50 | m_srtt = m_rttEstimator.getSmoothedRtt(); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | //////////////////////////////////////////////////////////////////////////////// |
| 54 | //////////////////////////////////////////////////////////////////////////////// |
| 55 | |
| 56 | FaceInfo::FaceInfo() |
| 57 | : m_isTimeoutScheduled(false) |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 58 | , m_nSilentTimeouts(0) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 59 | { |
| 60 | } |
| 61 | |
| 62 | FaceInfo::~FaceInfo() |
| 63 | { |
| 64 | cancelTimeoutEvent(); |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 65 | m_measurementExpirationId.cancel(); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 69 | FaceInfo::setTimeoutEvent(const scheduler::EventId& id, const Name& interestName) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 70 | { |
| 71 | if (!m_isTimeoutScheduled) { |
| 72 | m_timeoutEventId = id; |
| 73 | m_isTimeoutScheduled = true; |
| 74 | m_lastInterestName = interestName; |
| 75 | } |
| 76 | else { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 77 | 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] | 78 | } |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | FaceInfo::cancelTimeoutEvent() |
| 83 | { |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 84 | m_timeoutEventId.cancel(); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 85 | m_isTimeoutScheduled = false; |
| 86 | } |
| 87 | |
| 88 | void |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 89 | FaceInfo::cancelTimeoutEvent(const Name& prefix) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 90 | { |
| 91 | if (isTimeoutScheduled() && doesNameMatchLastInterest(prefix)) { |
| 92 | cancelTimeoutEvent(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | bool |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 97 | FaceInfo::doesNameMatchLastInterest(const Name& name) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 98 | { |
| 99 | return m_lastInterestName.isPrefixOf(name); |
| 100 | } |
| 101 | |
| 102 | void |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 103 | FaceInfo::recordRtt(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 104 | { |
| 105 | // Calculate RTT |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 106 | auto outRecord = pitEntry->getOutRecord(inFace, 0); |
Ashlesh Gawande | d3ac777 | 2016-11-06 00:53:05 -0600 | [diff] [blame] | 107 | |
| 108 | if (outRecord == pitEntry->out_end()) { // no out-record |
| 109 | NFD_LOG_TRACE(pitEntry->getInterest() << " dataFrom inFace=" << inFace.getId() << " no-out-record"); |
| 110 | return; |
| 111 | } |
| 112 | |
Ernest McCracken | 1402fa1 | 2019-06-09 00:36:28 -0700 | [diff] [blame] | 113 | m_rttStats.addRttMeasurement(time::steady_clock::now() - outRecord->getLastRenewed()); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 114 | NFD_LOG_TRACE("Recording RTT for FaceId: " << inFace.getId() |
Ernest McCracken | 1402fa1 | 2019-06-09 00:36:28 -0700 | [diff] [blame] | 115 | << " RTT: " << m_rttStats.getRtt() << " SRTT: " << m_rttStats.getSrtt()); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 119 | FaceInfo::recordTimeout(const Name& interestName) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 120 | { |
| 121 | m_rttStats.recordTimeout(); |
| 122 | cancelTimeoutEvent(interestName); |
| 123 | } |
| 124 | |
| 125 | //////////////////////////////////////////////////////////////////////////////// |
| 126 | //////////////////////////////////////////////////////////////////////////////// |
| 127 | |
| 128 | NamespaceInfo::NamespaceInfo() |
| 129 | : m_isProbingDue(false) |
| 130 | , m_hasFirstProbeBeenScheduled(false) |
| 131 | { |
| 132 | } |
| 133 | |
| 134 | FaceInfo* |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 135 | NamespaceInfo::getFaceInfo(const fib::Entry&, FaceId faceId) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 136 | { |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 137 | auto it = m_fit.find(faceId); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 138 | if (it != m_fit.end()) { |
| 139 | return &it->second; |
| 140 | } |
| 141 | else { |
| 142 | return nullptr; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | FaceInfo& |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 147 | NamespaceInfo::getOrCreateFaceInfo(const fib::Entry&, FaceId faceId) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 148 | { |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 149 | auto it = m_fit.find(faceId); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 150 | FaceInfo* info = nullptr; |
| 151 | |
| 152 | if (it == m_fit.end()) { |
Ernest McCracken | 1402fa1 | 2019-06-09 00:36:28 -0700 | [diff] [blame] | 153 | const auto& pair = m_fit.emplace(std::piecewise_construct, |
| 154 | std::forward_as_tuple(faceId), |
| 155 | std::forward_as_tuple()); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 156 | info = &pair.first->second; |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 157 | extendFaceInfoLifetime(*info, faceId); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 158 | } |
| 159 | else { |
| 160 | info = &it->second; |
| 161 | } |
| 162 | |
| 163 | return *info; |
| 164 | } |
| 165 | |
| 166 | void |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 167 | NamespaceInfo::expireFaceInfo(FaceId faceId) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 168 | { |
| 169 | m_fit.erase(faceId); |
| 170 | } |
| 171 | |
| 172 | void |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 173 | NamespaceInfo::extendFaceInfoLifetime(FaceInfo& info, FaceId faceId) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 174 | { |
| 175 | // Cancel previous expiration |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 176 | info.getMeasurementExpirationEventId().cancel(); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 177 | |
| 178 | // Refresh measurement |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 179 | auto id = getScheduler().schedule(AsfMeasurements::MEASUREMENTS_LIFETIME, |
| 180 | [=] { expireFaceInfo(faceId); }); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 181 | info.setMeasurementExpirationEventId(id); |
| 182 | } |
| 183 | |
| 184 | //////////////////////////////////////////////////////////////////////////////// |
| 185 | //////////////////////////////////////////////////////////////////////////////// |
| 186 | |
| 187 | constexpr time::microseconds AsfMeasurements::MEASUREMENTS_LIFETIME; |
| 188 | |
| 189 | AsfMeasurements::AsfMeasurements(MeasurementsAccessor& measurements) |
| 190 | : m_measurements(measurements) |
| 191 | { |
| 192 | } |
| 193 | |
| 194 | FaceInfo* |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 195 | AsfMeasurements::getFaceInfo(const fib::Entry& fibEntry, const Interest& interest, FaceId faceId) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 196 | { |
| 197 | NamespaceInfo& info = getOrCreateNamespaceInfo(fibEntry, interest); |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 198 | return info.getFaceInfo(fibEntry, faceId); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | FaceInfo& |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 202 | AsfMeasurements::getOrCreateFaceInfo(const fib::Entry& fibEntry, const Interest& interest, |
| 203 | FaceId faceId) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 204 | { |
| 205 | NamespaceInfo& info = getOrCreateNamespaceInfo(fibEntry, interest); |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 206 | return info.getOrCreateFaceInfo(fibEntry, faceId); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 209 | NamespaceInfo* |
| 210 | AsfMeasurements::getNamespaceInfo(const Name& prefix) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 211 | { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 212 | measurements::Entry* me = m_measurements.findLongestPrefixMatch(prefix); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 213 | if (me == nullptr) { |
| 214 | return nullptr; |
| 215 | } |
| 216 | |
| 217 | // Set or update entry lifetime |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 218 | extendLifetime(*me); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 219 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 220 | NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>().first; |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 221 | BOOST_ASSERT(info != nullptr); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 222 | return info; |
| 223 | } |
| 224 | |
| 225 | NamespaceInfo& |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 226 | AsfMeasurements::getOrCreateNamespaceInfo(const fib::Entry& fibEntry, const Interest& interest) |
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.get(fibEntry); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 229 | |
| 230 | // If the FIB entry is not under the strategy's namespace, find a part of the prefix |
| 231 | // that falls under the strategy's namespace |
| 232 | for (size_t prefixLen = fibEntry.getPrefix().size() + 1; |
| 233 | me == nullptr && prefixLen <= interest.getName().size(); ++prefixLen) { |
| 234 | me = m_measurements.get(interest.getName().getPrefix(prefixLen)); |
| 235 | } |
| 236 | |
| 237 | // Either the FIB entry or the Interest's name must be under this strategy's namespace |
| 238 | BOOST_ASSERT(me != nullptr); |
| 239 | |
| 240 | // Set or update entry lifetime |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 241 | extendLifetime(*me); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 242 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 243 | NamespaceInfo* info = me->insertStrategyInfo<NamespaceInfo>().first; |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 244 | BOOST_ASSERT(info != nullptr); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 245 | return *info; |
| 246 | } |
| 247 | |
| 248 | void |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 249 | AsfMeasurements::extendLifetime(measurements::Entry& me) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 250 | { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 251 | m_measurements.extendLifetime(me, MEASUREMENTS_LIFETIME); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | } // namespace asf |
| 255 | } // namespace fw |
| 256 | } // namespace nfd |