3 * DEBUG: section 62 Generic Histogram
4 * AUTHOR: Duane Wessels
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
40 static StatHistBinDumper statHistBinDumper
;
49 /* low level init, higher level functions has less params */
51 StatHist::init(unsigned int newCapacity
, hbase_f
* val_in_
, hbase_f
* val_out_
, double newMin
, double newMax
)
53 /* check before we divide to get scale_ */
54 assert(val_in_(newMax
- newMin
) > 0);
57 capacity_
= newCapacity
;
60 bins
= static_cast<bins_type
*>(xcalloc(capacity_
, sizeof(bins_type
)));
61 scale_
= capacity_
/ val_in(max_
- min_
);
64 StatHist::StatHist(const StatHist
&src
) :
65 capacity_(src
.capacity_
), min_(src
.min_
), max_(src
.max_
),
66 scale_(src
.scale_
), val_in(src
.val_in
), val_out(src
.val_out
)
69 bins
= static_cast<bins_type
*>(xcalloc(src
.capacity_
, sizeof(bins_type
)));
70 memcpy(bins
,src
.bins
,capacity_
*sizeof(*bins
));
75 StatHist::count(double v
)
77 if (bins
==NULL
) //do not count before initialization or after destruction
79 const unsigned int bin
= findBin(v
);
84 StatHist::findBin(double v
)
87 v
-= min_
; /* offset */
89 if (v
<= 0.0) /* too small */
93 double tmp_bin
=floor(scale_
* val_in(v
) + 0.5);
95 if (tmp_bin
< 0.0) // should not happen
97 bin
= static_cast <unsigned int>(tmp_bin
);
99 if (bin
>= capacity_
) /* too big */
106 StatHist::val(unsigned int bin
) const
108 return val_out((double) bin
/ scale_
) + min_
;
112 statHistDeltaMedian(const StatHist
& A
, const StatHist
& B
)
114 return statHistDeltaPctile(A
, B
, 0.5);
118 statHistDeltaPctile(const StatHist
& A
, const StatHist
& B
, double pctile
)
120 return A
.deltaPctile(B
, pctile
);
124 StatHist::deltaPctile(const StatHist
& B
, double pctile
) const
132 unsigned int J
= capacity_
;
136 assert(capacity_
== B
.capacity_
);
138 int *D
= static_cast<int *>(xcalloc(capacity_
, sizeof(int)));
140 for (i
= 0; i
< capacity_
; ++i
) {
141 D
[i
] = B
.bins
[i
] - bins
[i
];
145 for (i
= 0; i
< capacity_
; ++i
)
148 h
= int(s1
* pctile
);
150 for (i
= 0; i
< capacity_
; ++i
) {
154 if (a
<= h
&& h
<= b
)
176 f
= (h
- a
) / (b
- a
);
178 K
= (unsigned int) floor(f
* (double) (J
- I
) + I
);
184 statHistBinDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
187 storeAppendPrintf(sentry
, "\t%3d/%f\t%d\t%f\n",
188 idx
, val
, count
, count
/ size
);
192 StatHist::dump(StoreEntry
* sentry
, StatHistBinDumper
* bd
) const
194 double left_border
= min_
;
197 bd
= statHistBinDumper
;
199 for (unsigned int i
= 0; i
< capacity_
; ++i
) {
200 const double right_border
= val(i
+ 1);
201 assert(right_border
- left_border
> 0.0);
202 bd(sentry
, i
, left_border
, right_border
- left_border
, bins
[i
]);
203 left_border
= right_border
;
208 StatHist::operator += (const StatHist
&B
)
210 Must(capacity_
== B
.capacity_
);
211 Must(min_
== B
.min_
);
212 Must(max_
== B
.max_
);
214 if (B
.bins
== NULL
) { // B was not yet initializted
217 if (bins
== NULL
) { // this histogram was not yet initialized
221 for (unsigned int i
= 0; i
< capacity_
; ++i
) {
222 bins
[i
] += B
.bins
[i
];
227 /* log based histogram */
231 assert((x
+ 1.0) >= 0.0);
242 StatHist::logInit(unsigned int capacity
, double min
, double max
)
244 init(capacity
, Math::Log
, Math::Exp
, min
, max
);
247 /* linear histogram for enums */
248 /* we want to be have [-1,last_enum+1] range to track out of range enums */
256 StatHist::enumInit(unsigned int last_enum
)
258 init(last_enum
+ 3, Math::Null
, Math::Null
, -1.0, (2.0 + last_enum
));
262 statHistEnumDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
265 storeAppendPrintf(sentry
, "%2d\t %5d\t %5d\n",
266 idx
, (int) val
, count
);
270 statHistIntDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
273 storeAppendPrintf(sentry
, "%9d\t%9d\n", (int) val
, count
);