Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | #include <grpc/support/log.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 42 | #include <grpc/support/port_platform.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
| 50 | struct 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 Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 69 | uint32_t *buckets; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | /* determine a bucket index given a value - does no bounds checking */ |
| 73 | static 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 */ |
| 78 | static size_t bucket_for(gpr_histogram *h, double x) { |
vjpai | 6db7224 | 2015-04-21 10:50:12 -0700 | [diff] [blame] | 79 | size_t bucket = bucket_for_unchecked(h, GPR_CLAMP(x, 1.0, h->max_possible)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 80 | GPR_ASSERT(bucket < h->num_buckets); |
| 81 | return bucket; |
| 82 | } |
| 83 | |
| 84 | /* at what value does a bucket start? */ |
| 85 | static double bucket_start(gpr_histogram *h, double x) { |
| 86 | return pow(h->multiplier, x); |
| 87 | } |
| 88 | |
| 89 | gpr_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 Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 105 | h->buckets = gpr_malloc(sizeof(uint32_t) * h->num_buckets); |
| 106 | memset(h->buckets, 0, sizeof(uint32_t) * h->num_buckets); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 107 | return h; |
| 108 | } |
| 109 | |
| 110 | void gpr_histogram_destroy(gpr_histogram *h) { |
| 111 | gpr_free(h->buckets); |
| 112 | gpr_free(h); |
| 113 | } |
| 114 | |
| 115 | void 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 | |
vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 128 | int gpr_histogram_merge(gpr_histogram *dst, const gpr_histogram *src) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 129 | 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 Tiller | 76877c3 | 2015-03-03 16:04:23 -0800 | [diff] [blame] | 134 | 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 Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 140 | void gpr_histogram_merge_contents(gpr_histogram *dst, const uint32_t *data, |
Craig Tiller | 76877c3 | 2015-03-03 16:04:23 -0800 | [diff] [blame] | 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 151 | } |
Craig Tiller | 76877c3 | 2015-03-03 16:04:23 -0800 | [diff] [blame] | 152 | if (max_seen > dst->max_seen) { |
| 153 | dst->max_seen = max_seen; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 154 | } |
| 155 | for (i = 0; i < dst->num_buckets; i++) { |
Craig Tiller | 76877c3 | 2015-03-03 16:04:23 -0800 | [diff] [blame] | 156 | dst->buckets[i] += data[i]; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 157 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static double threshold_for_count_below(gpr_histogram *h, double count_below) { |
| 161 | double count_so_far; |
| 162 | double lower_bound; |
| 163 | double upper_bound; |
jtattermusch | 98bffb7 | 2014-12-09 12:47:19 -0800 | [diff] [blame] | 164 | size_t lower_idx; |
| 165 | size_t upper_idx; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 166 | |
Craig Tiller | 80ca516 | 2015-06-22 14:31:44 -0700 | [diff] [blame] | 167 | if (h->count == 0) { |
| 168 | return 0.0; |
| 169 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 170 | |
| 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 Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 194 | return (bucket_start(h, (double)lower_idx) + |
| 195 | bucket_start(h, (double)upper_idx)) / |
| 196 | 2.0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 197 | } else { |
| 198 | /* treat values as uniform throughout the bucket, and find where this value |
| 199 | should lie */ |
murgatroid99 | 5e71d7a | 2015-06-19 12:24:44 -0700 | [diff] [blame] | 200 | lower_bound = bucket_start(h, (double)lower_idx); |
| 201 | upper_bound = bucket_start(h, (double)(lower_idx + 1)); |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 202 | return GPR_CLAMP(upper_bound - |
| 203 | (upper_bound - lower_bound) * |
| 204 | (count_so_far - count_below) / |
| 205 | h->buckets[lower_idx], |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 206 | h->min_seen, h->max_seen); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | double gpr_histogram_percentile(gpr_histogram *h, double percentile) { |
| 211 | return threshold_for_count_below(h, h->count * percentile / 100.0); |
| 212 | } |
| 213 | |
| 214 | double gpr_histogram_mean(gpr_histogram *h) { |
sreek | 7d3ea59 | 2015-09-29 14:12:44 -0700 | [diff] [blame] | 215 | GPR_ASSERT(h->count != 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 216 | return h->sum / h->count; |
| 217 | } |
| 218 | |
| 219 | double gpr_histogram_stddev(gpr_histogram *h) { |
| 220 | return sqrt(gpr_histogram_variance(h)); |
| 221 | } |
| 222 | |
| 223 | double 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 | |
| 229 | double gpr_histogram_maximum(gpr_histogram *h) { return h->max_seen; } |
| 230 | |
| 231 | double gpr_histogram_minimum(gpr_histogram *h) { return h->min_seen; } |
| 232 | |
| 233 | double gpr_histogram_count(gpr_histogram *h) { return h->count; } |
| 234 | |
| 235 | double gpr_histogram_sum(gpr_histogram *h) { return h->sum; } |
| 236 | |
| 237 | double gpr_histogram_sum_of_squares(gpr_histogram *h) { |
| 238 | return h->sum_of_squares; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 239 | } |
Craig Tiller | 76877c3 | 2015-03-03 16:04:23 -0800 | [diff] [blame] | 240 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 241 | const uint32_t *gpr_histogram_get_contents(gpr_histogram *h, size_t *size) { |
Craig Tiller | 76877c3 | 2015-03-03 16:04:23 -0800 | [diff] [blame] | 242 | *size = h->num_buckets; |
| 243 | return h->buckets; |
Craig Tiller | f282514 | 2015-03-03 17:15:36 -0800 | [diff] [blame] | 244 | } |