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

Storages: Fix memory trace for UniversalPageId #9163

Merged
merged 12 commits into from
Jun 27, 2024
40 changes: 39 additions & 1 deletion dbms/src/Storages/Page/V3/Universal/UniversalPageId.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,44 @@
#include <Storages/Page/V3/Universal/UniversalPageId.h>
#include <Storages/Page/V3/Universal/UniversalPageIdFormatImpl.h>

namespace DB
{
UniversalPageId::~UniversalPageId()
{
PS::PageStorageMemorySummary::uni_page_id_bytes.fetch_sub(id.size());
}

UniversalPageId::UniversalPageId(UniversalPageId && other)
{
id = std::move(other.id);
CalvinNeo marked this conversation as resolved.
Show resolved Hide resolved
}
UniversalPageId::UniversalPageId(const UniversalPageId & other)
{
PS::PageStorageMemorySummary::uni_page_id_bytes.fetch_add(other.size());
CalvinNeo marked this conversation as resolved.
Show resolved Hide resolved
id = other.id;
}
UniversalPageId & UniversalPageId::operator=(UniversalPageId && other) noexcept
{
PS::PageStorageMemorySummary::uni_page_id_bytes.fetch_sub(size());
CalvinNeo marked this conversation as resolved.
Show resolved Hide resolved
id = std::move(other.id);
return *this;
}
UniversalPageId & UniversalPageId::operator=(const UniversalPageId & other) noexcept
{
PS::PageStorageMemorySummary::uni_page_id_bytes.fetch_sub(size());
PS::PageStorageMemorySummary::uni_page_id_bytes.fetch_add(other.size());
CalvinNeo marked this conversation as resolved.
Show resolved Hide resolved
id = other.id;
return *this;
}
UniversalPageId & UniversalPageId::operator=(String && id_) noexcept
{
PS::PageStorageMemorySummary::uni_page_id_bytes.fetch_sub(id.size());
PS::PageStorageMemorySummary::uni_page_id_bytes.fetch_add(id_.size());
id.swap(id_);
return *this;
}
} // namespace DB

namespace DB::details
{

Expand All @@ -31,4 +69,4 @@ String UniversalPageIdFormatHelper::format(const DB::UniversalPageId & value)
DB::UniversalPageIdFormat::getU64ID(value));
}

} // namespace DB::details
} // namespace DB::details
27 changes: 6 additions & 21 deletions dbms/src/Storages/Page/V3/Universal/UniversalPageId.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ class UniversalPageId final
{
public:
UniversalPageId() { PS::PageStorageMemorySummary::uni_page_id_bytes.fetch_add(id.size()); }
UniversalPageId(const UniversalPageId & other)
: id(other.id)
{
PS::PageStorageMemorySummary::uni_page_id_bytes.fetch_add(id.size());
}

UniversalPageId(const UniversalPageId & other);
UniversalPageId(UniversalPageId && other);
UniversalPageId(String id_) // NOLINT(google-explicit-constructor)
: id(std::move(id_))
{
Expand All @@ -48,22 +44,11 @@ class UniversalPageId final
PS::PageStorageMemorySummary::uni_page_id_bytes.fetch_add(id.size());
}

~UniversalPageId() { PS::PageStorageMemorySummary::uni_page_id_bytes.fetch_sub(id.size()); }
~UniversalPageId();

UniversalPageId & operator=(String && id_) noexcept
{
if (id.size() == id_.size()) {}
else if (id.size() > id_.size())
{
PS::PageStorageMemorySummary::uni_page_id_bytes.fetch_sub(id.size() - id_.size());
}
else
{
PS::PageStorageMemorySummary::uni_page_id_bytes.fetch_add(id_.size() - id.size());
}
id.swap(id_);
return *this;
}
UniversalPageId & operator=(UniversalPageId && id_) noexcept;
UniversalPageId & operator=(const UniversalPageId & id_) noexcept;
UniversalPageId & operator=(String && id_) noexcept;
bool operator==(const UniversalPageId & rhs) const noexcept { return id == rhs.id; }
bool operator!=(const UniversalPageId & rhs) const noexcept { return id != rhs.id; }
bool operator>=(const UniversalPageId & rhs) const noexcept { return id >= rhs.id; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,5 +561,25 @@ TEST(UniPageStorageIdTest, UniversalPageId)
}
}

TEST(UniPageStorageIdTest, UniversalPageIdMemoryTrace)
{
auto prim_mem = PS::PageStorageMemorySummary::uni_page_id_bytes.load();
{
auto u_id = UniversalPageIdFormat::toFullPageId("aaa", 100);
auto page1_mem = PS::PageStorageMemorySummary::uni_page_id_bytes.load();
auto ps = page1_mem - prim_mem;
auto u_id_cpy = u_id;
ASSERT_EQ(PS::PageStorageMemorySummary::uni_page_id_bytes.load(), prim_mem + ps * 2);
UniversalPageId u_id_mv = UniversalPageIdFormat::toFullPageId("aaa", 100);
ASSERT_EQ(PS::PageStorageMemorySummary::uni_page_id_bytes.load(), prim_mem + ps * 3);
u_id_mv = std::move(u_id_cpy);
ASSERT_EQ(PS::PageStorageMemorySummary::uni_page_id_bytes.load(), prim_mem + ps * 2);
UniversalPageId u_id_cpy2 = UniversalPageIdFormat::toFullPageId("aaa", 100);
u_id_cpy2 = u_id_mv;
ASSERT_EQ(PS::PageStorageMemorySummary::uni_page_id_bytes.load(), prim_mem + ps * 3);
}
ASSERT_EQ(PS::PageStorageMemorySummary::uni_page_id_bytes.load(), prim_mem);
}

} // namespace PS::universal::tests
} // namespace DB