<div dir="ltr">For the curious folks, here's a description of <span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">SpurSelectiveCompactor:</span><div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"><br></span></div><div><span style="text-align:start;text-indent:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline;font-size:12.8px"><div style="font-style:italic">SpurSelectiveCompactor compacts memory by selecting the memory segments with the most free space and compacting only those, to limit fragmentation while being really quick to perform. The algorithm is fast mostly because it does not update pointers: they are updated lazily during the next marking phase, so there is no need to read the fields of objects in other memory segments that the one compacted.</div><div style="font-style:italic"><br></div><div style="font-style:italic">The algorithm works as follow. First, a global sweep pass iterates over the memory linearly, changing unmarked objects to free chunks and concatenating free chunks. During the global sweep phase, the segments of the heap are analysed to determine the percentage of occupation. Second, the least occupied segments are compacted by copying the remaining live objects into an entirely free segment, called regionToFill (we detail later in the paragraph where regionToFill comes from), changing their values to forwarding objects and marking the free chunks as unavailable (removed from free list and marked as data objects). Third, the next marking phase removes all forwarders. Fourth, at the beginning of the next compaction phase the compacted segments from the previous GC can be entirely marked as free space (No need to check anything inside, there were only forwarders and trash data). One of the compacted segment is then selected as the segmentToFill, others are just marked as free chunks. </div><div style="font-style:italic"><br></div><div style="font-style:italic">The compaction is effectively partial, compacting only the most critical segments of the heap to limit fragmentation. Compaction time is crazy low, since a low number of objects are moved and pointer updated is lazily done during the next marking phase, while still preventing memory fragmentation.</div><div style="font-style:italic"><br></div><div style="">Although it's relevant research-wise, we don't need SelectiveCompactor in the short term. Full GC pause time is currently due to the Stop the World Mark and Compact algorithm. Mark pause is longer and we need to implement a tri-color incremental marking algorithm to solve this problem. Once done, compaction time becomes the biggest pause, which can still be a problem. SelectiveCompactor is a solution to decrease the compaction pause (SelectiveCompaction effectively does a sweep, which is very fast, and partial compaction without pointer update). The runtime may be a little slowed down due to the presence of more forwarders. Marking time is a little bit longer since it needs to remove more forwarders, though, as it is incremental there should not be longer pauses. Overall, the throughput might be a little lower (to confirm with benchmarks), but pauses are definitely smaller.</div><div style=""><br></div></span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Apr 26, 2018 at 11:12 AM,  <span dir="ltr"><<a href="mailto:commits@source.squeak.org" target="_blank">commits@source.squeak.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> <br>
ClementBera uploaded a new version of VMMaker to project VM Maker:<br>
<a href="http://source.squeak.org/VMMaker/VMMaker.oscog-cb.2373.mcz" rel="noreferrer" target="_blank">http://source.squeak.org/<wbr>VMMaker/VMMaker.oscog-cb.2373.<wbr>mcz</a><br>
<br>
==================== Summary ====================<br>
<br>
Name: VMMaker.oscog-cb.2373<br>
Author: cb<br>
Time: 26 April 2018, 11:11:47.778949 am<br>
UUID: 9b389323-2181-4503-a361-<wbr>d66ad87fa2de<br>
Ancestors: VMMaker.oscog-cb.2372<br>
<br>
Remove the APIs I added to iterate over free chunks (there was an existing API)<br>
<br>
Added assertValidFreeObject: to avoid stepping all the time in isValidFreeObject to know what's wrong. Obviously this new method cannot be used in the C code or we will have code in assertion-free VM, leading to the following pattern:<br>
self "Sorry stepping over isValidFreeObject all the time was killing me"<br>
                        cCode: [self assert: (self isValidFreeObject: child)]<br>
                        inSmalltalk: [self assertValidFreeObject: child].<br>
