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
|
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#include <stddef.h>
#include <cassert>
#include <cstdint>
#include <limits>
#include <utility>
#include <vector>
#include "opentelemetry/nostd/variant.h"
#include "opentelemetry/sdk/metrics/data/circular_buffer.h"
#include "opentelemetry/version.h"
OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{
namespace
{
struct AdaptingIntegerArrayIncrement
{
size_t index;
uint64_t count;
template <typename T>
uint64_t operator()(std::vector<T> &backing)
{
const uint64_t result = backing[index] + count;
if OPENTELEMETRY_LIKELY_CONDITION (result <= uint64_t(std::numeric_limits<T>::max()))
{
backing[index] = static_cast<T>(result);
return 0;
}
return result;
}
};
struct AdaptingIntegerArrayGet
{
size_t index;
template <typename T>
uint64_t operator()(const std::vector<T> &backing)
{
return backing[index];
}
};
struct AdaptingIntegerArraySize
{
template <typename T>
size_t operator()(const std::vector<T> &backing)
{
return backing.size();
}
};
struct AdaptingIntegerArrayClear
{
template <typename T>
void operator()(std::vector<T> &backing)
{
backing.assign(backing.size(), static_cast<T>(0));
}
};
struct AdaptingIntegerArrayCopy
{
template <class T1, class T2>
void operator()(const std::vector<T1> &from, std::vector<T2> &to)
{
for (size_t i = 0; i < from.size(); i++)
{
to[i] = static_cast<T2>(from[i]);
}
}
};
} // namespace
void AdaptingIntegerArray::Increment(size_t index, uint64_t count)
{
/* May or may not fit */
const uint64_t result = nostd::visit(AdaptingIntegerArrayIncrement{index, count}, backing_);
if OPENTELEMETRY_LIKELY_CONDITION (result == 0)
{
return;
}
EnlargeToFit(result);
/* Must fit, buffer was enlarged for the value to store */
OPENTELEMETRY_MAYBE_UNUSED const uint64_t result2 =
nostd::visit(AdaptingIntegerArrayIncrement{index, count}, backing_);
assert(result2 == 0);
}
uint64_t AdaptingIntegerArray::Get(size_t index) const
{
return nostd::visit(AdaptingIntegerArrayGet{index}, backing_);
}
size_t AdaptingIntegerArray::Size() const
{
return nostd::visit(AdaptingIntegerArraySize{}, backing_);
}
void AdaptingIntegerArray::Clear()
{
nostd::visit(AdaptingIntegerArrayClear{}, backing_);
}
void AdaptingIntegerArray::EnlargeToFit(uint64_t value)
{
const size_t backing_size = Size();
decltype(backing_) backing;
if (value <= std::numeric_limits<uint16_t>::max())
{
backing = std::vector<uint16_t>(backing_size, 0);
}
else if (value <= std::numeric_limits<uint32_t>::max())
{
backing = std::vector<uint32_t>(backing_size, 0);
}
else
{
backing = std::vector<uint64_t>(backing_size, 0);
}
nostd::visit(AdaptingIntegerArrayCopy{}, backing_, backing);
backing_ = std::move(backing);
}
void AdaptingCircularBufferCounter::Clear()
{
start_index_ = kNullIndex;
end_index_ = kNullIndex;
base_index_ = kNullIndex;
backing_.Clear();
}
bool AdaptingCircularBufferCounter::Increment(int32_t index, uint64_t delta)
{
if (Empty())
{
start_index_ = index;
end_index_ = index;
base_index_ = index;
backing_.Increment(0, delta);
return true;
}
if (index > end_index_)
{
// Move end, check max size.
if (index + 1 > static_cast<int32_t>(backing_.Size()) + start_index_)
{
return false;
}
end_index_ = index;
}
else if (index < start_index_)
{
// Move end, check max size.
if (end_index_ + 1 > static_cast<int32_t>(backing_.Size()) + index)
{
return false;
}
start_index_ = index;
}
backing_.Increment(ToBufferIndex(index), delta);
return true;
}
uint64_t AdaptingCircularBufferCounter::Get(int32_t index) const
{
if (index < start_index_ || index > end_index_)
{
return 0;
}
return backing_.Get(ToBufferIndex(index));
}
size_t AdaptingCircularBufferCounter::ToBufferIndex(int32_t index) const
{
// Figure out the index relative to the start of the circular buffer.
if (index < base_index_)
{
// If index is before the base one, wrap around.
return static_cast<size_t>(index + backing_.Size() - base_index_);
}
return static_cast<size_t>(index - base_index_);
}
} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
|