blob: 5e88f7c1ffda7dd673856d39fe0f683991b3db7f [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>
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -070029#include <limits>
Weiwei Liu245d7912016-07-28 00:04:25 -070030
31namespace ndn {
32namespace chunks {
33namespace aimd {
34
35RttEstimator::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 Ghasemi3dae1092017-12-21 12:39:08 -070040 , 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 Liu245d7912016-07-28 00:04:25 -070044{
45 if (m_options.isVerbose) {
46 std::cerr << m_options;
47 }
48}
49
50void
51RttEstimator::addMeasurement(uint64_t segNo, Milliseconds rtt, size_t nExpectedSamples)
52{
53 BOOST_ASSERT(nExpectedSamples > 0);
54
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -070055 if (m_nRttSamples == 0) { // first measurement
Weiwei Liu245d7912016-07-28 00:04:25 -070056 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 Liu245d7912016-07-28 00:04:25 -070069 afterRttMeasurement({segNo, rtt, m_sRtt, m_rttVar, m_rto});
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -070070
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 Liu245d7912016-07-28 00:04:25 -070075}
76
77void
78RttEstimator::backoffRto()
79{
80 m_rto = ndn::clamp(m_rto * m_options.rtoBackoffMultiplier,
81 m_options.minRto, m_options.maxRto);
82}
83
84std::ostream&
85operator<<(std::ostream& os, const RttEstimator::Options& options)
86{
Davide Pesavento44b3b232017-12-23 16:58:25 -050087 os << "RTT estimator parameters:\n"
Weiwei Liu245d7912016-07-28 00:04:25 -070088 << "\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