2 * Copyright (C) 1996-2014 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
) :
42 capacity_(src
.capacity_
), min_(src
.min_
), max_(src
.max_
),
43 scale_(src
.scale_
), val_in(src
.val_in
), val_out(src
.val_out
)
46 bins
= static_cast<bins_type
*>(xcalloc(src
.capacity_
, sizeof(bins_type
)));
47 memcpy(bins
,src
.bins
,capacity_
*sizeof(*bins
));
52 StatHist::count(double v
)
54 if (bins
==NULL
) //do not count before initialization or after destruction
56 const unsigned int bin
= findBin(v
);
61 StatHist::findBin(double v
)
64 v
-= min_
; /* offset */
66 if (v
<= 0.0) /* too small */
70 double tmp_bin
=floor(scale_
* val_in(v
) + 0.5);
72 if (tmp_bin
< 0.0) // should not happen
74 bin
= static_cast <unsigned int>(tmp_bin
);
76 if (bin
>= capacity_
) /* too big */
83 StatHist::val(unsigned int bin
) const
85 return val_out((double) bin
/ scale_
) + min_
;
89 statHistDeltaMedian(const StatHist
& A
, const StatHist
& B
)
91 return statHistDeltaPctile(A
, B
, 0.5);
95 statHistDeltaPctile(const StatHist
& A
, const StatHist
& B
, double pctile
)
97 return A
.deltaPctile(B
, pctile
);
101 StatHist::deltaPctile(const StatHist
& B
, double pctile
) const
109 unsigned int J
= capacity_
;
113 assert(capacity_
== B
.capacity_
);
115 int *D
= static_cast<int *>(xcalloc(capacity_
, sizeof(int)));
117 for (i
= 0; i
< capacity_
; ++i
) {
118 D
[i
] = B
.bins
[i
] - bins
[i
];
122 for (i
= 0; i
< capacity_
; ++i
)
125 h
= int(s1
* pctile
);
127 for (i
= 0; i
< capacity_
; ++i
) {
131 if (a
<= h
&& h
<= b
)
153 f
= (h
- a
) / (b
- a
);
155 K
= (unsigned int) floor(f
* (double) (J
- I
) + I
);
161 statHistBinDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
164 storeAppendPrintf(sentry
, "\t%3d/%f\t%d\t%f\n",
165 idx
, val
, count
, count
/ size
);
169 StatHist::dump(StoreEntry
* sentry
, StatHistBinDumper
* bd
) const
171 double left_border
= min_
;
174 bd
= statHistBinDumper
;
176 for (unsigned int i
= 0; i
< capacity_
; ++i
) {
177 const double right_border
= val(i
+ 1);
178 assert(right_border
- left_border
> 0.0);
179 bd(sentry
, i
, left_border
, right_border
- left_border
, bins
[i
]);
180 left_border
= right_border
;
185 StatHist::operator += (const StatHist
&B
)
187 Must(capacity_
== B
.capacity_
);
188 Must(min_
== B
.min_
);
189 Must(max_
== B
.max_
);
191 if (B
.bins
== NULL
) { // B was not yet initializted
194 if (bins
== NULL
) { // this histogram was not yet initialized
198 for (unsigned int i
= 0; i
< capacity_
; ++i
) {
199 bins
[i
] += B
.bins
[i
];
204 /* log based histogram */
208 assert((x
+ 1.0) >= 0.0);
219 StatHist::logInit(unsigned int capacity
, double min
, double max
)
221 init(capacity
, Math::Log
, Math::Exp
, min
, max
);
224 /* linear histogram for enums */
225 /* we want to be have [-1,last_enum+1] range to track out of range enums */
233 StatHist::enumInit(unsigned int last_enum
)
235 init(last_enum
+ 3, Math::Null
, Math::Null
, -1.0, (2.0 + last_enum
));
239 statHistEnumDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
242 storeAppendPrintf(sentry
, "%2d\t %5d\t %5d\n",
243 idx
, (int) val
, count
);
247 statHistIntDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
250 storeAppendPrintf(sentry
, "%9d\t%9d\n", (int) val
, count
);