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" |
| 25 | |
| 26 | NS_LOG_COMPONENT_DEFINE ("ndn.Limits"); |
| 27 | |
| 28 | namespace ns3 { |
| 29 | namespace ndn { |
| 30 | |
| 31 | NS_OBJECT_ENSURE_REGISTERED (Limits); |
| 32 | |
| 33 | TypeId |
| 34 | Limits::GetTypeId () |
| 35 | { |
| 36 | static TypeId tid = TypeId ("ns3::ndn::Limits") |
| 37 | .SetGroupName ("Ndn") |
| 38 | .SetParent <Object> () |
| 39 | .AddConstructor <Limits> () |
| 40 | |
| 41 | .AddTraceSource ("CurMaxLimit", |
| 42 | "Current maximum limit", |
| 43 | MakeTraceSourceAccessor (&Limits::m_curMaxLimit)) |
| 44 | |
| 45 | .AddTraceSource ("Outstanding", |
| 46 | "Number of outstanding interests", |
| 47 | MakeTraceSourceAccessor (&Limits::m_outstanding)) |
| 48 | ; |
| 49 | return tid; |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | Limits::SetMaxLimit (uint32_t max) |
| 54 | { |
| 55 | m_maxLimit = max; |
| 56 | m_curMaxLimit = max; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | void |
| 61 | Limits::DecayCurrentLimit () |
| 62 | { |
| 63 | if (m_maxLimit == 0) return; |
| 64 | |
| 65 | if (!m_lastDecay.IsZero ()) |
| 66 | { |
| 67 | const double tau = 100.0; // seconds |
| 68 | double timeDiff = (Simulator::Now () - m_lastDecay).ToDouble (Time::S); |
| 69 | |
| 70 | NS_LOG_DEBUG ("m_maxLimit - (m_maxLimit - m_curMaxLimit) * exp (-timeDiff / tau)"); |
| 71 | NS_LOG_DEBUG (m_maxLimit << " - " << " ( " << m_maxLimit << " - " << (double)m_curMaxLimit << " ) " << " * " << " exp (- " << timeDiff << " / " << tau << " ) "); |
| 72 | |
| 73 | m_curMaxLimit = m_maxLimit - (m_maxLimit - m_curMaxLimit) * exp (-timeDiff / tau); |
| 74 | } |
| 75 | |
| 76 | m_lastDecay = Simulator::Now (); |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | Limits::IncreaseLimit () |
| 81 | { |
| 82 | if (m_maxLimit == 0) return; |
| 83 | |
| 84 | // Additive increase |
| 85 | m_curMaxLimit = std::min (1.0 * m_maxLimit, |
| 86 | (double)m_curMaxLimit + 1.0 / (double)m_curMaxLimit); |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | Limits::DecreaseLimit () |
| 91 | { |
| 92 | if (m_maxLimit == 0) return; |
| 93 | |
| 94 | const double maxDecreaseFrequency = 10.0; |
| 95 | |
| 96 | if (!m_lastDecrease.IsZero () && Simulator::Now () - m_lastDecrease < Seconds (1 / maxDecreaseFrequency)) |
| 97 | return; |
| 98 | |
| 99 | // Multiplicative decrease... almost |
| 100 | m_curMaxLimit = 0.5 * m_curMaxLimit; |
| 101 | |
| 102 | m_lastDecrease = Simulator::Now (); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | bool |
| 107 | Limits::IsBelowLimit () |
| 108 | { |
| 109 | if (m_maxLimit == 0) return true; |
| 110 | |
| 111 | if (m_curMaxLimit - m_outstanding > 1.0) |
| 112 | { |
| 113 | m_outstanding += 1; |
| 114 | return true; |
| 115 | } |
| 116 | else |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | Limits::RemoveOutstanding () |
| 122 | { |
| 123 | if (m_maxLimit == 0) return; //limits are disabled |
| 124 | |
| 125 | NS_LOG_DEBUG (m_outstanding); |
| 126 | NS_ASSERT_MSG (m_outstanding >= 1, "Should not be possible, unless we decreasing this number twice somewhere"); |
| 127 | m_outstanding -= 1; |
| 128 | } |
| 129 | |
| 130 | } // namespace ndn |
| 131 | } // namespace ns3 |