Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 25 | #include "ns3/random-variable.h" |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 26 | |
| 27 | NS_LOG_COMPONENT_DEFINE ("ndn.Limits"); |
| 28 | |
| 29 | namespace ns3 { |
| 30 | namespace ndn { |
| 31 | |
| 32 | NS_OBJECT_ENSURE_REGISTERED (Limits); |
| 33 | |
| 34 | TypeId |
| 35 | Limits::GetTypeId () |
| 36 | { |
| 37 | static TypeId tid = TypeId ("ns3::ndn::Limits") |
| 38 | .SetGroupName ("Ndn") |
| 39 | .SetParent <Object> () |
| 40 | .AddConstructor <Limits> () |
| 41 | |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 42 | .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 Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 66 | .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 | |
| 77 | void |
| 78 | Limits::SetMaxLimit (uint32_t max) |
| 79 | { |
| 80 | m_maxLimit = max; |
| 81 | m_curMaxLimit = max; |
| 82 | } |
| 83 | |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 84 | uint32_t |
| 85 | Limits::GetMaxLimit () const |
| 86 | { |
| 87 | return m_maxLimit; |
| 88 | } |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 89 | |
| 90 | void |
| 91 | Limits::DecayCurrentLimit () |
| 92 | { |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 93 | if (!IsEnabled ()) return; |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 94 | |
| 95 | if (!m_lastDecay.IsZero ()) |
| 96 | { |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 97 | double timeDiff = (Simulator::Now () - m_lastDecay).ToDouble (Time::S); |
| 98 | |
| 99 | NS_LOG_DEBUG ("m_maxLimit - (m_maxLimit - m_curMaxLimit) * exp (-timeDiff / tau)"); |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 100 | NS_LOG_DEBUG (m_maxLimit << " - " << " ( " << m_maxLimit << " - " << (double)m_curMaxLimit << " ) " << " * " << " exp (- " << timeDiff << " / " << m_exponentialDecayTau.ToDouble (Time::S) << " ) "); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 101 | |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 102 | m_curMaxLimit = m_maxLimit - (m_maxLimit - m_curMaxLimit) * exp (-timeDiff / m_exponentialDecayTau.ToDouble (Time::S)); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | m_lastDecay = Simulator::Now (); |
| 106 | } |
| 107 | |
| 108 | void |
| 109 | Limits::IncreaseLimit () |
| 110 | { |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 111 | if (!IsEnabled ()) return; |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 112 | |
| 113 | // Additive increase |
| 114 | m_curMaxLimit = std::min (1.0 * m_maxLimit, |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 115 | (double)m_curMaxLimit + m_additiveIncrease / (double)m_curMaxLimit); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void |
| 119 | Limits::DecreaseLimit () |
| 120 | { |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 121 | if (!IsEnabled ()) return; |
| 122 | |
| 123 | // m_curMaxLimit = std::max (0.0, |
| 124 | // (double)m_curMaxLimit - m_multiplicativeDecrease / (double)m_curMaxLimit); |
| 125 | // } |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 126 | |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 127 | 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 Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 132 | |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 133 | m_lastDecrease = Simulator::Now (); |
| 134 | } |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | |
| 138 | bool |
| 139 | Limits::IsBelowLimit () |
| 140 | { |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 141 | if (!IsEnabled ()) return true; |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 142 | |
Alexander Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 143 | if (m_curMaxLimit - m_outstanding >= 1.0) |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 144 | { |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 145 | // 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; |
Alexander Afanasyev | 187cba2 | 2012-08-28 11:16:52 -0700 | [diff] [blame] | 152 | // if (Simulator::GetContext () == 6) std::cout << "." << std::flush; |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 153 | return true; |
| 154 | } |
| 155 | else |
| 156 | return false; |
| 157 | |
| 158 | // m_outstanding += 1; |
| 159 | // return true; |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 160 | } |
| 161 | else |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | Limits::RemoveOutstanding () |
| 167 | { |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 168 | if (!IsEnabled ()) return; //limits are disabled |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 169 | |
Alexander Afanasyev | 187cba2 | 2012-08-28 11:16:52 -0700 | [diff] [blame] | 170 | // if (Simulator::GetContext () == 6) std::cout << "*" << std::flush; |
| 171 | |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 172 | NS_LOG_DEBUG (m_outstanding); |
Alexander Afanasyev | cdd223c | 2012-09-03 19:56:19 -0700 | [diff] [blame] | 173 | NS_ASSERT_MSG (m_outstanding >= (uint32_t)1, "Should not be possible, unless we decreasing this number twice somewhere"); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 174 | m_outstanding -= 1; |
| 175 | } |
| 176 | |
| 177 | } // namespace ndn |
| 178 | } // namespace ns3 |