From c5bc94eef6466d3396b943c247576b6ac7efee4f Mon Sep 17 00:00:00 2001 From: Peter Hofer Date: Tue, 7 May 2024 10:19:22 +0200 Subject: [PATCH] Revert "Disable sweeping due to low fragmentation." This reverts commit d5c351c1c450dac36eea6ea636c2173efe4a52ed. --- .../compacting/PlanningVisitor.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/substratevm/src/com.oracle.svm.core.genscavenge/src/com/oracle/svm/core/genscavenge/compacting/PlanningVisitor.java b/substratevm/src/com.oracle.svm.core.genscavenge/src/com/oracle/svm/core/genscavenge/compacting/PlanningVisitor.java index 4473f77c3ed9e..2a778347c3787 100644 --- a/substratevm/src/com.oracle.svm.core.genscavenge/src/com/oracle/svm/core/genscavenge/compacting/PlanningVisitor.java +++ b/substratevm/src/com.oracle.svm.core.genscavenge/src/com/oracle/svm/core/genscavenge/compacting/PlanningVisitor.java @@ -33,6 +33,7 @@ import com.oracle.svm.core.config.ConfigurationValues; import com.oracle.svm.core.genscavenge.AlignedHeapChunk; import com.oracle.svm.core.genscavenge.HeapChunk; +import com.oracle.svm.core.genscavenge.HeapParameters; import com.oracle.svm.core.genscavenge.ObjectHeaderImpl; import com.oracle.svm.core.genscavenge.Space; import com.oracle.svm.core.genscavenge.remset.BrickTable; @@ -47,6 +48,8 @@ * or overwrite dead objects. */ public final class PlanningVisitor implements AlignedHeapChunk.Visitor { + private static final ResetNewLocationsForSweepingVisitor RESET_NEW_LOCATIONS_FOR_SWEEPING_VISITOR = new ResetNewLocationsForSweepingVisitor(); + private AlignedHeapChunk.AlignedHeader allocChunk; private Pointer allocPointer; @@ -62,7 +65,9 @@ public boolean visitChunk(AlignedHeapChunk.AlignedHeader chunk) { Pointer objSeq = AlignedHeapChunk.getObjectsStart(chunk); UnsignedWord gapSize = WordFactory.zero(); UnsignedWord objSeqSize = WordFactory.zero(); + UnsignedWord brickIndex = WordFactory.zero(); + UnsignedWord totalUnusedBytes = WordFactory.zero(); /* Initialize the move info structure at the chunk's object start location. */ ObjectMoveInfo.setNewAddress(objSeq, allocPointer); @@ -107,6 +112,7 @@ public boolean visitChunk(AlignedHeapChunk.AlignedHeader chunk) { objSeq = p; ObjectMoveInfo.setNextObjectSeqOffset(objSeq, WordFactory.zero()); + totalUnusedBytes = totalUnusedBytes.add(gapSize); gapSize = WordFactory.zero(); } @@ -141,6 +147,12 @@ public boolean visitChunk(AlignedHeapChunk.AlignedHeader chunk) { ObjectMoveInfo.setObjectSeqSize(objSeq, objSeqSize); } + totalUnusedBytes = totalUnusedBytes.add(HeapChunk.getEndOffset(chunk).subtract(HeapChunk.getTopOffset(chunk))); + if (!sweeping && shouldSweepBasedOnFragmentation(totalUnusedBytes)) { + sweeping = true; + chunk.setShouldSweepInsteadOfCompact(true); + ObjectMoveInfo.visit(chunk, RESET_NEW_LOCATIONS_FOR_SWEEPING_VISITOR); + } if (sweeping) { /* * Continue allocating for compaction after the swept memory. Note that this forfeits @@ -173,8 +185,21 @@ private Pointer allocate(UnsignedWord size) { return p; } + private static boolean shouldSweepBasedOnFragmentation(UnsignedWord unusedBytes) { + UnsignedWord limit = HeapParameters.getAlignedHeapChunkSize().unsignedDivide(16); + return unusedBytes.aboveOrEqual(0) && unusedBytes.belowThan(limit); + } + public void init(Space space) { allocChunk = space.getFirstAlignedHeapChunk(); allocPointer = AlignedHeapChunk.getObjectsStart(allocChunk); } + + private static class ResetNewLocationsForSweepingVisitor implements ObjectMoveInfo.Visitor { + @Override + public boolean visit(Pointer objSeq, UnsignedWord size, Pointer newAddress, Pointer nextObjSeq) { + ObjectMoveInfo.setNewAddress(objSeq, objSeq); + return true; + } + } }