forked from open-telemetry/opentelemetry-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularBufferBuckets.cs
More file actions
292 lines (241 loc) · 8.02 KB
/
CircularBufferBuckets.cs
File metadata and controls
292 lines (241 loc) · 8.02 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Diagnostics;
using System.Runtime.CompilerServices;
using OpenTelemetry.Internal;
namespace OpenTelemetry.Metrics;
/// <summary>
/// A histogram buckets implementation based on circular buffer.
/// </summary>
internal sealed class CircularBufferBuckets
{
private long[]? trait;
private int begin = 0;
private int end = -1;
public CircularBufferBuckets(int capacity)
{
Guard.ThrowIfOutOfRange(capacity, min: 1);
this.Capacity = capacity;
}
/// <summary>
/// Gets the capacity of the <see cref="CircularBufferBuckets"/>.
/// </summary>
public int Capacity { get; }
/// <summary>
/// Gets the offset of the start index for the <see cref="CircularBufferBuckets"/>.
/// </summary>
public int Offset => this.begin;
/// <summary>
/// Gets the size of the <see cref="CircularBufferBuckets"/>.
/// </summary>
public int Size => this.end - this.begin + 1;
/// <summary>
/// Returns the value of <c>Bucket[index]</c>.
/// </summary>
/// <param name="index">The index of the bucket.</param>
/// <remarks>
/// The "index" value can be positive, zero or negative.
/// This method does not validate if "index" falls into [begin, end],
/// the caller is responsible for the validation.
/// </remarks>
public long this[int index]
{
get
{
Debug.Assert(this.trait != null, "trait was null");
return this.trait![this.ModuloIndex(index)];
}
}
/// <summary>
/// Attempts to increment the value of <c>Bucket[index]</c> by <c>value</c>.
/// </summary>
/// <param name="index">The index of the bucket.</param>
/// <param name="value">The increment.</param>
/// <returns>
/// Returns <c>0</c> if the increment attempt succeeded;
/// Returns a positive integer indicating the minimum scale reduction level
/// if the increment attempt failed.
/// </returns>
/// <remarks>
/// The "index" value can be positive, zero or negative.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int TryIncrement(int index, long value = 1)
{
var capacity = this.Capacity;
if (this.trait == null)
{
this.trait = new long[capacity];
this.begin = index;
this.end = index;
this.trait[this.ModuloIndex(index)] += value;
return 0;
}
var begin = this.begin;
var end = this.end;
if (index > end)
{
end = index;
}
else if (index < begin)
{
begin = index;
}
else
{
this.trait[this.ModuloIndex(index)] += value;
return 0;
}
var diff = end - begin;
if (diff >= capacity || diff < 0)
{
return CalculateScaleReduction(begin, end, capacity);
}
this.begin = begin;
this.end = end;
this.trait[this.ModuloIndex(index)] += value;
return 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static int CalculateScaleReduction(int begin, int end, int capacity)
{
Debug.Assert(capacity >= 2, "The capacity must be at least 2.");
var retval = 0;
var diff = end - begin;
while (diff >= capacity || diff < 0)
{
begin >>= 1;
end >>= 1;
diff = end - begin;
retval++;
}
return retval;
}
}
public void ScaleDown(int level = 1)
{
Debug.Assert(level > 0, "The scale down level must be a positive integer.");
if (this.trait == null)
{
return;
}
// 0 <= offset < capacity <= 2147483647
uint capacity = (uint)this.Capacity;
var offset = (uint)this.ModuloIndex(this.begin);
var currentBegin = this.begin;
var currentEnd = this.end;
for (int i = 0; i < level; i++)
{
var newBegin = currentBegin >> 1;
var newEnd = currentEnd >> 1;
if (currentBegin != currentEnd)
{
if (currentBegin % 2 == 0)
{
ScaleDownInternal(this.trait, offset, currentBegin, currentEnd, capacity);
}
else
{
currentBegin++;
if (currentBegin != currentEnd)
{
ScaleDownInternal(this.trait, offset + 1, currentBegin, currentEnd, capacity);
}
}
}
currentBegin = newBegin;
currentEnd = newEnd;
}
this.begin = currentBegin;
this.end = currentEnd;
if (capacity > 1)
{
AdjustPosition(this.trait, offset, (uint)this.ModuloIndex(currentBegin), (uint)(currentEnd - currentBegin + 1), capacity);
}
return;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void ScaleDownInternal(long[] array, uint offset, int begin, int end, uint capacity)
{
for (var index = begin + 1; index < end; index++)
{
Consolidate(array, (offset + (uint)(index - begin)) % capacity, (offset + (uint)((index >> 1) - (begin >> 1))) % capacity);
}
// Don't merge below call into above for loop.
// Merging causes above loop to be infinite if end = int.MaxValue, because index <= int.MaxValue is always true.
Consolidate(array, (offset + (uint)(end - begin)) % capacity, (offset + (uint)((end >> 1) - (begin >> 1))) % capacity);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void AdjustPosition(long[] array, uint src, uint dst, uint size, uint capacity)
{
var advancement = (dst + capacity - src) % capacity;
if (advancement == 0)
{
return;
}
if (size - 1 == advancement && advancement << 1 == capacity)
{
Exchange(array, src++, dst++);
size -= 2;
}
else if (advancement < size)
{
src = src + size - 1;
dst = dst + size - 1;
while (size-- != 0)
{
Move(array, src-- % capacity, dst-- % capacity);
}
return;
}
while (size-- != 0)
{
Move(array, src++ % capacity, dst++ % capacity);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void Consolidate(long[] array, uint src, uint dst)
{
array[dst] += array[src];
array[src] = 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void Exchange(long[] array, uint src, uint dst)
{
var value = array[dst];
array[dst] = array[src];
array[src] = value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void Move(long[] array, uint src, uint dst)
{
array[dst] = array[src];
array[src] = 0;
}
}
internal void Reset()
{
if (this.trait != null)
{
#if NET
Array.Clear(this.trait);
#else
Array.Clear(this.trait, 0, this.trait.Length);
#endif
}
}
internal void Copy(long[] dst)
{
Debug.Assert(dst.Length == this.Capacity, "The length of the destination array must equal the capacity.");
if (this.trait != null)
{
for (var i = 0; i < this.Size; ++i)
{
dst[i] = this[this.Offset + i];
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int ModuloIndex(int value)
{
return MathHelper.PositiveModulo32(value, this.Capacity);
}
}