<br>
Since I now use forwarders in fullGC, adapted heap space integrity check.<br>
<br>
I was a little bit too aggressive in assertion in detachFreeObject: in last commit, reverted that.<br>
<br>
And SpurSelectiveCompactor is now working as an alternative compactor to Planning, Pig compactors and Sweeper! So exciting. Still needs some tuning for production use (Mostly snapshots consume high memory). SpurSelectiveCompactor compaction time is crazy low (almost as fast as a Sweep algorithm).<br>
<br>
I may write yet another compactor since I need to compare SelectiveCompactor with Garbage First multi-remembered table approach for research purpose...<br>
<br>
=============== Diff against VMMaker.oscog-cb.2372 ===============<br>
<br>
Item was removed:<br>
- ----- Method: SpurMemoryManager>><wbr>allOldSpaceFreeChunksDo: (in category 'object enumeration') -----<br>
- allOldSpaceFreeChunksDo: aBlock<br>
-       <inline: true><br>
-       <doNotGenerate> "Could be generated, but used for debug only"<br>
-       self allOldSpaceFreeChunksFrom: self firstObject do: aBlock!<br>
<br>
Item was removed:<br>
- ----- Method: SpurMemoryManager>><wbr>allOldSpaceFreeChunksFrom:do: (in category 'object enumeration') -----<br>
- allOldSpaceFreeChunksFrom: initialObject do: aBlock<br>
-       <inline: true><br>
-       <doNotGenerate> "Could be generated, but used for debug only"<br>
-       self allOldSpaceEntitiesFrom: initialObject<br>
-               do: [:objOop|<br>
-                        (self isFreeObject: objOop) ifTrue:<br>
-                               [aBlock value: objOop]]!<br>
<br>
Item was changed:<br>
  ----- Method: SpurMemoryManager>><wbr>allocateOldSpaceChunkOfExactly<wbr>Bytes:suchThat: (in category 'free space') -----<br>
  allocateOldSpaceChunkOfExactly<wbr>Bytes: chunkBytes suchThat: acceptanceBlock<br>
        "Answer a chunk of oldSpace from the free lists that satisfies acceptanceBlock,<br>
         if one of this size is available, otherwise answer nil.  N.B.  the chunk is simply a<br>
         pointer, it has no valid header.  The caller *must* fill in the header correctly."<br>
        <var: #chunkBytes type: #usqInt><br>
        | index node next prev child childBytes |<br>
        <inline: true> "must inline for acceptanceBlock"<br>
        "for debugging:" "totalFreeOldSpace := self totalFreeListBytes"<br>
<br>
        index := chunkBytes / self allocationUnit.<br>
        index < self numFreeLists ifTrue:<br>
                [(freeListsMask anyMask: 1 << index) ifTrue:<br>
                        [(node := freeLists at: index) = 0<br>
                                ifTrue: [freeListsMask := freeListsMask - (1 << index)]<br>
                                ifFalse:<br>
                                        [prev := 0.<br>
                                         [node ~= 0] whileTrue:<br>
                                                [self assert: node = (self startOfObject: node).<br>
                                                 self assert: (self isValidFreeObject: node).<br>
                                                 next := self fetchPointer: self freeChunkNextIndex ofFreeChunk: node.<br>
                                                 (acceptanceBlock value: node) ifTrue:<br>
                                                        [prev = 0<br>
                                                                ifTrue: [freeLists at: index put: next]<br>
                                                                ifFalse: [self storePointer: self freeChunkNextIndex ofFreeChunk: prev withValue: next].<br>
                                                         totalFreeOldSpace := totalFreeOldSpace - chunkBytes.<br>
                                                         ^node].<br>
                                                 prev := node.<br>
                                                 node := next]]].<br>
                 ^nil].<br>
<br>
        "Large chunk.  Search the large chunk list.<br>
         Large chunk list organized as a tree, each node of which is a list of<br>
         chunks of the same size. Beneath the node are smaller and larger<br>
         blocks.  When the search ends parent should hold the first chunk of<br>
         the same size as chunkBytes, or 0 if none."<br>
        node := 0.<br>
        child := freeLists at: 0.<br>
        [child ~= 0] whileTrue:<br>
