A Discrete-Event Network Simulator
API
rtt-estimator.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2006 Georgia Tech Research Corporation
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License version 2 as
6 // published by the Free Software Foundation;
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // Author: Rajib Bhattacharjea<raj.b@gatech.edu>
18 //
19 
20 // Ported from:
21 // Georgia Tech Network Simulator - Round Trip Time Estimator Class
22 // George F. Riley. Georgia Tech, Spring 2002
23 
24 // Base class allows variations of round trip time estimators to be
25 // implemented
26 
27 #include "rtt-estimator.h"
28 
29 #include "ns3/double.h"
30 #include "ns3/log.h"
31 
32 #include <cmath>
33 #include <iostream>
34 
35 namespace ns3
36 {
37 
38 NS_LOG_COMPONENT_DEFINE("RttEstimator");
39 
40 NS_OBJECT_ENSURE_REGISTERED(RttEstimator);
41 
43 static const double TOLERANCE = 1e-6;
44 
45 TypeId
47 {
48  static TypeId tid = TypeId("ns3::RttEstimator")
49  .SetParent<Object>()
50  .SetGroupName("Internet")
51  .AddAttribute("InitialEstimation",
52  "Initial RTT estimate",
53  TimeValue(Seconds(1.0)),
55  MakeTimeChecker());
56  return tid;
57 }
58 
59 Time
61 {
62  return m_estimatedRtt;
63 }
64 
65 Time
67 {
68  return m_estimatedVariation;
69 }
70 
71 // Base class methods
72 
74  : m_nSamples(0)
75 {
76  NS_LOG_FUNCTION(this);
77 
78  // We need attributes initialized here, not later, so use the
79  // ConstructSelf() technique documented in the manual
83  NS_LOG_DEBUG("Initialize m_estimatedRtt to " << m_estimatedRtt.GetSeconds() << " sec.");
84 }
85 
87  : Object(c),
88  m_initialEstimatedRtt(c.m_initialEstimatedRtt),
89  m_estimatedRtt(c.m_estimatedRtt),
90  m_estimatedVariation(c.m_estimatedVariation),
91  m_nSamples(c.m_nSamples)
92 {
93  NS_LOG_FUNCTION(this);
94 }
95 
97 {
98  NS_LOG_FUNCTION(this);
99 }
100 
101 TypeId
103 {
104  return GetTypeId();
105 }
106 
107 void
109 {
110  NS_LOG_FUNCTION(this);
111  // Reset to initial state
114  m_nSamples = 0;
115 }
116 
117 uint32_t
119 {
120  return m_nSamples;
121 }
122 
123 //-----------------------------------------------------------------------------
124 //-----------------------------------------------------------------------------
125 // Mean-Deviation Estimator
126 
128 
129 TypeId
131 {
132  static TypeId tid =
133  TypeId("ns3::RttMeanDeviation")
135  .SetGroupName("Internet")
136  .AddConstructor<RttMeanDeviation>()
137  .AddAttribute("Alpha",
138  "Gain used in estimating the RTT, must be 0 <= alpha <= 1",
139  DoubleValue(0.125),
141  MakeDoubleChecker<double>(0, 1))
142  .AddAttribute("Beta",
143  "Gain used in estimating the RTT variation, must be 0 <= beta <= 1",
144  DoubleValue(0.25),
146  MakeDoubleChecker<double>(0, 1));
147  return tid;
148 }
149 
151 {
152  NS_LOG_FUNCTION(this);
153 }
154 
156  : RttEstimator(c),
157  m_alpha(c.m_alpha),
158  m_beta(c.m_beta)
159 {
160  NS_LOG_FUNCTION(this);
161 }
162 
163 TypeId
165 {
166  return GetTypeId();
167 }
168 
169 uint32_t
171 {
172  NS_LOG_FUNCTION(this << val);
173  if (val < TOLERANCE)
174  {
175  return 0;
176  }
177  // supports 1/32, 1/16, 1/8, 1/4, 1/2
178  if (std::abs(1 / val - 8) < TOLERANCE)
179  {
180  return 3;
181  }
182  if (std::abs(1 / val - 4) < TOLERANCE)
183  {
184  return 2;
185  }
186  if (std::abs(1 / val - 32) < TOLERANCE)
187  {
188  return 5;
189  }
190  if (std::abs(1 / val - 16) < TOLERANCE)
191  {
192  return 4;
193  }
194  if (std::abs(1 / val - 2) < TOLERANCE)
195  {
196  return 1;
197  }
198  return 0;
199 }
200 
201 void
203 {
204  NS_LOG_FUNCTION(this << m);
205 
206  // EWMA formulas are implemented as suggested in
207  // Jacobson/Karels paper appendix A.2
208 
209  // SRTT <- (1 - alpha) * SRTT + alpha * R'
210  Time err(m - m_estimatedRtt);
211  double gErr = err.ToDouble(Time::S) * m_alpha;
213 
214  // RTTVAR <- (1 - beta) * RTTVAR + beta * |SRTT - R'|
215  Time difference = Abs(err) - m_estimatedVariation;
216  m_estimatedVariation += difference * m_beta;
217 }
218 
219 void
220 RttMeanDeviation::IntegerUpdate(Time m, uint32_t rttShift, uint32_t variationShift)
221 {
222  NS_LOG_FUNCTION(this << m << rttShift << variationShift);
223  // Jacobson/Karels paper appendix A.2
224  int64_t meas = m.GetInteger();
225  int64_t delta = meas - m_estimatedRtt.GetInteger();
226  int64_t srtt = (m_estimatedRtt.GetInteger() << rttShift) + delta;
227  m_estimatedRtt = Time::From(srtt >> rttShift);
228  if (delta < 0)
229  {
230  delta = -delta;
231  }
233  int64_t rttvar = m_estimatedVariation.GetInteger() << variationShift;
234  rttvar += delta;
235  m_estimatedVariation = Time::From(rttvar >> variationShift);
236 }
237 
238 void
240 {
241  NS_LOG_FUNCTION(this << m);
242  if (m_nSamples)
243  {
244  // If both alpha and beta are reciprocal powers of two, updating can
245  // be done with integer arithmetic according to Jacobson/Karels paper.
246  // If not, since class Time only supports integer multiplication,
247  // must convert Time to floating point and back again
248  uint32_t rttShift = CheckForReciprocalPowerOfTwo(m_alpha);
249  uint32_t variationShift = CheckForReciprocalPowerOfTwo(m_beta);
250  if (rttShift && variationShift)
251  {
252  IntegerUpdate(m, rttShift, variationShift);
253  }
254  else
255  {
257  }
258  }
259  else
260  { // First sample
261  m_estimatedRtt = m; // Set estimate to current
262  m_estimatedVariation = m / 2; // And variation to current / 2
263  NS_LOG_DEBUG("(first sample) m_estimatedVariation += " << m);
264  }
265  m_nSamples++;
266 }
267 
270 {
271  NS_LOG_FUNCTION(this);
272  return CopyObject<RttMeanDeviation>(this);
273 }
274 
275 void
277 {
278  NS_LOG_FUNCTION(this);
280 }
281 
282 } // 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:42
void ConstructSelf(const AttributeConstructionList &attributes)
Complete construction of ObjectBase; invoked by derived classes.
Definition: object-base.cc:85
A base class which provides memory management and object aggregation.
Definition: object.h:89
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Base class for all RTT Estimators.
Definition: rtt-estimator.h:43
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:105
static Time From(const int64x64_t &value)
Create a Time in the current unit.
Definition: nstime.h:481
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
int64_t GetInteger() const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:455
@ S
second
Definition: nstime.h:116
static Time FromDouble(double value, Unit unit)
Create a Time equal to value in unit unit.
Definition: nstime.h:516
double ToDouble(Unit unit) const
Get the Time value expressed in a particular unit.
Definition: nstime.h:573
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
int64x64_t Abs(const int64x64_t &value)
Absolute value.
Definition: int64x64.h:215
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#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:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:839
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1414
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:533
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition: double.h:43
static const double TOLERANCE
Tolerance used to check reciprocal of two numbers.