2 * Copyright (C) 1996-2018 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.
9 /* DEBUG: section 62 Generic Histogram */
17 static StatHistBinDumper statHistBinDumper
;
26 /* low level init, higher level functions has less params */
28 StatHist::init(unsigned int newCapacity
, hbase_f
* val_in_
, hbase_f
* val_out_
, double newMin
, double newMax
)
30 /* check before we divide to get scale_ */
31 assert(val_in_(newMax
- newMin
) > 0);
34 capacity_
= newCapacity
;
37 bins
= static_cast<bins_type
*>(xcalloc(capacity_
, sizeof(bins_type
)));
38 scale_
= capacity_
/ val_in(max_
- min_
);
41 StatHist::StatHist(const StatHist
&src
) :
43 capacity_(src
.capacity_
),
51 bins
= static_cast<bins_type
*>(xcalloc(src
.capacity_
, sizeof(bins_type
)));
52 memcpy(bins
,src
.bins
,capacity_
*sizeof(*bins
));
57 StatHist::count(double v
)
59 if (bins
==NULL
) //do not count before initialization or after destruction
61 const unsigned int bin
= findBin(v
);
66 StatHist::findBin(double v
)
68 v
-= min_
; /* offset */
70 if (v
<= 0.0) /* too small */
74 double tmp_bin
=floor(scale_
* val_in(v
) + 0.5);
76 if (tmp_bin
< 0.0) // should not happen
78 bin
= static_cast <unsigned int>(tmp_bin
);
80 if (bin
>= capacity_
) /* too big */
87 StatHist::val(unsigned int bin
) const
89 return val_out((double) bin
/ scale_
) + min_
;
93 statHistDeltaMedian(const StatHist
& A
, const StatHist
& B
)
95 return statHistDeltaPctile(A
, B
, 0.5);
99 statHistDeltaPctile(const StatHist
& A
, const StatHist
& B
, double pctile
)
101 return A
.deltaPctile(B
, pctile
);
105 StatHist::deltaPctile(const StatHist
& B
, double pctile
) const
113 unsigned int J
= capacity_
;
117 assert(capacity_
== B
.capacity_
);
119 int *D
= static_cast<int *>(xcalloc(capacity_
, sizeof(int)));
121 for (i
= 0; i
< capacity_
; ++i
) {
122 D
[i
] = B
.bins
[i
] - bins
[i
];
126 for (i
= 0; i
< capacity_
; ++i
)
129 h
= int(s1
* pctile
);
131 for (i
= 0; i
< capacity_
; ++i
) {
135 if (a
<= h
&& h
<= b
)
157 f
= (h
- a
) / (b
- a
);
159 K
= (unsigned int) floor(f
* (double) (J
- I
) + I
);
165 statHistBinDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
168 storeAppendPrintf(sentry
, "\t%3d/%f\t%d\t%f\n",
169 idx
, val
, count
, count
/ size
);
173 StatHist::dump(StoreEntry
* sentry
, StatHistBinDumper
* bd
) const
175 double left_border
= min_
;
178 bd
= statHistBinDumper
;
180 for (unsigned int i
= 0; i
< capacity_
; ++i
) {
181 const double right_border
= val(i
+ 1);
182 assert(right_border
- left_border
> 0.0);
183 bd(sentry
, i
, left_border
, right_border
- left_border
, bins
[i
]);
184 left_border
= right_border
;
189 StatHist::operator += (const StatHist
&B
)
191 Must(capacity_
== B
.capacity_
);
192 Must(min_
== B
.min_
);
193 Must(max_
== B
.max_
);
195 if (B
.bins
== NULL
) { // B was not yet initializted
198 if (bins
== NULL
) { // this histogram was not yet initialized
202 for (unsigned int i
= 0; i
< capacity_
; ++i
) {
203 bins
[i
] += B
.bins
[i
];
208 /* log based histogram */
212 assert((x
+ 1.0) >= 0.0);
223 StatHist::logInit(unsigned int capacity
, double min
, double max
)
225 init(capacity
, Math::Log
, Math::Exp
, min
, max
);
228 /* linear histogram for enums */
229 /* we want to be have [-1,last_enum+1] range to track out of range enums */
237 StatHist::enumInit(unsigned int last_enum
)
239 init(last_enum
+ 3, Math::Null
, Math::Null
, -1.0, (2.0 + last_enum
));
243 statHistEnumDumper(StoreEntry
* sentry
, int idx
, double val
, double, int count
)
246 storeAppendPrintf(sentry
, "%2d\t %5d\t %5d\n",
247 idx
, (int) val
, count
);
251 statHistIntDumper(StoreEntry
* sentry
, int, double val
, double, int count
)
254 storeAppendPrintf(sentry
, "%9d\t%9d\n", (int) val
, count
);