A Discrete-Event Network Simulator
API
rtt-estimator.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 //
3 // Copyright (c) 2006 Georgia Tech Research Corporation
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License version 2 as
7 // published by the Free Software Foundation;
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 //
18 // Author: Rajib Bhattacharjea<raj.b@gatech.edu>
19 //
20 
21 // Ported from:
22 // Georgia Tech Network Simulator - Round Trip Time Estimator Class
23 // George F. Riley. Georgia Tech, Spring 2002
24 
25 // Base class allows variations of round trip time estimators to be
26 // implemented
27 
28 #include <iostream>
29 #include <cmath>
30 
31 #include "rtt-estimator.h"
32 #include "ns3/double.h"
33 #include "ns3/log.h"
34 
35 namespace ns3 {
36 
37 NS_LOG_COMPONENT_DEFINE ("RttEstimator");
38 
39 NS_OBJECT_ENSURE_REGISTERED (RttEstimator);
40 
41 static const double TOLERANCE = 1e-6;
42 
43 TypeId
45 {
46  static TypeId tid = TypeId ("ns3::RttEstimator")
47  .SetParent<Object> ()
48  .AddAttribute ("InitialEstimation",
49  "Initial RTT estimate",
50  TimeValue (Seconds (1.0)),
52  MakeTimeChecker ())
53  ;
54  return tid;
55 }
56 
57 Time
59 {
60  return m_estimatedRtt;
61 }
62 
63 Time
65 {
66  return m_estimatedVariation;
67 }
68 
69 
70 // Base class methods
71 
73  : m_nSamples (0)
74 {
75  NS_LOG_FUNCTION (this);
76 
77  // We need attributes initialized here, not later, so use the
78  // ConstructSelf() technique documented in the manual
82  NS_LOG_DEBUG ("Initialize m_estimatedRtt to " << m_estimatedRtt.GetSeconds () << " sec.");
83 }
84 
86  : Object (c),
87  m_initialEstimatedRtt (c.m_initialEstimatedRtt),
88  m_estimatedRtt (c.m_estimatedRtt),
89  m_estimatedVariation (c.m_estimatedVariation),
90  m_nSamples (c.m_nSamples)
91 {
92  NS_LOG_FUNCTION (this);
93 }
94 
96 {
97  NS_LOG_FUNCTION (this);
98 }
99 
100 TypeId
102 {
103  return GetTypeId ();
104 }
105 
107 {
108  NS_LOG_FUNCTION (this);
109  // Reset to initial state
112  m_nSamples = 0;
113 }
114 
115 uint32_t
117 {
118  return m_nSamples;
119 }
120 
121 //-----------------------------------------------------------------------------
122 //-----------------------------------------------------------------------------
123 // Mean-Deviation Estimator
124 
126 
127 TypeId
129 {
130  static TypeId tid = TypeId ("ns3::RttMeanDeviation")
132  .AddConstructor<RttMeanDeviation> ()
133  .AddAttribute ("Alpha",
134  "Gain used in estimating the RTT, must be 0 <= alpha <= 1",
135  DoubleValue (0.125),
137  MakeDoubleChecker<double> (0, 1))
138  .AddAttribute ("Beta",
139  "Gain used in estimating the RTT variation, must be 0 <= beta <= 1",
140  DoubleValue (0.25),
142  MakeDoubleChecker<double> (0, 1))
143  ;
144  return tid;
145 }
146 
148 {
149  NS_LOG_FUNCTION (this);
150 }
151 
153  : RttEstimator (c), m_alpha (c.m_alpha), m_beta (c.m_beta)
154 {
155  NS_LOG_FUNCTION (this);
156 }
157 
158 TypeId
160 {
161  return GetTypeId ();
162 }
163 
164 uint32_t
166 {
167  NS_LOG_FUNCTION (this << val);
168  if (val < TOLERANCE)
169  {
170  return 0;
171  }
172  // supports 1/32, 1/16, 1/8, 1/4, 1/2
173  if (abs (1/val - 8) < TOLERANCE)
174  {
175  return 3;
176  }
177  if (abs (1/val - 4) < TOLERANCE)
178  {
179  return 2;
180  }
181  if (abs (1/val - 32) < TOLERANCE)
182  {
183  return 5;
184  }
185  if (abs (1/val - 16) < TOLERANCE)
186  {
187  return 4;
188  }
189  if (abs (1/val - 2) < TOLERANCE)
190  {
191  return 1;
192  }
193  return 0;
194 }
195 
196 void
198 {
199  NS_LOG_FUNCTION (this << m);
200 
201  // EWMA formulas are implemented as suggested in
202  // Jacobson/Karels paper appendix A.2
203 
204  // SRTT <- (1 - alpha) * SRTT + alpha * R'
205  Time err (m - m_estimatedRtt);
206  double gErr = err.ToDouble (Time::S) * m_alpha;
208 
209  // RTTVAR <- (1 - beta) * RTTVAR + beta * |SRTT - R'|
210  Time difference = Abs (err) - m_estimatedVariation;
212 
213  return;
214 }
215 
216 void
217 RttMeanDeviation::IntegerUpdate (Time m, uint32_t rttShift, uint32_t variationShift)
218 {
219  NS_LOG_FUNCTION (this << m << rttShift << variationShift);
220  // Jacobson/Karels paper appendix A.2
221  int64_t meas = m.GetInteger ();
222  int64_t delta = meas - m_estimatedRtt.GetInteger ();
223  int64_t srtt = (m_estimatedRtt.GetInteger () << rttShift) + delta;
224  m_estimatedRtt = Time::From (srtt >> rttShift);
225  if (delta < 0)
226  {
227  delta = -delta;
228  }
229  delta -= m_estimatedVariation.GetInteger ();
230  int64_t rttvar = m_estimatedVariation.GetInteger () << variationShift;
231  rttvar += delta;
232  m_estimatedVariation = Time::From (rttvar >> variationShift);
233  return;
234 }
235 
236 void
238 {
239  NS_LOG_FUNCTION (this << m);
240  if (m_nSamples)
241  {
242  // If both alpha and beta are reciprocal powers of two, updating can
243  // be done with integer arithmetic according to Jacobson/Karels paper.
244  // If not, since class Time only supports integer multiplication,
245  // must convert Time to floating point and back again
246  uint32_t rttShift = CheckForReciprocalPowerOfTwo (m_alpha);
247  uint32_t variationShift = CheckForReciprocalPowerOfTwo (m_beta);
248  if (rttShift && variationShift)
249  {
250  IntegerUpdate (m, rttShift, variationShift);
251  }
252  else
253  {
255  }
256  }
257  else
258  { // First sample
259  m_estimatedRtt = m; // Set estimate to current
260  m_estimatedVariation = m / 2; // And variation to current / 2
261  NS_LOG_DEBUG ("(first sample) m_estimatedVariation += " << m);
262  }
263  m_nSamples++;
264 }
265 
268 {
269  NS_LOG_FUNCTION (this);
270  return CopyObject<RttMeanDeviation> (this);
271 }
272 
273 void
275 {
276  NS_LOG_FUNCTION (this);
278 }
279 
280 } //namespace ns3
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:95
uint32_t GetNSamples(void) const
gets the number of samples used in the estimates
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
static TypeId GetTypeId(void)
Get the type ID.
double m_alpha
Filter gain for average.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
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...
The "Mean--Deviation" RTT estimator, as discussed by Van Jacobson.
static Time From(const int64x64_t &value)
Create a Time in the current unit.
Definition: nstime.h:392
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
second
Definition: nstime.h:107
static TypeId GetTypeId(void)
Get the type ID.
Time m_initialEstimatedRtt
Initial RTT estimation.
virtual TypeId GetInstanceTypeId(void) const
Implement the GetInstanceTypeId method defined in ObjectBase.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:439
virtual ~RttEstimator()
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:327
AttributeValue implementation for Time.
Definition: nstime.h:921
double ToDouble(enum Unit unit) const
Get the Time value expressed in a particular unit.
Definition: nstime.h:461
Base class for all RTT Estimators.
Definition: rtt-estimator.h:43
List of Attribute name, value and checker triples used to construct Objects.
int64x64_t Abs(const int64x64_t &value)
Absolute value.
Definition: int64x64.h:178
Time m_estimatedRtt
Current estimate.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Time GetVariation(void) const
Note that this is not a formal statistical variance; it has the the same units as the estimate...
void ConstructSelf(const AttributeConstructionList &attributes)
Complete construction of ObjectBase; invoked by derived classes.
Definition: object-base.cc:79
uint32_t m_nSamples
Number of samples.
int64_t GetInteger(void) const
Definition: nstime.h:365
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:922
virtual void Reset()
Resets the estimation to its initial state.
void Reset()
Resets the estimator.
double m_beta
Filter gain for variation.
void FloatingPointUpdate(Time m)
Method to update the rtt and variation estimates using floating point arithmetic, used when the value...
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:42
static Time FromDouble(double value, enum Unit unit)
Create a Time equal to value in unit unit.
Definition: nstime.h:417
Ptr< RttEstimator > Copy() const
Copy object (including current internal state)
void Measurement(Time measure)
Add a new measurement to the estimator.
virtual TypeId GetInstanceTypeId(void) const
Implement the GetInstanceTypeId method defined in ObjectBase.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:859
static const double TOLERANCE
Time GetEstimate(void) const
gets the RTT estimate.
A base class which provides memory management and object aggregation.
Definition: object.h:87
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41
a unique identifier for an interface.
Definition: type-id.h:51
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
Time m_estimatedVariation
Current estimate variation.
uint32_t CheckForReciprocalPowerOfTwo(double val) const
Utility function to check for possible conversion of a double value (0 < value < 1) to a reciprocal p...