From: Francesco Chemolli Date: Thu, 15 Dec 2011 23:14:48 +0000 (+0100) Subject: Moved some more typedefs to StatHist.h X-Git-Tag: BumpSslServerFirst.take05~12^2~120^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a343943fe8740295bf9734b2f512b006675df785;p=thirdparty%2Fsquid.git Moved some more typedefs to StatHist.h Improved documentation Removed squid.h include Changed StatHist::operator= to almost be an actual assignment operator Fixed findBin corner-case --- diff --git a/src/StatHist.cc b/src/StatHist.cc index c857de3b73..395708c77a 100644 --- a/src/StatHist.cc +++ b/src/StatHist.cc @@ -1,7 +1,5 @@ /* - * $Id$ - * * DEBUG: section 62 Generic Histogram * AUTHOR: Duane Wessels * @@ -33,21 +31,8 @@ * */ -/* - * Important restrictions on val_in and val_out functions: - * - * - val_in: ascending, defined on [0, oo), val_in(0) == 0; - * - val_out: x == val_out(val_in(x)) where val_in(x) is defined - * - * In practice, the requirements are less strict, - * but then it gets hard to define them without math notation. - * val_in is applied after offseting the value but before scaling - * See log and linear based histograms for examples - */ - -#include "squid.h" +#include "config.h" #include "StatHist.h" -#include "Store.h" /* Local functions */ static StatHistBinDumper statHistBinDumper; @@ -72,7 +57,7 @@ StatHist::init(int capacity_, hbase_f * val_in_, hbase_f * val_out_, double min_ capacity = capacity_; val_in = val_in_; val_out = val_out_; - bins = (int *)xcalloc(capacity, sizeof(int)); + bins = static_cast(xcalloc(capacity, sizeof(int))); scale = capacity / val_in(max - min); /* check that functions are valid */ @@ -99,18 +84,22 @@ StatHist::~StatHist() } } -/* assumes that somebody already called init for Dest */ -/* TODO: remove assumption */ StatHist& StatHist::operator =(const StatHist & src) { - assert(src.bins != NULL); - assert(capacity==src.capacity); - assert(min==src.min); - assert(max==src.max); - assert(fabs(scale - src.scale) < 0.0000001); - assert(val_in==src.val_in); - assert(val_out==src.val_out); + assert(src.bins != NULL); // TODO: remove after initializing bins at construction time + if (capacity != src.capacity) { + // need to resize. + xfree(bins); + bins = static_cast(xcalloc(src.capacity, sizeof(int))); + capacity=src.capacity; + + } + min=src.min; + max=src.max; + scale=src.scale; + val_in=src.val_in; + val_out=src.val_out; memcpy(bins,src.bins,capacity*sizeof(*bins)); return *this; } @@ -137,7 +126,7 @@ StatHist::findBin(double v) bin = (int) floor(scale * val_in(v) + 0.5); if (bin < 0) /* should not happen */ - bin = 0; + return 0; if (bin >= capacity) /* too big */ bin = capacity - 1; @@ -154,13 +143,13 @@ StatHist::val(int bin) const double statHistDeltaMedian(const StatHist & A, const StatHist & B) { - return statHistDeltaPctile(A,B, 0.5); + return statHistDeltaPctile(A, B, 0.5); } double statHistDeltaPctile(const StatHist & A, const StatHist & B, double pctile) { - return A.deltaPctile(B,pctile); + return A.deltaPctile(B, pctile); } double @@ -178,7 +167,7 @@ StatHist::deltaPctile(const StatHist & B, double pctile) const assert(capacity == B.capacity); - int *D = (int *)xcalloc(capacity, sizeof(int)); + int *D = static_cast(xcalloc(capacity, sizeof(int))); for (i = 0; i < capacity; ++i) { D[i] = B.bins[i] - bins[i]; @@ -279,7 +268,7 @@ Math::Null(double x) void StatHist::enumInit(int last_enum) { - init(last_enum + 3, Math::Null, Math::Null, (double) -1, (double) (last_enum + 1 + 1)); + init(last_enum + 3, Math::Null, Math::Null, -1.0, (2.0 + last_enum)); } void @@ -296,4 +285,3 @@ statHistIntDumper(StoreEntry * sentry, int idx, double val, double size, int cou if (count) storeAppendPrintf(sentry, "%9d\t%9d\n", (int) val, count); } - diff --git a/src/StatHist.h b/src/StatHist.h index 0b6ce1d05a..4660958c7b 100644 --- a/src/StatHist.h +++ b/src/StatHist.h @@ -1,4 +1,6 @@ /* + * AUTHOR: Francesco Chemolli + * * SQUID Web Proxy Cache http://www.squid-cache.org/ * ---------------------------------------------------------- * @@ -24,26 +26,33 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. - * - * AUTHOR: Francesco Chemolli */ #ifndef STATHIST_H_ #define STATHIST_H_ -#include "typedefs.h" +/* for StoreEntry */ +#include "Store.h" + + +/// function signature for in/out StatHist adaptation +typedef double hbase_f(double); + +/// function signature for StatHist dumping functions +typedef void StatHistBinDumper(StoreEntry *, int idx, double val, double size, int count); /** Generic histogram class * - * see important comments on hbase_f restrictions in StatHist.c + * see important comments on hbase_f restrictions in StatHist.cc */ class StatHist { public: - /** Default constructor - * + /** * \note the default constructor doesn't fully initialize. * you have to call one of the *init functions to specialize the * histogram + * \todo merge functionality from the *init functions to the constructor and + * drop these * \todo specialize the class in a small hierarchy so that all * relevant initializations are done at build-time */ @@ -82,7 +91,17 @@ public: */ void enumInit(int last_enum); protected: - /// low-level initialize function. called by *Init high-level functions + /** low-level initialize function. called by *Init high-level functions + * \note Important restrictions on val_in and val_out functions: + * + * - val_in: ascending, defined on [0, oo), val_in(0) == 0; + * - val_out: x == val_out(val_in(x)) where val_in(x) is defined + * + * In practice, the requirements are less strict, + * but then it gets hard to define them without math notation. + * val_in is applied after offseting the value but before scaling + * See log and linear based histograms for examples + */ void init(int capacity, hbase_f * val_in, hbase_f * val_out, double min, double max); /// find what entry in the histogram corresponds to v, by applying /// the preset input transformation function @@ -100,7 +119,6 @@ protected: hbase_f *val_out; /* e.g., exp() for log based histogram */ }; -/* StatHist */ double statHistDeltaMedian(const StatHist & A, const StatHist & B); double statHistDeltaPctile(const StatHist & A, const StatHist & B, double pctile); StatHistBinDumper statHistEnumDumper; diff --git a/src/typedefs.h b/src/typedefs.h index 6b539ffcf2..438ffc85f9 100644 --- a/src/typedefs.h +++ b/src/typedefs.h @@ -142,9 +142,6 @@ typedef void HLPSONEQ(void *); typedef void HLPCMDOPTS(int *argc, char **argv); typedef void IDNSCB(void *, rfc1035_rr *, int, const char *); -typedef double hbase_f(double); -typedef void StatHistBinDumper(StoreEntry *, int idx, double val, double size, int count); - /* MD5 cache keys */ typedef unsigned char cache_key;