@@ -41,9 +41,9 @@ uint32_t GetScaleReduction(int32_t start_index, int32_t end_index, size_t max_bu
41
41
return scale_reduction;
42
42
}
43
43
44
- void DownscaleBuckets (AdaptingCircularBufferCounter &buckets, uint32_t by) noexcept
44
+ void DownscaleBuckets (std::unique_ptr< AdaptingCircularBufferCounter> &buckets, uint32_t by) noexcept
45
45
{
46
- if (buckets. Empty ())
46
+ if (buckets-> Empty ())
47
47
{
48
48
return ;
49
49
}
@@ -52,15 +52,15 @@ void DownscaleBuckets(AdaptingCircularBufferCounter &buckets, uint32_t by) noexc
52
52
// Instead of creating a new counter, we copy the existing one (for bucket size
53
53
// optimisations), and clear the values before writing the new ones.
54
54
// 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 ();
57
57
58
- for (auto i = buckets. StartIndex (); i <= buckets. EndIndex (); ++i)
58
+ for (auto i = buckets-> StartIndex (); i <= buckets-> EndIndex (); ++i)
59
59
{
60
- const uint64_t count = buckets. Get (i);
60
+ const uint64_t count = buckets-> Get (i);
61
61
if (count > 0 )
62
62
{
63
- new_buckets. Increment (i >> by, count);
63
+ new_buckets-> Increment (i >> by, count);
64
64
}
65
65
}
66
66
buckets = std::move (new_buckets);
@@ -87,19 +87,19 @@ Base2ExponentialHistogramAggregation::Base2ExponentialHistogramAggregation(
87
87
indexer_ = Base2ExponentialHistogramIndexer (point_data_.scale_ );
88
88
}
89
89
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
+ // {}
96
96
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
+ // {}
103
103
104
104
void Base2ExponentialHistogramAggregation::Aggregate (
105
105
int64_t value,
@@ -138,24 +138,24 @@ void Base2ExponentialHistogramAggregation::Aggregate(
138
138
}
139
139
140
140
void Base2ExponentialHistogramAggregation::AggregateIntoBuckets (
141
- AdaptingCircularBufferCounter &buckets,
141
+ std::unique_ptr< AdaptingCircularBufferCounter> &buckets,
142
142
double value) noexcept
143
143
{
144
- if (buckets. MaxSize () == 0 )
144
+ if (buckets-> MaxSize () == 0 )
145
145
{
146
- buckets = AdaptingCircularBufferCounter{ point_data_.max_buckets_ } ;
146
+ buckets = std::make_unique< AdaptingCircularBufferCounter>( point_data_.max_buckets_ ) ;
147
147
}
148
148
149
149
const int32_t index = indexer_.ComputeIndex (value);
150
- if (!buckets. Increment (index , 1 ))
150
+ if (!buckets-> Increment (index , 1 ))
151
151
{
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 );
154
154
const uint32_t scale_reduction =
155
155
GetScaleReduction (start_index, end_index, point_data_.max_buckets_ );
156
156
Downscale (scale_reduction);
157
157
158
- buckets. Increment (index >> scale_reduction, 1 );
158
+ buckets-> Increment (index >> scale_reduction, 1 );
159
159
}
160
160
}
161
161
@@ -247,13 +247,13 @@ std::unique_ptr<Aggregation> Base2ExponentialHistogramAggregation::Merge(
247
247
}
248
248
249
249
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 ());
251
251
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 ());
253
253
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 ());
255
255
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 ());
257
257
258
258
if (static_cast <size_t >(pos_max_index) >
259
259
static_cast <size_t >(pos_min_index) + result_value.max_buckets_ ||
@@ -272,10 +272,10 @@ std::unique_ptr<Aggregation> Base2ExponentialHistogramAggregation::Merge(
272
272
result_value.scale_ -= scale_reduction;
273
273
}
274
274
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_ ) );
279
279
280
280
return std::unique_ptr<Base2ExponentialHistogramAggregation>{
281
281
new Base2ExponentialHistogramAggregation (std::move (result_value))};
@@ -303,13 +303,13 @@ std::unique_ptr<Aggregation> Base2ExponentialHistogramAggregation::Diff(
303
303
}
304
304
305
305
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 ());
307
307
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 ());
309
309
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 ());
311
311
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 ());
313
313
314
314
if (static_cast <size_t >(pos_max_index) >
315
315
static_cast <size_t >(pos_min_index) + low_res.max_buckets_ ||
@@ -338,35 +338,37 @@ std::unique_ptr<Aggregation> Base2ExponentialHistogramAggregation::Diff(
338
338
result_value.sum_ = (right.sum_ >= left.sum_ ) ? (right.sum_ - left.sum_ ) : 0.0 ;
339
339
result_value.zero_count_ =
340
340
(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_ );
343
345
344
- if (! left.positive_buckets_ . Empty () && ! right.positive_buckets_ . Empty () )
346
+ if (left.positive_buckets_ && right.positive_buckets_ )
345
347
{
346
348
for (auto i = pos_min_index; i <= pos_max_index; i++)
347
349
{
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);
350
352
// 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 )
353
355
{
354
- result_value.positive_buckets_ . Increment (i, delta);
356
+ result_value.positive_buckets_ -> Increment (i, delta);
355
357
}
356
358
}
357
359
}
358
360
359
- if (! left.negative_buckets_ . Empty () && ! right.negative_buckets_ . Empty () )
361
+ if (left.negative_buckets_ && right.negative_buckets_ )
360
362
{
361
363
for (auto i = neg_min_index; i <= neg_max_index; i++)
362
364
{
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);
365
367
// 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);
367
369
if (delta > 0 )
368
370
{
369
- result_value.negative_buckets_ . Increment (i, delta);
371
+ result_value.negative_buckets_ -> Increment (i, delta);
370
372
}
371
373
}
372
374
}
@@ -378,7 +380,28 @@ std::unique_ptr<Aggregation> Base2ExponentialHistogramAggregation::Diff(
378
380
PointType Base2ExponentialHistogramAggregation::ToPoint () const noexcept
379
381
{
380
382
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;
382
405
}
383
406
384
407
} // namespace metrics
0 commit comments