Skip to content

Commit be880d4

Browse files
committed
core: Record change in size after defrag
Signed-off-by: Abhijat Malviya <abhijat@dragonflydb.io>
1 parent ba791bf commit be880d4

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/core/compact_object.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1633,11 +1633,18 @@ MemoryResource* CompactObj::memory_resource() {
16331633
}
16341634

16351635
bool CompactObj::JsonConsT::DefragIfNeeded(PageUsage* page_usage) {
1636-
if (JsonType* old = json_ptr; ShouldDefragment(page_usage)) {
1636+
JsonType* old = json_ptr;
1637+
if (ShouldDefragment(page_usage)) {
1638+
const MiMemoryResource* mr = static_cast<MiMemoryResource*>(memory_resource());
1639+
const ssize_t before = mr->used();
16371640
json_ptr = AllocateMR<JsonType>(DeepCopyJSON(old));
16381641
DeleteMR<JsonType>(old);
1642+
if (const ssize_t delta = mr->used() - before; delta != 0) {
1643+
bytes_used += delta;
1644+
}
16391645
return true;
16401646
}
1647+
16411648
return false;
16421649
}
16431650

src/core/page_usage_stats_test.cc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,31 @@ TEST_F(PageUsageStatsTest, JSONCons) {
190190
// still fail. This is because freeing the compact object code path takes the wrong branch based
191191
// on encoding. The flat encoding was tested manually adjusting this same test with changed
192192
// encoding.
193-
std::string_view data{R"#({"data": "some", "count": 1, "checked": false})#"};
193+
std::string data = R"({"contents":[)";
194+
for (size_t i = 0; i < 1000; ++i) {
195+
const auto si = std::to_string(i);
196+
data += R"({"id":)" + si + R"(,"class":"v___)" + si + R"("})";
197+
if (i < 999) {
198+
data += ",";
199+
}
200+
}
201+
data += R"(], "data": "some", "count": 1, "checked": false})";
202+
203+
auto* mr = static_cast<MiMemoryResource*>(CompactObj::memory_resource());
204+
size_t before = mr->used();
194205

195206
auto parsed = ParseJsonUsingShardHeap(data);
196207
EXPECT_TRUE(parsed.has_value());
197208

198209
c_obj_.SetJson(std::move(parsed.value()));
210+
c_obj_.SetJsonSize(mr->used() - before);
211+
EXPECT_GT(c_obj_.MallocUsed(), 0);
199212

200213
PageUsage p{CollectPageStats::YES, 0.1};
201214
p.SetForceReallocate(true);
202215

203216
c_obj_.DefragIfNeeded(&p);
217+
EXPECT_GT(c_obj_.MallocUsed(), 0);
204218

205219
const auto stats = p.CollectedStats();
206220
EXPECT_GT(stats.pages_scanned, 0);

src/server/engine_shard.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,19 @@ std::optional<CollectedPageStats> EngineShard::DoDefrag(CollectPageStats collect
351351
uint64_t attempts = 0;
352352

353353
PageUsage page_usage{collect_page_stats, threshold};
354+
DbTableStats& table_stats = slice.GetDBTable(defrag_state_.dbid)->stats;
354355
do {
355356
cur = prime_table->Traverse(cur, [&](PrimeIterator it) {
356357
// for each value check whether we should move it because it
357358
// seats on underutilized page of memory, and if so, do it.
358-
bool did = it->second.DefragIfNeeded(&page_usage);
359+
const ssize_t original_size = it->second.MallocUsed();
360+
const bool did = it->second.DefragIfNeeded(&page_usage);
359361
attempts++;
360362
if (did) {
361363
reallocations++;
364+
if (const ssize_t delta = it->second.MallocUsed() - original_size; delta != 0) {
365+
table_stats.AddTypeMemoryUsage(it->second.ObjType(), delta);
366+
}
362367
}
363368
});
364369
traverses_count++;

0 commit comments

Comments
 (0)