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

chore: Remove manual memory management from stats #2550

Merged
merged 1 commit into from
Feb 10, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/server/server_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1659,7 +1659,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());
Expand Down
40 changes: 8 additions & 32 deletions src/server/server_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this assertion is better move under the Stats definition

Copy link
Contributor

Choose a reason for hiding this comment

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

and instead of 8 use sizeof(int64_t)


for (int i = 0; i < NUM_TX_TYPES; ++i) {
this->tx_type_cnt[i] += other.tx_type_cnt[i];
Expand All @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

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

should never happen

}

return *this;
}

Expand Down Expand Up @@ -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() {
Expand Down
28 changes: 11 additions & 17 deletions src/server/server_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include <optional>
#include <valarray>
#include <vector>

#include "base/histogram.h"
Expand Down Expand Up @@ -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;

Expand All @@ -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.
Expand Down
7 changes: 2 additions & 5 deletions src/server/test_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading