-
Notifications
You must be signed in to change notification settings - Fork 455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SDK] Add AdaptingCircularBufferCounter for exponential histograms #2158
Merged
Merged
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a4f05d0
Add AdaptingCircularBufferCounter for exponential histograms
5b8cdb8
do not use TEST_F unless needed
59c9897
add one more test and update CMakeLists.txt so that code coverage is …
4d7f769
rename test to avoid name conflict
cb6753f
Merge branch 'main' of github.com:open-telemetry/opentelemetry-cpp in…
553a6b7
format fixes
773775e
silence msvc
12a4450
improve coverage a bit and simplify ToBufferIndex
e502a8b
replace erronous int by size_t & fix misleading comment
2dd5ed9
hopefuly fix msvc a bit more
6f78173
Merge branch 'main' of github.com:open-telemetry/opentelemetry-cpp in…
394af67
Merge branch 'main' into expo-histo
esigo 3c9dcd7
Merge branch 'main' into expo-histo
esigo a00630a
Merge branch 'main' into expo-histo
esigo 71cb32c
fix msvc, make index signed, add moving constructors
7f3687e
add comments
4a34046
fix one more sign compiler warning
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
sdk/include/opentelemetry/sdk/metrics/data/circular_buffer.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include "opentelemetry/nostd/variant.h" | ||
|
||
#include <limits> | ||
#include <vector> | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace metrics | ||
{ | ||
|
||
/** | ||
* An integer array that automatically expands its memory consumption (via copy/allocation) when | ||
* reaching limits. This assumes counts remain low, to lower memory overhead. | ||
* | ||
* This class is NOT thread-safe. It is expected to be behind a synchronized incrementer. | ||
* | ||
* Instances start by attempting to store one-byte per-cell in the integer array. As values grow, | ||
* this will automatically instantiate the next-size integer array (uint8_t -> uint16_t -> uint32_t | ||
* -> uint64_t) and copy over values into the larger array. This class expects most usage to remain | ||
* within the uint8_t boundary (e.g. cell values < 256). | ||
*/ | ||
class AdaptingIntegerArray | ||
{ | ||
public: | ||
// Construct an adapting integer array of a given size. | ||
explicit AdaptingIntegerArray(size_t size) : backing_(std::vector<uint8_t>(size, 0)) {} | ||
AdaptingIntegerArray(const AdaptingIntegerArray &other) = default; | ||
AdaptingIntegerArray &operator=(const AdaptingIntegerArray &other) = default; | ||
|
||
/** | ||
* Increments the value at the specified index by the given count in the array. | ||
* | ||
* @param index The index of the value to increment. | ||
* @param count The count by which to increment the value. | ||
*/ | ||
void Increment(size_t index, uint64_t count); | ||
|
||
/** | ||
* Returns the value at the specified index from the array. | ||
* | ||
* @param index The index of the value to retrieve. | ||
* @return The value at the specified index. | ||
*/ | ||
uint64_t Get(size_t index) const; | ||
|
||
/** | ||
* Returns the size of the array. | ||
* | ||
* @return The size of the array. | ||
*/ | ||
size_t Size() const; | ||
|
||
/** | ||
* Clears the array, resetting all values to zero. | ||
*/ | ||
void Clear(); | ||
|
||
private: | ||
void EnlargeToFit(uint64_t value); | ||
|
||
nostd::variant<std::vector<uint8_t>, | ||
std::vector<uint16_t>, | ||
std::vector<uint32_t>, | ||
std::vector<uint64_t>> | ||
backing_; | ||
}; | ||
|
||
/** | ||
* A circle-buffer-backed exponential counter. | ||
* | ||
* The first recorded value becomes the 'base_index'. Going backwards leads to start/stop index. | ||
* | ||
* This expand start/end index as it sees values. | ||
* | ||
* This class is NOT thread-safe. It is expected to be behind a synchronized incrementer. | ||
*/ | ||
class AdaptingCircularBufferCounter | ||
{ | ||
public: | ||
explicit AdaptingCircularBufferCounter(size_t max_size) : backing_(max_size) {} | ||
AdaptingCircularBufferCounter(const AdaptingCircularBufferCounter &other) = default; | ||
AdaptingCircularBufferCounter &operator=(const AdaptingCircularBufferCounter &other) = default; | ||
|
||
/** | ||
* The first index with a recording. May be negative. | ||
* | ||
* Note: the returned value is not meaningful when Empty returns true. | ||
* | ||
* @return the first index with a recording. | ||
*/ | ||
size_t StartIndex() const { return start_index_; } | ||
|
||
/** | ||
* The last index with a recording. May be negative. | ||
* | ||
* Note: the returned value is not meaningful when Empty returns true. | ||
* | ||
* @return The last index with a recording. | ||
*/ | ||
size_t EndIndex() const { return end_index_; } | ||
|
||
/** | ||
* Returns true if no recordings, false if at least one recording. | ||
*/ | ||
bool Empty() const { return base_index_ == kNullIndex; } | ||
|
||
/** | ||
* Returns the maximum number of buckets allowed in this counter. | ||
*/ | ||
size_t MaxSize() const { return backing_.Size(); } | ||
|
||
/** Resets all bucket counts to zero and resets index start/end tracking. **/ | ||
void Clear(); | ||
|
||
/** | ||
* Persist new data at index, incrementing by delta amount. | ||
* | ||
* @param index The index of where to perform the incrementation. | ||
* @param delta How much to increment the index by. | ||
* @return success status. | ||
*/ | ||
bool Increment(size_t index, uint64_t delta); | ||
|
||
/** | ||
* Get the number of recordings for the given index. | ||
* | ||
* @return the number of recordings for the index, or 0 if the index is out of bounds. | ||
*/ | ||
uint64_t Get(size_t index); | ||
|
||
private: | ||
size_t ToBufferIndex(size_t index) const; | ||
|
||
static constexpr size_t kNullIndex = std::numeric_limits<size_t>::max(); | ||
|
||
size_t start_index_ = kNullIndex; | ||
size_t end_index_ = kNullIndex; | ||
size_t base_index_ = kNullIndex; | ||
AdaptingIntegerArray backing_; | ||
}; | ||
|
||
} // namespace metrics | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "opentelemetry/sdk/metrics/data/circular_buffer.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; | ||
OPENTELEMETRY_LIKELY_IF(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> | ||
uint64_t operator()(const std::vector<T> &backing) | ||
{ | ||
return backing.size(); | ||
} | ||
}; | ||
|
||
struct AdaptingIntegerArrayClear | ||
{ | ||
template <typename T> | ||
void operator()(std::vector<T> &backing) | ||
{ | ||
std::fill(backing.begin(), backing.end(), 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you may have to typecast to |
||
} | ||
}; | ||
|
||
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) | ||
{ | ||
const uint64_t result = nostd::visit(AdaptingIntegerArrayIncrement{index, count}, backing_); | ||
OPENTELEMETRY_LIKELY_IF(result == 0) { return; } | ||
EnlargeToFit(result); | ||
Increment(index, count); | ||
} | ||
|
||
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); | ||
} | ||
std::swap(backing_, backing); | ||
nostd::visit(AdaptingIntegerArrayCopy{}, backing, backing_); | ||
} | ||
|
||
void AdaptingCircularBufferCounter::Clear() | ||
{ | ||
start_index_ = kNullIndex; | ||
end_index_ = kNullIndex; | ||
base_index_ = kNullIndex; | ||
backing_.Clear(); | ||
} | ||
|
||
bool AdaptingCircularBufferCounter::Increment(size_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 > backing_.Size() + start_index_) | ||
{ | ||
return false; | ||
} | ||
end_index_ = index; | ||
} | ||
else if (index < start_index_) | ||
{ | ||
// Move end, check max size. | ||
if (end_index_ + 1 > backing_.Size() + index) | ||
{ | ||
return false; | ||
} | ||
start_index_ = index; | ||
} | ||
backing_.Increment(ToBufferIndex(index), delta); | ||
return true; | ||
} | ||
|
||
uint64_t AdaptingCircularBufferCounter::Get(size_t index) | ||
{ | ||
if (index < start_index_ || index > end_index_) | ||
{ | ||
return 0; | ||
} | ||
return backing_.Get(ToBufferIndex(index)); | ||
} | ||
|
||
size_t AdaptingCircularBufferCounter::ToBufferIndex(size_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 index + backing_.Size() - base_index_; | ||
} | ||
return index - base_index_; | ||
} | ||
|
||
} // namespace metrics | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
while these variable names are self-descriptive, can you still add a comment before each for what they represent. Thanks.