1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/*
* librdkafka - Apache Kafka C library
*
* Copyright (c) 2018 Magnus Edenhill
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _RDAVG_H_
#define _RDAVG_H_
#if WITH_HDRHISTOGRAM
#include "rdhdrhistogram.h"
#endif
typedef struct rd_avg_s {
struct {
int64_t maxv;
int64_t minv;
int64_t avg;
int64_t sum;
int cnt;
rd_ts_t start;
} ra_v;
mtx_t ra_lock;
int ra_enabled;
enum { RD_AVG_GAUGE,
RD_AVG_COUNTER,
} ra_type;
#if WITH_HDRHISTOGRAM
rd_hdr_histogram_t *ra_hdr;
#endif
/* Histogram results, calculated for dst in rollover().
* Will be all zeroes if histograms are not supported. */
struct {
/* Quantiles */
int64_t p50;
int64_t p75;
int64_t p90;
int64_t p95;
int64_t p99;
int64_t p99_99;
int64_t oor; /**< Values out of range */
int32_t hdrsize; /**< hdr.allocatedSize */
double stddev;
double mean;
} ra_hist;
} rd_avg_t;
/**
* @brief Add value \p v to averager \p ra.
*/
static RD_UNUSED void rd_avg_add(rd_avg_t *ra, int64_t v) {
mtx_lock(&ra->ra_lock);
if (!ra->ra_enabled) {
mtx_unlock(&ra->ra_lock);
return;
}
if (v > ra->ra_v.maxv)
ra->ra_v.maxv = v;
if (ra->ra_v.minv == 0 || v < ra->ra_v.minv)
ra->ra_v.minv = v;
ra->ra_v.sum += v;
ra->ra_v.cnt++;
#if WITH_HDRHISTOGRAM
rd_hdr_histogram_record(ra->ra_hdr, v);
#endif
mtx_unlock(&ra->ra_lock);
}
/**
* @brief Calculate the average
*/
static RD_UNUSED void rd_avg_calc(rd_avg_t *ra, rd_ts_t now) {
if (ra->ra_type == RD_AVG_GAUGE) {
if (ra->ra_v.cnt)
ra->ra_v.avg = ra->ra_v.sum / ra->ra_v.cnt;
else
ra->ra_v.avg = 0;
} else {
rd_ts_t elapsed = now - ra->ra_v.start;
if (elapsed)
ra->ra_v.avg = (ra->ra_v.sum * 1000000llu) / elapsed;
else
ra->ra_v.avg = 0;
ra->ra_v.start = elapsed;
}
}
/**
* @returns the quantile \q for \p ra, or 0 if histograms are not supported
* in this build.
*
* @remark ra will be not locked by this function.
*/
static RD_UNUSED int64_t rd_avg_quantile(const rd_avg_t *ra, double q) {
#if WITH_HDRHISTOGRAM
return rd_hdr_histogram_quantile(ra->ra_hdr, q);
#else
return 0;
#endif
}
/**
* @brief Rolls over statistics in \p src and stores the average in \p dst.
* \p src is cleared and ready to be reused.
*
* Caller must free avg internal members by calling rd_avg_destroy()
* on the \p dst.
*/
static RD_UNUSED void rd_avg_rollover(rd_avg_t *dst, rd_avg_t *src) {
rd_ts_t now;
mtx_lock(&src->ra_lock);
if (!src->ra_enabled) {
memset(dst, 0, sizeof(*dst));
dst->ra_type = src->ra_type;
mtx_unlock(&src->ra_lock);
return;
}
mtx_init(&dst->ra_lock, mtx_plain);
dst->ra_type = src->ra_type;
dst->ra_v = src->ra_v;
#if WITH_HDRHISTOGRAM
dst->ra_hdr = NULL;
dst->ra_hist.stddev = rd_hdr_histogram_stddev(src->ra_hdr);
dst->ra_hist.mean = rd_hdr_histogram_mean(src->ra_hdr);
dst->ra_hist.oor = src->ra_hdr->outOfRangeCount;
dst->ra_hist.hdrsize = src->ra_hdr->allocatedSize;
dst->ra_hist.p50 = rd_hdr_histogram_quantile(src->ra_hdr, 50.0);
dst->ra_hist.p75 = rd_hdr_histogram_quantile(src->ra_hdr, 75.0);
dst->ra_hist.p90 = rd_hdr_histogram_quantile(src->ra_hdr, 90.0);
dst->ra_hist.p95 = rd_hdr_histogram_quantile(src->ra_hdr, 95.0);
dst->ra_hist.p99 = rd_hdr_histogram_quantile(src->ra_hdr, 99.0);
dst->ra_hist.p99_99 = rd_hdr_histogram_quantile(src->ra_hdr, 99.99);
#else
memset(&dst->ra_hist, 0, sizeof(dst->ra_hist));
#endif
memset(&src->ra_v, 0, sizeof(src->ra_v));
now = rd_clock();
src->ra_v.start = now;
#if WITH_HDRHISTOGRAM
/* Adapt histogram span to fit future out of range entries
* from this period. */
if (src->ra_hdr->totalCount > 0) {
int64_t vmin = src->ra_hdr->lowestTrackableValue;
int64_t vmax = src->ra_hdr->highestTrackableValue;
int64_t mindiff, maxdiff;
mindiff = src->ra_hdr->lowestTrackableValue -
src->ra_hdr->lowestOutOfRange;
if (mindiff > 0) {
/* There were low out of range values, grow lower
* span to fit lowest out of range value + 20%. */
vmin = src->ra_hdr->lowestOutOfRange +
(int64_t)((double)mindiff * 0.2);
}
maxdiff = src->ra_hdr->highestOutOfRange -
src->ra_hdr->highestTrackableValue;
if (maxdiff > 0) {
/* There were high out of range values, grow higher
* span to fit highest out of range value + 20%. */
vmax = src->ra_hdr->highestOutOfRange +
(int64_t)((double)maxdiff * 0.2);
}
if (vmin == src->ra_hdr->lowestTrackableValue &&
vmax == src->ra_hdr->highestTrackableValue) {
/* No change in min,max, use existing hdr */
rd_hdr_histogram_reset(src->ra_hdr);
} else {
int sigfigs = (int)src->ra_hdr->significantFigures;
/* Create new hdr for adapted range */
rd_hdr_histogram_destroy(src->ra_hdr);
src->ra_hdr = rd_hdr_histogram_new(vmin, vmax, sigfigs);
}
} else {
/* No records, no need to reset. */
}
#endif
mtx_unlock(&src->ra_lock);
rd_avg_calc(dst, now);
}
/**
* Initialize an averager
*/
static RD_UNUSED void rd_avg_init(rd_avg_t *ra,
int type,
int64_t exp_min,
int64_t exp_max,
int sigfigs,
int enable) {
memset(ra, 0, sizeof(*ra));
mtx_init(&ra->ra_lock, 0);
ra->ra_enabled = enable;
if (!enable)
return;
ra->ra_type = type;
ra->ra_v.start = rd_clock();
#if WITH_HDRHISTOGRAM
/* Start off the histogram with expected min,max span,
* we'll adapt the size on each rollover. */
ra->ra_hdr = rd_hdr_histogram_new(exp_min, exp_max, sigfigs);
#endif
}
/**
* Destroy averager
*/
static RD_UNUSED void rd_avg_destroy(rd_avg_t *ra) {
#if WITH_HDRHISTOGRAM
if (ra->ra_hdr)
rd_hdr_histogram_destroy(ra->ra_hdr);
#endif
mtx_destroy(&ra->ra_lock);
}
#endif /* _RDAVG_H_ */
|