+               [self "Sorry stepping over isValidFreeObject all the time was killing me"<br>
+                       cCode: [self assert: (self isValidFreeObject: child)]<br>
+                       inSmalltalk: [self assertValidFreeObject: child].<br>
-               [self assert: (self isValidFreeObject: child).<br>
                 childBytes := self bytesInObject: child.<br>
                 childBytes = chunkBytes<br>
                        ifTrue: "size match; try to remove from list at node first."<br>
                                [node := child.<br>
                                 [prev := node.<br>
                                  node := self fetchPointer: self freeChunkNextIndex ofFreeChunk: node.<br>
                                  node ~= 0] whileTrue:<br>
                                        [(acceptanceBlock value: node) ifTrue:<br>
                                                [self assert: (self isValidFreeObject: node).<br>
                                                 self storePointer: self freeChunkNextIndex<br>
                                                        ofFreeChunk: prev<br>
                                                        withValue: (self fetchPointer: self freeChunkNextIndex ofFreeChunk: node).<br>
                                                 totalFreeOldSpace := totalFreeOldSpace - chunkBytes.<br>
                                                 ^self startOfObject: node]].<br>
                                 (acceptanceBlock value: child) ifFalse:<br>
                                        [^nil]. "node was right size but unaceptable."<br>
                                 next := self fetchPointer: self freeChunkNextIndex ofFreeChunk: child.<br>
                                 next = 0<br>
                                        ifTrue: "no list; remove the interior node"<br>
                                                [self unlinkSolitaryFreeTreeNode: child]<br>
                                        ifFalse: "list; replace node with it"<br>
                                                [self inFreeTreeReplace: child with: next].<br>
                                 totalFreeOldSpace := totalFreeOldSpace - chunkBytes.<br>
                                 ^self startOfObject: child]<br>
                        ifFalse: "no size match; walk down the tree"<br>
                                [child := self fetchPointer: (childBytes < chunkBytes<br>
                                                                                                ifTrue: [self freeChunkLargerIndex]<br>
                                                                                                ifFalse: [self freeChunkSmallerIndex])<br>
                                                        ofFreeChunk: child]].<br>
        ^nil!<br>
<br>
Item was added:<br>
+ ----- Method: SpurMemoryManager>><wbr>assertValidFreeObject: (in category 'free space') -----<br>
+ assertValidFreeObject: objOop<br>
+       <doNotGenerate> "If you want to generate this you want to use 'self assert: (self isValidFreeObject: objOop)' instead not to generate code in assertion-free VMs"<br>
+       | chunk |<br>
+       "duplicated assertions from isValidFreeObject: because I need to know what is wrong not only that it is not valid (I got bored of stepping inside isValidFreeObject:...)"<br>
+       self assert: (self oop: (self addressAfter: objOop) isLessThanOrEqualTo: endOfMemory).<br>
+       chunk := self fetchPointer: self freeChunkNextIndex ofFreeChunk: objOop.<br>
+       self assert: (chunk = 0 or: [self isFreeOop: chunk]).<br>
+       (self isLargeFreeObject: objOop) ifTrue: [<br>
+               "Tree assertions"<br>
+               chunk := self fetchPointer: self freeChunkParentIndex ofFreeChunk: objOop.<br>
+               self assert: (chunk = 0 or: [(self isFreeOop: chunk) and: [self isLargeFreeObject: chunk]]).<br>
+               chunk := self fetchPointer: self freeChunkSmallerIndex ofFreeChunk: objOop.<br>
+               self assert: (chunk = 0 or: [(self isFreeOop: chunk) and: [self isLargeFreeObject: chunk]]).<br>
+               chunk := self fetchPointer: self freeChunkLargerIndex ofFreeChunk: objOop.<br>
+               self assert: (chunk = 0 or: [(self isFreeOop: chunk) and: [self isLargeFreeObject: chunk]]). ]!<br>
<br>
Item was changed:<br>
  ----- Method: SpurMemoryManager>><wbr>checkHeapFreeSpaceIntegrity (in category 'debug support') -----<br>
  checkHeapFreeSpaceIntegrity<br>
        "Perform an integrity/leak check using the heapMap.  Assume clearLeakMapAndMapAccessibleFr<wbr>eeSpace<br>
         has set a bit at each free chunk's header.  Scan all objects in the heap checking that no pointer points<br>
         to a free chunk and that all free chunks that refer to others refer to marked chunks.  Answer if all checks pass."<br>
        | ok total |<br>
        <inline: false><br>
        <var: 'total' type: #usqInt><br>
        ok := true.<br>
        total := 0.<br>
        0 to: self numFreeLists - 1 do:<br>
                [:i|<br>
                (freeLists at: i) ~= 0 ifTrue:<br>
                        [(heapMap heapMapAtWord: (self pointerForOop: (freeLists at: i))) = 0 ifTrue:<br>
                                [coInterpreter print: 'leak in free list '; printNum: i; print: ' to non-free '; printHex: (freeLists at: i); cr.<br>
                                 self eek.<br>
                                 ok := false]]].<br>
<br>
        "Excuse the duplication but performance is at a premium and we avoid<br>
         some tests by splitting the newSpace and oldSpace enumerations."<br>
        self allNewSpaceEntitiesDo:<br>
                [:obj| | fieldOop |<br>
                 (self isFreeObject: obj)<br>
                        ifTrue:<br>
                                [coInterpreter print: 'young object '; printHex: obj; print: ' is free'; cr.<br>
                                 self eek.<br>
                                 ok := false]<br>
                        ifFalse:<br>
                                [0 to: (self numPointerSlotsOf: obj) - 1 do:<br>
                                        [:fi|<br>
                                         fieldOop := self fetchPointer: fi ofObject: obj.<br>
                                         (self isNonImmediate: fieldOop) ifTrue:<br>
                                                [(heapMap heapMapAtWord: (self pointerForOop: fieldOop)) ~= 0 ifTrue:<br>
                                                        [coInterpreter print: 'object leak in '; printHex: obj; print: ' @ '; printNum: fi; print: ' = '; printHex: fieldOop; print: ' is free'; cr.<br>
                                                         self eek.<br>
                                                         ok := false]]]]].<br>
        self allOldSpaceEntitiesDo:<br>
                [:obj| | fieldOop |<br>
                (self isFreeObject: obj)<br>
                        ifTrue:<br>
                                [(heapMap heapMapAtWord: (self pointerForOop: obj)) = 0 ifTrue:<br>
                                        [coInterpreter print: 'leak in free chunk '; printHex: obj; print: ' is unmapped?!! '; cr.<br>
                                         self eek.<br>
                                         ok := false].<br>
                                 fieldOop := self fetchPointer: self freeChunkNextIndex ofFreeChunk: obj.<br>
                                 (fieldOop ~= 0<br>
                                 and: [(heapMap heapMapAtWord: (self pointerForOop: fieldOop)) = 0]) ifTrue:<br>
                                        [coInterpreter print: 'leak in free chunk '; printHex: obj; print: ' @ 0 = '; printHex: fieldOop; print: ' is unmapped'; cr.<br>
                                         self eek.<br>
                                         ok := false].<br>
                                (self isLargeFreeObject: obj) ifTrue:<br>
                                        [self freeChunkParentIndex to: self freeChunkLargerIndex do:<br>
                                                [:fi|<br>
                                                 fieldOop := self fetchPointer: fi ofFreeChunk: obj.<br>
                                                 (fieldOop ~= 0<br>
                                                 and: [(heapMap heapMapAtWord: (self pointerForOop: fieldOop)) = 0]) ifTrue:<br>
                                                        [coInterpreter print: 'leak in free chunk '; printHex: obj; print: ' @ '; printNum: fi; print: ' = '; printHex: fieldOop; print: ' is unmapped'; cr.<br>
                                                         self eek.<br>
                                                         ok := false]]].<br>
                                total := total + (self bytesInObject: obj)]<br>
                        ifFalse:<br>
                                [0 to: (self numPointerSlotsOf: obj) - 1 do:<br>
                                        [:fi|<br>
+                                        (self isForwarded: obj)<br>
+                                               ifTrue: <br>
+                                                       [self assert: fi = 0. "I'm now trying to use forwarders in GC algorithms..."<br>
+                                                        fieldOop := self fetchPointer: fi ofMaybeForwardedObject: obj] <br>
+                                               ifFalse: "We keep #fetchPointer:ofObject: API here for assertions"<br>
+                                                       [fieldOop := self fetchPointer: fi ofObject: obj].<br>
-                                        fieldOop := self fetchPointer: fi ofObject: obj.<br>
                                         (self isNonImmediate: fieldOop) ifTrue:<br>
                                                [(heapMap heapMapAtWord: (self pointerForOop: fieldOop)) ~= 0 ifTrue:<br>
                                                        [coInterpreter print: 'object leak in '; printHex: obj; print: ' @ '; printNum: fi; print: ' = '; printHex: fieldOop; print: ' is free'; cr.<br>
                                                         self eek.<br>
                                                         ok := false]]]]].<br>
        total ~= totalFreeOldSpace ifTrue:<br>
                [coInterpreter print: 'incorrect totalFreeOldSpace; expected '; printNum: totalFreeOldSpace; print: ' found '; printNum: total; cr.<br>
                 self eek.<br>
                 ok := false].<br>
        ^ok!<br>
<br>
Item was changed:<br>
  ----- Method: SpurMemoryManager>><wbr>detachFreeObject: (in category 'free space') -----<br>
  detachFreeObject: freeChunk<br>
        "This is a rare operation, so its efficiency isn't critical.<br>
         Having a valid prev link for tree nodes would help."<br>
        <inline: false><br>
        | chunkBytes result |<br>
        chunkBytes := self bytesInObject: freeChunk.<br>
        result := self allocateOldSpaceChunkOfExactly<wbr>Bytes: chunkBytes suchThat: [:f| f = freeChunk].<br>
        self assert: result = (self startOfObject: freeChunk).<br>
-       "Following is assertion only. Typical problem is that the free structures (tree/list) keep references to detached object somehow"<br>
-       self cCode: '' inSmalltalk: <br>
-               [self allOldSpaceFreeChunksDo: <br>
-                       [ :f | self assert: (self isValidFreeObject: f)]].<br>
        !<br>
<br>
Item was changed:<br>
  ----- Method: SpurSelectiveCompactor>><wbr>compactSegmentsToCompact (in category 'compaction') -----<br>
  compactSegmentsToCompact<br>
        "Forwards all objects in segments to compact and removes their freechunks"<br>
        | freeStart |<br>
        freeStart := segmentToFill segStart.<br>
+       <br>
+        "Removes initial free chunk in segment to fill... (Segment is entirely free)"<br>
+       manager detachFreeObject: (manager objectStartingAt: freeStart).<br>
+       <br>
+        "Compact each segment to compact..."<br>
        0 to: manager numSegments - 1 do:<br>
                [:i| | segInfo |<br>
                 segInfo := self addressOf: (manager segmentManager segments at: i).<br>
                (self isSegmentBeingCompacted: segInfo)<br>
                        ifTrue: [freeStart := self compactSegment: segInfo freeStart: freeStart ]].<br>
<br>
         "Final free chunk in segment to fill..."<br>
         manager <br>
                addFreeChunkWithBytes: segmentToFill segSize - manager bridgeSize + segmentToFill segStart - freeStart <br>
                at: freeStart.<br>
<br>
+        "Follow stack zone and caches..."<br>
        self postForwardingAction<br>
        !<br>
<br>
Item was changed:<br>
  ----- Method: SpurSelectiveCompactor>><wbr>findAndSetSegmentToFill (in category 'freeing') -----<br>
  findAndSetSegmentToFill<br>
        0 to: manager numSegments - 1 do:<br>
                [:i| | segInfo firstEntity |<br>
                 segInfo := self addressOf: (manager segmentManager segments at: i).<br>
                 firstEntity := manager objectStartingAt: segInfo segStart.<br>
                 ((manager isFreeObject: firstEntity) and: [(manager objectAfter: firstEntity limit: manager endOfMemory) = (manager segmentManager bridgeFor: segInfo)])<br>
+                       ifTrue: [segmentToFill := segInfo. ^0]].<br>
-                       ifTrue: [segmentToFill := segInfo. manager detachFreeObject: firstEntity. ^0]].<br>
        !<br>
