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",
42 TimeValue(Seconds(1.0)),
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
92{
93 return GetTypeId();
94}
95
96void
98{
99 NS_LOG_FUNCTION(this);
100 // Reset to initial state
103 m_nSamples = 0;
104}
105
108{
109 return m_nSamples;
110}
111
112//-----------------------------------------------------------------------------
113//-----------------------------------------------------------------------------
114// Mean-Deviation Estimator
115
117
118TypeId
120{
121 static TypeId tid =
122 TypeId("ns3::RttMeanDeviation")
124 .SetGroupName("Internet")
125 .AddConstructor<RttMeanDeviation>()
126 .AddAttribute("Alpha",
127 "Gain used in estimating the RTT, must be 0 <= alpha <= 1",
128 DoubleValue(0.125),
131 .AddAttribute("Beta",
132 "Gain used in estimating the RTT variation, must be 0 <= beta <= 1",
133 DoubleValue(0.25),
136 return tid;
137}
138
143
145 : RttEstimator(c),
146 m_alpha(c.m_alpha),
147 m_beta(c.m_beta)
148{
149 NS_LOG_FUNCTION(this);
150}
151
152TypeId
154{
155 return GetTypeId();
156}
157
160{
161 NS_LOG_FUNCTION(this << val);
162 if (val < TOLERANCE)
163 {
164 return 0;
165 }
166 // supports 1/32, 1/16, 1/8, 1/4, 1/2
167 if (std::abs(1 / val - 8) < TOLERANCE)
168 {
169 return 3;
170 }
171 if (std::abs(1 / val - 4) < TOLERANCE)
172 {
173 return 2;
174 }
175 if (std::abs(1 / val - 32) < TOLERANCE)
176 {
177 return 5;
178 }
179 if (std::abs(1 / val - 16) < TOLERANCE)
180 {
181 return 4;
182 }
183 if (std::abs(1 / val - 2) < TOLERANCE)
184 {
185 return 1;
186 }
187 return 0;
188}
189
190void
192{
193 NS_LOG_FUNCTION(this << m);
194
195 // EWMA formulas are implemented as suggested in
196 // Jacobson/Karels paper appendix A.2
197
198 // SRTT <- (1 - alpha) * SRTT + alpha * R'
199 Time err(m - m_estimatedRtt);
200 double gErr = err.ToDouble(Time::S) * m_alpha;
202
203 // RTTVAR <- (1 - beta) * RTTVAR + beta * |SRTT - R'|
204 Time difference = Abs(err) - m_estimatedVariation;
205 m_estimatedVariation += difference * m_beta;
206}
207
208void
210{
211 NS_LOG_FUNCTION(this << m << rttShift << variationShift);
212 // Jacobson/Karels paper appendix A.2
213 int64_t meas = m.GetInteger();
214 int64_t delta = meas - m_estimatedRtt.GetInteger();
215 int64_t srtt = (m_estimatedRtt.GetInteger() << rttShift) + delta;
216 m_estimatedRtt = Time::From(srtt >> rttShift);
217 if (delta < 0)
218 {
219 delta = -delta;
220 }
222 int64_t rttvar = m_estimatedVariation.GetInteger() << variationShift;
223 rttvar += delta;
224 m_estimatedVariation = Time::From(rttvar >> variationShift);
225}
226
227void
229{
230 NS_LOG_FUNCTION(this << m);
231 if (m_nSamples)
232 {
233 // If both alpha and beta are reciprocal powers of two, updating can
234 // be done with integer arithmetic according to Jacobson/Karels paper.
235 // If not, since class Time only supports integer multiplication,
236 // must convert Time to floating point and back again
239 if (rttShift && variationShift)
240 {
241 IntegerUpdate(m, rttShift, variationShift);
242 }
243 else
244 {
246 }
247 }
248 else
249 { // First sample
250 m_estimatedRtt = m; // Set estimate to current
251 m_estimatedVariation = m / 2; // And variation to current / 2
252 NS_LOG_DEBUG("(first sample) m_estimatedVariation += " << m);
253 }
254 m_nSamples++;
255}
256
259{
260 NS_LOG_FUNCTION(this);
261 return CopyObject<RttMeanDeviation>(this);
262}
263
264void
270
271} // 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.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
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.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
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:505
double ToDouble(Unit unit) const
Get the Time value expressed in a particular unit.
Definition nstime.h:562
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
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:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition nstime.h:1396
Ptr< const AttributeChecker > MakeDoubleChecker()
Definition double.h:82
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition double.h:32
static const double TOLERANCE
Tolerance used to check reciprocal of two numbers.
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition nstime.h:1416