blob: 2d52786ce52d629bdd3a261b19c4fb09e8342cdd [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include <grpc/support/histogram.h>
35
36#include <math.h>
37#include <stddef.h>
38#include <string.h>
39
40#include <grpc/support/alloc.h>
41#include <grpc/support/port_platform.h>
42#include <grpc/support/log.h>
43#include <grpc/support/useful.h>
44
45/* Histograms are stored with exponentially increasing bucket sizes.
46 The first bucket is [0, m) where m = 1 + resolution
47 Bucket n (n>=1) contains [m**n, m**(n+1))
48 There are sufficient buckets to reach max_bucket_start */
49
50struct gpr_histogram {
51 /* Sum of all values seen so far */
52 double sum;
53 /* Sum of squares of all values seen so far */
54 double sum_of_squares;
55 /* number of values seen so far */
56 double count;
57 /* m in the description */
58 double multiplier;
59 double one_on_log_multiplier;
60 /* minimum value seen */
61 double min_seen;
62 /* maximum value seen */
63 double max_seen;
64 /* maximum representable value */
65 double max_possible;
66 /* number of buckets */
67 size_t num_buckets;
68 /* the buckets themselves */
69 gpr_uint32 *buckets;
70};
71
72/* determine a bucket index given a value - does no bounds checking */
73static size_t bucket_for_unchecked(gpr_histogram *h, double x) {
74 return (size_t)(log(x) * h->one_on_log_multiplier);
75}
76
77/* bounds checked version of the above */
78static size_t bucket_for(gpr_histogram *h, double x) {
79 size_t bucket = bucket_for_unchecked(h, GPR_CLAMP(x, 0, h->max_possible));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080080 GPR_ASSERT(bucket < h->num_buckets);
81 return bucket;
82}
83
84/* at what value does a bucket start? */
85static double bucket_start(gpr_histogram *h, double x) {
86 return pow(h->multiplier, x);
87}
88
89gpr_histogram *gpr_histogram_create(double resolution,
90 double max_bucket_start) {
91 gpr_histogram *h = gpr_malloc(sizeof(gpr_histogram));
92 GPR_ASSERT(resolution > 0.0);
93 GPR_ASSERT(max_bucket_start > resolution);
94 h->sum = 0.0;
95 h->sum_of_squares = 0.0;
96 h->multiplier = 1.0 + resolution;
97 h->one_on_log_multiplier = 1.0 / log(1.0 + resolution);
98 h->max_possible = max_bucket_start;
99 h->count = 0.0;
100 h->min_seen = max_bucket_start;
101 h->max_seen = 0.0;
102 h->num_buckets = bucket_for_unchecked(h, max_bucket_start) + 1;
103 GPR_ASSERT(h->num_buckets > 1);
104 GPR_ASSERT(h->num_buckets < 100000000);
105 h->buckets = gpr_malloc(sizeof(gpr_uint32) * h->num_buckets);
106 memset(h->buckets, 0, sizeof(gpr_uint32) * h->num_buckets);
107 return h;
108}
109
110void gpr_histogram_destroy(gpr_histogram *h) {
111 gpr_free(h->buckets);
112 gpr_free(h);
113}
114
115void gpr_histogram_add(gpr_histogram *h, double x) {
116 h->sum += x;
117 h->sum_of_squares += x * x;
118 h->count++;
119 if (x < h->min_seen) {
120 h->min_seen = x;
121 }
122 if (x > h->max_seen) {
123 h->max_seen = x;
124 }
125 h->buckets[bucket_for(h, x)]++;
126}
127
128int gpr_histogram_merge(gpr_histogram *dst, gpr_histogram *src) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800129 if ((dst->num_buckets != src->num_buckets) ||
130 (dst->multiplier != src->multiplier)) {
131 /* Fail because these histograms don't match */
132 return 0;
133 }
Craig Tiller76877c32015-03-03 16:04:23 -0800134 gpr_histogram_merge_contents(dst, src->buckets, src->num_buckets,
135 src->min_seen, src->max_seen, src->sum,
136 src->sum_of_squares, src->count);
137 return 1;
138}
139
140void gpr_histogram_merge_contents(gpr_histogram *dst, const gpr_uint32 *data,
141 size_t data_count, double min_seen,
142 double max_seen, double sum,
143 double sum_of_squares, double count) {
144 size_t i;
145 GPR_ASSERT(dst->num_buckets == data_count);
146 dst->sum += sum;
147 dst->sum_of_squares += sum_of_squares;
148 dst->count += count;
149 if (min_seen < dst->min_seen) {
150 dst->min_seen = min_seen;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800151 }
Craig Tiller76877c32015-03-03 16:04:23 -0800152 if (max_seen > dst->max_seen) {
153 dst->max_seen = max_seen;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800154 }
155 for (i = 0; i < dst->num_buckets; i++) {
Craig Tiller76877c32015-03-03 16:04:23 -0800156 dst->buckets[i] += data[i];
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800157 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800158}
159
160static double threshold_for_count_below(gpr_histogram *h, double count_below) {
161 double count_so_far;
162 double lower_bound;
163 double upper_bound;
jtattermusch98bffb72014-12-09 12:47:19 -0800164 size_t lower_idx;
165 size_t upper_idx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800166
167 GPR_ASSERT(h->count >= 1);
168
169 if (count_below <= 0) {
170 return h->min_seen;
171 }
172 if (count_below >= h->count) {
173 return h->max_seen;
174 }
175
176 /* find the lowest bucket that gets us above count_below */
177 count_so_far = 0.0;
178 for (lower_idx = 0; lower_idx < h->num_buckets; lower_idx++) {
179 count_so_far += h->buckets[lower_idx];
180 if (count_so_far >= count_below) {
181 break;
182 }
183 }
184 if (count_so_far == count_below) {
185 /* this bucket hits the threshold exactly... we should be midway through
186 any run of zero values following the bucket */
187 for (upper_idx = lower_idx + 1; upper_idx < h->num_buckets; upper_idx++) {
188 if (h->buckets[upper_idx]) {
189 break;
190 }
191 }
192 return (bucket_start(h, lower_idx) + bucket_start(h, upper_idx)) / 2.0;
193 } else {
194 /* treat values as uniform throughout the bucket, and find where this value
195 should lie */
196 lower_bound = bucket_start(h, lower_idx);
197 upper_bound = bucket_start(h, lower_idx + 1);
Yang Gao5fd0d292015-01-26 00:19:48 -0800198 return GPR_CLAMP(upper_bound - (upper_bound - lower_bound) *
199 (count_so_far - count_below) /
200 h->buckets[lower_idx],
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800201 h->min_seen, h->max_seen);
202 }
203}
204
205double gpr_histogram_percentile(gpr_histogram *h, double percentile) {
206 return threshold_for_count_below(h, h->count * percentile / 100.0);
207}
208
209double gpr_histogram_mean(gpr_histogram *h) {
210 GPR_ASSERT(h->count);
211 return h->sum / h->count;
212}
213
214double gpr_histogram_stddev(gpr_histogram *h) {
215 return sqrt(gpr_histogram_variance(h));
216}
217
218double gpr_histogram_variance(gpr_histogram *h) {
219 if (h->count == 0) return 0.0;
220 return (h->sum_of_squares * h->count - h->sum * h->sum) /
221 (h->count * h->count);
222}
223
224double gpr_histogram_maximum(gpr_histogram *h) { return h->max_seen; }
225
226double gpr_histogram_minimum(gpr_histogram *h) { return h->min_seen; }
227
228double gpr_histogram_count(gpr_histogram *h) { return h->count; }
229
230double gpr_histogram_sum(gpr_histogram *h) { return h->sum; }
231
232double gpr_histogram_sum_of_squares(gpr_histogram *h) {
233 return h->sum_of_squares;
Craig Tiller190d3602015-02-18 09:23:38 -0800234}
Craig Tiller76877c32015-03-03 16:04:23 -0800235
236const gpr_uint32 *gpr_histogram_get_contents(gpr_histogram *h, size_t *size) {
237 *size = h->num_buckets;
238 return h->buckets;
239}