]> git.ipfire.org Git - thirdparty/squid.git/blob - src/StatHist.h
Merge StatHist feature-branch:
[thirdparty/squid.git] / src / StatHist.h
1 /*
2 * AUTHOR: Francesco Chemolli
3 *
4 * SQUID Web Proxy Cache http://www.squid-cache.org/
5 * ----------------------------------------------------------
6 *
7 * Squid is the result of efforts by numerous individuals from
8 * the Internet community; see the CONTRIBUTORS file for full
9 * details. Many organizations have provided support for Squid's
10 * development; see the SPONSORS file for full details. Squid is
11 * Copyrighted (C) 2001 by the Regents of the University of
12 * California; see the COPYRIGHT file for full details. Squid
13 * incorporates software developed and/or copyrighted by other
14 * sources; see the CREDITS file for full details.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
29 */
30
31 #ifndef STATHIST_H_
32 #define STATHIST_H_
33
34 /* for StoreEntry */
35 #include "Store.h"
36
37
38 /// function signature for in/out StatHist adaptation
39 typedef double hbase_f(double);
40
41 /// function signature for StatHist dumping functions
42 typedef void StatHistBinDumper(StoreEntry *, int idx, double val, double size, int count);
43
44 /** Generic histogram class
45 *
46 * see important comments on hbase_f restrictions in StatHist.cc
47 */
48 class StatHist {
49 public:
50 /**
51 * \note the default constructor doesn't fully initialize.
52 * you have to call one of the *init functions to specialize the
53 * histogram
54 * \todo merge functionality from the *init functions to the constructor and
55 * drop these
56 * \todo specialize the class in a small hierarchy so that all
57 * relevant initializations are done at build-time
58 */
59 StatHist() : scale_(1.0) {};
60 StatHist(const StatHist&);
61 StatHist &operator=(const StatHist &);
62 virtual ~StatHist();
63 /** clear the contents of the histograms
64 *
65 * \todo remove: this function has been replaced in its purpose
66 * by the destructor
67 */
68 void clear();
69
70 /** Calculate the percentile for value pctile for the difference between
71 * this and the supplied histogram.
72 */
73 double deltaPctile(const StatHist &B, double pctile) const;
74 /** obtain the output-transformed value from the specified bin
75 *
76 */
77 double val(int bin) const;
78 /** increment the counter for the histogram entry
79 * associated to the supplied value
80 */
81 void count(double val);
82 /** iterate the supplied bd function over the histogram values
83 */
84 void dump(StoreEntry *sentry, StatHistBinDumper * bd) const;
85 /** Initialize the Histogram using a logarithmic values distribution
86 *
87 */
88 void logInit(int capacity, double min, double max);
89 /** initialize the histogram to count occurrences in an enum-represented set
90 *
91 */
92 void enumInit(int last_enum);
93 protected:
94 /** low-level initialize function. called by *Init high-level functions
95 * \note Important restrictions on val_in and val_out functions:
96 *
97 * - val_in: ascending, defined on [0, oo), val_in(0) == 0;
98 * - val_out: x == val_out(val_in(x)) where val_in(x) is defined
99 *
100 * In practice, the requirements are less strict,
101 * but then it gets hard to define them without math notation.
102 * val_in is applied after offseting the value but before scaling
103 * See log and linear based histograms for examples
104 */
105 void init(int capacity, hbase_f * val_in, hbase_f * val_out, double min, double max);
106 /// find what entry in the histogram corresponds to v, by applying
107 /// the preset input transformation function
108 int findBin(double v);
109 /// the histogram counters
110 int *bins;
111 int capacity_;
112 /// minimum value to be stored, corresponding to the first bin
113 double min_;
114 /// value of the maximum counter in the histogram
115 double max_;
116 /// scaling factor when looking for a bin
117 double scale_;
118 hbase_f *val_in; /* e.g., log() for log-based histogram */
119 hbase_f *val_out; /* e.g., exp() for log based histogram */
120 };
121
122 double statHistDeltaMedian(const StatHist & A, const StatHist & B);
123 double statHistDeltaPctile(const StatHist & A, const StatHist & B, double pctile);
124 StatHistBinDumper statHistEnumDumper;
125 StatHistBinDumper statHistIntDumper;
126
127 #endif /* STATHIST_H_ */