2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
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.
15 /// function signature for in/out StatHist adaptation
16 typedef double hbase_f(double);
18 /// function signature for StatHist dumping functions
19 typedef void StatHistBinDumper(StoreEntry
*, int idx
, double val
, double size
, int count
);
21 /** Generic histogram class
23 * see important comments on hbase_f restrictions in StatHist.cc
29 * \note the default constructor doesn't fully initialize.
30 * you have to call one of the *init functions to specialize the
32 * \todo merge functionality from the *init functions to the constructor and
34 * \todo specialize the class in a small hierarchy so that all
35 * relevant initializations are done at build-time
38 StatHist(const StatHist
&);
40 xfree(bins
); // can handle case of bins being nullptr
41 capacity_
= 0; // mark as destructed, may be needed for troubleshooting
44 typedef uint64_t bins_type
;
46 StatHist
&operator=(const StatHist
&);
48 /** Calculate the percentile for value pctile for the difference between
49 * this and the supplied histogram.
51 double deltaPctile(const StatHist
&B
, double pctile
) const;
53 /** obtain the output-transformed value from the specified bin
56 double val(unsigned int bin
) const;
58 /** increment the counter for the histogram entry
59 * associated to the supplied value
61 void count(double val
);
63 /** iterate the supplied bd function over the histogram values
65 void dump(StoreEntry
*sentry
, StatHistBinDumper
* bd
) const;
67 /** Initialize the Histogram using a logarithmic values distribution
69 void logInit(unsigned int capacity
, double min
, double max
);
71 /** initialize the histogram to count occurrences in an enum-represented set
73 void enumInit(unsigned int last_enum
);
75 /** Import values from another histogram
77 * \note: the two histograms MUST have the same capicity, min and max or
78 * an exception will be raised
80 StatHist
&operator += (const StatHist
&B
);
83 /** low-level initialize function. called by *Init high-level functions
84 * \note Important restrictions on val_in and val_out functions:
86 * - val_in: ascending, defined on [0, oo), val_in(0) == 0;
87 * - val_out: x == val_out(val_in(x)) where val_in(x) is defined
89 * In practice, the requirements are less strict,
90 * but then it gets hard to define them without math notation.
91 * val_in is applied after offseting the value but before scaling
92 * See log and linear based histograms for examples
94 void init(unsigned int capacity
, hbase_f
* val_in
, hbase_f
* val_out
, double min
, double max
);
96 /// find what entry in the histogram corresponds to v, by applying
97 /// the preset input transformation function
98 unsigned int findBin(double v
);
100 /// the histogram counters
101 bins_type
*bins
= nullptr;
102 unsigned int capacity_
= 0;
104 /// minimum value to be stored, corresponding to the first bin
107 /// value of the maximum counter in the histogram
110 /// scaling factor when looking for a bin
112 hbase_f
*val_in
= nullptr; /* e.g., log() for log-based histogram */
113 hbase_f
*val_out
= nullptr; /* e.g., exp() for log based histogram */
116 double statHistDeltaMedian(const StatHist
& A
, const StatHist
& B
);
117 double statHistDeltaPctile(const StatHist
& A
, const StatHist
& B
, double pctile
);
118 StatHistBinDumper statHistEnumDumper
;
119 StatHistBinDumper statHistIntDumper
;
122 StatHist::operator =(const StatHist
& src
)
124 if (this==&src
) //handle self-assignment
126 if (capacity_
!= src
.capacity_
) {
127 xfree(bins
); // xfree can handle NULL pointers, no need to check
128 capacity_
=src
.capacity_
;
129 bins
= static_cast<bins_type
*>(xcalloc(src
.capacity_
, sizeof(bins_type
)));
137 memcpy(bins
,src
.bins
,capacity_
*sizeof(*bins
));
141 #endif /* STATHIST_H_ */