Skip to content
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
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions core/benchmarks/counter_bench.cc
Original file line number Diff line number Diff line change
@@ -1,23 +1,54 @@
#include <benchmark/benchmark.h>
#include <prometheus/registry.h>

static void BM_Counter_IncrementBaseline(benchmark::State& state) {
struct {
void Increment() { v += 1.0; }
double v;
} counter;

for (auto _ : state) {
counter.Increment();
}
benchmark::DoNotOptimize(counter.v);
}
BENCHMARK(BM_Counter_IncrementBaseline);

static void BM_Counter_Increment(benchmark::State& state) {
using prometheus::Registry;
using prometheus::Counter;
using prometheus::BuildCounter;
using prometheus::Counter;
using prometheus::Registry;
Registry registry;
auto& counter_family =
BuildCounter().Name("benchmark_counter").Help("").Register(registry);
auto& counter = counter_family.Add({});

while (state.KeepRunning()) counter.Increment();
for (auto _ : state) {
counter.Increment();
}
benchmark::DoNotOptimize(counter.Value());
}
BENCHMARK(BM_Counter_Increment);

class BM_Counter : public benchmark::Fixture {
protected:
BM_Counter() { this->ThreadPerCpu(); }

prometheus::Counter counter{};
};

BENCHMARK_F(BM_Counter, ConcurrentIncrement)
(benchmark::State& state) {
for (auto _ : state) {
counter.Increment();
}
benchmark::DoNotOptimize(counter.Value());
}

static void BM_Counter_Collect(benchmark::State& state) {
using prometheus::Registry;
using prometheus::Counter;
using prometheus::BuildCounter;
using prometheus::Counter;
using prometheus::Registry;
Registry registry;
auto& counter_family =
BuildCounter().Name("benchmark_counter").Help("").Register(registry);
Expand Down
30 changes: 22 additions & 8 deletions core/benchmarks/gauge_bench.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#include <prometheus/registry.h>

static void BM_Gauge_Increment(benchmark::State& state) {
using prometheus::Registry;
using prometheus::Gauge;
using prometheus::BuildGauge;
using prometheus::Gauge;
using prometheus::Registry;
Registry registry;
auto& gauge_family =
BuildGauge().Name("benchmark_gauge").Help("").Register(registry);
Expand All @@ -14,10 +14,24 @@ static void BM_Gauge_Increment(benchmark::State& state) {
}
BENCHMARK(BM_Gauge_Increment);

class BM_Gauge : public benchmark::Fixture {
protected:
BM_Gauge() { this->ThreadPerCpu(); }

prometheus::Gauge gauge{};
};

BENCHMARK_F(BM_Gauge, ConcurrentIncrement)
(benchmark::State& state) {
for (auto _ : state) {
gauge.Increment();
}
}

static void BM_Gauge_Decrement(benchmark::State& state) {
using prometheus::Registry;
using prometheus::Gauge;
using prometheus::BuildGauge;
using prometheus::Gauge;
using prometheus::Registry;
Registry registry;
auto& gauge_family =
BuildGauge().Name("benchmark_gauge").Help("").Register(registry);
Expand All @@ -28,9 +42,9 @@ static void BM_Gauge_Decrement(benchmark::State& state) {
BENCHMARK(BM_Gauge_Decrement);

static void BM_Gauge_SetToCurrentTime(benchmark::State& state) {
using prometheus::Registry;
using prometheus::Gauge;
using prometheus::BuildGauge;
using prometheus::Gauge;
using prometheus::Registry;
Registry registry;
auto& gauge_family =
BuildGauge().Name("benchmark_gauge").Help("").Register(registry);
Expand All @@ -41,9 +55,9 @@ static void BM_Gauge_SetToCurrentTime(benchmark::State& state) {
BENCHMARK(BM_Gauge_SetToCurrentTime);

static void BM_Gauge_Collect(benchmark::State& state) {
using prometheus::Registry;
using prometheus::Gauge;
using prometheus::BuildGauge;
using prometheus::Gauge;
using prometheus::Registry;
Registry registry;
auto& gauge_family =
BuildGauge().Name("benchmark_gauge").Help("").Register(registry);
Expand Down
58 changes: 53 additions & 5 deletions core/include/prometheus/counter.h
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 {
Expand All @@ -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.
Expand All @@ -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;
Expand All @@ -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();
Copy link

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?

}

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};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to remove the load there?
We can keep a second counter (eg: Cacheline.localV) that is not atomic (and does not need to be, as it is only accessed from this thread). Only the store needs to be atomic, right?

(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
3 changes: 3 additions & 0 deletions core/include/prometheus/gauge.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ namespace prometheus {
/// memory usage, but also "counts" that can go up and down, like the number of
/// running processes.
///
/// If an montonically increasing counter is applicable a Counter shall be
/// prefered to a Gauge because of a better update performance.
///
/// The class is thread-safe. No concurrent call to any API of this type causes
/// a data race.
class Gauge {
Expand Down
12 changes: 7 additions & 5 deletions core/src/counter.cc
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "prometheus/counter.h"

namespace prometheus {

void Counter::Increment() { gauge_.Increment(); }
#include <numeric>

void Counter::Increment(const double val) { gauge_.Increment(val); }
namespace prometheus {

double Counter::Value() const { return gauge_.Value(); }
double Counter::Value() const {
return std::accumulate(
std::begin(per_thread_counter_), std::end(per_thread_counter_), 0.0,
[](const double a, const CacheLine& b) { return a + b.v; });
}

ClientMetric Counter::Collect() const {
ClientMetric metric;
Expand Down
46 changes: 46 additions & 0 deletions core/tests/counter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

#include <gmock/gmock.h>

#include <thread>
#include <vector>

#include <prometheus/counter.h>

namespace prometheus {
namespace {

Expand Down Expand Up @@ -37,5 +42,46 @@ TEST(CounterTest, inc_negative_value) {
EXPECT_EQ(counter.Value(), 5.0);
}

TEST(CounterTest, concurrent_writes) {
Counter counter;
std::vector<std::thread> threads(std::thread::hardware_concurrency());

for (auto& thread : threads) {
thread = std::thread{[&counter]() {
for (int i{0}; i < 100000; ++i) {
counter.Increment();
}
}};
}

for (auto& thread : threads) {
thread.join();
}

EXPECT_EQ(100000 * threads.size(), counter.Value());
}

TEST(CounterTest, concurrent_read_write) {
Counter counter;
std::vector<double> values;
values.reserve(100000);

std::thread reader{[&counter, &values]() {
for (int i{0}; i < 100000; ++i) {
values.push_back(counter.Value());
}
}};
std::thread writer{[&counter]() {
for (int i{0}; i < 100000; ++i) {
counter.Increment();
}
}};

reader.join();
writer.join();

EXPECT_TRUE(std::is_sorted(std::begin(values), std::end(values)));
}

} // namespace
} // namespace prometheus