<br>
Item was changed:<br>
  ----- Method: SpurSelectiveCompactor>><wbr>freePastSegmentsAndSetSegmentT<wbr>oFill (in category 'freeing') -----<br>
  freePastSegmentsAndSetSegmentT<wbr>oFill   <br>
+       "The first segment being claimed met becomes the segmentToFill. The others are just freed."<br>
-       "The first segment being claimed met becomes the segmentToFill. The others are just freed"<br>
        segmentToFill := nil.<br>
        0 to: manager numSegments - 1 do:<br>
                [:i| | segInfo |<br>
                 segInfo := self addressOf: (manager segmentManager segments at: i).<br>
                 (self isSegmentBeingCompacted: segInfo)<br>
                        ifTrue: <br>
+                               [self freeSegment: segInfo.<br>
+                                segmentToFill ifNil: [segmentToFill := segInfo]]]!<br>
-                               [segmentToFill<br>
-                                       ifNil: [segmentToFill := segInfo]<br>
-                                       ifNotNil: [self freeSegment: segInfo]]]!<br>
<br>
Item was changed:<br>
  ----- Method: SpurSelectiveCompactor>><wbr>selectiveCompaction (in category 'compaction') -----<br>
  selectiveCompaction<br>
        "Figures out which segments to compact and compact them into segmentToFill"<br>
        | atLeastOneSegmentToCompact |<br>
        self assertNoSegmentBeingCompacted.<br>
        atLeastOneSegmentToCompact := self computeSegmentsToCompact.<br>
        "If no compaction we don't pay forwarding cost (stack scan, cache scan, etc.)<br>
         and we don't allocate segmentToFill if none available."<br>
        atLeastOneSegmentToCompact <br>
                ifTrue:<br>
                        [self assert: segmentToFill ~~ nil.<br>
+                        self compactSegmentsToCompact].<br>
-                        self compactSegmentsToCompact]<br>
-               ifFalse: <br>
-                       [segmentToFill ifNotNil: [self freeSegment: segmentToFill]].<br>
        manager checkFreeSpace: GCModeFull.!<br>
<br>
Item was added:<br>
+ ----- Method: SpurSelectiveCompactorSimulato<wbr>r>>selectiveCompaction (in category 'compaction') -----<br>
+ selectiveCompaction<br>
+       super selectiveCompaction.<br>
+       manager allFreeObjectsDo: [:objOop | manager assertValidFreeObject: objOop]!<br>
<br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><span style="font-size:12.8px">Clément Béra<br></span><span style="color:rgb(0,0,238)"><a href="https://clementbera.github.io/" target="_blank">https://clementbera.github.io/</a></span><div style="font-size:12.8px"><a href="https://clementbera.wordpress.com/" target="_blank">https://clementbera.wordpress.com/</a></div></div></div></div></div></div>
</div>