Davide Pesavento | 44b3b23 | 2017-12-23 16:58:25 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2016-2017, 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 |
| 23 | */ |
| 24 | |
| 25 | #include "aimd-rtt-estimator.hpp" |
| 26 | #include <cmath> |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace chunks { |
| 30 | namespace aimd { |
| 31 | |
| 32 | RttEstimator::RttEstimator(const Options& options) |
| 33 | : m_options(options) |
| 34 | , m_sRtt(std::numeric_limits<double>::quiet_NaN()) |
| 35 | , m_rttVar(std::numeric_limits<double>::quiet_NaN()) |
| 36 | , m_rto(m_options.initialRto.count()) |
| 37 | { |
| 38 | if (m_options.isVerbose) { |
| 39 | std::cerr << m_options; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | void |
| 44 | RttEstimator::addMeasurement(uint64_t segNo, Milliseconds rtt, size_t nExpectedSamples) |
| 45 | { |
| 46 | BOOST_ASSERT(nExpectedSamples > 0); |
| 47 | |
| 48 | if (std::isnan(m_sRtt.count())) { // first measurement |
| 49 | m_sRtt = rtt; |
| 50 | m_rttVar = m_sRtt / 2; |
| 51 | m_rto = m_sRtt + m_options.k * m_rttVar; |
| 52 | } |
| 53 | else { |
| 54 | double alpha = m_options.alpha / nExpectedSamples; |
| 55 | double beta = m_options.beta / nExpectedSamples; |
| 56 | m_rttVar = (1 - beta) * m_rttVar + beta * time::abs(m_sRtt - rtt); |
| 57 | m_sRtt = (1 - alpha) * m_sRtt + alpha * rtt; |
| 58 | m_rto = m_sRtt + m_options.k * m_rttVar; |
| 59 | } |
| 60 | |
| 61 | m_rto = ndn::clamp(m_rto, m_options.minRto, m_options.maxRto); |
| 62 | |
| 63 | afterRttMeasurement({segNo, rtt, m_sRtt, m_rttVar, m_rto}); |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | RttEstimator::backoffRto() |
| 68 | { |
| 69 | m_rto = ndn::clamp(m_rto * m_options.rtoBackoffMultiplier, |
| 70 | m_options.minRto, m_options.maxRto); |
| 71 | } |
| 72 | |
| 73 | std::ostream& |
| 74 | operator<<(std::ostream& os, const RttEstimator::Options& options) |
| 75 | { |
Davide Pesavento | 44b3b23 | 2017-12-23 16:58:25 -0500 | [diff] [blame] | 76 | os << "RTT estimator parameters:\n" |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 77 | << "\tAlpha = " << options.alpha << "\n" |
| 78 | << "\tBeta = " << options.beta << "\n" |
| 79 | << "\tK = " << options.k << "\n" |
| 80 | << "\tInitial RTO = " << options.initialRto << "\n" |
| 81 | << "\tMin RTO = " << options.minRto << "\n" |
| 82 | << "\tMax RTO = " << options.maxRto << "\n"; |
| 83 | return os; |
| 84 | } |
| 85 | |
| 86 | } // namespace aimd |
| 87 | } // namespace chunks |
| 88 | } // namespace ndn |