BandwidthThrottle.cpp

Go to the documentation of this file.
00001 /* vim: set ts=4 expandtab shiftwidth=4: */
00002 
00003 /******************************************************************************
00004 
00005     Copyright 2006-2007 Digital Aggregates Corp., Arvada CO 80001-0587, USA.
00006     This file is part of the Digital Aggregates Desperado library.
00007     
00008     This library is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU Lesser General Public
00010     License as published by the Free Software Foundation; either
00011     version 2.1 of the License, or (at your option) any later version.
00012 
00013     As a special exception, if other files instantiate templates or
00014     use macros or inline functions from this file, or you compile
00015     this file and link it with other works to produce a work based on
00016     this file, this file does not by itself cause the resulting work
00017     to be covered by the GNU Lesser General Public License. However
00018     the source code for this file must still be made available in
00019     accordance with the GNU Lesser General Public License.
00020 
00021     This exception does not invalidate any other reasons why a work
00022     based on this file might be covered by the GNU Lesser General
00023     Public License.
00024 
00025     Alternative commercial licensing terms are available from the copyright
00026     holder. Contact Digital Aggregates Corporation for more information.
00027 
00028     This library is distributed in the hope that it will be useful,
00029     but WITHOUT ANY WARRANTY; without even the implied warranty of
00030     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00031     GNU Lesser General Public License for more details.
00032 
00033     You should have received a copy of the GNU Lesser General
00034     Public License along with this library; if not, write to the
00035     Free Software Foundation, Inc., 59 Temple Place, Suite 330,
00036     Boston, MA 02111-1307 USA, or http://www.gnu.org/copyleft/lesser.txt.
00037 
00038     $Name:  $
00039 
00040     $Id: BandwidthThrottle.cpp,v 1.8 2007/01/31 23:23:56 jsloan Exp $
00041 
00042 ******************************************************************************/
00043 
00044 
00060 #include <new>
00061 #include "generics.h"
00062 #include "BandwidthThrottle.h"
00063 #include "Constant.h"
00064 #include "Platform.h"
00065 #include "Print.h"
00066 
00067 
00068 static Gcra gcra;
00069 
00070 
00071 //
00072 //  Compute the peak increment.
00073 //
00074 ticks_t BandwidthThrottle::increment(
00075     uint32_t pbps,
00076     uint32_t /* jt */
00077 ) {
00078     ticks_t hz = gcra.frequency();
00079     ticks_t increment = (0 < pbps) ? (hz + pbps - 1) / pbps : intmaxof(ticks_t);
00080     return increment;
00081 }
00082 
00083 
00084 //
00085 //  Compute the peak limit.
00086 //
00087 ticks_t BandwidthThrottle::limit(
00088     uint32_t /* pbps */,
00089     uint32_t jt
00090 ) {
00091     ticks_t hz = gcra.frequency();
00092     ticks_t limit = (hz * jt) / Constant::ns_per_s;
00093     return limit;
00094 }
00095 
00096 
00097 //
00098 //  Compute the sustainable increment.
00099 //
00100 ticks_t BandwidthThrottle::increment(
00101    uint32_t /* pbps */,
00102    uint32_t /* jt */,
00103    uint32_t sbps,
00104    uint32_t /* mbs */
00105 ) {
00106     ticks_t hz = gcra.frequency();
00107     ticks_t increment = (0 < sbps) ? (hz + sbps - 1) / sbps : intmaxof(ticks_t);
00108     return increment;
00109 }
00110 
00111 
00112 //
00113 //  Compute the sustainable limit.
00114 //
00115 ticks_t BandwidthThrottle::limit(
00116    uint32_t pbps,
00117    uint32_t jt,
00118    uint32_t sbps,
00119    uint32_t mbs
00120 ) {
00121     ticks_t limit = BandwidthThrottle::limit(pbps, jt);
00122     ticks_t pincrement = BandwidthThrottle::increment(pbps, jt);
00123     ticks_t sincrement = BandwidthThrottle::increment(pbps, jt, sbps, mbs);
00124     if ((1 < mbs) && (0 < pincrement) && (pincrement < sincrement)) {
00125         limit += (mbs - 1) * (sincrement - pincrement);
00126     }
00127     return limit;
00128 }
00129 
00130 
00131 //
00132 //  Constructor.
00133 //
00134 BandwidthThrottle::BandwidthThrottle() :
00135     CompoundThrottle(),
00136     peak(),
00137     sustained()
00138 {
00139 }
00140 
00141 
00142 //
00143 //  Constructor.
00144 //
00145 BandwidthThrottle::BandwidthThrottle(
00146     uint32_t pbps,
00147     uint32_t jt
00148 ) :
00149     CompoundThrottle(peak),
00150     peak(
00151         this->increment(pbps, jt),
00152         this->limit(pbps, jt)
00153     )
00154 {
00155 }
00156 
00157 
00158 //
00159 //  Constructor.
00160 //
00161 BandwidthThrottle::BandwidthThrottle(
00162     uint32_t pbps,
00163     uint32_t jt,
00164     uint32_t sbps,
00165     uint32_t mbs
00166 ) :
00167     CompoundThrottle(peak, sustained),
00168     peak(
00169         this->increment(pbps, jt),
00170         this->limit(pbps, jt)
00171     ),
00172     sustained(
00173         this->increment(pbps, jt, sbps, mbs),
00174         this->limit(pbps, jt, sbps, mbs)
00175     )
00176 {
00177 }
00178 
00179 
00180 
00181 //
00182 //  Destructor.
00183 //
00184 BandwidthThrottle::~BandwidthThrottle() {
00185 }
00186 
00187 
00188 //
00189 //    Initializer.
00190 //
00191 bool BandwidthThrottle::initialize() {
00192     bool rc = false;
00193     try {
00194         this->BandwidthThrottle::~BandwidthThrottle();
00195         new(this) BandwidthThrottle;
00196         rc = true;
00197     } catch (...) {
00198         rc = false;
00199     }
00200     return rc;
00201 }
00202 
00203 
00204 //
00205 //    Initializer.
00206 //
00207 bool BandwidthThrottle::initialize(
00208     uint32_t pbps,
00209     uint32_t jt
00210 ) {
00211     bool rc = false;
00212     try {
00213         this->BandwidthThrottle::~BandwidthThrottle();
00214         new(this) BandwidthThrottle(pbps, jt);
00215         rc = true;
00216     } catch (...) {
00217         rc = false;
00218     }
00219     return rc;
00220 }
00221 
00222 
00223 //
00224 //    Initializer.
00225 //
00226 bool BandwidthThrottle::initialize(
00227     uint32_t pbps,
00228     uint32_t jt,
00229     uint32_t sbps,
00230     uint32_t mbs
00231 ) {
00232     bool rc = false;
00233     try {
00234         this->BandwidthThrottle::~BandwidthThrottle();
00235         new(this) BandwidthThrottle(pbps, jt, sbps, mbs);
00236         rc = true;
00237     } catch (...) {
00238         rc = false;
00239     }
00240     return rc;
00241 }
00242 
00243 //
00244 //  Show this object on the output object.
00245 //
00246 void BandwidthThrottle::show(int level, Output* display, int indent) const {
00247     Platform& pl = Platform::instance();
00248     Print printf(display);
00249     const char* sp = printf.output().indentation(indent);
00250     char component[sizeof(__FILE__)];
00251     printf("%s%s(%p)[%lu]:\n",
00252         sp, pl.component(__FILE__, component, sizeof(component)),
00253         this, sizeof(*this));
00254     this->CompoundThrottle::show(level, display, indent + 1);
00255     printf("%s peak:\n", sp);
00256     this->peak.show(level, display, indent + 2);
00257     printf("%s sustained:\n", sp);
00258     this->sustained.show(level, display, indent + 2);
00259 }
Copyright © 2006 Digital Aggregates Corp., Arvada CO 80001-0587, USA.
Distributed under the terms of the GNU Free Documentation License.
http://www.diag.com/navigation/downloads/Desperado.html