blob: d53e370f64479dbfaae52f1d4be17da909ed8132 [file] [log] [blame]
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2011 University of California, Los Angeles
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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21#include "ndn-limits.h"
22
23#include "ns3/log.h"
24#include "ns3/simulator.h"
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -070025#include "ns3/random-variable.h"
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070026
27NS_LOG_COMPONENT_DEFINE ("ndn.Limits");
28
29namespace ns3 {
30namespace ndn {
31
32NS_OBJECT_ENSURE_REGISTERED (Limits);
33
34TypeId
35Limits::GetTypeId ()
36{
37 static TypeId tid = TypeId ("ns3::ndn::Limits")
38 .SetGroupName ("Ndn")
39 .SetParent <Object> ()
40 .AddConstructor <Limits> ()
41
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -070042 .AddAttribute ("ExpDecayTau", "Parameter 'tau' for the exponential delay of the current maximum limit variable",
43 TimeValue (Seconds (100.0)),
44 MakeTimeAccessor (&Limits::m_exponentialDecayTau),
45 MakeTimeChecker ()
46 )
47
48 .AddAttribute ("NonDecreasePeriod", "Only one decrease is allowed per one NonDecreasePeriod",
49 TimeValue (Seconds (0.1)),
50 MakeTimeAccessor (&Limits::m_nonDecreasePeriod),
51 MakeTimeChecker ()
52 )
53
54 .AddAttribute ("AdditiveIncrease", "Parameter for additive increase",
55 DoubleValue (1.0),
56 MakeDoubleAccessor (&Limits::m_additiveIncrease),
57 MakeDoubleChecker<double> ()
58 )
59
60 .AddAttribute ("MultiplicativeDecrease", "Parameter for multiplicative decrease",
61 DoubleValue (0.5),
62 MakeDoubleAccessor (&Limits::m_multiplicativeDecrease),
63 MakeDoubleChecker<double> ()
64 )
65
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070066 .AddTraceSource ("CurMaxLimit",
67 "Current maximum limit",
68 MakeTraceSourceAccessor (&Limits::m_curMaxLimit))
69
70 .AddTraceSource ("Outstanding",
71 "Number of outstanding interests",
72 MakeTraceSourceAccessor (&Limits::m_outstanding))
73 ;
74 return tid;
75}
76
77void
78Limits::SetMaxLimit (uint32_t max)
79{
80 m_maxLimit = max;
81 m_curMaxLimit = max;
82}
83
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -070084uint32_t
85Limits::GetMaxLimit () const
86{
87 return m_maxLimit;
88}
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070089
90void
91Limits::DecayCurrentLimit ()
92{
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -070093 if (!IsEnabled ()) return;
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070094
95 if (!m_lastDecay.IsZero ())
96 {
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070097 double timeDiff = (Simulator::Now () - m_lastDecay).ToDouble (Time::S);
98
99 NS_LOG_DEBUG ("m_maxLimit - (m_maxLimit - m_curMaxLimit) * exp (-timeDiff / tau)");
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700100 NS_LOG_DEBUG (m_maxLimit << " - " << " ( " << m_maxLimit << " - " << (double)m_curMaxLimit << " ) " << " * " << " exp (- " << timeDiff << " / " << m_exponentialDecayTau.ToDouble (Time::S) << " ) ");
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700101
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700102 m_curMaxLimit = m_maxLimit - (m_maxLimit - m_curMaxLimit) * exp (-timeDiff / m_exponentialDecayTau.ToDouble (Time::S));
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700103 }
104
105 m_lastDecay = Simulator::Now ();
106}
107
108void
109Limits::IncreaseLimit ()
110{
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700111 if (!IsEnabled ()) return;
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700112
113 // Additive increase
114 m_curMaxLimit = std::min (1.0 * m_maxLimit,
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700115 (double)m_curMaxLimit + m_additiveIncrease / (double)m_curMaxLimit);
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700116}
117
118void
119Limits::DecreaseLimit ()
120{
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700121 if (!IsEnabled ()) return;
122
123// m_curMaxLimit = std::max (0.0,
124// (double)m_curMaxLimit - m_multiplicativeDecrease / (double)m_curMaxLimit);
125// }
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700126
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700127 if (m_lastDecrease.IsZero () ||
128 (!m_lastDecrease.IsZero () && Simulator::Now () - m_lastDecrease > m_nonDecreasePeriod)) // allow
129 {
130 // Multiplicative decrease... almost
131 m_curMaxLimit = m_multiplicativeDecrease * m_curMaxLimit;
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700132
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700133 m_lastDecrease = Simulator::Now ();
134 }
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700135}
136
137
138bool
139Limits::IsBelowLimit ()
140{
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700141 if (!IsEnabled ()) return true;
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700142
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700143 if (m_curMaxLimit - m_outstanding >= 1.0)
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700144 {
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700145 // static UniformVariable acceptanceProbability (0, m_curMaxLimit);
146 // double value = acceptanceProbability.GetValue ();
147 double value = m_outstanding+ 1;
148
149 if (m_outstanding < value)
150 {
151 m_outstanding += 1.0;
152 return true;
153 }
154 else
155 return false;
156
157 // m_outstanding += 1;
158 // return true;
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700159 }
160 else
161 return false;
162}
163
164void
165Limits::RemoveOutstanding ()
166{
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700167 if (!IsEnabled ()) return; //limits are disabled
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700168
169 NS_LOG_DEBUG (m_outstanding);
170 NS_ASSERT_MSG (m_outstanding >= 1, "Should not be possible, unless we decreasing this number twice somewhere");
171 m_outstanding -= 1;
172}
173
174} // namespace ndn
175} // namespace ns3