blob: 8de3078c659674972f01307bfb1d4281e8379171 [file] [log] [blame]
Junxiao Shi02e32242014-05-17 20:51:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Eric Newberry185ab292017-03-28 06:45:39 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis
Junxiao Shi02e32242014-05-17 20:51:11 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 **/
25
26#include "rtt-estimator.hpp"
27
28namespace nfd {
29
30RttEstimator::RttEstimator(uint16_t maxMultiplier, Duration minRto, double gain)
31 : m_maxMultiplier(maxMultiplier)
32 , m_minRto(minRto.count())
33 , m_rtt(RttEstimator::getInitialRtt().count())
34 , m_gain(gain)
35 , m_variance(0)
36 , m_multiplier(1)
37 , m_nSamples(0)
38{
39}
40
41void
42RttEstimator::addMeasurement(Duration measure)
43{
44 double m = static_cast<double>(measure.count());
45 if (m_nSamples > 0) {
46 double err = m - m_rtt;
47 double gErr = err * m_gain;
48 m_rtt += gErr;
49 double difference = std::abs(err) - m_variance;
50 m_variance += difference * m_gain;
Eric Newberry185ab292017-03-28 06:45:39 +000051 }
52 else {
Junxiao Shi02e32242014-05-17 20:51:11 -070053 m_rtt = m;
54 m_variance = m;
55 }
56 ++m_nSamples;
57 m_multiplier = 1;
58}
59
60void
61RttEstimator::incrementMultiplier()
62{
63 m_multiplier = std::min(static_cast<uint16_t>(m_multiplier + 1), m_maxMultiplier);
64}
65
66void
67RttEstimator::doubleMultiplier()
68{
69 m_multiplier = std::min(static_cast<uint16_t>(m_multiplier * 2), m_maxMultiplier);
70}
71
72RttEstimator::Duration
73RttEstimator::computeRto() const
74{
75 double rto = std::max(m_minRto, m_rtt + 4 * m_variance);
76 rto *= m_multiplier;
77 return Duration(static_cast<Duration::rep>(rto));
78}
79
80} // namespace nfd