diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
commit | e6918187568dbd01842d8d1d2c808ce16a894239 (patch) | |
tree | 64f88b554b444a49f656b6c656111a145cbbaa28 /src/jaegertracing/opentelemetry-cpp/sdk/test/common/baseline_circular_buffer.h | |
parent | Initial commit. (diff) | |
download | ceph-e6918187568dbd01842d8d1d2c808ce16a894239.tar.xz ceph-e6918187568dbd01842d8d1d2c808ce16a894239.zip |
Adding upstream version 18.2.2.upstream/18.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/jaegertracing/opentelemetry-cpp/sdk/test/common/baseline_circular_buffer.h')
-rw-r--r-- | src/jaegertracing/opentelemetry-cpp/sdk/test/common/baseline_circular_buffer.h | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/jaegertracing/opentelemetry-cpp/sdk/test/common/baseline_circular_buffer.h b/src/jaegertracing/opentelemetry-cpp/sdk/test/common/baseline_circular_buffer.h new file mode 100644 index 000000000..398a4d038 --- /dev/null +++ b/src/jaegertracing/opentelemetry-cpp/sdk/test/common/baseline_circular_buffer.h @@ -0,0 +1,88 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include <cstdint> +#include <memory> +#include <mutex> +#include <vector> + +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace testing +{ +/** + * A locking circular buffer. + * + * Used as a baseline in benchmarking. + */ +template <class T> +class BaselineCircularBuffer +{ +public: + explicit BaselineCircularBuffer(size_t max_size) : data_{max_size} {} + + /** + * Add an element to the circular buffer. + * @param element the element to add + * @return true if the element was added successfully + */ + bool Add(std::unique_ptr<T> &element) noexcept { return this->Add(std::move(element)); } + + bool Add(std::unique_ptr<T> &&element) noexcept + { + std::lock_guard<std::mutex> lock_guard{mutex_}; + if (tail_ + data_.size() == head_) + { + return false; + } + data_[head_ % data_.size()] = std::move(element); + head_ += 1; + return true; + } + + /** + * Consume elements in the circular buffer. + * @param f the callback to call for each element + */ + template <class F> + void Consume(F f) noexcept + { + std::lock_guard<std::mutex> lock_guard{mutex_}; + if (head_ == tail_) + { + return; + } + auto tail_index = tail_ % data_.size(); + auto head_index = head_ % data_.size(); + if (tail_index < head_index) + { + for (auto i = tail_index; i < head_index; ++i) + { + f(std::move(data_[i])); + } + } + else + { + for (auto i = tail_index; i < data_.size(); ++i) + { + f(std::move(data_[i])); + } + for (auto i = 0ull; i < head_index; ++i) + { + f(std::move(data_[i])); + } + } + tail_ = head_; + } + +private: + std::mutex mutex_; + uint64_t head_{0}; + uint64_t tail_{0}; + std::vector<std::unique_ptr<T>> data_; +}; +} // namespace testing +OPENTELEMETRY_END_NAMESPACE |