Skip to content

Commit cdc31fc

Browse files
authored
fix: handle corner case when comitted memory is negative (dragonflydb#570)
Set mem_defrag_threshold so that defrag will be trigerred when memory grows. Signed-off-by: Roman Gershman <[email protected]> Signed-off-by: Roman Gershman <[email protected]>
1 parent 5f572f0 commit cdc31fc

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/server/engine_shard_set.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ ABSL_FLAG(bool, cache_mode, false,
3131
"by evicting entries when getting close to maxmemory limit");
3232

3333
// memory defragmented related flags
34-
ABSL_FLAG(float, mem_defrag_threshold,
35-
1, // The default now is to disable the task from running! change this to 0.05!!
36-
"Minimum percentage of used memory relative to total available memory before running "
34+
ABSL_FLAG(float, mem_defrag_threshold, 0.7,
35+
"Minimum percentage of used memory relative to maxmemory cap before running "
3736
"defragmentation");
3837

3938
ABSL_FLAG(float, commit_use_threshold, 1.3,
@@ -80,20 +79,22 @@ EngineShard::Stats& EngineShard::Stats::operator+=(const EngineShard::Stats& o)
8079
// 3. in case the above is OK, make sure that we have a "gap" between usage and commited memory
8180
// (control by commit_use_threshold flag)
8281
bool EngineShard::DefragTaskState::IsRequired() const {
83-
const uint64_t threshold_mem = max_memory_limit * GetFlag(FLAGS_mem_defrag_threshold);
82+
const int64_t threshold_mem = max_memory_limit * GetFlag(FLAGS_mem_defrag_threshold);
8483
const double commit_use_threshold = GetFlag(FLAGS_commit_use_threshold);
8584

8685
if (cursor > kCursorDoneState) {
8786
return true;
8887
}
8988

90-
uint64_t commited = GetMallocCurrentCommitted();
89+
// can be negative due to weird accounting of mimalloc.
90+
int64_t commited = GetMallocCurrentCommitted();
91+
9192
uint64_t mem_in_use = used_mem_current.load(memory_order_relaxed);
9293

9394
// we want to make sure that we are not running this to many times - i.e.
9495
// if there was no change to the memory, don't run this
9596
if (threshold_mem < commited && mem_in_use > 0 &&
96-
(uint64_t(mem_in_use * commit_use_threshold) < commited)) {
97+
(uint64_t(mem_in_use * commit_use_threshold) < uint64_t(commited))) {
9798
// we have way more commited then actual usage
9899
return true;
99100
}

0 commit comments

Comments
 (0)