Skip to content

Commit

Permalink
Improve memory tracking (#1631)
Browse files Browse the repository at this point in the history
  • Loading branch information
imilinovic committed Jan 14, 2024
1 parent 0a7a7bc commit 23dff58
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/memory/query_memory_control.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Memgraph Ltd.
// Copyright 2024 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
Expand Down Expand Up @@ -36,14 +36,22 @@ namespace memgraph::memory {

void QueriesMemoryControl::UpdateThreadToTransactionId(const std::thread::id &thread_id, uint64_t transaction_id) {
auto accessor = thread_id_to_transaction_id.access();
accessor.insert({thread_id, transaction_id});
auto elem = accessor.find(thread_id);
if (elem == accessor.end()) {
accessor.insert({thread_id, {transaction_id, 1}});
} else {
elem->transaction_id.cnt++;
}
}

void QueriesMemoryControl::EraseThreadToTransactionId(const std::thread::id &thread_id, uint64_t transaction_id) {
auto accessor = thread_id_to_transaction_id.access();
auto elem = accessor.find(thread_id);
MG_ASSERT(elem != accessor.end() && elem->transaction_id == transaction_id);
accessor.remove(thread_id);
elem->transaction_id.cnt--;
if (elem->transaction_id.cnt == 0) {
accessor.remove(thread_id);
}
}

void QueriesMemoryControl::TrackAllocOnCurrentThread(size_t size) {
Expand Down
18 changes: 16 additions & 2 deletions src/memory/query_memory_control.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Memgraph Ltd.
// Copyright 2024 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
Expand Down Expand Up @@ -78,9 +78,20 @@ class QueriesMemoryControl {
bool IsThreadTracked();

private:
struct TransactionId {
uint64_t id;
uint64_t cnt;

bool operator<(const TransactionId &other) const { return id < other.id; }
bool operator==(const TransactionId &other) const { return id == other.id; }

bool operator<(uint64_t other) const { return id < other; }
bool operator==(uint64_t other) const { return id == other; }
};

struct ThreadIdToTransactionId {
std::thread::id thread_id;
uint64_t transaction_id;
TransactionId transaction_id;

bool operator<(const ThreadIdToTransactionId &other) const { return thread_id < other.thread_id; }
bool operator==(const ThreadIdToTransactionId &other) const { return thread_id == other.thread_id; }
Expand All @@ -98,6 +109,9 @@ class QueriesMemoryControl {

bool operator<(uint64_t other) const { return transaction_id < other; }
bool operator==(uint64_t other) const { return transaction_id == other; }

bool operator<(TransactionId other) const { return transaction_id < other.id; }
bool operator==(TransactionId other) const { return transaction_id == other.id; }
};

utils::SkipList<ThreadIdToTransactionId> thread_id_to_transaction_id;
Expand Down

0 comments on commit 23dff58

Please sign in to comment.