From 623c7fdd7f5b74369fbd5ef684306445099cda1e Mon Sep 17 00:00:00 2001 From: imilinovic Date: Wed, 20 Dec 2023 13:36:25 +0100 Subject: [PATCH 1/3] add counter to thread tracking --- src/memory/query_memory_control.cpp | 14 +++++++++++--- src/memory/query_memory_control.hpp | 13 ++++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/memory/query_memory_control.cpp b/src/memory/query_memory_control.cpp index 91730c900a..b14d6399f2 100644 --- a/src/memory/query_memory_control.cpp +++ b/src/memory/query_memory_control.cpp @@ -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); + MG_ASSERT(elem != accessor.end() && elem->transaction_id.id == transaction_id); + elem->transaction_id.cnt--; + if (elem->transaction_id.cnt == 0) { + accessor.remove(thread_id); + } } void QueriesMemoryControl::TrackAllocOnCurrentThread(size_t size) { diff --git a/src/memory/query_memory_control.hpp b/src/memory/query_memory_control.hpp index 901917757c..248aefd911 100644 --- a/src/memory/query_memory_control.hpp +++ b/src/memory/query_memory_control.hpp @@ -78,9 +78,17 @@ 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; } + }; + 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; } @@ -98,6 +106,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 thread_id_to_transaction_id; From b6ea1beee30c7a1fa638c9100270e8212a5facb7 Mon Sep 17 00:00:00 2001 From: imilinovic Date: Thu, 11 Jan 2024 09:03:59 +0100 Subject: [PATCH 2/3] add comparator to transaction_id --- src/memory/query_memory_control.cpp | 4 ++-- src/memory/query_memory_control.hpp | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/memory/query_memory_control.cpp b/src/memory/query_memory_control.cpp index b14d6399f2..5e569bd139 100644 --- a/src/memory/query_memory_control.cpp +++ b/src/memory/query_memory_control.cpp @@ -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 @@ -47,7 +47,7 @@ void QueriesMemoryControl::UpdateThreadToTransactionId(const std::thread::id &th 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.id == transaction_id); + MG_ASSERT(elem != accessor.end() && elem->transaction_id == transaction_id); elem->transaction_id.cnt--; if (elem->transaction_id.cnt == 0) { accessor.remove(thread_id); diff --git a/src/memory/query_memory_control.hpp b/src/memory/query_memory_control.hpp index 248aefd911..3a7dd37f32 100644 --- a/src/memory/query_memory_control.hpp +++ b/src/memory/query_memory_control.hpp @@ -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 @@ -84,6 +84,9 @@ class QueriesMemoryControl { bool operator<(const TransactionId &other) const { return id < other.id; } bool operator==(const TransactionId &other) const { return id == other.id; } + + bool operator<(const uint64_t &other) const { return id < other; } + bool operator==(const uint64_t &other) const { return id == other; } }; struct ThreadIdToTransactionId { From f1aec94205c59f9a346df2bd9dd350f780327bb0 Mon Sep 17 00:00:00 2001 From: imilinovic Date: Thu, 11 Jan 2024 09:05:26 +0100 Subject: [PATCH 3/3] change function params --- src/memory/query_memory_control.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/memory/query_memory_control.hpp b/src/memory/query_memory_control.hpp index 3a7dd37f32..3852027a58 100644 --- a/src/memory/query_memory_control.hpp +++ b/src/memory/query_memory_control.hpp @@ -85,8 +85,8 @@ class QueriesMemoryControl { bool operator<(const TransactionId &other) const { return id < other.id; } bool operator==(const TransactionId &other) const { return id == other.id; } - bool operator<(const uint64_t &other) const { return id < other; } - bool operator==(const uint64_t &other) const { return id == other; } + bool operator<(uint64_t other) const { return id < other; } + bool operator==(uint64_t other) const { return id == other; } }; struct ThreadIdToTransactionId {