rtt-estimator.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (C) 2016-2019, 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  * @author Davide Pesavento
25  */
26 
28 
29 namespace ndn {
30 namespace util {
31 
32 RttEstimator::RttEstimator(shared_ptr<const Options> options)
33  : m_options(options ? std::move(options) : make_shared<const Options>())
34  , m_rto(m_options->initialRto)
35 {
36  BOOST_ASSERT(m_options->alpha >= 0 && m_options->alpha <= 1);
37  BOOST_ASSERT(m_options->beta >= 0 && m_options->beta <= 1);
38  BOOST_ASSERT(m_options->initialRto >= 0_ns);
39  BOOST_ASSERT(m_options->minRto >= 0_ns);
40  BOOST_ASSERT(m_options->maxRto >= m_options->minRto);
41  BOOST_ASSERT(m_options->k >= 0);
42  BOOST_ASSERT(m_options->rtoBackoffMultiplier >= 1);
43 }
44 
45 void
46 RttEstimator::addMeasurement(time::nanoseconds rtt, size_t nExpectedSamples)
47 {
48  BOOST_ASSERT(rtt >= 0_ns);
49  BOOST_ASSERT(nExpectedSamples > 0);
50 
51  if (!hasSamples()) { // first measurement
52  m_sRtt = rtt;
53  m_rttVar = m_sRtt / 2;
54  }
55  else {
56  double alpha = m_options->alpha / nExpectedSamples;
57  double beta = m_options->beta / nExpectedSamples;
58  m_rttVar = time::duration_cast<time::nanoseconds>((1 - beta) * m_rttVar +
59  beta * time::abs(m_sRtt - rtt));
60  m_sRtt = time::duration_cast<time::nanoseconds>((1 - alpha) * m_sRtt + alpha * rtt);
61  }
62  m_rto = clamp(m_sRtt + m_options->k * m_rttVar,
63  m_options->minRto, m_options->maxRto);
64 }
65 
66 void
68 {
69  m_rto = clamp(m_rto * m_options->rtoBackoffMultiplier,
70  m_options->minRto, m_options->maxRto);
71 }
72 
73 void
75 {
76  RttEstimator::addMeasurement(rtt, nExpectedSamples);
77 
78  m_rttAvg = (m_nRttSamples * m_rttAvg + rtt) / (m_nRttSamples + 1);
79  m_rttMax = std::max(rtt, m_rttMax);
80  m_rttMin = std::min(rtt, m_rttMin);
81  m_nRttSamples++;
82 }
83 
84 } // namespace util
85 } // namespace ndn
void addMeasurement(time::nanoseconds rtt, size_t nExpectedSamples=1)
Records a new RTT measurement.
void backoffRto()
Backoff RTO by a factor of Options::rtoBackoffMultiplier.
void addMeasurement(time::nanoseconds rtt, size_t nExpectedSamples=1)
Records a new RTT measurement.
shared_ptr< const Options > m_options
RttEstimator(shared_ptr< const Options > options=nullptr)
Constructor.
constexpr duration< Rep, Period > abs(duration< Rep, Period > d)
Definition: time.hpp:58
boost::chrono::nanoseconds nanoseconds
Definition: time.hpp:50
Definition: data.cpp:25
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition: backports.hpp:112