snazzy-d/sdc

Keep track of dirty regions

deadalnix opened this issue · 1 comments

The current region allocator never release memory to the system. And for "good" reasons: it doesn't track which memory has been used and which hasn't.

Newly allocated pages need to be marked as clean, while pages that are returned to the RegionAllocator must be marked as dirty.

Each region needs to keep an accounting of the number of dirty pages the contain. Regions with a higher number of dirty pages should be preferentially used.

Dirty pages can be purged, using madvise's MADV_FREE to be converted back to free pages.

A queue of run of dirty pages must be maintained so that they can be purged starting by the oldest ones. The RegionAllocator API must be extended to request the purge of a certain number of pages.

For reference, the code that registers a region:

sdc/sdlib/d/gc/region.d

Lines 134 to 156 in 6cfcf22

void registerRegion(Region* toRegister) {
Region r = *toRegister;
unusedRegions.insert(toRegister);
// First, merge adjacent ranges.
while (true) {
auto adjacent = regionsByRange.extract(&r);
if (adjacent is null) {
break;
}
r.merge(adjacent);
regionsByClass.remove(adjacent);
unusedRegions.insert(adjacent);
}
toRegister = unusedRegions.pop();
assert(toRegister !is null);
toRegister.clone(&r);
regionsByClass.insert(toRegister);
regionsByRange.insert(toRegister);
}

Working solution: