00001 /* 00002 * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By 00003 * downloading, copying, installing or using the software you agree to 00004 * this license. If you do not agree to this license, do not download, 00005 * install, copy or use the software. 00006 * 00007 * Intel Open Source License 00008 * 00009 * Copyright (c) 2006 Intel Corporation. All rights reserved. 00010 * 00011 * Redistribution and use in source and binary forms, with or without 00012 * modification, are permitted provided that the following conditions are 00013 * met: 00014 * 00015 * Redistributions of source code must retain the above copyright 00016 * notice, this list of conditions and the following disclaimer. 00017 * 00018 * Redistributions in binary form must reproduce the above copyright 00019 * notice, this list of conditions and the following disclaimer in the 00020 * documentation and/or other materials provided with the distribution. 00021 * 00022 * Neither the name of the Intel Corporation nor the names of its 00023 * contributors may be used to endorse or promote products derived from 00024 * this software without specific prior written permission. 00025 * 00026 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00027 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00028 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00029 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR 00030 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00031 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00032 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00033 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00034 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00035 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00036 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00037 */ 00038 00039 #include "TokenBucket.h" 00040 00041 namespace oasys { 00042 00043 //---------------------------------------------------------------------- 00044 TokenBucket::TokenBucket(const char* logpath, 00045 u_int32_t depth, /* in bits */ 00046 u_int32_t rate /* in seconds */) 00047 : Logger("TokenBucket", logpath), 00048 depth_(depth), 00049 rate_(rate), 00050 tokens_(depth) // initialize full 00051 { 00052 log_debug("initialized token bucket with depth %u and rate %u", 00053 depth_, rate_); 00054 last_update_.get_time(); 00055 } 00056 00057 //---------------------------------------------------------------------- 00058 void 00059 TokenBucket::update() 00060 { 00061 Time now; 00062 now.get_time(); 00063 00064 if (tokens_ == depth_) { 00065 log_debug("update: bucket already full, nothing to update"); 00066 last_update_ = now; 00067 return; 00068 } 00069 00070 u_int32_t elapsed = (now - last_update_).in_milliseconds(); 00071 u_int32_t new_tokens = (rate_ * elapsed) / 1000; 00072 00073 if (new_tokens != 0) { 00074 if ((tokens_ + new_tokens) > depth_) { 00075 new_tokens = depth_ - tokens_; 00076 } 00077 00078 log_debug("update: filling %u/%u spent tokens after %u milliseconds", 00079 new_tokens, depth_ - tokens_, elapsed); 00080 tokens_ += new_tokens; 00081 last_update_ = now; 00082 00083 } else { 00084 // there's a chance that, for a slow rate, that the elapsed 00085 // time isn't enough to fill even a single token. in this 00086 // case, we leave last_update_ to where it was before, 00087 // otherwise we might starve the bucket. 00088 log_debug("update: %u milliseconds elapsed not enough to fill any tokens", 00089 elapsed); 00090 } 00091 } 00092 00093 //---------------------------------------------------------------------- 00094 bool 00095 TokenBucket::drain(u_int32_t length) 00096 { 00097 update(); 00098 00099 if (length <= tokens_) { 00100 log_debug("drain: draining %u/%u tokens from bucket", 00101 length, tokens_); 00102 tokens_ -= length; 00103 return true; 00104 } else { 00105 log_debug("drain: not enough tokens (%u) to drain %u from bucket", 00106 tokens_, length); 00107 return false; 00108 } 00109 } 00110 00111 //---------------------------------------------------------------------- 00112 u_int32_t 00113 TokenBucket::time_to_fill() 00114 { 00115 update(); 00116 00117 u_int32_t t = ((depth_ - tokens_) * 1000) / rate_; 00118 00119 log_debug("time_to_fill: %u tokens will be full in %u msecs", 00120 (depth_ - tokens_), t); 00121 return t; 00122 } 00123 00124 //---------------------------------------------------------------------- 00125 void 00126 TokenBucket::empty() 00127 { 00128 tokens_ = 0; 00129 last_update_.get_time(); 00130 00131 log_debug("empty: clearing bucket"); 00132 } 00133 00134 } // namespace oasys