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.
38 static StatHistBinDumper statHistBinDumper
;
47 /* low level init, higher level functions has less params */
49 StatHist::init(unsigned int newCapacity
, hbase_f
* val_in_
, hbase_f
* val_out_
, double newMin
, double newMax
)
51 /* check before we divide to get scale_ */
52 assert(val_in_(newMax
- newMin
) > 0);
55 capacity_
= newCapacity
;
58 bins
= static_cast<bins_type
*>(xcalloc(capacity_
, sizeof(bins_type
)));
59 scale_
= capacity_
/ val_in(max_
- min_
);
65 for (unsigned int i
=0; i
<capacity_
; ++i
)
69 StatHist::StatHist(const StatHist
&src
) :
70 capacity_(src
.capacity_
), min_(src
.min_
), max_(src
.max_
),
71 scale_(src
.scale_
), val_in(src
.val_in
), val_out(src
.val_out
)
74 bins
= static_cast<bins_type
*>(xcalloc(src
.capacity_
, sizeof(int)));
75 memcpy(bins
,src
.bins
,capacity_
*sizeof(*bins
));
80 StatHist::count(double val
)
82 if (bins
==NULL
) //do not count before initialization or after destruction
84 const unsigned int bin
= findBin(val
);
89 StatHist::findBin(double v
)
92 v
-= min_
; /* offset */
94 if (v
<= 0.0) /* too small */
98 double tmp_bin
=floor(scale_
* val_in(v
) + 0.5);
100 if (tmp_bin
< 0.0) // should not happen
102 bin
= static_cast <unsigned int>(tmp_bin
);
104 if (bin
>= capacity_
) /* too big */
111 StatHist::val(unsigned int bin
) const
113 return val_out((double) bin
/ scale_
) + min_
;
117 statHistDeltaMedian(const StatHist
& A
, const StatHist
& B
)
119 return statHistDeltaPctile(A
, B
, 0.5);
123 statHistDeltaPctile(const StatHist
& A
, const StatHist
& B
, double pctile
)
125 return A
.deltaPctile(B
, pctile
);
129 StatHist::deltaPctile(const StatHist
& B
, double pctile
) const
137 unsigned int J
= capacity_
;
141 assert(capacity_
== B
.capacity_
);
143 int *D
= static_cast<int *>(xcalloc(capacity_
, sizeof(int)));
145 for (i
= 0; i
< capacity_
; ++i
) {
146 D
[i
] = B
.bins
[i
] - bins
[i
];
150 for (i
= 0; i
< capacity_
; ++i
)
153 h
= int(s1
* pctile
);
155 for (i
= 0; i
< capacity_
; ++i
) {
159 if (a
<= h
&& h
<= b
)
181 f
= (h
- a
) / (b
- a
);
183 K
= (unsigned int) floor(f
* (double) (J
- I
) + I
);
189 statHistBinDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
192 storeAppendPrintf(sentry
, "\t%3d/%f\t%d\t%f\n",
193 idx
, val
, count
, count
/ size
);
197 StatHist::dump(StoreEntry
* sentry
, StatHistBinDumper
* bd
) const
199 double left_border
= min_
;
202 bd
= statHistBinDumper
;
204 for (unsigned int i
= 0; i
< capacity_
; ++i
) {
205 const double right_border
= val(i
+ 1);
206 assert(right_border
- left_border
> 0.0);
207 bd(sentry
, i
, left_border
, right_border
- left_border
, bins
[i
]);
208 left_border
= right_border
;
212 /* log based histogram */
216 assert((x
+ 1.0) >= 0.0);
227 StatHist::logInit(unsigned int capacity
, double min
, double max
)
229 init(capacity
, Math::Log
, Math::Exp
, min
, max
);
232 /* linear histogram for enums */
233 /* we want to be have [-1,last_enum+1] range to track out of range enums */
241 StatHist::enumInit(unsigned int last_enum
)
243 init(last_enum
+ 3, Math::Null
, Math::Null
, -1.0, (2.0 + last_enum
));
247 statHistEnumDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
250 storeAppendPrintf(sentry
, "%2d\t %5d\t %5d\n",
251 idx
, (int) val
, count
);
255 statHistIntDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
258 storeAppendPrintf(sentry
, "%9d\t%9d\n", (int) val
, count
);