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
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
67
68void
70{
71 NS_LOG_FUNCTION(this);
73 NS_LOG_DEBUG("Initialize m_estimatedRtt to " << m_estimatedRtt.GetSeconds() << " sec.");
74}
75
85
90
91void
93{
94 NS_LOG_FUNCTION(this);
95 // Reset to initial state
98 m_nSamples = 0;
99}
100
103{
104 return m_nSamples;
105}
106
107//-----------------------------------------------------------------------------
108//-----------------------------------------------------------------------------
109// Mean-Deviation Estimator
110
112
113TypeId
115{
116 static TypeId tid =
117 TypeId("ns3::RttMeanDeviation")
119 .SetGroupName("Internet")
120 .AddConstructor<RttMeanDeviation>()
121 .AddAttribute("Alpha",
122 "Gain used in estimating the RTT, must be 0 <= alpha <= 1",
123 DoubleValue(0.125),
126 .AddAttribute("Beta",
127 "Gain used in estimating the RTT variation, must be 0 <= beta <= 1",
128 DoubleValue(0.25),
131 return tid;
132}
133
138
146
149{
150 NS_LOG_FUNCTION(this << val);
151 if (val < TOLERANCE)
152 {
153 return 0;
154 }
155 // supports 1/32, 1/16, 1/8, 1/4, 1/2
156 if (std::abs(1 / val - 8) < TOLERANCE)
157 {
158 return 3;
159 }
160 if (std::abs(1 / val - 4) < TOLERANCE)
161 {
162 return 2;
163 }
164 if (std::abs(1 / val - 32) < TOLERANCE)
165 {
166 return 5;
167 }
168 if (std::abs(1 / val - 16) < TOLERANCE)
169 {
170 return 4;
171 }
172 if (std::abs(1 / val - 2) < TOLERANCE)
173 {
174 return 1;
175 }
176 return 0;
177}
178
179void
181{
182 NS_LOG_FUNCTION(this << m);
183
184 // EWMA formulas are implemented as suggested in
185 // Jacobson/Karels paper appendix A.2
186
187 // SRTT <- (1 - alpha) * SRTT + alpha * R'
188 Time err(m - m_estimatedRtt);
189 double gErr = err.ToDouble(Time::S) * m_alpha;
191
192 // RTTVAR <- (1 - beta) * RTTVAR + beta * |SRTT - R'|
193 Time difference = Abs(err) - m_estimatedVariation;
194 m_estimatedVariation += difference * m_beta;
195}
196
197void
199{
200 NS_LOG_FUNCTION(this << m << rttShift << variationShift);
201 // Jacobson/Karels paper appendix A.2
202 int64_t meas = m.GetInteger();
203 int64_t delta = meas - m_estimatedRtt.GetInteger();
204 int64_t srtt = (m_estimatedRtt.GetInteger() << rttShift) + delta;
205 m_estimatedRtt = Time::From(srtt >> rttShift);
206 if (delta < 0)
207 {
208 delta = -delta;
209 }
210 delta -= m_estimatedVariation.GetInteger();
211 int64_t rttvar = m_estimatedVariation.GetInteger() << variationShift;
212 rttvar += delta;
213 m_estimatedVariation = Time::From(rttvar >> variationShift);
214}
215
216void
218{
219 NS_LOG_FUNCTION(this << m);
220 if (m_nSamples)
221 {
222 // If both alpha and beta are reciprocal powers of two, updating can
223 // be done with integer arithmetic according to Jacobson/Karels paper.
224 // If not, since class Time only supports integer multiplication,
225 // must convert Time to floating point and back again
228 if (rttShift && variationShift)
229 {
230 IntegerUpdate(m, rttShift, variationShift);
231 }
232 else
233 {
235 }
236 }
237 else
238 { // First sample
239 m_estimatedRtt = m; // Set estimate to current
240 m_estimatedVariation = m / 2; // And variation to current / 2
241 NS_LOG_DEBUG("(first sample) m_estimatedVariation += " << m);
242 }
243 m_nSamples++;
244}
245
248{
249 NS_LOG_FUNCTION(this);
250 return CopyObject<RttMeanDeviation>(this);
251}
252
253void
259
260} // namespace ns3
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
friend Ptr< T > CopyObject(Ptr< T > object)
Copy an Object.
Definition object.h:581
Object()
Constructor.
Definition object.cc:96
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:67
Base class for all RTT Estimators.
Time m_initialEstimatedRtt
Initial RTT estimation.
Time GetEstimate() const
gets the RTT estimate.
void NotifyConstructionCompleted() override
Notifier called once the ObjectBase is fully constructed.
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:96
static Time From(const int64x64_t &value)
Create a Time in the current unit.
Definition nstime.h:472
@ S
second
Definition nstime.h:107
static Time FromDouble(double value, Unit unit)
Create a Time equal to value in unit unit.
Definition nstime.h:513
double ToDouble(Unit unit) const
Get the Time value expressed in a particular unit.
Definition nstime.h:588
AttributeValue implementation for Time.
Definition nstime.h:1456
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:1457
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition nstime.h:1477
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:1369
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.