Debian Codegraph @

test/core/util/histogram.cc

raw
1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "test/core/util/histogram.h"
22
23 #include <math.h>
24 #include <stddef.h>
25
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28
29 #include "src/core/lib/gpr/useful.h"
30
31 /* Histograms are stored with exponentially increasing bucket sizes.
32 The first bucket is [0, m) where m = 1 + resolution
33 Bucket n (n>=1) contains [m**n, m**(n+1))
34 There are sufficient buckets to reach max_bucket_start */
35
36 struct grpc_histogram {
37 /* Sum of all values seen so far */
38 double sum;
39 /* Sum of squares of all values seen so far */
40 double sum_of_squares;
41 /* number of values seen so far */
42 double count;
43 /* m in the description */
44 double multiplier;
45 double one_on_log_multiplier;
46 /* minimum value seen */
47 double min_seen;
48 /* maximum value seen */
49 double max_seen;
50 /* maximum representable value */
51 double max_possible;
52 /* number of buckets */
53 size_t num_buckets;
54 /* the buckets themselves */
55 uint32_t* buckets;
56 };
57
58 /* determine a bucket index given a value - does no bounds checking */
59 static size_t bucket_for_unchecked(grpc_histogram* h, double x) {
60 return static_cast<size_t>(log(x) * h->one_on_log_multiplier);
61 }
62
63 /* bounds checked version of the above */
64 static size_t bucket_for(grpc_histogram* h, double x) {
65 size_t bucket =
66 bucket_for_unchecked(h, grpc_core::Clamp(x, 1.0, h->max_possible));
67 GPR_ASSERT(bucket < h->num_buckets);
68 return bucket;
69 }
70
71 /* at what value does a bucket start? */
72 static double bucket_start(grpc_histogram* h, double x) {
73 return pow(h->multiplier, x);
74 }
75
76 grpc_histogram* grpc_histogram_create(double resolution,
77 double max_bucket_start) {
78 grpc_histogram* h =
79 static_cast<grpc_histogram*>(gpr_malloc(sizeof(grpc_histogram)));
80 GPR_ASSERT(resolution > 0.0);
81 GPR_ASSERT(max_bucket_start > resolution);
82 h->sum = 0.0;
83 h->sum_of_squares = 0.0;
84 h->multiplier = 1.0 + resolution;
85 h->one_on_log_multiplier = 1.0 / log(1.0 + resolution);
86 h->max_possible = max_bucket_start;
87 h->count = 0.0;
88 h->min_seen = max_bucket_start;
89 h->max_seen = 0.0;
90 h->num_buckets = bucket_for_unchecked(h, max_bucket_start) + 1;
91 GPR_ASSERT(h->num_buckets > 1);
92 GPR_ASSERT(h->num_buckets < 100000000);
93 h->buckets =
94 static_cast<uint32_t*>(gpr_zalloc(sizeof(uint32_t) * h->num_buckets));
95 return h;
96 }
97
98 void grpc_histogram_destroy(grpc_histogram* h) {
99 gpr_free(h->buckets);
100 gpr_free(h);
101 }
102
103 void grpc_histogram_add(grpc_histogram* h, double x) {
104 h->sum += x;
105 h->sum_of_squares += x * x;
106 h->count++;
107 if (x < h->min_seen) {
108 h->min_seen = x;
109 }
110 if (x > h->max_seen) {
111 h->max_seen = x;
112 }
113 h->buckets[bucket_for(h, x)]++;
114 }
115
116 int grpc_histogram_merge(grpc_histogram* dst, const grpc_histogram* src) {
117 if ((dst->num_buckets != src->num_buckets) ||
118 (dst->multiplier != src->multiplier)) {
119 /* Fail because these histograms don't match */
120 return 0;
121 }
122 grpc_histogram_merge_contents(dst, src->buckets, src->num_buckets,
123 src->min_seen, src->max_seen, src->sum,
124 src->sum_of_squares, src->count);
125 return 1;
126 }
127
128 void grpc_histogram_merge_contents(grpc_histogram* histogram,
129 const uint32_t* data, size_t data_count,
130 double min_seen, double max_seen, double sum,
131 double sum_of_squares, double count) {
132 size_t i;
133 GPR_ASSERT(histogram->num_buckets == data_count);
134 histogram->sum += sum;
135 histogram->sum_of_squares += sum_of_squares;
136 histogram->count += count;
137 if (min_seen < histogram->min_seen) {
138 histogram->min_seen = min_seen;
139 }
140 if (max_seen > histogram->max_seen) {
141 histogram->max_seen = max_seen;
142 }
143 for (i = 0; i < histogram->num_buckets; i++) {
144 histogram->buckets[i] += data[i];
145 }
146 }
147
148 static double threshold_for_count_below(grpc_histogram* h, double count_below) {
149 double count_so_far;
150 double lower_bound;
151 double upper_bound;
152 size_t lower_idx;
153 size_t upper_idx;
154
155 if (h->count == 0) {
156 return 0.0;
157 }
158
159 if (count_below <= 0) {
160 return h->min_seen;
161 }
162 if (count_below >= h->count) {
163 return h->max_seen;
164 }
165
166 /* find the lowest bucket that gets us above count_below */
167 count_so_far = 0.0;
168 for (lower_idx = 0; lower_idx < h->num_buckets; lower_idx++) {
169 count_so_far += h->buckets[lower_idx];
170 if (count_so_far >= count_below) {
171 break;
172 }
173 }
174 if (count_so_far == count_below) {
175 /* this bucket hits the threshold exactly... we should be midway through
176 any run of zero values following the bucket */
177 for (upper_idx = lower_idx + 1; upper_idx < h->num_buckets; upper_idx++) {
178 if (h->buckets[upper_idx]) {
179 break;
180 }
181 }
182 return (bucket_start(h, static_cast<double>(lower_idx)) +
183 bucket_start(h, static_cast<double>(upper_idx))) /
184 2.0;
185 } else {
186 /* treat values as uniform throughout the bucket, and find where this value
187 should lie */
188 lower_bound = bucket_start(h, static_cast<double>(lower_idx));
189 upper_bound = bucket_start(h, static_cast<double>(lower_idx + 1));
190 return grpc_core::Clamp(upper_bound - (upper_bound - lower_bound) *
191 (count_so_far - count_below) /
192 h->buckets[lower_idx],
193 h->min_seen, h->max_seen);
194 }
195 }
196
197 double grpc_histogram_percentile(grpc_histogram* h, double percentile) {
198 return threshold_for_count_below(h, h->count * percentile / 100.0);
199 }
200
201 double grpc_histogram_mean(grpc_histogram* h) {
202 GPR_ASSERT(h->count != 0);
203 return h->sum / h->count;
204 }
205
206 double grpc_histogram_stddev(grpc_histogram* h) {
207 return sqrt(grpc_histogram_variance(h));
208 }
209
210 double grpc_histogram_variance(grpc_histogram* h) {
211 if (h->count == 0) return 0.0;
212 return (h->sum_of_squares * h->count - h->sum * h->sum) /
213 (h->count * h->count);
214 }
215
216 double grpc_histogram_maximum(grpc_histogram* h) { return h->max_seen; }
217
218 double grpc_histogram_minimum(grpc_histogram* h) { return h->min_seen; }
219
220 double grpc_histogram_count(grpc_histogram* h) { return h->count; }
221
222 double grpc_histogram_sum(grpc_histogram* h) { return h->sum; }
223
224 double grpc_histogram_sum_of_squares(grpc_histogram* h) {
225 return h->sum_of_squares;
226 }
227
228 const uint32_t* grpc_histogram_get_contents(grpc_histogram* histogram,
229 size_t* count) {
230 *count = histogram->num_buckets;
231 return histogram->buckets;
232 }