/*
- * $Id$
- *
* DEBUG: section 62 Generic Histogram
* AUTHOR: Duane Wessels
*
*
*/
-/*
- * 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;
capacity = capacity_;
val_in = val_in_;
val_out = val_out_;
- bins = (int *)xcalloc(capacity, sizeof(int));
+ bins = static_cast<int *>(xcalloc(capacity, sizeof(int)));
scale = capacity / val_in(max - min);
/* check that functions are valid */
}
}
-/* 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<int *>(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;
}
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;
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
assert(capacity == B.capacity);
- int *D = (int *)xcalloc(capacity, sizeof(int));
+ int *D = static_cast<int *>(xcalloc(capacity, sizeof(int)));
for (i = 0; i < capacity; ++i) {
D[i] = B.bins[i] - bins[i];
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
if (count)
storeAppendPrintf(sentry, "%9d\t%9d\n", (int) val, count);
}
-
/*
+ * AUTHOR: Francesco Chemolli
+ *
* SQUID Web Proxy Cache http://www.squid-cache.org/
* ----------------------------------------------------------
*
* 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
*/
*/
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
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;
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;