00001 /* 00002 * BandwidthFilter.cc : part of the Mace toolkit for building distributed systems 00003 * 00004 * Copyright (c) 2007, Charles Killian, Dejan Kostic, Ryan Braud, James W. Anderson, John Fisher-Ogden, Calvin Hubble, Duy Nguyen, Justin Burke, David Oppenheimer, Amin Vahdat, Adolfo Rodriguez, Sooraj Bhat 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions are met: 00009 * 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in 00014 * the documentation and/or other materials provided with the 00015 * distribution. 00016 * * Neither the names of Duke University nor The University of 00017 * California, San Diego, nor the names of the authors or contributors 00018 * may be used to endorse or promote products derived from 00019 * this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00022 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00023 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00024 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 00025 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00026 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00027 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00028 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00029 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 00030 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 * 00032 * ----END-OF-LEGAL-STUFF---- */ 00033 #include "BandwidthFilter.h" 00034 #include "TimeUtil.h" 00035 00036 using std::list; 00037 00038 BandwidthFilter::BandwidthFilter() { 00039 history_count =0; 00040 value = 0.0; 00041 time_initial = TimeUtil::timeu(); 00042 } 00043 00044 BandwidthFilter::~BandwidthFilter() { 00045 } 00046 00047 // ---------------------------------------------- 00048 // getValue 00049 // ---------------------------------------------- 00050 00051 double BandwidthFilter::getValue() { 00052 check(); 00053 double result = value; 00054 return result; 00055 } 00056 00057 00058 // ---------------------------------------------- 00059 // clear 00060 // ---------------------------------------------- 00061 00062 void BandwidthFilter::clear() { 00063 history.clear (); 00064 value = 0.0; 00065 } 00066 00067 00068 // ---------------------------------------------- 00069 // update 00070 // ---------------------------------------------- 00071 00072 void BandwidthFilter::update(int size) { 00073 uint64_t now = TimeUtil::timeu(); 00074 00075 history.push_back(BandwidthPair (size,now)); 00076 history_count++; 00077 check(); 00078 } 00079 00080 00081 00082 // ---------------------------------------------- 00083 // check 00084 // ---------------------------------------------- 00085 00086 void BandwidthFilter::check() { 00087 uint64_t elapsed_time; 00088 uint64_t now = TimeUtil::timeu(); 00089 00090 while (history_count && (now - history.front().when) > BANDWIDTH_WINDOW) { 00091 history.pop_front(); 00092 history_count--; 00093 } 00094 00095 if (history_count == 0) { 00096 value = 0.0; 00097 } 00098 else { 00099 if (now - time_initial < BANDWIDTH_WINDOW) 00100 elapsed_time = now - time_initial; 00101 else 00102 elapsed_time = BANDWIDTH_WINDOW; 00103 int total_bytes = 0; 00104 for(list<BandwidthPair>::iterator it = history.begin(); 00105 it != history.end(); ++it) { 00106 BandwidthPair r=*it; 00107 total_bytes+=r.size; 00108 } 00109 value = total_bytes*8/((double)elapsed_time/1000000); 00110 } 00111 } 00112 00113