blob: 2e1852fc819052e121b33c96dafb9e36eba74b4d [file] [log] [blame]
Davide Pesavento44b3b232017-12-23 16:58:25 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -07003 * Copyright (c) 2016-2018, Arizona Board of Regents.
Weiwei Liu245d7912016-07-28 00:04:25 -07004 *
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 Ghasemi3dae1092017-12-21 12:39:08 -070023 * @author Chavoosh Ghasemi
Weiwei Liu245d7912016-07-28 00:04:25 -070024 */
25
26#include "aimd-rtt-estimator.hpp"
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -070027
Weiwei Liu245d7912016-07-28 00:04:25 -070028#include <cmath>
29
30namespace ndn {
31namespace chunks {
32namespace aimd {
33
34RttEstimator::RttEstimator(const Options& options)
35 : m_options(options)
36 , m_sRtt(std::numeric_limits<double>::quiet_NaN())
37 , m_rttVar(std::numeric_limits<double>::quiet_NaN())
38 , m_rto(m_options.initialRto.count())
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -070039 , m_rttMin(std::numeric_limits<double>::max())
40 , m_rttMax(std::numeric_limits<double>::min())
41 , m_rttAvg(0.0)
42 , m_nRttSamples(0)
Weiwei Liu245d7912016-07-28 00:04:25 -070043{
44 if (m_options.isVerbose) {
45 std::cerr << m_options;
46 }
47}
48
49void
50RttEstimator::addMeasurement(uint64_t segNo, Milliseconds rtt, size_t nExpectedSamples)
51{
52 BOOST_ASSERT(nExpectedSamples > 0);
53
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -070054 if (m_nRttSamples == 0) { // first measurement
Weiwei Liu245d7912016-07-28 00:04:25 -070055 m_sRtt = rtt;
56 m_rttVar = m_sRtt / 2;
57 m_rto = m_sRtt + m_options.k * m_rttVar;
58 }
59 else {
60 double alpha = m_options.alpha / nExpectedSamples;
61 double beta = m_options.beta / nExpectedSamples;
62 m_rttVar = (1 - beta) * m_rttVar + beta * time::abs(m_sRtt - rtt);
63 m_sRtt = (1 - alpha) * m_sRtt + alpha * rtt;
64 m_rto = m_sRtt + m_options.k * m_rttVar;
65 }
66
67 m_rto = ndn::clamp(m_rto, m_options.minRto, m_options.maxRto);
Weiwei Liu245d7912016-07-28 00:04:25 -070068 afterRttMeasurement({segNo, rtt, m_sRtt, m_rttVar, m_rto});
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -070069
70 m_rttAvg = (m_nRttSamples * m_rttAvg + rtt.count()) / (m_nRttSamples + 1);
71 m_rttMax = std::max(rtt.count(), m_rttMax);
72 m_rttMin = std::min(rtt.count(), m_rttMin);
73 m_nRttSamples++;
Weiwei Liu245d7912016-07-28 00:04:25 -070074}
75
76void
77RttEstimator::backoffRto()
78{
79 m_rto = ndn::clamp(m_rto * m_options.rtoBackoffMultiplier,
80 m_options.minRto, m_options.maxRto);
81}
82
83std::ostream&
84operator<<(std::ostream& os, const RttEstimator::Options& options)
85{
Davide Pesavento44b3b232017-12-23 16:58:25 -050086 os << "RTT estimator parameters:\n"
Weiwei Liu245d7912016-07-28 00:04:25 -070087 << "\tAlpha = " << options.alpha << "\n"
88 << "\tBeta = " << options.beta << "\n"
89 << "\tK = " << options.k << "\n"
90 << "\tInitial RTO = " << options.initialRto << "\n"
91 << "\tMin RTO = " << options.minRto << "\n"
92 << "\tMax RTO = " << options.maxRto << "\n";
93 return os;
94}
95
96} // namespace aimd
97} // namespace chunks
98} // namespace ndn