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  .SetGroupName ("Internet")
49  .AddAttribute ("InitialEstimation",
50  "Initial RTT estimate",
51  TimeValue (Seconds (1.0)),
53  MakeTimeChecker ())
54  ;
55  return tid;
56 }
57 
58 Time
60 {
61  return m_estimatedRtt;
62 }
63 
64 Time
66 {
67  return m_estimatedVariation;
68 }
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 
108 {
109  NS_LOG_FUNCTION (this);
110  // Reset to initial state
113  m_nSamples = 0;
114 }
115 
116 uint32_t
118 {
119  return m_nSamples;
120 }
121 
122 //-----------------------------------------------------------------------------
123 //-----------------------------------------------------------------------------
124 // Mean-Deviation Estimator
125 
127 
128 TypeId
130 {
131  static TypeId tid = TypeId ("ns3::RttMeanDeviation")
133  .SetGroupName ("Internet")
134  .AddConstructor<RttMeanDeviation> ()
135  .AddAttribute ("Alpha",
136  "Gain used in estimating the RTT, must be 0 <= alpha <= 1",
137  DoubleValue (0.125),
139  MakeDoubleChecker<double> (0, 1))
140  .AddAttribute ("Beta",
141  "Gain used in estimating the RTT variation, must be 0 <= beta <= 1",
142  DoubleValue (0.25),
144  MakeDoubleChecker<double> (0, 1))
145  ;
146  return tid;
147 }
148 
150 {
151  NS_LOG_FUNCTION (this);
152 }
153 
155  : RttEstimator (c), m_alpha (c.m_alpha), m_beta (c.m_beta)
156 {
157  NS_LOG_FUNCTION (this);
158 }
159 
160 TypeId
162 {
163  return GetTypeId ();
164 }
165 
166 uint32_t
168 {
169  NS_LOG_FUNCTION (this << val);
170  if (val < TOLERANCE)
171  {
172  return 0;
173  }
174  // supports 1/32, 1/16, 1/8, 1/4, 1/2
175  if (std::abs (1/val - 8) < TOLERANCE)
176  {
177  return 3;
178  }
179  if (std::abs (1/val - 4) < TOLERANCE)
180  {
181  return 2;
182  }
183  if (std::abs (1/val - 32) < TOLERANCE)
184  {
185  return 5;
186  }
187  if (std::abs (1/val - 16) < TOLERANCE)
188  {
189  return 4;
190  }
191  if (std::abs (1/val - 2) < TOLERANCE)
192  {
193  return 1;
194  }
195  return 0;
196 }
197 
198 void
200 {
201  NS_LOG_FUNCTION (this << m);
202 
203  // EWMA formulas are implemented as suggested in
204  // Jacobson/Karels paper appendix A.2
205 
206  // SRTT <- (1 - alpha) * SRTT + alpha * R'
207  Time err (m - m_estimatedRtt);
208  double gErr = err.ToDouble (Time::S) * m_alpha;
210 
211  // RTTVAR <- (1 - beta) * RTTVAR + beta * |SRTT - R'|
212  Time difference = Abs (err) - m_estimatedVariation;
214 
215  return;
216 }
217 
218 void
219 RttMeanDeviation::IntegerUpdate (Time m, uint32_t rttShift, uint32_t variationShift)
220 {
221  NS_LOG_FUNCTION (this << m << rttShift << variationShift);
222  // Jacobson/Karels paper appendix A.2
223  int64_t meas = m.GetInteger ();
224  int64_t delta = meas - m_estimatedRtt.GetInteger ();
225  int64_t srtt = (m_estimatedRtt.GetInteger () << rttShift) + delta;
226  m_estimatedRtt = Time::From (srtt >> rttShift);
227  if (delta < 0)
228  {
229  delta = -delta;
230  }
231  delta -= m_estimatedVariation.GetInteger ();
232  int64_t rttvar = m_estimatedVariation.GetInteger () << variationShift;
233  rttvar += delta;
234  m_estimatedVariation = Time::From (rttvar >> variationShift);
235  return;
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
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
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:399
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
second
Definition: nstime.h:114
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:446
virtual ~RttEstimator()
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:334
AttributeValue implementation for Time.
Definition: nstime.h:928
double ToDouble(enum Unit unit) const
Get the Time value expressed in a particular unit.
Definition: nstime.h:468
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:184
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:372
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:929
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:424
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:866
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:57
TypeId SetParent(TypeId tid)
Definition: type-id.cc:638
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...