-
Notifications
You must be signed in to change notification settings - Fork 337
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
Scalable counter #179
Open
jerryct
wants to merge
3
commits into
jupp0r:master
Choose a base branch
from
jerryct:scalable_counter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Scalable counter #179
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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 |
---|---|---|
@@ -1,7 +1,10 @@ | ||
#pragma once | ||
|
||
#include <array> | ||
#include <atomic> | ||
#include <exception> | ||
|
||
#include "prometheus/client_metric.h" | ||
#include "prometheus/gauge.h" | ||
#include "prometheus/metric_type.h" | ||
|
||
namespace prometheus { | ||
|
@@ -17,7 +20,17 @@ namespace prometheus { | |
/// - errors | ||
/// | ||
/// Do not use a counter to expose a value that can decrease - instead use a | ||
/// Gauge. | ||
/// Gauge. If an montonically increasing counter is applicable a counter shall | ||
/// be prefered to a Gauge because of a better update performance. | ||
/// | ||
/// The implementation exhibits a performance which is near a sequential | ||
/// implementation and scales linearly with increasing number of updater threads | ||
/// in a multi-threaded environment invoking Increment(). However, this | ||
/// excellent update-side scalability comes at read-side expense invoking | ||
/// Collect(). Increment() can therefor be used in the fast-path of the code, | ||
/// where the count is updated extremely frequently. The Collect() function on | ||
/// the other hand shall read the counter at a low sample rate, e.g., in the | ||
/// order of milliseconds. | ||
/// | ||
/// The class is thread-safe. No concurrent call to any API of this type causes | ||
/// a data race. | ||
|
@@ -29,12 +42,17 @@ class Counter { | |
Counter() = default; | ||
|
||
/// \brief Increment the counter by 1. | ||
void Increment(); | ||
void Increment() { IncrementUnchecked(1.0); } | ||
|
||
/// \brief Increment the counter by a given amount. | ||
/// | ||
/// The counter will not change if the given amount is negative. | ||
void Increment(double); | ||
void Increment(const double value) { | ||
if (value < 0.0) { | ||
return; | ||
} | ||
IncrementUnchecked(value); | ||
} | ||
|
||
/// \brief Get the current value of the counter. | ||
double Value() const; | ||
|
@@ -45,7 +63,37 @@ class Counter { | |
ClientMetric Collect() const; | ||
|
||
private: | ||
Gauge gauge_{0.0}; | ||
int ThreadId() { | ||
thread_local int id{-1}; | ||
|
||
if (id == -1) { | ||
id = AssignThreadId(); | ||
} | ||
return id; | ||
} | ||
|
||
int AssignThreadId() { | ||
const int id{count_.fetch_add(1)}; | ||
|
||
if (id >= per_thread_counter_.size()) { | ||
std::terminate(); | ||
} | ||
|
||
return id; | ||
} | ||
|
||
void IncrementUnchecked(const double v) { | ||
CacheLine& c = per_thread_counter_[ThreadId()]; | ||
const double new_value{c.v.load(std::memory_order_relaxed) + v}; | ||
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. is it possible to remove the load there? (just passing by. thanks for the book recommendation!) |
||
c.v.store(new_value, std::memory_order_relaxed); | ||
} | ||
|
||
struct alignas(128) CacheLine { | ||
std::atomic<double> v{0.0}; | ||
}; | ||
|
||
std::atomic<int> count_{0}; | ||
std::array<CacheLine, 256> per_thread_counter_{}; | ||
}; | ||
|
||
} // namespace prometheus |
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
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
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.
You may initialize zero element in
per_thread_counter_
and use it if threads count goes beyond of array. But this does not work with load/store scheme. Why are not you using fetch_add with relaxed policy?