blob: 425421c5b97b626921ab39d9b77f3d17943124a4 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
3 * Copyright 2014, Google Inc.
4 * 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));
80 GPR_ASSERT(bucket >= 0);
81 GPR_ASSERT(bucket < h->num_buckets);
82 return bucket;
83}
84
85/* at what value does a bucket start? */
86static double bucket_start(gpr_histogram *h, double x) {
87 return pow(h->multiplier, x);
88}
89
90gpr_histogram *gpr_histogram_create(double resolution,
91 double max_bucket_start) {
92 gpr_histogram *h = gpr_malloc(sizeof(gpr_histogram));
93 GPR_ASSERT(resolution > 0.0);
94 GPR_ASSERT(max_bucket_start > resolution);
95 h->sum = 0.0;
96 h->sum_of_squares = 0.0;
97 h->multiplier = 1.0 + resolution;
98 h->one_on_log_multiplier = 1.0 / log(1.0 + resolution);
99 h->max_possible = max_bucket_start;
100 h->count = 0.0;
101 h->min_seen = max_bucket_start;
102 h->max_seen = 0.0;
103 h->num_buckets = bucket_for_unchecked(h, max_bucket_start) + 1;
104 GPR_ASSERT(h->num_buckets > 1);
105 GPR_ASSERT(h->num_buckets < 100000000);
106 h->buckets = gpr_malloc(sizeof(gpr_uint32) * h->num_buckets);
107 memset(h->buckets, 0, sizeof(gpr_uint32) * h->num_buckets);
108 return h;
109}
110
111void gpr_histogram_destroy(gpr_histogram *h) {
112 gpr_free(h->buckets);
113 gpr_free(h);
114}
115
116void gpr_histogram_add(gpr_histogram *h, double x) {
117 h->sum += x;
118 h->sum_of_squares += x * x;
119 h->count++;
120 if (x < h->min_seen) {
121 h->min_seen = x;
122 }
123 if (x > h->max_seen) {
124 h->max_seen = x;
125 }
126 h->buckets[bucket_for(h, x)]++;
127}
128
129int gpr_histogram_merge(gpr_histogram *dst, gpr_histogram *src) {
jtattermusch98bffb72014-12-09 12:47:19 -0800130 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800131 if ((dst->num_buckets != src->num_buckets) ||
132 (dst->multiplier != src->multiplier)) {
133 /* Fail because these histograms don't match */
134 return 0;
135 }
136 dst->sum += src->sum;
137 dst->sum_of_squares += src->sum_of_squares;
138 dst->count += src->count;
139 if (src->min_seen < dst->min_seen) {
140 dst->min_seen = src->min_seen;
141 }
142 if (src->max_seen > dst->max_seen) {
143 dst->max_seen = src->max_seen;
144 }
145 for (i = 0; i < dst->num_buckets; i++) {
146 dst->buckets[i] += src->buckets[i];
147 }
148 return 1;
149}
150
151static double threshold_for_count_below(gpr_histogram *h, double count_below) {
152 double count_so_far;
153 double lower_bound;
154 double upper_bound;
jtattermusch98bffb72014-12-09 12:47:19 -0800155 size_t lower_idx;
156 size_t upper_idx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800157
158 GPR_ASSERT(h->count >= 1);
159
160 if (count_below <= 0) {
161 return h->min_seen;
162 }
163 if (count_below >= h->count) {
164 return h->max_seen;
165 }
166
167 /* find the lowest bucket that gets us above count_below */
168 count_so_far = 0.0;
169 for (lower_idx = 0; lower_idx < h->num_buckets; lower_idx++) {
170 count_so_far += h->buckets[lower_idx];
171 if (count_so_far >= count_below) {
172 break;
173 }
174 }
175 if (count_so_far == count_below) {
176 /* this bucket hits the threshold exactly... we should be midway through
177 any run of zero values following the bucket */
178 for (upper_idx = lower_idx + 1; upper_idx < h->num_buckets; upper_idx++) {
179 if (h->buckets[upper_idx]) {
180 break;
181 }
182 }
183 return (bucket_start(h, lower_idx) + bucket_start(h, upper_idx)) / 2.0;
184 } else {
185 /* treat values as uniform throughout the bucket, and find where this value
186 should lie */
187 lower_bound = bucket_start(h, lower_idx);
188 upper_bound = bucket_start(h, lower_idx + 1);
Yang Gao5fd0d292015-01-26 00:19:48 -0800189 return GPR_CLAMP(upper_bound - (upper_bound - lower_bound) *
190 (count_so_far - count_below) /
191 h->buckets[lower_idx],
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800192 h->min_seen, h->max_seen);
193 }
194}
195
196double gpr_histogram_percentile(gpr_histogram *h, double percentile) {
197 return threshold_for_count_below(h, h->count * percentile / 100.0);
198}
199
200double gpr_histogram_mean(gpr_histogram *h) {
201 GPR_ASSERT(h->count);
202 return h->sum / h->count;
203}
204
205double gpr_histogram_stddev(gpr_histogram *h) {
206 return sqrt(gpr_histogram_variance(h));
207}
208
209double gpr_histogram_variance(gpr_histogram *h) {
210 if (h->count == 0) return 0.0;
211 return (h->sum_of_squares * h->count - h->sum * h->sum) /
212 (h->count * h->count);
213}
214
215double gpr_histogram_maximum(gpr_histogram *h) { return h->max_seen; }
216
217double gpr_histogram_minimum(gpr_histogram *h) { return h->min_seen; }
218
219double gpr_histogram_count(gpr_histogram *h) { return h->count; }
220
221double gpr_histogram_sum(gpr_histogram *h) { return h->sum; }
222
223double gpr_histogram_sum_of_squares(gpr_histogram *h) {
224 return h->sum_of_squares;
225}