Skip to content

Commit 5641bbd

Browse files
committed
[wip] make buckets unique ptr
1 parent 42a904e commit 5641bbd

File tree

3 files changed

+101
-66
lines changed

3 files changed

+101
-66
lines changed

sdk/include/opentelemetry/sdk/metrics/aggregation/base2_exponential_histogram_aggregation.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ class Base2ExponentialHistogramAggregation : public Aggregation
4545
PointType ToPoint() const noexcept override;
4646

4747
private:
48-
void AggregateIntoBuckets(AdaptingCircularBufferCounter &buckets, double value) noexcept;
48+
void AggregateIntoBuckets(std::unique_ptr<AdaptingCircularBufferCounter> &buckets,
49+
double value) noexcept;
4950
void Downscale(uint32_t by) noexcept;
5051

5152
mutable opentelemetry::common::SpinLockMutex lock_;

sdk/include/opentelemetry/sdk/metrics/data/point_data.h

+24-13
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,34 @@ class HistogramPointData
6969
class Base2ExponentialHistogramPointData
7070
{
7171
public:
72-
// TODO: remove ctors and initializers when GCC<5 stops shipping on Ubuntu
73-
Base2ExponentialHistogramPointData(Base2ExponentialHistogramPointData &&) = default;
74-
Base2ExponentialHistogramPointData &operator=(Base2ExponentialHistogramPointData &&) = default;
75-
Base2ExponentialHistogramPointData(const Base2ExponentialHistogramPointData &) = default;
76-
Base2ExponentialHistogramPointData() = default;
72+
// Delete copy constructor and copy assignment operator
73+
Base2ExponentialHistogramPointData(const Base2ExponentialHistogramPointData &) = delete;
74+
Base2ExponentialHistogramPointData &operator=(const Base2ExponentialHistogramPointData &) =
75+
delete;
7776

78-
uint64_t count_ = {};
79-
double sum_ = {};
80-
int32_t scale_ = {};
81-
uint64_t zero_count_ = {};
82-
AdaptingCircularBufferCounter positive_buckets_{0};
83-
AdaptingCircularBufferCounter negative_buckets_{0};
77+
// Default move constructor and move assignment operator
78+
Base2ExponentialHistogramPointData(Base2ExponentialHistogramPointData &&) noexcept = default;
79+
Base2ExponentialHistogramPointData &operator=(Base2ExponentialHistogramPointData &&) noexcept =
80+
default;
81+
82+
// Default constructor
83+
Base2ExponentialHistogramPointData() = default;
84+
85+
double sum_ = {};
8486
double min_ = {};
8587
double max_ = {};
8688
double zero_threshold_ = {};
87-
bool record_min_max_ = true;
88-
size_t max_buckets_ = {};
89+
uint64_t count_ = {};
90+
uint64_t zero_count_ = {};
91+
92+
std::unique_ptr<AdaptingCircularBufferCounter> positive_buckets_ =
93+
std::make_unique<AdaptingCircularBufferCounter>(0);
94+
std::unique_ptr<AdaptingCircularBufferCounter> negative_buckets_ =
95+
std::make_unique<AdaptingCircularBufferCounter>(0);
96+
97+
size_t max_buckets_ = {};
98+
int32_t scale_ = {};
99+
bool record_min_max_ = true;
89100
};
90101

91102
class DropPointData

sdk/src/metrics/aggregation/base2_exponential_histogram_aggregation.cc

+75-52
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ uint32_t GetScaleReduction(int32_t start_index, int32_t end_index, size_t max_bu
4141
return scale_reduction;
4242
}
4343

