TokenBucket.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using OpenSim.Framework;
  32. using log4net;
  33. namespace OpenSim.Region.ClientStack.LindenUDP
  34. {
  35. /// <summary>
  36. /// A hierarchical token bucket for bandwidth throttling. See
  37. /// http://en.wikipedia.org/wiki/Token_bucket for more information
  38. /// </summary>
  39. public class TokenBucket
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. private static Int32 m_counter = 0;
  43. protected const float m_timeScale = 1e-3f;
  44. /// <summary>
  45. /// minimum recovery rate, ie bandwith
  46. /// </summary>
  47. protected const float MINDRIPRATE = 500;
  48. // minimum and maximim burst size, ie max number of bytes token can have
  49. protected const float MINBURST = 1500; // can't be less than one MTU or it will block
  50. protected const float MAXBURST = 7500;
  51. /// <summary>Time of the last drip</summary>
  52. protected double m_lastDrip;
  53. /// <summary>
  54. /// The number of bytes that can be sent at this moment. This is the
  55. /// current number of tokens in the bucket
  56. /// </summary>
  57. protected float m_tokenCount;
  58. /// <summary>
  59. /// Map of children buckets and their requested maximum burst rate
  60. /// </summary>
  61. protected Dictionary<TokenBucket, float> m_children = new Dictionary<TokenBucket, float>();
  62. #region Properties
  63. /// <summary>
  64. /// The parent bucket of this bucket, or null if this bucket has no
  65. /// parent. The parent bucket will limit the aggregate bandwidth of all
  66. /// of its children buckets
  67. /// </summary>
  68. protected TokenBucket m_parent;
  69. public TokenBucket Parent
  70. {
  71. get { return m_parent; }
  72. set { m_parent = value; }
  73. }
  74. /// <summary>
  75. /// This is the maximum number
  76. /// of tokens that can accumulate in the bucket at any one time. This
  77. /// also sets the total request for leaf nodes
  78. /// </summary>
  79. protected float m_burst;
  80. protected float m_maxDripRate = 0;
  81. public virtual float MaxDripRate
  82. {
  83. get { return m_maxDripRate; }
  84. set { m_maxDripRate = value; }
  85. }
  86. public float RequestedBurst
  87. {
  88. get { return m_burst; }
  89. set {
  90. float rate = (value < 0 ? 0 : value);
  91. if (rate < MINBURST)
  92. rate = MINBURST;
  93. else if (rate > MAXBURST)
  94. rate = MAXBURST;
  95. m_burst = rate;
  96. }
  97. }
  98. public float Burst
  99. {
  100. get {
  101. float rate = RequestedBurst * BurstModifier();
  102. if (rate < MINBURST)
  103. rate = MINBURST;
  104. return (float)rate;
  105. }
  106. }
  107. /// <summary>
  108. /// The requested drip rate for this particular bucket.
  109. /// </summary>
  110. /// <remarks>
  111. /// 0 then TotalDripRequest is used instead.
  112. /// Can never be above MaxDripRate.
  113. /// Tokens are added to the bucket at any time
  114. /// <seealso cref="RemoveTokens"/> is called, at the granularity of
  115. /// the system tick interval (typically around 15-22ms)</remarks>
  116. protected float m_dripRate;
  117. public float RequestedDripRate
  118. {
  119. get { return (m_dripRate == 0 ? m_totalDripRequest : m_dripRate); }
  120. set {
  121. m_dripRate = (value < 0 ? 0 : value);
  122. m_totalDripRequest = m_dripRate;
  123. if (m_parent != null)
  124. m_parent.RegisterRequest(this,m_dripRate);
  125. }
  126. }
  127. public float DripRate
  128. {
  129. get {
  130. float rate = Math.Min(RequestedDripRate,TotalDripRequest);
  131. if (m_parent == null)
  132. return rate;
  133. rate *= m_parent.DripRateModifier();
  134. if (rate < MINDRIPRATE)
  135. rate = MINDRIPRATE;
  136. return (float)rate;
  137. }
  138. }
  139. /// <summary>
  140. /// The current total of the requested maximum burst rates of children buckets.
  141. /// </summary>
  142. protected float m_totalDripRequest;
  143. public float TotalDripRequest
  144. {
  145. get { return m_totalDripRequest; }
  146. set { m_totalDripRequest = value; }
  147. }
  148. #endregion Properties
  149. #region Constructor
  150. /// <summary>
  151. /// Default constructor
  152. /// </summary>
  153. /// <param name="identifier">Identifier for this token bucket</param>
  154. /// <param name="parent">Parent bucket if this is a child bucket, or
  155. /// null if this is a root bucket</param>
  156. /// <param name="maxBurst">Maximum size of the bucket in bytes, or
  157. /// zero if this bucket has no maximum capacity</param>
  158. /// <param name="dripRate">Rate that the bucket fills, in bytes per
  159. /// second. If zero, the bucket always remains full</param>
  160. public TokenBucket(TokenBucket parent, float dripRate, float MaxBurst)
  161. {
  162. m_counter++;
  163. Parent = parent;
  164. RequestedDripRate = dripRate;
  165. RequestedBurst = MaxBurst;
  166. m_lastDrip = Util.GetTimeStampMS() + 100000.0; // skip first drip
  167. }
  168. #endregion Constructor
  169. /// <summary>
  170. /// Compute a modifier for the MaxBurst rate. This is 1.0, meaning
  171. /// no modification if the requested bandwidth is less than the
  172. /// max burst bandwidth all the way to the root of the throttle
  173. /// hierarchy. However, if any of the parents is over-booked, then
  174. /// the modifier will be less than 1.
  175. /// </summary>
  176. protected float DripRateModifier()
  177. {
  178. float driprate = DripRate;
  179. return driprate >= TotalDripRequest ? 1.0f : (driprate / TotalDripRequest);
  180. }
  181. /// <summary>
  182. /// </summary>
  183. protected float BurstModifier()
  184. {
  185. // for now... burst rate is always m_quantumsPerBurst (constant)
  186. // larger than drip rate so the ratio of burst requests is the
  187. // same as the drip ratio
  188. return DripRateModifier();
  189. }
  190. /// <summary>
  191. /// Register drip rate requested by a child of this throttle. Pass the
  192. /// changes up the hierarchy.
  193. /// </summary>
  194. public void RegisterRequest(TokenBucket child, float request)
  195. {
  196. lock (m_children)
  197. {
  198. m_children[child] = request;
  199. m_totalDripRequest = 0;
  200. foreach (KeyValuePair<TokenBucket, float> cref in m_children)
  201. m_totalDripRequest += cref.Value;
  202. }
  203. // Pass the new values up to the parent
  204. if (m_parent != null)
  205. m_parent.RegisterRequest(this, Math.Min(RequestedDripRate, TotalDripRequest));
  206. }
  207. /// <summary>
  208. /// Remove the rate requested by a child of this throttle. Pass the
  209. /// changes up the hierarchy.
  210. /// </summary>
  211. public void UnregisterRequest(TokenBucket child)
  212. {
  213. lock (m_children)
  214. {
  215. m_children.Remove(child);
  216. m_totalDripRequest = 0;
  217. foreach (KeyValuePair<TokenBucket, float> cref in m_children)
  218. m_totalDripRequest += cref.Value;
  219. }
  220. // Pass the new values up to the parent
  221. if (Parent != null)
  222. Parent.RegisterRequest(this,Math.Min(RequestedDripRate, TotalDripRequest));
  223. }
  224. /// <summary>
  225. /// Remove a given number of tokens from the bucket
  226. /// </summary>
  227. /// <param name="amount">Number of tokens to remove from the bucket</param>
  228. /// <returns>True if the requested number of tokens were removed from
  229. /// the bucket, otherwise false</returns>
  230. public bool RemoveTokens(int amount)
  231. {
  232. // Deposit tokens for this interval
  233. Drip();
  234. // If we have enough tokens then remove them and return
  235. if (m_tokenCount - amount >= 0)
  236. {
  237. // we don't have to remove from the parent, the drip rate is already
  238. // reflective of the drip rate limits in the parent
  239. m_tokenCount -= amount;
  240. return true;
  241. }
  242. return false;
  243. }
  244. public bool CheckTokens(int amount)
  245. {
  246. return (m_tokenCount - amount >= 0);
  247. }
  248. public int GetCatBytesCanSend(int timeMS)
  249. {
  250. // return (int)(m_tokenCount + timeMS * m_dripRate * 1e-3);
  251. return (int)(timeMS * DripRate * 1e-3);
  252. }
  253. /// <summary>
  254. /// Add tokens to the bucket over time. The number of tokens added each
  255. /// call depends on the length of time that has passed since the last
  256. /// call to Drip
  257. /// </summary>
  258. /// <returns>True if tokens were added to the bucket, otherwise false</returns>
  259. protected void Drip()
  260. {
  261. // This should never happen... means we are a leaf node and were created
  262. // with no drip rate...
  263. if (DripRate == 0)
  264. {
  265. m_log.WarnFormat("[TOKENBUCKET] something odd is happening and drip rate is 0 for {0}", m_counter);
  266. return;
  267. }
  268. double now = Util.GetTimeStampMS();
  269. double deltaMS = now - m_lastDrip;
  270. m_lastDrip = now;
  271. if (deltaMS <= 0)
  272. return;
  273. m_tokenCount += (float)deltaMS * DripRate * m_timeScale;
  274. float burst = Burst;
  275. if (m_tokenCount > burst)
  276. m_tokenCount = burst;
  277. }
  278. }
  279. public class AdaptiveTokenBucket : TokenBucket
  280. {
  281. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  282. public bool AdaptiveEnabled { get; set; }
  283. /// <summary>
  284. /// The minimum rate for flow control. Minimum drip rate is one
  285. /// packet per second.
  286. /// </summary>
  287. protected const float m_minimumFlow = 50000;
  288. // <summary>
  289. // The maximum rate for flow control. Drip rate can never be
  290. // greater than this.
  291. // </summary>
  292. public override float MaxDripRate
  293. {
  294. get { return (m_maxDripRate == 0 ? m_totalDripRequest : m_maxDripRate); }
  295. set
  296. {
  297. m_maxDripRate = (value == 0 ? m_totalDripRequest : Math.Max(value, m_minimumFlow));
  298. }
  299. }
  300. private bool m_enabled = false;
  301. // <summary>
  302. // Adjust drip rate in response to network conditions.
  303. // </summary>
  304. public float AdjustedDripRate
  305. {
  306. get { return m_dripRate; }
  307. set
  308. {
  309. m_dripRate = OpenSim.Framework.Util.Clamp<float>(value, m_minimumFlow, MaxDripRate);
  310. if (m_parent != null)
  311. m_parent.RegisterRequest(this, m_dripRate);
  312. }
  313. }
  314. // <summary>
  315. //
  316. // </summary>
  317. public AdaptiveTokenBucket(TokenBucket parent, float maxDripRate, float maxBurst, bool enabled)
  318. : base(parent, maxDripRate, maxBurst)
  319. {
  320. m_enabled = enabled;
  321. m_maxDripRate = (maxDripRate == 0 ? m_totalDripRequest : Math.Max(maxDripRate, m_minimumFlow));
  322. if (enabled)
  323. m_dripRate = m_maxDripRate * .5f;
  324. else
  325. m_dripRate = m_maxDripRate;
  326. if (m_parent != null)
  327. m_parent.RegisterRequest(this, m_dripRate);
  328. }
  329. /// <summary>
  330. /// Reliable packets sent to the client for which we never received an ack adjust the drip rate down.
  331. /// <param name="packets">Number of packets that expired without successful delivery</param>
  332. /// </summary>
  333. public void ExpirePackets(Int32 count)
  334. {
  335. // m_log.WarnFormat("[ADAPTIVEBUCKET] drop {0} by {1} expired packets",AdjustedDripRate,count);
  336. if (m_enabled)
  337. AdjustedDripRate = (Int64)(AdjustedDripRate / Math.Pow(2, count));
  338. }
  339. // <summary>
  340. //
  341. // </summary>
  342. public void AcknowledgePackets(Int32 count)
  343. {
  344. if (m_enabled)
  345. AdjustedDripRate = AdjustedDripRate + count;
  346. }
  347. }
  348. }