chore: Remove manual memory management from stats (#2550)

Simplift stats memory management

Signed-off-by: Vladislav Oleshko <vlad@dragonflydb.io>
This commit is contained in:
Vladislav 2024-02-10 10:30:30 +03:00 committed by GitHub
parent 908efff7bd
commit 815976c9dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 22 additions and 55 deletions

View file

@ -1674,7 +1674,7 @@ Metrics ServerFamily::GetMetrics() const {
result.fiber_longrun_cnt += fb2::FiberLongRunCnt();
result.fiber_longrun_usec += fb2::FiberLongRunSumUsec();
result.coordinator_stats.Add(shard_set->size(), ss->stats);
result.coordinator_stats.Add(ss->stats);
result.uptime = time(NULL) - this->start_time_;
result.qps += uint64_t(ss->MovingSum6());

View file

@ -23,32 +23,12 @@ namespace dfly {
__thread ServerState* ServerState::state_ = nullptr;
ServerState::Stats::Stats() {
ServerState::Stats::Stats(unsigned num_shards) : tx_width_freq_arr(num_shards) {
tx_type_cnt.fill(0);
}
ServerState::Stats::~Stats() {
delete[] tx_width_freq_arr;
}
auto ServerState::Stats::operator=(Stats&& other) -> Stats& {
for (int i = 0; i < NUM_TX_TYPES; ++i) {
this->tx_type_cnt[i] = other.tx_type_cnt[i];
}
this->eval_io_coordination_cnt = other.eval_io_coordination_cnt;
this->eval_shardlocal_coordination_cnt = other.eval_shardlocal_coordination_cnt;
this->eval_squashed_flushes = other.eval_squashed_flushes;
this->tx_schedule_cancel_cnt = other.tx_schedule_cancel_cnt;
delete[] this->tx_width_freq_arr;
this->tx_width_freq_arr = other.tx_width_freq_arr;
other.tx_width_freq_arr = nullptr;
return *this;
}
ServerState::Stats& ServerState::Stats::Add(unsigned num_shards, const ServerState::Stats& other) {
static_assert(sizeof(Stats) == 13 * 8, "Stats size mismatch");
ServerState::Stats& ServerState::Stats::Add(const ServerState::Stats& other) {
static_assert(sizeof(Stats) == 14 * 8, "Stats size mismatch");
for (int i = 0; i < NUM_TX_TYPES; ++i) {
this->tx_type_cnt[i] += other.tx_type_cnt[i];
@ -65,15 +45,12 @@ ServerState::Stats& ServerState::Stats::Add(unsigned num_shards, const ServerSta
this->blocked_on_interpreter += other.blocked_on_interpreter;
if (this->tx_width_freq_arr == nullptr) {
this->tx_width_freq_arr = new uint64_t[num_shards];
std::copy_n(other.tx_width_freq_arr, num_shards, this->tx_width_freq_arr);
if (this->tx_width_freq_arr.size() > 0) {
DCHECK_EQ(this->tx_width_freq_arr.size(), other.tx_width_freq_arr.size());
this->tx_width_freq_arr += other.tx_width_freq_arr;
} else {
for (unsigned i = 0; i < num_shards; ++i) {
this->tx_width_freq_arr[i] += other.tx_width_freq_arr[i];
}
this->tx_width_freq_arr = other.tx_width_freq_arr;
}
return *this;
}
@ -121,8 +98,7 @@ void ServerState::Init(uint32_t thread_index, uint32_t num_shards, acl::UserRegi
state_->gstate_ = GlobalState::ACTIVE;
state_->thread_index_ = thread_index;
state_->user_registry = registry;
state_->stats.tx_width_freq_arr = new uint64_t[num_shards];
std::fill_n(state_->stats.tx_width_freq_arr, num_shards, 0);
state_->stats = Stats(num_shards);
}
void ServerState::Destroy() {

View file

@ -5,6 +5,7 @@
#pragma once
#include <optional>
#include <valarray>
#include <vector>
#include "base/histogram.h"
@ -95,6 +96,15 @@ class ServerState { // public struct - to allow initialization.
public:
enum TxType { GLOBAL, NORMAL, QUICK, INLINE, NUM_TX_TYPES };
struct Stats {
Stats(unsigned num_shards = 0); // Default initialization should be valid for Add()
Stats(Stats&& other) = default;
Stats& operator=(Stats&& other) = default;
Stats(const Stats&) = delete;
Stats& operator=(const Stats& other) = delete;
Stats& Add(const Stats& other);
std::array<uint64_t, NUM_TX_TYPES> tx_type_cnt;
uint64_t tx_schedule_cancel_cnt = 0;
@ -108,23 +118,7 @@ class ServerState { // public struct - to allow initialization.
uint64_t blocked_on_interpreter = 0;
// Array of size of number of shards.
// Each entry is how many transactions we had with this width (unique_shard_cnt).
uint64_t* tx_width_freq_arr = nullptr;
Stats();
~Stats();
Stats(Stats&& other) {
*this = std::move(other);
}
Stats& operator=(Stats&& other);
Stats& Add(unsigned num_shards, const Stats& other);
Stats(const Stats&) = delete;
Stats& operator=(const Stats&) = delete;
std::valarray<uint64_t> tx_width_freq_arr;
};
// Unsafe version.

View file

@ -318,11 +318,8 @@ unsigned BaseFamilyTest::NumLocked() {
}
void BaseFamilyTest::ClearMetrics() {
shard_set->pool()->Await([](auto*) {
ServerState::Stats stats;
stats.tx_width_freq_arr = new uint64_t[shard_set->size()];
ServerState::tlocal()->stats = std::move(stats);
});
shard_set->pool()->Await(
[](auto*) { ServerState::tlocal()->stats = ServerState::Stats(shard_set->size()); });
}
void BaseFamilyTest::WaitUntilLocked(DbIndex db_index, string_view key, double timeout) {