blob: f79b6e435e196068779943732bc8ec04a16be1d3 [file] [log] [blame]
Eric Newberrye345baa2018-05-23 18:17:07 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (C) 2016-2018, Arizona Board of Regents.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <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
23 * @author Chavoosh Ghasemi
24 */
25
26#include "rtt-estimator.hpp"
27
28#include <cmath>
29#include <limits>
30
31namespace ndn {
32namespace util {
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())
39 , m_rttMin(std::numeric_limits<double>::max())
40 , m_rttMax(std::numeric_limits<double>::min())
41 , m_rttAvg(0.0)
42 , m_nRttSamples(0)
43{
44}
45
46void
47RttEstimator::addMeasurement(MillisecondsDouble rtt, size_t nExpectedSamples)
48{
49 BOOST_ASSERT(nExpectedSamples > 0);
50
51 if (m_nRttSamples == 0) { // first measurement
52 m_sRtt = rtt;
53 m_rttVar = m_sRtt / 2;
54 m_rto = m_sRtt + m_options.k * m_rttVar;
55 }
56 else {
57 double alpha = m_options.alpha / nExpectedSamples;
58 double beta = m_options.beta / nExpectedSamples;
59 m_rttVar = (1 - beta) * m_rttVar + beta * time::abs(m_sRtt - rtt);
60 m_sRtt = (1 - alpha) * m_sRtt + alpha * rtt;
61 m_rto = m_sRtt + m_options.k * m_rttVar;
62 }
63
64 m_rto = clamp(m_rto, m_options.minRto, m_options.maxRto);
65
66 m_rttAvg = MillisecondsDouble((m_nRttSamples * m_rttAvg.count() + rtt.count()) / (m_nRttSamples + 1));
67 m_rttMax = std::max<MillisecondsDouble>(rtt, m_rttMax);
68 m_rttMin = std::min<MillisecondsDouble>(rtt, m_rttMin);
69 m_nRttSamples++;
70}
71
72void
73RttEstimator::backoffRto()
74{
75 m_rto = clamp(m_rto * m_options.rtoBackoffMultiplier,
76 m_options.minRto, m_options.maxRto);
77}
78
79} // namespace util
80} // namespace ndn