blob: fd3a592af44fe29c14aa74c6c744e0aa33187b76 [file] [log] [blame]
Davide Pesavento44b3b232017-12-23 16:58:25 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
schneiderklausd8197df2019-03-16 11:31:40 -07003 * Copyright (c) 2016-2019, 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
schneiderklausd8197df2019-03-16 11:31:40 -070026#include "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 {
Weiwei Liu245d7912016-07-28 00:04:25 -070032
33RttEstimator::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 Ghasemi3dae1092017-12-21 12:39:08 -070038 , 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 Liu245d7912016-07-28 00:04:25 -070042{
43 if (m_options.isVerbose) {
44 std::cerr << m_options;
45 }
46}
47
48void
49RttEstimator::addMeasurement(uint64_t segNo, Milliseconds rtt, size_t nExpectedSamples)
50{
51 BOOST_ASSERT(nExpectedSamples > 0);
52
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -070053 if (m_nRttSamples == 0) { // first measurement
Weiwei Liu245d7912016-07-28 00:04:25 -070054 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 Liu245d7912016-07-28 00:04:25 -070067 afterRttMeasurement({segNo, rtt, m_sRtt, m_rttVar, m_rto});
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -070068
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 Liu245d7912016-07-28 00:04:25 -070073}
74
75void
76RttEstimator::backoffRto()
77{
78 m_rto = ndn::clamp(m_rto * m_options.rtoBackoffMultiplier,
79 m_options.minRto, m_options.maxRto);
80}
81
82std::ostream&
83operator<<(std::ostream& os, const RttEstimator::Options& options)
84{
Davide Pesavento44b3b232017-12-23 16:58:25 -050085 os << "RTT estimator parameters:\n"
Weiwei Liu245d7912016-07-28 00:04:25 -070086 << "\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 Liu245d7912016-07-28 00:04:25 -070095} // namespace chunks
96} // namespace ndn