blob: 97b2ab0be7647dad4503383bea7842aa11e84ae5 [file] [log] [blame]
Junxiao Shi02e32242014-05-17 20:51:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 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
10 *
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;
51 } else {
52 m_rtt = m;
53 m_variance = m;
54 }
55 ++m_nSamples;
56 m_multiplier = 1;
57}
58
59void
60RttEstimator::incrementMultiplier()
61{
62 m_multiplier = std::min(static_cast<uint16_t>(m_multiplier + 1), m_maxMultiplier);
63}
64
65void
66RttEstimator::doubleMultiplier()
67{
68 m_multiplier = std::min(static_cast<uint16_t>(m_multiplier * 2), m_maxMultiplier);
69}
70
71RttEstimator::Duration
72RttEstimator::computeRto() const
73{
74 double rto = std::max(m_minRto, m_rtt + 4 * m_variance);
75 rto *= m_multiplier;
76 return Duration(static_cast<Duration::rep>(rto));
77}
78
79} // namespace nfd