44-
void DownscaleBuckets(AdaptingCircularBufferCounter &buckets, uint32_t by) noexcept
44+
void DownscaleBuckets(std::unique_ptr<AdaptingCircularBufferCounter> &buckets, uint32_t by) noexcept
4545
{
46-
if (buckets.Empty())
46+
if (buckets->Empty())
4747
{
4848
return;
4949
}
@@ -52,15 +52,15 @@ void DownscaleBuckets(AdaptingCircularBufferCounter &buckets, uint32_t by) noexc
5252
// Instead of creating a new counter, we copy the existing one (for bucket size
5353
// optimisations), and clear the values before writing the new ones.
5454
// TODO(euroelessar): Do downscaling in-place.
55-
auto new_buckets = buckets;
56-
new_buckets.Clear();
55+
auto new_buckets = std::make_unique<AdaptingCircularBufferCounter>(buckets->MaxSize());
56+
new_buckets->Clear();
5757

58-
for (auto i = buckets.StartIndex(); i <= buckets.EndIndex(); ++i)
58+
for (auto i = buckets->StartIndex(); i <= buckets->EndIndex(); ++i)
5959
{
60-
const uint64_t count = buckets.Get(i);
60+
const uint64_t count = buckets->Get(i);
6161
if (count > 0)
6262
{
63-
new_buckets.Increment(i >> by, count);
63+
new_buckets->Increment(i >> by, count);
6464
}
6565
}
6666
buckets = std::move(new_buckets);
@@ -87,19 +87,19 @@ Base2ExponentialHistogramAggregation::Base2ExponentialHistogramAggregation(
8787
indexer_ = Base2ExponentialHistogramIndexer(point_data_.scale_);
8888
}
8989

90-
Base2ExponentialHistogramAggregation::Base2ExponentialHistogramAggregation(
91-
const Base2ExponentialHistogramPointData &point_data)
92-
: point_data_{point_data},
93-
indexer_(point_data.scale_),
94-
record_min_max_{point_data.record_min_max_}
95-
{}
90+
// Base2ExponentialHistogramAggregation::Base2ExponentialHistogramAggregation(
91+
// const Base2ExponentialHistogramPointData &point_data)
92+
// : point_data_{point_data},
93+
// indexer_(point_data.scale_),
94+
// record_min_max_{point_data.record_min_max_}
95+
// {}
9696

97-
Base2ExponentialHistogramAggregation::Base2ExponentialHistogramAggregation(
98-
Base2ExponentialHistogramPointData &&point_data)
99-
: point_data_{std::move(point_data)},
100-
indexer_(point_data_.scale_),
101-
record_min_max_{point_data_.record_min_max_}
102-
{}
97+
// Base2ExponentialHistogramAggregation::Base2ExponentialHistogramAggregation(
98+
// Base2ExponentialHistogramPointData &&point_data)
99+
// : point_data_{std::move(point_data)},
100+
// indexer_(point_data_.scale_),
101+
// record_min_max_{point_data_.record_min_max_}
102+
// {}
103103

104104
void Base2ExponentialHistogramAggregation::Aggregate(
105105
int64_t value,
@@ -138,24 +138,24 @@ void Base2ExponentialHistogramAggregation::Aggregate(
138138
}
139139

140140
void Base2ExponentialHistogramAggregation::AggregateIntoBuckets(
141-
AdaptingCircularBufferCounter &buckets,
141+
std::unique_ptr<AdaptingCircularBufferCounter> &buckets,
142142
double value) noexcept
143143
{
144-
if (buckets.MaxSize() == 0)
144+
if (buckets->MaxSize() == 0)
145145
{
146-
buckets = AdaptingCircularBufferCounter{point_data_.max_buckets_};
146+
buckets = std::make_unique<AdaptingCircularBufferCounter>(point_data_.max_buckets_);
147147
}
148148

149149
const int32_t index = indexer_.ComputeIndex(value);
150-
if (!buckets.Increment(index, 1))
150+
if (!buckets->Increment(index, 1))
151151
{
152-
const int32_t start_index = (std::min)(buckets.StartIndex(), index);
153-
const int32_t end_index = (std::max)(buckets.EndIndex(), index);
152+
const int32_t start_index = (std::min)(buckets->StartIndex(), index);
153+
const int32_t end_index = (std::max)(buckets->EndIndex(), index);
154154
const uint32_t scale_reduction =
155155
GetScaleReduction(start_index, end_index, point_data_.max_buckets_);
156156
Downscale(scale_reduction);
157157

158-
buckets.Increment(index >> scale_reduction, 1);
158+
buckets->Increment(index >> scale_reduction, 1);
159159
}
160160
}
161161

@@ -247,13 +247,13 @@ std::unique_ptr<Aggregation> Base2ExponentialHistogramAggregation::Merge(
247247
}
248248

249249
auto pos_min_index =
250-
(std::min)(low_res.positive_buckets_.StartIndex(), high_res.positive_buckets_.StartIndex());
250+
(std::min)(low_res.positive_buckets_->StartIndex(), high_res.positive_buckets_->StartIndex());
251251
auto pos_max_index =
252-
(std::max)(low_res.positive_buckets_.EndIndex(), high_res.positive_buckets_.EndIndex());
252+
(std::max)(low_res.positive_buckets_->EndIndex(), high_res.positive_buckets_->EndIndex());
253253
auto neg_min_index =
254-
(std::min)(low_res.negative_buckets_.StartIndex(), high_res.negative_buckets_.StartIndex());
254+
(std::min)(low_res.negative_buckets_->StartIndex(), high_res.negative_buckets_->StartIndex());
255255
auto neg_max_index =
256-
(std::max)(low_res.negative_buckets_.EndIndex(), high_res.negative_buckets_.EndIndex());
256+
(std::max)(low_res.negative_buckets_->EndIndex(), high_res.negative_buckets_->EndIndex());
257257

258258
if (static_cast<size_t>(pos_max_index) >
259259
static_cast<size_t>(pos_min_index) + result_value.max_buckets_ ||
@@ -272,10 +272,10 @@ std::unique_ptr<Aggregation> Base2ExponentialHistogramAggregation::Merge(
272272
result_value.scale_ -= scale_reduction;
273273
}
274274

275-
result_value.positive_buckets_ = MergeBuckets(
276-
result_value.max_buckets_, low_res.positive_buckets_, high_res.positive_buckets_);
277-
result_value.negative_buckets_ = MergeBuckets(
278-
result_value.max_buckets_, low_res.negative_buckets_, high_res.negative_buckets_);
275+
result_value.positive_buckets_ = std::make_unique<AdaptingCircularBufferCounter>(MergeBuckets(
276+
result_value.max_buckets_, *low_res.positive_buckets_, *high_res.positive_buckets_));
277+
result_value.negative_buckets_ = std::make_unique<AdaptingCircularBufferCounter>(MergeBuckets(
278+
result_value.max_buckets_, *low_res.negative_buckets_, *high_res.negative_buckets_));
279279

280280
return std::unique_ptr<Base2ExponentialHistogramAggregation>{
281281
new Base2ExponentialHistogramAggregation(std::move(result_value))};
@@ -303,13 +303,13 @@ std::unique_ptr<Aggregation> Base2ExponentialHistogramAggregation::Diff(
303303
}
304304

305305
auto pos_min_index =
306-
(std::min)(left.positive_buckets_.StartIndex(), right.positive_buckets_.StartIndex());
306+
(std::min)(left.positive_buckets_->StartIndex(), right.positive_buckets_->StartIndex());
307307
auto pos_max_index =
308-
(std::max)(left.positive_buckets_.EndIndex(), right.positive_buckets_.EndIndex());
308+
(std::max)(left.positive_buckets_->EndIndex(), right.positive_buckets_->EndIndex());
309309
auto neg_min_index =
310-
(std::min)(left.negative_buckets_.StartIndex(), right.negative_buckets_.StartIndex());
310+
(std::min)(left.negative_buckets_->StartIndex(), right.negative_buckets_->StartIndex());
311311
auto neg_max_index =
312-
(std::max)(left.negative_buckets_.EndIndex(), right.negative_buckets_.EndIndex());
312+
(std::max)(left.negative_buckets_->EndIndex(), right.negative_buckets_->EndIndex());
313313

314314
if (static_cast<size_t>(pos_max_index) >
315315
static_cast<size_t>(pos_min_index) + low_res.max_buckets_ ||
@@ -338,35 +338,37 @@ std::unique_ptr<Aggregation> Base2ExponentialHistogramAggregation::Diff(
338338
result_value.sum_ = (right.sum_ >= left.sum_) ? (right.sum_ - left.sum_) : 0.0;
339339
result_value.zero_count_ =
340340
(right.zero_count_ >= left.zero_count_) ? (right.zero_count_ - left.zero_count_) : 0;
341-
result_value.positive_buckets_ = AdaptingCircularBufferCounter{right.max_buckets_};
342-
result_value.negative_buckets_ = AdaptingCircularBufferCounter{right.max_buckets_};
341+
result_value.positive_buckets_ =
342+
std::make_unique<AdaptingCircularBufferCounter>(right.max_buckets_);
343+
result_value.negative_buckets_ =
344+
std::make_unique<AdaptingCircularBufferCounter>(right.max_buckets_);
343345

344-
if (!left.positive_buckets_.Empty() && !right.positive_buckets_.Empty())
346+
if (left.positive_buckets_ && right.positive_buckets_)
345347
{
346348
for (auto i = pos_min_index; i <= pos_max_index; i++)
347349
{
348-
auto l_cnt = left.positive_buckets_.Get(i);
349-
auto r_cnt = right.positive_buckets_.Get(i);
350+
auto l_cnt = left.positive_buckets_->Get(i);
351+
auto r_cnt = right.positive_buckets_->Get(i);
350352
// expect right >= left since metric points should be monotonically increasing
351-
auto delta = (std::max)(static_cast<uint64_t>(0), r_cnt - l_cnt);
352-
if (l_cnt > 0)
353+
auto delta = std::max(static_cast<uint64_t>(0), r_cnt - l_cnt);
354+
if (delta > 0)
353355
{
354-
result_value.positive_buckets_.Increment(i, delta);
356+
result_value.positive_buckets_->Increment(i, delta);
355357
}
356358
}
357359
}
358360

359-
if (!left.negative_buckets_.Empty() && !right.negative_buckets_.Empty())
361+
if (left.negative_buckets_ && right.negative_buckets_)
360362
{
361363
for (auto i = neg_min_index; i <= neg_max_index; i++)
362364
{
363-
auto l_cnt = left.negative_buckets_.Get(i);
364-
auto r_cnt = right.negative_buckets_.Get(i);
365+
auto l_cnt = left.negative_buckets_->Get(i);
366+
auto r_cnt = right.negative_buckets_->Get(i);
365367
// expect right >= left since metric points should be monotonically increasing
366-
auto delta = (std::max)(static_cast<uint64_t>(0), r_cnt - l_cnt);
368+
auto delta = std::max(static_cast<uint64_t>(0), r_cnt - l_cnt);
367369
if (delta > 0)
368370
{
369-
result_value.negative_buckets_.Increment(i, delta);
371+
result_value.negative_buckets_->Increment(i, delta);
370372
}
371373
}
372374
}
@@ -378,7 +380,28 @@ std::unique_ptr<Aggregation> Base2ExponentialHistogramAggregation::Diff(
378380
PointType Base2ExponentialHistogramAggregation::ToPoint() const noexcept
379381
{
380382
const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_);
381-
return point_data_;
383+
384+
Base2ExponentialHistogramPointData copy;
385+
copy.sum_ = point_data_.sum_;
386+
copy.min_ = point_data_.min_;
387+
copy.max_ = point_data_.max_;
388+
copy.zero_threshold_ = point_data_.zero_threshold_;
389+
copy.count_ = point_data_.count_;
390+
copy.zero_count_ = point_data_.zero_count_;
391+
copy.max_buckets_ = point_data_.max_buckets_;
392+
copy.scale_ = point_data_.scale_;
393+
copy.record_min_max_ = point_data_.record_min_max_;
394+
395+
if (point_data_.positive_buckets_)
396+
{
397+
copy.positive_buckets_ = std::make_unique<AdaptingCircularBufferCounter>(*point_data_.positive_buckets_);
398+
}
399+
if (point_data_.negative_buckets_)
400+
{
401+
copy.negative_buckets_ = std::make_unique<AdaptingCircularBufferCounter>(*point_data_.negative_buckets_);
402+
}
403+
404+
return copy;
382405
}
383406

384407
} // namespace metrics

0 commit comments

Comments
 (0)