Davide Pesavento | 44b3b23 | 2017-12-23 16:58:25 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Chavoosh Ghasemi | 3dae109 | 2017-12-21 12:39:08 -0700 | [diff] [blame] | 3 | * Copyright (c) 2016-2018, Arizona Board of Regents. |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 6 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 7 | * |
| 8 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | * |
| 21 | * @author Shuo Yang |
| 22 | * @author Weiwei Liu |
Chavoosh Ghasemi | 3dae109 | 2017-12-21 12:39:08 -0700 | [diff] [blame] | 23 | * @author Chavoosh Ghasemi |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include "aimd-rtt-estimator.hpp" |
Chavoosh Ghasemi | 3dae109 | 2017-12-21 12:39:08 -0700 | [diff] [blame] | 27 | |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 28 | #include <cmath> |
Chavoosh Ghasemi | 3dae109 | 2017-12-21 12:39:08 -0700 | [diff] [blame] | 29 | #include <limits> |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 30 | |
| 31 | namespace ndn { |
| 32 | namespace chunks { |
| 33 | namespace aimd { |
| 34 | |
| 35 | RttEstimator::RttEstimator(const Options& options) |
| 36 | : m_options(options) |
| 37 | , m_sRtt(std::numeric_limits<double>::quiet_NaN()) |
| 38 | , m_rttVar(std::numeric_limits<double>::quiet_NaN()) |
| 39 | , m_rto(m_options.initialRto.count()) |
Chavoosh Ghasemi | 3dae109 | 2017-12-21 12:39:08 -0700 | [diff] [blame] | 40 | , m_rttMin(std::numeric_limits<double>::max()) |
| 41 | , m_rttMax(std::numeric_limits<double>::min()) |
| 42 | , m_rttAvg(0.0) |
| 43 | , m_nRttSamples(0) |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 44 | { |
| 45 | if (m_options.isVerbose) { |
| 46 | std::cerr << m_options; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | RttEstimator::addMeasurement(uint64_t segNo, Milliseconds rtt, size_t nExpectedSamples) |
| 52 | { |
| 53 | BOOST_ASSERT(nExpectedSamples > 0); |
| 54 | |
Chavoosh Ghasemi | 3dae109 | 2017-12-21 12:39:08 -0700 | [diff] [blame] | 55 | if (m_nRttSamples == 0) { // first measurement |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 56 | m_sRtt = rtt; |
| 57 | m_rttVar = m_sRtt / 2; |
| 58 | m_rto = m_sRtt + m_options.k * m_rttVar; |
| 59 | } |
| 60 | else { |
| 61 | double alpha = m_options.alpha / nExpectedSamples; |
| 62 | double beta = m_options.beta / nExpectedSamples; |
| 63 | m_rttVar = (1 - beta) * m_rttVar + beta * time::abs(m_sRtt - rtt); |
| 64 | m_sRtt = (1 - alpha) * m_sRtt + alpha * rtt; |
| 65 | m_rto = m_sRtt + m_options.k * m_rttVar; |
| 66 | } |
| 67 | |
| 68 | m_rto = ndn::clamp(m_rto, m_options.minRto, m_options.maxRto); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 69 | afterRttMeasurement({segNo, rtt, m_sRtt, m_rttVar, m_rto}); |
Chavoosh Ghasemi | 3dae109 | 2017-12-21 12:39:08 -0700 | [diff] [blame] | 70 | |
| 71 | m_rttAvg = (m_nRttSamples * m_rttAvg + rtt.count()) / (m_nRttSamples + 1); |
| 72 | m_rttMax = std::max(rtt.count(), m_rttMax); |
| 73 | m_rttMin = std::min(rtt.count(), m_rttMin); |
| 74 | m_nRttSamples++; |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void |
| 78 | RttEstimator::backoffRto() |
| 79 | { |
| 80 | m_rto = ndn::clamp(m_rto * m_options.rtoBackoffMultiplier, |
| 81 | m_options.minRto, m_options.maxRto); |
| 82 | } |
| 83 | |
| 84 | std::ostream& |
| 85 | operator<<(std::ostream& os, const RttEstimator::Options& options) |
| 86 | { |
Davide Pesavento | 44b3b23 | 2017-12-23 16:58:25 -0500 | [diff] [blame] | 87 | os << "RTT estimator parameters:\n" |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 88 | << "\tAlpha = " << options.alpha << "\n" |
| 89 | << "\tBeta = " << options.beta << "\n" |
| 90 | << "\tK = " << options.k << "\n" |
| 91 | << "\tInitial RTO = " << options.initialRto << "\n" |
| 92 | << "\tMin RTO = " << options.minRto << "\n" |
| 93 | << "\tMax RTO = " << options.maxRto << "\n"; |
| 94 | return os; |
| 95 | } |
| 96 | |
| 97 | } // namespace aimd |
| 98 | } // namespace chunks |
| 99 | } // namespace ndn |