A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
rtt-estimator.cc
Go to the documentation of this file.
1//
2// Copyright (c) 2006 Georgia Tech Research Corporation
3//
4// SPDX-License-Identifier: GPL-2.0-only
5//
6// Author: Rajib Bhattacharjea<raj.b@gatech.edu>
7//
8
9// Ported from:
10// Georgia Tech Network Simulator - Round Trip Time Estimator Class
11// George F. Riley. Georgia Tech, Spring 2002
12
13// Base class allows variations of round trip time estimators to be
14// implemented
15
16#include "rtt-estimator.h"
17
18#include "ns3/double.h"
19#include "ns3/log.h"
20
21#include <cmath>
22#include <iostream>
23
24namespace ns3
25{
26
27NS_LOG_COMPONENT_DEFINE("RttEstimator");
28
29NS_OBJECT_ENSURE_REGISTERED(RttEstimator);
30
31/// Tolerance used to check reciprocal of two numbers.
32static const double TOLERANCE = 1e-6;
33
36{
37 static TypeId tid = TypeId("ns3::RttEstimator")
39 .SetGroupName("Internet")
40 .AddAttribute("InitialEstimation",
41 "Initial RTT estimate",
45 return tid;
46}
47
48Time
50{
51 return m_estimatedRtt;
52}
53
54Time
59
60// Base class methods
61
63 : m_nSamples(0)
64{
65 NS_LOG_FUNCTION(this);
66
67 // We need attributes initialized here, not later, so use the
68 // ConstructSelf() technique documented in the manual
72 NS_LOG_DEBUG("Initialize m_estimatedRtt to " << m_estimatedRtt.GetSeconds() << " sec.");
73}
74
76 : Object(c),
77 m_initialEstimatedRtt(c.m_initialEstimatedRtt),
78 m_estimatedRtt(c.m_estimatedRtt),
79 m_estimatedVariation(c.m_estimatedVariation),
80 m_nSamples(c.m_nSamples)
81{
82 NS_LOG_FUNCTION(this);
83}
84
89
90void
92{
93 NS_LOG_FUNCTION(this);
94 // Reset to initial state
97 m_nSamples = 0;
98}
99
102{
103 return m_nSamples;
104}
105
106//-----------------------------------------------------------------------------
107//-----------------------------------------------------------------------------
108// Mean-Deviation Estimator
109
111
112TypeId
114{
115 static TypeId tid =
116 TypeId("ns3::RttMeanDeviation")
118 .SetGroupName("Internet")
119 .AddConstructor<RttMeanDeviation>()
120 .AddAttribute("Alpha",
121 "Gain used in estimating the RTT, must be 0 <= alpha <= 1",
122 DoubleValue(0.125),
125 .AddAttribute("Beta",
126 "Gain used in estimating the RTT variation, must be 0 <= beta <= 1",
127 DoubleValue(0.25),
130 return tid;
131}
132
137
139 : RttEstimator(c),
140 m_alpha(c.m_alpha),
141 m_beta(c.m_beta)
142{
143 NS_LOG_FUNCTION(this);
144}
145
148{
149 NS_LOG_FUNCTION(this << val);
150 if (val < TOLERANCE)
151 {
152 return 0;
153 }
154 // supports 1/32, 1/16, 1/8, 1/4, 1/2
155 if (std::abs(1 / val - 8) < TOLERANCE)
156 {
157 return 3;
158 }
159 if (std::abs(1 / val - 4) < TOLERANCE)
160 {
161 return 2;
162 }
163 if (std::abs(1 / val - 32) < TOLERANCE)
164 {
165 return 5;
166 }
167 if (std::abs(1 / val - 16) < TOLERANCE)
168 {
169 return 4;
170 }
171 if (std::abs(1 / val - 2) < TOLERANCE)
172 {
173 return 1;
174 }
175 return 0;
176}
177
178void
180{
181 NS_LOG_FUNCTION(this << m);
182
183 // EWMA formulas are implemented as suggested in
184 // Jacobson/Karels paper appendix A.2
185
186 // SRTT <- (1 - alpha) * SRTT + alpha * R'
187 Time err(m - m_estimatedRtt);
188 double gErr = err.ToDouble(Time::S) * m_alpha;
190
191 // RTTVAR <- (1 - beta) * RTTVAR + beta * |SRTT - R'|
192 Time difference = Abs(err) - m_estimatedVariation;
193 m_estimatedVariation += difference * m_beta;
194}
195
196void
198{
199 NS_LOG_FUNCTION(this << m << rttShift << variationShift);
200 // Jacobson/Karels paper appendix A.2
201 int64_t meas = m.GetInteger();
202 int64_t delta = meas - m_estimatedRtt.GetInteger();
203 int64_t srtt = (m_estimatedRtt.GetInteger() << rttShift) + delta;
204 m_estimatedRtt = Time::From(srtt >> rttShift);
205 if (delta < 0)
206 {
207 delta = -delta;
208 }
210 int64_t rttvar = m_estimatedVariation.GetInteger() << variationShift;
211 rttvar += delta;
212 m_estimatedVariation = Time::From(rttvar >> variationShift);
213}
214
215void
217{
218 NS_LOG_FUNCTION(this << m);
219 if (m_nSamples)
220 {
221 // If both alpha and beta are reciprocal powers of two, updating can
222 // be done with integer arithmetic according to Jacobson/Karels paper.
223 // If not, since class Time only supports integer multiplication,
224 // must convert Time to floating point and back again
227 if (rttShift && variationShift)
228 {
229 IntegerUpdate(m, rttShift, variationShift);
230 }
231 else
232 {
234 }
235 }
236 else
237 { // First sample
238 m_estimatedRtt = m; // Set estimate to current
239 m_estimatedVariation = m / 2; // And variation to current / 2
240 NS_LOG_DEBUG("(first sample) m_estimatedVariation += " << m);
241 }
242 m_nSamples++;
243}
244
247{
248 NS_LOG_FUNCTION(this);
249 return CopyObject<RttMeanDeviation>(this);
250}
251
252void
258
259} // namespace ns3
List of Attribute name, value and checker triples used to construct Objects.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
void ConstructSelf(const AttributeConstructionList &attributes)
Complete construction of ObjectBase; invoked by derived classes.
A base class which provides memory management and object aggregation.
Definition object.h:78
friend Ptr< T > CopyObject(Ptr< T > object)
Copy an Object.
Definition object.h:581
Smart pointer class similar to boost::intrusive_ptr.
Base class for all RTT Estimators.
Time m_initialEstimatedRtt
Initial RTT estimation.
Time GetEstimate() const
gets the RTT estimate.
virtual void Reset()
Resets the estimation to its initial state.
~RttEstimator() override
Time m_estimatedVariation
Current estimate variation.
uint32_t m_nSamples
Number of samples.
Time m_estimatedRtt
Current estimate.
Time GetVariation() const
Note that this is not a formal statistical variance; it has the the same units as the estimate.
static TypeId GetTypeId()
Get the type ID.
uint32_t GetNSamples() const
gets the number of samples used in the estimates
The "Mean--Deviation" RTT estimator, as discussed by Van Jacobson.
double m_beta
Filter gain for variation.
void Reset() override
Resets the estimator.
void FloatingPointUpdate(Time m)
Method to update the rtt and variation estimates using floating point arithmetic, used when the value...
Ptr< RttEstimator > Copy() const override
Copy object (including current internal state)
static TypeId GetTypeId()
Get the type ID.
uint32_t CheckForReciprocalPowerOfTwo(double val) const
Utility function to check for possible conversion of a double value (0 < value < 1) to a reciprocal p...
void Measurement(Time measure) override
Add a new measurement to the estimator.
double m_alpha
Filter gain for average.
void IntegerUpdate(Time m, uint32_t rttShift, uint32_t variationShift)
Method to update the rtt and variation estimates using integer arithmetic, used when the values of Al...
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
static Time From(const int64x64_t &value)
Create a Time in the current unit.
Definition nstime.h:470
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
int64_t GetInteger() const
Get the raw time value, in the current resolution unit.
Definition nstime.h:444
@ S
second
Definition nstime.h:105
static Time FromDouble(double value, Unit unit)
Create a Time equal to value in unit unit.
Definition nstime.h:511
double ToDouble(Unit unit) const
Get the Time value expressed in a particular unit.
Definition nstime.h:586
AttributeValue implementation for Time.
Definition nstime.h:1432
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Ptr< const AttributeChecker > MakeDoubleChecker()
Definition double.h:82
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition double.h:32
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition nstime.h:1433
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition nstime.h:1453
int64x64_t Abs(const int64x64_t &value)
Absolute value.
Definition int64x64.h:203
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1345
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static const double TOLERANCE
Tolerance used to check reciprocal of two numbers.