GC in Smalltalk MarkAndCompactGC gera Javier Burroni ESUG 2010 - - PowerPoint PPT Presentation

gc in smalltalk
SMART_READER_LITE
LIVE PREVIEW

GC in Smalltalk MarkAndCompactGC gera Javier Burroni ESUG 2010 - - PowerPoint PPT Presentation

GC in Smalltalk MarkAndCompactGC gera Javier Burroni ESUG 2010 Object subclass: #GenerationalGC generational purgeRoots from collect generational follow: object self followRoots; generational moveToOldOrTo: object followStack; to


slide-1
SLIDE 1

GC in Smalltalk

MarkAndCompactGC

Javier Burroni gera

slide-2
SLIDE 2

ESUG 2010

Object subclass: #GenerationalGC from to collect self followRoots; followStack; rescueEphemerons; fixWeakContainers; flipSpaces generational purgeRoots generational follow: object generational moveToOldOrTo: object generational fixReferencesOrSetTombstone: weakContainer generational addInterrupt

slide-3
SLIDE 3

ESUG 2011

Object subclass: #VMGarbageCollector VMGarbageCollector subclass: #GenerationalGC VMGarbageCollector subclass: #MarkAndCompactGC

slide-4
SLIDE 4

ESUG 2011

  • ld

from to

  • ld

from collect self unseeWellKnownObjects; followAll; setNewPositions: oldSpace; setNewPositions: fromSpace; prepareForCompact; compact: oldSpace; compact: fromSpace; updateOldSpace; makeRescuedEphemeronsNonWeak; resetFrom; decommitSlack; addInterrupt VMGarbageCollector subclass: #MarkAndCompactGC

slide-5
SLIDE 5

MarkAndCompactGC

Three phases Follow (mark) Set new positions Compact (move)

collect self ... followAll; setNewPositions: oldSpace; setNewPositions: fromSpace; prepareForCompact; compact: oldSpace; compact: fromSpace; ...

slide-6
SLIDE 6

MarkAndCompactGC

VMGarbageCollector subclass: #MarkAndCompactGC Arena from

  • ld

Libraries Characters followAll

Precondition: all objects unseen

true false nil

slide-7
SLIDE 7

MarkAndCompactGC

VMGarbageCollector subclass: #MarkAndCompactGC Libraries Characters true false nil unseeWellKnownObjects nil _beUnseenInLibrary. true _beUnseenInLibrary. false _beUnseenInLibrary. self unseeLibraryObjects; unseeCharacters; unseeSKernel followAll

Precondition: all objects unseen

slide-8
SLIDE 8

MarkAndCompactGC

unseen: “natural” state in Arena

Arena from

  • ld

followAll

Precondition: all objects unseen

VMGarbageCollector subclass: #MarkAndCompactGC

slide-9
SLIDE 9

MarkAndCompactGC

VMGarbageCollector subclass: #MarkAndCompactGC Arena from

  • ld

Libraries Characters followAll

Recognize & Mark all living objects

true false nil

slide-10
SLIDE 10

MarkAndCompactGC

VMGarbageCollector subclass: #MarkAndCompactGC Arena from

  • ld

Libraries Characters

Recognize & Mark all living objects

followAll followAll self followSKernel; followStack; followExtraRoots; rescueEphemerons; fixWeakContainers; followRescuedEphemerons true false nil

slide-11
SLIDE 11

MarkAndCompactGC

VMGarbageCollector subclass: #MarkAndCompactGC Arena from

  • ld

Libraries Characters followAll followsSKernel self follow: self sKernel count: self sKernelSize startingAt: 1

Recognize & Mark all living objects

true false nil

slide-12
SLIDE 12

MarkAndCompactGC

VMGarbageCollector subclass: #MarkAndCompactGC Arena from

  • ld

arenaIncludes: object ^(oldSpace includes: object) or: [fromSpace includes: object] follow: root count: size startingAt: base

Recognize & Mark all living objects

slide-13
SLIDE 13

MarkAndCompactGC

Libraries Characters follow: root count: size startingAt: base

