blob: 7b016bbc78790d760b84067f38157ea1840e4a6b [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * 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>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041#include <grpc/support/log.h>
Craig Tillerf40df232016-03-25 13:38:14 -070042#include <grpc/support/port_platform.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043#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 */
Craig Tiller7536af02015-12-22 13:49:30 -080069 uint32_t *buckets;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080070};
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) {
vjpai6db72242015-04-21 10:50:12 -070079 size_t bucket = bucket_for_unchecked(h, GPR_CLAMP(x, 1.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);
Craig Tiller7536af02015-12-22 13:49:30 -0800105 h->buckets = gpr_malloc(sizeof(uint32_t) * h->num_buckets);
106 memset(h->buckets, 0, sizeof(uint32_t) * h->num_buckets);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800107 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
vjpai119c1032015-10-29 01:21:04 -0700128int gpr_histogram_merge(gpr_histogram *dst, const 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
Craig Tiller7536af02015-12-22 13:49:30 -0800140void gpr_histogram_merge_contents(gpr_histogram *dst, const uint32_t *data,
Craig Tiller76877c32015-03-03 16:04:23 -0800141 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
Craig Tiller80ca5162015-06-22 14:31:44 -0700167 if (h->count == 0) {
168 return 0.0;
169 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800170
171 if (count_below <= 0) {
172 return h->min_seen;
173 }
174 if (count_below >= h->count) {
175 return h->max_seen;
176 }
177
178 /* find the lowest bucket that gets us above count_below */
179 count_so_far = 0.0;
180 for (lower_idx = 0; lower_idx < h->num_buckets; lower_idx++) {
181 count_so_far += h->buckets[lower_idx];
182 if (count_so_far >= count_below) {
183 break;
184 }
185 }
186 if (count_so_far == count_below) {
187 /* this bucket hits the threshold exactly... we should be midway through
188 any run of zero values following the bucket */
189 for (upper_idx = lower_idx + 1; upper_idx < h->num_buckets; upper_idx++) {
190 if (h->buckets[upper_idx]) {
191 break;
192 }
193 }
Craig Tillerd6c98df2015-08-18 09:33:44 -0700194 return (bucket_start(h, (double)lower_idx) +
195 bucket_start(h, (double)upper_idx)) /
196 2.0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800197 } else {
198 /* treat values as uniform throughout the bucket, and find where this value
199 should lie */
murgatroid995e71d7a2015-06-19 12:24:44 -0700200 lower_bound = bucket_start(h, (double)lower_idx);
201 upper_bound = bucket_start(h, (double)(lower_idx + 1));
Craig Tillerd6c98df2015-08-18 09:33:44 -0700202 return GPR_CLAMP(upper_bound -
203 (upper_bound - lower_bound) *
204 (count_so_far - count_below) /
205 h->buckets[lower_idx],
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800206 h->min_seen, h->max_seen);
207 }
208}
209
210double gpr_histogram_percentile(gpr_histogram *h, double percentile) {
211 return threshold_for_count_below(h, h->count * percentile / 100.0);
212}
213
214double gpr_histogram_mean(gpr_histogram *h) {
sreek7d3ea592015-09-29 14:12:44 -0700215 GPR_ASSERT(h->count != 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800216 return h->sum / h->count;
217}
218
219double gpr_histogram_stddev(gpr_histogram *h) {
220 return sqrt(gpr_histogram_variance(h));
221}
222
223double gpr_histogram_variance(gpr_histogram *h) {
224 if (h->count == 0) return 0.0;
225 return (h->sum_of_squares * h->count - h->sum * h->sum) /
226 (h->count * h->count);
227}
228
229double gpr_histogram_maximum(gpr_histogram *h) { return h->max_seen; }
230
231double gpr_histogram_minimum(gpr_histogram *h) { return h->min_seen; }
232
233double gpr_histogram_count(gpr_histogram *h) { return h->count; }
234
235double gpr_histogram_sum(gpr_histogram *h) { return h->sum; }
236
237double gpr_histogram_sum_of_squares(gpr_histogram *h) {
238 return h->sum_of_squares;
Craig Tiller190d3602015-02-18 09:23:38 -0800239}
Craig Tiller76877c32015-03-03 16:04:23 -0800240
Craig Tiller7536af02015-12-22 13:49:30 -0800241const uint32_t *gpr_histogram_get_contents(gpr_histogram *h, size_t *size) {
Craig Tiller76877c32015-03-03 16:04:23 -0800242 *size = h->num_buckets;
243 return h->buckets;
Craig Tillerf2825142015-03-03 17:15:36 -0800244}