blob: 4cf5586f0ee7866a2d3c2beaef298a4dc9917f04 [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#ifndef NDN_CXX_UTIL_RTT_ESTIMATOR_HPP
27#define NDN_CXX_UTIL_RTT_ESTIMATOR_HPP
28
29#include "../common.hpp"
30#include "signal.hpp"
31#include "time.hpp"
32
33namespace ndn {
34namespace util {
35
36/**
37 * @brief RTT Estimator.
38 *
39 * This class implements the "Mean-Deviation" RTT estimator, as discussed in RFC 6298,
40 * with the modifications to RTO calculation described in RFC 7323 Appendix G.
41 */
42class RttEstimator
43{
44public:
45 using MillisecondsDouble = time::duration<double, time::milliseconds::period>;
46
47public:
48 class Options
49 {
50 public:
51 constexpr
52 Options() noexcept
53 {
54 }
55
56 public:
57 double alpha = 0.125; ///< weight of exponential moving average for meanRtt
58 double beta = 0.25; ///< weight of exponential moving average for varRtt
59 int k = 4; ///< factor of RTT variation when calculating RTO
60 MillisecondsDouble initialRto = MillisecondsDouble(1000.0); ///< initial RTO value
61 MillisecondsDouble minRto = MillisecondsDouble(200.0); ///< lower bound of RTO
62 MillisecondsDouble maxRto = MillisecondsDouble(20000.0); ///< upper bound of RTO
63 int rtoBackoffMultiplier = 2;
64 };
65
66 /**
67 * @brief Create a RTT Estimator
68 *
69 * Configures the RTT Estimator with the default parameters if an instance of Options
70 * is not passed to the constructor.
71 */
72 explicit
73 RttEstimator(const Options& options = Options());
74
75 /**
76 * @brief Add a new RTT measurement to the estimator.
77 *
78 * @param rtt the sampled rtt
79 * @param nExpectedSamples number of expected samples, must be greater than 0.
80 * It should be set to current number of in-flight Interests. Please
81 * refer to Appendix G of RFC 7323 for details.
82 * @note Don't add RTT measurements for retransmissions
83 */
84 void
85 addMeasurement(MillisecondsDouble rtt, size_t nExpectedSamples);
86
87 /**
88 * @brief Returns the estimated RTO value
89 */
90 MillisecondsDouble
91 getEstimatedRto() const
92 {
93 return m_rto;
94 }
95
96 /**
97 * @brief Returns the minimum RTT observed
98 */
99 MillisecondsDouble
100 getMinRtt() const
101 {
102 return m_rttMin;
103 }
104
105 /**
106 * @brief Returns the maximum RTT observed
107 */
108 MillisecondsDouble
109 getMaxRtt() const
110 {
111 return m_rttMax;
112 }
113
114 /**
115 * @brief Returns the average RTT
116 */
117 MillisecondsDouble
118 getAvgRtt() const
119 {
120 return m_rttAvg;
121 }
122
123 /**
124 * @brief Backoff RTO by a factor of Options::rtoBackoffMultiplier
125 */
126 void
127 backoffRto();
128
129private:
130 const Options m_options;
131 MillisecondsDouble m_sRtt; ///< smoothed round-trip time
132 MillisecondsDouble m_rttVar; ///< round-trip time variation
133 MillisecondsDouble m_rto; ///< retransmission timeout
134 MillisecondsDouble m_rttMin;
135 MillisecondsDouble m_rttMax;
136 MillisecondsDouble m_rttAvg;
137 int64_t m_nRttSamples; ///< number of RTT samples
138};
139
140} // namespace util
141} // namespace ndn
142
143#endif // NDN_CXX_UTIL_RTT_ESTIMATOR_HPP