2019-07-07 02:05:30 +02:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2019-07-07 02:05:30 +02:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
2019-08-13 22:52:54 +02:00
|
|
|
|
2019-08-14 02:13:34 +02:00
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/Slice.h"
|
2019-08-12 13:45:57 +02:00
|
|
|
#include "td/utils/StringBuilder.h"
|
2019-08-13 22:52:54 +02:00
|
|
|
#include "td/utils/ThreadLocalStorage.h"
|
2019-07-07 02:05:30 +02:00
|
|
|
|
|
|
|
#include <array>
|
2019-08-14 02:13:34 +02:00
|
|
|
#include <atomic>
|
2019-08-12 13:45:57 +02:00
|
|
|
#include <mutex>
|
2019-07-07 02:05:30 +02:00
|
|
|
|
|
|
|
namespace td {
|
2020-06-26 01:24:13 +02:00
|
|
|
|
2019-08-12 13:45:57 +02:00
|
|
|
template <size_t N>
|
|
|
|
class ThreadSafeMultiCounter {
|
|
|
|
public:
|
|
|
|
void add(size_t index, int64 diff) {
|
|
|
|
CHECK(index < N);
|
|
|
|
tls_.get()[index].fetch_add(diff, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 sum(size_t index) const {
|
|
|
|
CHECK(index < N);
|
|
|
|
int64 res = 0;
|
2020-08-04 20:37:47 +02:00
|
|
|
tls_.for_each([&res, &index](auto &value) { res += value[index].load(std::memory_order_relaxed); });
|
2019-08-12 13:45:57 +02:00
|
|
|
return res;
|
|
|
|
}
|
2020-06-24 13:47:36 +02:00
|
|
|
void clear() {
|
2020-06-26 01:24:13 +02:00
|
|
|
tls_.for_each([](auto &value) {
|
2020-06-24 13:47:36 +02:00
|
|
|
for (auto &x : value) {
|
|
|
|
x = 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-08-12 13:45:57 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
ThreadLocalStorage<std::array<std::atomic<int64>, N>> tls_;
|
|
|
|
};
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2019-07-07 02:05:30 +02:00
|
|
|
class ThreadSafeCounter {
|
|
|
|
public:
|
|
|
|
void add(int64 diff) {
|
2019-08-12 13:45:57 +02:00
|
|
|
counter_.add(0, diff);
|
2019-07-07 02:05:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int64 sum() const {
|
2019-08-12 13:45:57 +02:00
|
|
|
return counter_.sum(0);
|
2019-07-07 02:05:30 +02:00
|
|
|
}
|
|
|
|
|
2021-08-18 18:26:03 +02:00
|
|
|
void clear() {
|
|
|
|
counter_.clear();
|
|
|
|
}
|
|
|
|
|
2019-07-07 02:05:30 +02:00
|
|
|
private:
|
2019-08-12 13:45:57 +02:00
|
|
|
ThreadSafeMultiCounter<1> counter_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class NamedThreadSafeCounter {
|
|
|
|
static constexpr int N = 128;
|
|
|
|
using Counter = ThreadSafeMultiCounter<N>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
class CounterRef {
|
|
|
|
public:
|
|
|
|
CounterRef() = default;
|
|
|
|
CounterRef(size_t index, Counter *counter) : index_(index), counter_(counter) {
|
|
|
|
}
|
|
|
|
void add(int64 diff) {
|
|
|
|
counter_->add(index_, diff);
|
|
|
|
}
|
|
|
|
int64 sum() const {
|
|
|
|
return counter_->sum(index_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t index_{0};
|
|
|
|
Counter *counter_{nullptr};
|
2019-07-07 02:05:30 +02:00
|
|
|
};
|
|
|
|
|
2019-08-12 13:45:57 +02:00
|
|
|
CounterRef get_counter(Slice name) {
|
|
|
|
std::unique_lock<std::mutex> guard(mutex_);
|
|
|
|
for (size_t i = 0; i < names_.size(); i++) {
|
|
|
|
if (names_[i] == name) {
|
|
|
|
return get_counter_ref(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CHECK(names_.size() < N);
|
2020-06-24 13:47:36 +02:00
|
|
|
names_.emplace_back(name.begin(), name.size());
|
2019-08-12 13:45:57 +02:00
|
|
|
return get_counter_ref(names_.size() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
CounterRef get_counter_ref(size_t index) {
|
|
|
|
return CounterRef(index, &counter_);
|
|
|
|
}
|
|
|
|
|
|
|
|
static NamedThreadSafeCounter &get_default() {
|
|
|
|
static NamedThreadSafeCounter res;
|
|
|
|
return res;
|
2019-07-07 02:05:30 +02:00
|
|
|
}
|
2019-08-12 13:45:57 +02:00
|
|
|
|
|
|
|
template <class F>
|
|
|
|
void for_each(F &&f) const {
|
|
|
|
std::unique_lock<std::mutex> guard(mutex_);
|
|
|
|
for (size_t i = 0; i < names_.size(); i++) {
|
|
|
|
f(names_[i], counter_.sum(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
void clear() {
|
|
|
|
std::unique_lock<std::mutex> guard(mutex_);
|
|
|
|
counter_.clear();
|
|
|
|
}
|
|
|
|
|
2019-08-12 13:45:57 +02:00
|
|
|
friend StringBuilder &operator<<(StringBuilder &sb, const NamedThreadSafeCounter &counter) {
|
|
|
|
counter.for_each([&sb](Slice name, int64 cnt) { sb << name << ": " << cnt << "\n"; });
|
|
|
|
return sb;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutable std::mutex mutex_;
|
|
|
|
std::vector<std::string> names_;
|
|
|
|
|
|
|
|
Counter counter_;
|
2019-07-07 02:05:30 +02:00
|
|
|
};
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2019-07-07 02:05:30 +02:00
|
|
|
} // namespace td
|