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.
41 static StatHistBinDumper statHistBinDumper
;
50 /* low level init, higher level functions has less params */
52 StatHist::init(unsigned int newCapacity
, hbase_f
* val_in_
, hbase_f
* val_out_
, double newMin
, double newMax
)
54 /* check before we divide to get scale_ */
55 assert(val_in_(newMax
- newMin
) > 0);
58 capacity_
= newCapacity
;
61 bins
= static_cast<bins_type
*>(xcalloc(capacity_
, sizeof(bins_type
)));
62 scale_
= capacity_
/ val_in(max_
- min_
);
65 StatHist::StatHist(const StatHist
&src
) :
66 capacity_(src
.capacity_
), min_(src
.min_
), max_(src
.max_
),
67 scale_(src
.scale_
), val_in(src
.val_in
), val_out(src
.val_out
)
70 bins
= static_cast<bins_type
*>(xcalloc(src
.capacity_
, sizeof(bins_type
)));
71 memcpy(bins
,src
.bins
,capacity_
*sizeof(*bins
));
76 StatHist::count(double v
)
78 if (bins
==NULL
) //do not count before initialization or after destruction
80 const unsigned int bin
= findBin(v
);
85 StatHist::findBin(double v
)
88 v
-= min_
; /* offset */
90 if (v
<= 0.0) /* too small */
94 double tmp_bin
=floor(scale_
* val_in(v
) + 0.5);
96 if (tmp_bin
< 0.0) // should not happen
98 bin
= static_cast <unsigned int>(tmp_bin
);
100 if (bin
>= capacity_
) /* too big */
107 StatHist::val(unsigned int bin
) const
109 return val_out((double) bin
/ scale_
) + min_
;
113 statHistDeltaMedian(const StatHist
& A
, const StatHist
& B
)
115 return statHistDeltaPctile(A
, B
, 0.5);
119 statHistDeltaPctile(const StatHist
& A
, const StatHist
& B
, double pctile
)
121 return A
.deltaPctile(B
, pctile
);
125 StatHist::deltaPctile(const StatHist
& B
, double pctile
) const
133 unsigned int J
= capacity_
;
137 assert(capacity_
== B
.capacity_
);
139 int *D
= static_cast<int *>(xcalloc(capacity_
, sizeof(int)));
141 for (i
= 0; i
< capacity_
; ++i
) {
142 D
[i
] = B
.bins
[i
] - bins
[i
];
146 for (i
= 0; i
< capacity_
; ++i
)
149 h
= int(s1
* pctile
);
151 for (i
= 0; i
< capacity_
; ++i
) {
155 if (a
<= h
&& h
<= b
)
177 f
= (h
- a
) / (b
- a
);
179 K
= (unsigned int) floor(f
* (double) (J
- I
) + I
);
185 statHistBinDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
188 storeAppendPrintf(sentry
, "\t%3d/%f\t%d\t%f\n",
189 idx
, val
, count
, count
/ size
);
193 StatHist::dump(StoreEntry
* sentry
, StatHistBinDumper
* bd
) const
195 double left_border
= min_
;
198 bd
= statHistBinDumper
;
200 for (unsigned int i
= 0; i
< capacity_
; ++i
) {
201 const double right_border
= val(i
+ 1);
202 assert(right_border
- left_border
> 0.0);
203 bd(sentry
, i
, left_border
, right_border
- left_border
, bins
[i
]);
204 left_border
= right_border
;
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 size
, int count
)
246 storeAppendPrintf(sentry
, "%2d\t %5d\t %5d\n",
247 idx
, (int) val
, count
);
251 statHistIntDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
254 storeAppendPrintf(sentry
, "%9d\t%9d\n", (int) val
, count
);