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-2024, 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::util {
30 
31 RttEstimator::RttEstimator(std::shared_ptr<const Options> options)
32  : m_options(options ? std::move(options) : std::make_shared<const Options>())
33  , m_rto(m_options->initialRto)
34 {
35  BOOST_ASSERT(m_options->alpha >= 0 && m_options->alpha <= 1);
36  BOOST_ASSERT(m_options->beta >= 0 && m_options->beta <= 1);
37  BOOST_ASSERT(m_options->initialRto >= 0_ns);
38  BOOST_ASSERT(m_options->minRto >= 0_ns);
39  BOOST_ASSERT(m_options->maxRto >= m_options->minRto);
40  BOOST_ASSERT(m_options->k >= 0);
41  BOOST_ASSERT(m_options->rtoBackoffMultiplier >= 1);
42 }
43 
44 void
45 RttEstimator::addMeasurement(time::nanoseconds rtt, size_t nExpectedSamples)
46 {
47  BOOST_ASSERT(rtt >= 0_ns);
48  BOOST_ASSERT(nExpectedSamples > 0);
49 
50  if (!hasSamples()) { // first measurement
51  m_sRtt = rtt;
52  m_rttVar = m_sRtt / 2;
53  }
54  else {
55  double alpha = m_options->alpha / nExpectedSamples;
56  double beta = m_options->beta / nExpectedSamples;
57  m_rttVar = time::duration_cast<time::nanoseconds>((1 - beta) * m_rttVar +
58  beta * time::abs(m_sRtt - rtt));
59  m_sRtt = time::duration_cast<time::nanoseconds>((1 - alpha) * m_sRtt + alpha * rtt);
60  }
61  m_rto = std::clamp(m_sRtt + m_options->k * m_rttVar,
62  m_options->minRto, m_options->maxRto);
63 }
64 
65 void
67 {
68  m_rto = std::clamp(m_rto * m_options->rtoBackoffMultiplier,
69  m_options->minRto, m_options->maxRto);
70 }
71 
72 void
74 {
75  RttEstimator::addMeasurement(rtt, nExpectedSamples);
76 
77  m_rttAvg = (m_nRttSamples * m_rttAvg + rtt) / (m_nRttSamples + 1);
78  m_rttMax = std::max(rtt, m_rttMax);
79  m_rttMin = std::min(rtt, m_rttMin);
80  m_nRttSamples++;
81 }
82 
83 } // namespace ndn::util
void addMeasurement(time::nanoseconds rtt, size_t nExpectedSamples=1)
Records a new RTT measurement.
std::shared_ptr< const Options > m_options
void backoffRto()
Backoff RTO by a factor of Options::rtoBackoffMultiplier.
void addMeasurement(time::nanoseconds rtt, size_t nExpectedSamples=1)
Records a new RTT measurement.
RttEstimator(std::shared_ptr< const Options > options=nullptr)
Constructor.
constexpr duration< Rep, Period > abs(duration< Rep, Period > d)
Returns the absolute value of the duration d.
Definition: time.hpp:63
::boost::chrono::nanoseconds nanoseconds
Definition: time.hpp:54