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