blob: 8675479cd519eea65554b5c2289dfeda8664c750 [file] [log] [blame]
Alexander Afanasyevd9a7f192013-03-07 13:58:14 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2//
3// Copyright (c) 2006 Georgia Tech Research Corporation
4// (c) 2013 University of Arizona
5// (c) 2013 University of California, Los Angeles
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License version 2 as
9// published by the Free Software Foundation;
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program; if not, write to the Free Software
18// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19//
20// Author: Rajib Bhattacharjea<raj.b@gatech.edu>
21// Cheng Yi <yic@email.arizona.edu>
22// Alexander Afanasyev <alexander.afanasyev@ucla.edu>
23//
24
25// Georgia Tech Network Simulator - Round Trip Time Estimation Class
26// George F. Riley. Georgia Tech, Spring 2002
27
28#include "ndn-rtt-mean-deviation.h"
29#include "ns3/simulator.h"
30#include "ns3/double.h"
31#include "ns3/integer.h"
32#include "ns3/uinteger.h"
33#include "ns3/log.h"
34
35NS_LOG_COMPONENT_DEFINE ("ndn.RttMeanDeviation");
36
37namespace ns3 {
38namespace ndn {
39
40//---------------------------------------------------------------------------------
41// A modified version of Mean-Deviation Estimator optimized for NDN packet delivery
42
43NS_OBJECT_ENSURE_REGISTERED (RttMeanDeviation);
44
45TypeId
46RttMeanDeviation::GetTypeId (void)
47{
48 static TypeId tid = TypeId ("ns3::ndn::RttMeanDeviation")
49 .SetParent<RttEstimator> ()
50 .AddConstructor<RttMeanDeviation> ()
51 .AddAttribute ("Gain",
52 "Gain used in estimating the RTT (smoothed RTT), must be 0 < Gain < 1",
53 DoubleValue (0.125),
54 MakeDoubleAccessor (&RttMeanDeviation::m_gain),
55 MakeDoubleChecker<double> ())
56 .AddAttribute ("Gain2",
57 "Gain2 used in estimating the RTT (variance), must be 0 < Gain2 < 1",
58 DoubleValue (0.25),
59 MakeDoubleAccessor (&RttMeanDeviation::m_gain2),
60 MakeDoubleChecker<double> ())
61 ;
62 return tid;
63}
64
65RttMeanDeviation::RttMeanDeviation() :
66 m_variance (0)
67{
68 NS_LOG_FUNCTION (this);
69}
70
71RttMeanDeviation::RttMeanDeviation (const RttMeanDeviation& c)
72 : RttEstimator (c), m_gain (c.m_gain), m_gain2 (c.m_gain2), m_variance (c.m_variance)
73{
74 NS_LOG_FUNCTION (this);
75}
76
77void RttMeanDeviation::Measurement (Time m)
78{
79 NS_LOG_FUNCTION (this << m);
80 if (m_nSamples)
81 { // Not first
82 Time err (m - m_currentEstimatedRtt);
83 double gErr = err.ToDouble (Time::S) * m_gain;
84 m_currentEstimatedRtt += Time::FromDouble (gErr, Time::S);
85 Time difference = Abs (err) - m_variance;
86 NS_LOG_DEBUG ("m_variance += " << Time::FromDouble (difference.ToDouble (Time::S) * m_gain2, Time::S));
87 m_variance += Time::FromDouble (difference.ToDouble (Time::S) * m_gain2, Time::S);
88 }
89 else
90 { // First sample
91 m_currentEstimatedRtt = m; // Set estimate to current
92 //variance = sample / 2; // And variance to current / 2
93 // m_variance = m; // try this why????
94 m_variance = Seconds (m.ToDouble (Time::S) / 2);
95 NS_LOG_DEBUG ("(first sample) m_variance += " << m);
96 }
97 m_nSamples++;
98}
99
100Time RttMeanDeviation::RetransmitTimeout ()
101{
102 NS_LOG_FUNCTION (this);
103
104 double retval = std::min (m_maxRto.ToDouble (Time::S),
105 std::max (m_multiplier*m_minRto.ToDouble (Time::S),
106 m_multiplier*(m_currentEstimatedRtt.ToDouble (Time::S) + 4 * m_variance.ToDouble (Time::S))));
107
108 NS_LOG_DEBUG ("RetransmitTimeout: return " << retval);
109
110 return Seconds (retval);
111}
112
113Ptr<RttEstimator> RttMeanDeviation::Copy () const
114{
115 NS_LOG_FUNCTION (this);
116 return CopyObject<RttMeanDeviation> (this);
117}
118
119void RttMeanDeviation::Reset ()
120{
121 NS_LOG_FUNCTION (this);
122 // Reset to initial state
123 m_variance = Seconds (0);
124 RttEstimator::Reset ();
125}
126
127void RttMeanDeviation::Gain (double g)
128{
129 NS_LOG_FUNCTION (this);
130 NS_ASSERT_MSG( (g > 0) && (g < 1), "RttMeanDeviation: Gain must be less than 1 and greater than 0" );
131 m_gain = g;
132}
133
134Time RttMeanDeviation::AckSeq (SequenceNumber32 ackSeq)
135{
136 NS_LOG_FUNCTION (this << ackSeq);
137 // An ack has been received, calculate rtt and log this measurement
138 // Note we use a linear search (O(n)) for this since for the common
139 // case the ack'ed packet will be at the head of the list
140 Time m = Seconds (0.0);
141 if (m_history.size () == 0) return (m); // No pending history, just exit
142
143 for (RttHistory_t::iterator i = m_history.begin (); i != m_history.end (); ++i)
144 {
145 if (ackSeq == i->seq)
146 { // Found it
147 m = Simulator::Now () - i->time;// Elapsed time
148 Measurement (m); // Log the measurement
149 ResetMultiplier (); // Reset multiplier on valid measurement
150 m_history.erase(i);
151 break;
152 }
153 }
154
155 return m;
156}
157
158} // namespace ndn
159} // namespace ns3
160