Recognize & Mark all living objects

(self arenaIncludes: object) ifFalse: [

  • bject _hasBeenSeenInLibrary ifFalse: [
  • bject _beSeenInLibrary.

self follow: object... true false nil

slide-14
SLIDE 14

MarkAndCompactGC

ifTrue: [

  • bject _threadWith: reference at: oldIndex.
  • bject _hasBeenSeenInSpace ifFalse: [

" object _beSeenInSpace " self follow: object...]] Arena from

  • ld

follow: root count: size startingAt: base

Recognize & Mark all living objects

(self arenaIncludes: object) ifFalse: [

  • bject _hasBeenSeenInLibrary ifFalse: [
  • bject _beSeenInLibrary.

self follow: object...

slide-15
SLIDE 15

MarkAndCompactGC

follow: root count: size startingAt: base ...

  • bject _threadWith: reference at: oldIndex.

A B C Z bits Morris, F. L. 1978. A time-and space-efficient garbage compaction algorithm. Communications of the ACM. 21, 8, 662-665.

slide-16
SLIDE 16

MarkAndCompactGC

follow: root count: size startingAt: base ...

  • bject _threadWith: reference at: oldIndex.

A bits B C Z Morris, F. L. 1978. A time-and space-efficient garbage compaction algorithm. Communications of the ACM. 21, 8, 662-665.

slide-17
SLIDE 17

MarkAndCompactGC

VMGarbageCollector subclass: #MarkAndCompactGC Arena from

  • ld

Libraries Characters followAll followExtraRoots self follow: self extraRoots count: 3 startingAt: 1

Recognize & Mark all living objects

true false nil followRescuedEphemerons self follow: rescuedEphemerons count:...

slide-18
SLIDE 18

MarkAndCompactGC

VMGarbageCollector subclass: #MarkAndCompactGC Arena from

  • ld

Libraries Characters followAll followStack super followStack

Recognize & Mark all living objects

true false nil rescueEphemerons super rescueEphemerons Implemented VMGarbageCollector

slide-19
SLIDE 19

MarkAndCompactGC

collect ... setNewPositions: oldSpace; setNewPositions: fromSpace; A bits B C Z

slide-20
SLIDE 20

MarkAndCompactGC

collect ... setNewPositions: oldSpace; setNewPositions: fromSpace; A B C Z bits Z'

slide-21
SLIDE 21

MarkAndCompactGC

setNewPositions: space self seenObjectsFrom: space base to: space nextFree do: [:object :headerSize | | newPosition nextReference reference headerBits | newPosition := auxSpace nextFree + headerSize. reference := object _headerBits _unrotate. [ headerBits := reference _basicAt: 1. reference _basicAt: 1 put: newPosition _toObject. nextReference := headerBits _unrotate. nextReference _isSmallInteger] whileFalse: [reference := nextReference].

  • bject _basicAt: -1 put: headerBits; _beSeenInSpace.

auxSpace nextFree: newPosition + object _byteSize] collect ... setNewPositions: oldSpace; setNewPositions: fromSpace;

slide-22
SLIDE 22

MarkAndCompactGC

setNewPositions: space self seenObjectsFrom: space base to: space nextFree do: [:object :headerSize | | newPosition nextReference reference headerBits | newPosition := auxSpace nextFree + headerSize. reference := object _headerBits _unrotate. [ headerBits := reference _basicAt: 1. reference _basicAt: 1 put: newPosition _toObject. nextReference := headerBits _unrotate. nextReference _isSmallInteger] whileFalse: [reference := nextReference].

  • bject _basicAt: -1 put: headerBits; _beSeenInSpace.

auxSpace nextFree: newPosition + object _byteSize] collect ... setNewPositions: oldSpace; setNewPositions: fromSpace;

Unthread references

slide-23
SLIDE 23

MarkAndCompactGC

collect ... compact: oldSpace; compact: fromSpace; A B C Z bits Z'

slide-24
SLIDE 24

MarkAndCompactGC

collect ... compact: oldSpace; compact: fromSpace; A B C Z' bits

slide-25
SLIDE 25

MarkAndCompactGC

compact: space self objectsFrom: space base to: space nextFree do: [:object |

  • bject _hasBeenSeenInSpace ifTrue: [| moved |

moved := auxSpace shallowCopy: object. moved _beUnseenInSpace]] collect ... compact: oldSpace; compact: fromSpace;

slide-26
SLIDE 26

MarkAndCompactGC

collect ... makeRescuedEphemeronsNonWeak; updateOldSpace; makeRescuedEphemeronsNonWeak rescuedEphemerons do: [:ephemeron | ephemeron _haveNoWeaks] updateOldSpace

  • ldSpace loadFrom: auxSpace
slide-27
SLIDE 27

MarkAndCompactGC

collect ... decommitSlack; decommitSlack | limit delta | limit := self nextFree + 16rFFF bitAnd: -16r1000. delta := self commitedLimit - limit. delta < 0 ifTrue: [^self grow]. delta > 0 ifTrue: [ limit _decommit: delta. self commitedLimit: limit]

Free resources

slide-28
SLIDE 28

MarkAndCompactGC

collect ... decommitSlack; assembleDecommit ... return := assembler pushConstant: 16r4000; pushArg; pushR; callTo: 'VirtualFree' from: 'KERNEL32.DLL'; convertToNative; shortJump

Free resources

slide-29
SLIDE 29

Stop

We have a Smalltalk Scavenger

slide-30
SLIDE 30

Stop

We have a Smalltalk Scavenger Written in Smalltalk

slide-31
SLIDE 31

Stop

We have a Smalltalk Scavenger Written in Smalltalk Using objects

slide-32
SLIDE 32

Stop

We have a Smalltalk Scavenger Written in Smalltalk Using objects Instantiating objects

slide-33
SLIDE 33

Stop

We have a Smalltalk Scavenger Written in Smalltalk Using objects Instantiating objects How can it work?

slide-34
SLIDE 34

Stop

We have a Smalltalk Scavenger Written in Smalltalk Using objects Instantiating objects How can it work?

Object Closure

slide-35
SLIDE 35

Object Closure

What happens in VegasGC stays in VegasGC Self contained objects graph Created objects do not live after GC No message new in our code Created objects: Block Closures Environment Contexts Arrays To interface with the Host VM

slide-36
SLIDE 36

Object Closure

BlockClosure

makeRescuedEphemeronsNonWeak rescuedEphemerons do: [:ephemeron | ephemeron _haveNoWeaks]

The VM will instantiate both at end of young space Environment context

compact: space | moved | self objectsFrom: space base to: space nextFree do: [:object |

  • bject _hasBeenSeenInSpace ifTrue: [

moved := auxSpace shallowCopy: object]

slide-37
SLIDE 37

Object Closure

BlockClosure

makeRescuedEphemeronsNonWeak rescuedEphemerons do: [:ephemeron | ephemeron _haveNoWeaks]

The VM will instantiate both at end of young space we only scan up to space's size when the GC starts Environment context

compact: space | moved | self objectsFrom: space base to: space nextFree do: [:object |

  • bject _hasBeenSeenInSpace ifTrue: [

moved := auxSpace shallowCopy: object]

slide-38
SLIDE 38

Object Closure

Host VM interface arrays

allocateArrays rememberSet on: oldSpace; emptyReserving: 16r100. literalsReferences emptyReserving: 16r200. classCheckReferences emptyReserving: 16r100. nativizedMethods emptyReserving: 16r100. self allocateWeakContainersArray; allocateEphemeronsArray; forgetNativeArrays

allocated at end of oldSpace, just before returning

slide-39
SLIDE 39

Object Closure

Host VM interface arrays

at: index ^contents _basicAt: index + 1

use _primitives instead of primitives

at: index put: value ^contents _basicAt: index + 1 put: value

slide-40
SLIDE 40

Demo

slide-41
SLIDE 41

[Audience hasQuestions] whileTrue: [ self answer: Audience nextQuestion]. Audience do: [:you | self thank: you]. self returnTo: Audience