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