]> git.ipfire.org Git - thirdparty/squid.git/blame - src/StatHist.h
Boilerplate: update copyright blurbs on src/
[thirdparty/squid.git] / src / StatHist.h
CommitLineData
00a7574e 1/*
bbc27441 2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
00a7574e 3 *
bbc27441
AJ
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
00a7574e
FC
7 */
8
9#ifndef STATHIST_H_
10#define STATHIST_H_
11
a343943f
FC
12/* for StoreEntry */
13#include "Store.h"
14
a343943f
FC
15/// function signature for in/out StatHist adaptation
16typedef double hbase_f(double);
17
18/// function signature for StatHist dumping functions
19typedef void StatHistBinDumper(StoreEntry *, int idx, double val, double size, int count);
2ebbe7a4 20
2a38d231
FC
21/** Generic histogram class
22 *
a343943f 23 * see important comments on hbase_f restrictions in StatHist.cc
00a7574e 24 */
75fbab53
A
25class StatHist
26{
00a7574e 27public:
a343943f 28 /**
2a38d231
FC
29 * \note the default constructor doesn't fully initialize.
30 * you have to call one of the *init functions to specialize the
31 * histogram
a343943f
FC
32 * \todo merge functionality from the *init functions to the constructor and
33 * drop these
2a38d231
FC
34 * \todo specialize the class in a small hierarchy so that all
35 * relevant initializations are done at build-time
36 */
4541d989 37 StatHist();
978901be 38 StatHist(const StatHist&); //not needed
b1bcd7da 39 ~StatHist() { clear(); };
978901be 40
4541d989
FC
41 typedef uint64_t bins_type;
42
978901be 43 StatHist &operator=(const StatHist &);
4541d989 44
4744c727 45 /** clear the contents of the histograms
2a38d231 46 *
4744c727
FC
47 * \todo remove: this function has been replaced in its purpose
48 * by the destructor
2a38d231 49 */
2ebbe7a4 50 void clear();
4744c727
FC
51
52 /** Calculate the percentile for value pctile for the difference between
53 * this and the supplied histogram.
54 */
5eeda86d 55 double deltaPctile(const StatHist &B, double pctile) const;
ff1eb053 56
4744c727
FC
57 /** obtain the output-transformed value from the specified bin
58 *
59 */
4541d989 60 double val(unsigned int bin) const;
ff1eb053 61
4744c727
FC
62 /** increment the counter for the histogram entry
63 * associated to the supplied value
64 */
f30f7998 65 void count(double val);
ff1eb053 66
4744c727
FC
67 /** iterate the supplied bd function over the histogram values
68 */
96886986 69 void dump(StoreEntry *sentry, StatHistBinDumper * bd) const;
ff1eb053 70
4744c727 71 /** Initialize the Histogram using a logarithmic values distribution
4744c727 72 */
4541d989 73 void logInit(unsigned int capacity, double min, double max);
ff1eb053 74
4744c727 75 /** initialize the histogram to count occurrences in an enum-represented set
4744c727 76 */
4541d989 77 void enumInit(unsigned int last_enum);
ff1eb053
FC
78
79 /** Import values from another histogram
80 *
81 * \note: the two histograms MUST have the same capicity, min and max or
82 * an exception will be raised
83 */
84 StatHist &operator += (const StatHist &B);
85
97d30a2e 86protected:
a343943f
FC
87 /** low-level initialize function. called by *Init high-level functions
88 * \note Important restrictions on val_in and val_out functions:
89 *
90 * - val_in: ascending, defined on [0, oo), val_in(0) == 0;
91 * - val_out: x == val_out(val_in(x)) where val_in(x) is defined
92 *
93 * In practice, the requirements are less strict,
94 * but then it gets hard to define them without math notation.
95 * val_in is applied after offseting the value but before scaling
96 * See log and linear based histograms for examples
97 */
4541d989 98 void init(unsigned int capacity, hbase_f * val_in, hbase_f * val_out, double min, double max);
ff1eb053 99
6278cecc 100 /// find what entry in the histogram corresponds to v, by applying
101 /// the preset input transformation function
4541d989 102 unsigned int findBin(double v);
ff1eb053 103
6278cecc 104 /// the histogram counters
4541d989
FC
105 bins_type *bins;
106 unsigned int capacity_;
ff1eb053 107
6278cecc 108 /// minimum value to be stored, corresponding to the first bin
532efbb9 109 double min_;
ff1eb053 110
6278cecc 111 /// value of the maximum counter in the histogram
532efbb9 112 double max_;
ff1eb053 113
6278cecc 114 /// scaling factor when looking for a bin
532efbb9 115 double scale_;
31686d2d
FC
116 hbase_f *val_in; /* e.g., log() for log-based histogram */
117 hbase_f *val_out; /* e.g., exp() for log based histogram */
00a7574e
FC
118};
119
3888201e
FC
120double statHistDeltaMedian(const StatHist & A, const StatHist & B);
121double statHistDeltaPctile(const StatHist & A, const StatHist & B, double pctile);
3888201e
FC
122StatHistBinDumper statHistEnumDumper;
123StatHistBinDumper statHistIntDumper;
00a7574e 124
4541d989
FC
125inline StatHist&
126StatHist::operator =(const StatHist & src)
127{
128 if (this==&src) //handle self-assignment
129 return *this;
96854ae2
TT
130 if (capacity_ != src.capacity_) {
131 xfree(bins); // xfree can handle NULL pointers, no need to check
132 capacity_=src.capacity_;
133 bins = static_cast<bins_type *>(xcalloc(src.capacity_, sizeof(bins_type)));
134 }
4541d989
FC
135 min_=src.min_;
136 max_=src.max_;
137 scale_=src.scale_;
138 val_in=src.val_in;
139 val_out=src.val_out;
96854ae2
TT
140 if (bins != NULL)
141 memcpy(bins,src.bins,capacity_*sizeof(*bins));
4541d989
FC
142 return *this;
143}
144
145inline
146StatHist::StatHist() :
147 bins(NULL), capacity_(0), min_(0), max_(0),
148 scale_(1.0), val_in(NULL), val_out(NULL)
149{}
150
3fb75f92
AJ
151inline void
152StatHist::clear()
153{
154 xfree(bins); // can handle case of bins being NULL
155 bins=NULL;
156 capacity_=0; // mark as destructed, may be needed for troubleshooting
157}
158
00a7574e 159#endif /* STATHIST_H_ */