gc in smalltalk

41
GC in Smalltalk MarkAndCompactGC Javier Burroni gera

Upload: esug

Post on 22-Nov-2014

716 views

Category:

Technology


0 download

DESCRIPTION

ESUG 2011, Edinburgh

TRANSCRIPT

Page 1: GC in Smalltalk

GC in SmalltalkMarkAndCompactGC

Javier Burroni gera

Page 2: GC in Smalltalk

ESUG 2010

Object subclass: #GenerationalGC

from

to

collectself

followRoots; followStack; rescueEphemerons; fixWeakContainers; flipSpaces

generational purgeRoots

generational follow: object

generational moveToOldOrTo: object

generationalfixReferencesOrSetTombstone:weakContainer

generational addInterrupt

Page 3: GC in Smalltalk

ESUG 2011

Object subclass: #VMGarbageCollector

VMGarbageCollector subclass: #GenerationalGC

VMGarbageCollector subclass: #MarkAndCompactGC

Page 4: GC in Smalltalk

ESUG 2011

old

from

to

old

from

collectself

unseeWellKnownObjects;followAll;setNewPositions: oldSpace;setNewPositions: fromSpace;prepareForCompact;compact: oldSpace;compact: fromSpace;updateOldSpace;makeRescuedEphemeronsNonWeak;resetFrom;decommitSlack;addInterrupt

VMGarbageCollector subclass: #MarkAndCompactGC

Page 5: GC in Smalltalk

MarkAndCompactGC

Three phases

Follow (mark)Set new positionsCompact (move)

collectself

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

Page 6: GC in Smalltalk

MarkAndCompactGC

VMGarbageCollector subclass: #MarkAndCompactGC

Arena

from

old

Libraries

Characters

followAll

Precondition: all objects unseen

true false nil

Page 7: GC in Smalltalk

MarkAndCompactGC

VMGarbageCollector subclass: #MarkAndCompactGC

Libraries

Characters

true false nil

unseeWellKnownObjectsnil _beUnseenInLibrary.true _beUnseenInLibrary.false _beUnseenInLibrary.self unseeLibraryObjects;

unseeCharacters; unseeSKernel

followAll

Precondition: all objects unseen

Page 8: GC in Smalltalk

MarkAndCompactGC

unseen: “natural” state in Arena

Arena

from

old

followAll

Precondition: all objects unseen

VMGarbageCollector subclass: #MarkAndCompactGC

Page 9: GC in Smalltalk

MarkAndCompactGC

VMGarbageCollector subclass: #MarkAndCompactGC

Arena

from

old

Libraries

Characters

followAll

Recognize & Mark all living objects

true false nil

Page 10: GC in Smalltalk

MarkAndCompactGC

VMGarbageCollector subclass: #MarkAndCompactGC

Arena

from

old

Libraries

Characters

Recognize & Mark all living objects

followAll

followAllself

followSKernel;followStack;followExtraRoots;rescueEphemerons;fixWeakContainers;followRescuedEphemerons

true false nil

Page 11: GC in Smalltalk

MarkAndCompactGC

VMGarbageCollector subclass: #MarkAndCompactGC

Arena

from

old

Libraries

Characters

followAll

followsSKernelself

follow: self sKernelcount: self sKernelSizestartingAt: 1

Recognize & Mark all living objects

true false nil

Page 12: GC in Smalltalk

MarkAndCompactGC

VMGarbageCollector subclass: #MarkAndCompactGC

Arena

from

old

arenaIncludes: object^(oldSpace includes: object) or: [fromSpace includes: object]

follow: root count: size startingAt: base

Recognize & Mark all living objects

Page 13: GC in Smalltalk

MarkAndCompactGC

Libraries

Characters

follow: root count: size startingAt: base

Recognize & Mark all living objects

(self arenaIncludes: object)ifFalse: [

object _hasBeenSeenInLibrary ifFalse: [ object _beSeenInLibrary.self follow: object...

true false nil

Page 14: GC in Smalltalk

MarkAndCompactGC

ifTrue: [object _threadWith: reference at: oldIndex.object _hasBeenSeenInSpace ifFalse: [

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

Arena

from

old

follow: root count: size startingAt: base

Recognize & Mark all living objects

(self arenaIncludes: object)ifFalse: [

object _hasBeenSeenInLibrary ifFalse: [ object _beSeenInLibrary.self follow: object...

Page 15: GC in Smalltalk

MarkAndCompactGC

follow: root count: size startingAt: base...object _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.

Page 16: GC in Smalltalk

MarkAndCompactGC

follow: root count: size startingAt: base...object _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.

Page 17: GC in Smalltalk

MarkAndCompactGC

VMGarbageCollector subclass: #MarkAndCompactGC

Arena

from

old

Libraries

Characters

followAll

followExtraRootsself follow: self extraRoots count: 3 startingAt: 1

Recognize & Mark all living objects

true false nil

followRescuedEphemeronsself follow: rescuedEphemerons count:...

Page 18: GC in Smalltalk

MarkAndCompactGC

VMGarbageCollector subclass: #MarkAndCompactGC

Arena

from

old

Libraries

Characters

followAll

followStacksuper followStack

Recognize & Mark all living objects

true false nil

rescueEphemeronssuper rescueEphemerons

Implemented VMGarbageCollector

Page 19: GC in Smalltalk

MarkAndCompactGC

collect...

setNewPositions: oldSpace;setNewPositions: fromSpace; A

bits

B

C

Z

Page 20: GC in Smalltalk

MarkAndCompactGC

collect...

setNewPositions: oldSpace;setNewPositions: fromSpace; A B

C

Z

bits

Z'

Page 21: GC in Smalltalk

MarkAndCompactGC

setNewPositions: spaceself

seenObjectsFrom: space baseto: space nextFreedo: [: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].

object _basicAt: -1 put: headerBits; _beSeenInSpace.auxSpace nextFree: newPosition + object _byteSize]

collect...

setNewPositions: oldSpace;setNewPositions: fromSpace;

Page 22: GC in Smalltalk

MarkAndCompactGC

setNewPositions: spaceself

seenObjectsFrom: space baseto: space nextFreedo: [: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].

object _basicAt: -1 put: headerBits; _beSeenInSpace.auxSpace nextFree: newPosition + object _byteSize]

collect...

setNewPositions: oldSpace;setNewPositions: fromSpace;

Unthread references

Page 23: GC in Smalltalk

MarkAndCompactGC

collect...

compact: oldSpace;compact: fromSpace; A B

C

Z

bits

Z'

Page 24: GC in Smalltalk

MarkAndCompactGC

collect...

compact: oldSpace;compact: fromSpace; A B

C

Z'

bits

Page 25: GC in Smalltalk

MarkAndCompactGC

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

object _hasBeenSeenInSpace ifTrue: [| moved |moved := auxSpace shallowCopy: object.moved _beUnseenInSpace]]

collect...

compact: oldSpace;compact: fromSpace;

Page 26: GC in Smalltalk

MarkAndCompactGC

collect...

makeRescuedEphemeronsNonWeak; updateOldSpace;

makeRescuedEphemeronsNonWeakrescuedEphemerons do: [:ephemeron | ephemeron _haveNoWeaks]

updateOldSpaceoldSpace loadFrom: auxSpace

Page 27: GC in Smalltalk

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

Page 28: GC in Smalltalk

MarkAndCompactGC

collect...

decommitSlack;

assembleDecommit...return := assembler

pushConstant: 16r4000;pushArg;pushR;callTo: 'VirtualFree' from: 'KERNEL32.DLL';convertToNative;shortJump

Free resources

Page 29: GC in Smalltalk

Stop

We have a Smalltalk Scavenger

Page 30: GC in Smalltalk

Stop

We have a Smalltalk Scavenger

Written in Smalltalk

Page 31: GC in Smalltalk

Stop

We have a Smalltalk Scavenger

Written in Smalltalk

Using objects

Page 32: GC in Smalltalk

Stop

We have a Smalltalk Scavenger

Written in Smalltalk

Using objects

Instantiating objects

Page 33: GC in Smalltalk

Stop

We have a Smalltalk Scavenger

Written in Smalltalk

Using objects

Instantiating objects

How can it work?

Page 34: GC in Smalltalk

Stop

We have a Smalltalk Scavenger

Written in Smalltalk

Using objects

Instantiating objects

How can it work?

Object Closure

Page 35: GC in Smalltalk

Object Closure

What happens in VegasGC stays in VegasGC

Self contained objects graphCreated objects do not live after GCNo message new in our codeCreated objects:

Block ClosuresEnvironment ContextsArrays

To interface with the Host VM

Page 36: GC in Smalltalk

Object Closure

BlockClosure

makeRescuedEphemeronsNonWeakrescuedEphemerons do: [:ephemeron | ephemeron _haveNoWeaks]

The VM will instantiate both at end of young space

Environment contextcompact: space

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

object _hasBeenSeenInSpace ifTrue: [moved := auxSpace shallowCopy: object]

Page 37: GC in Smalltalk

Object Closure

BlockClosure

makeRescuedEphemeronsNonWeakrescuedEphemerons do: [:ephemeron | ephemeron _haveNoWeaks]

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

Environment contextcompact: space

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

object _hasBeenSeenInSpace ifTrue: [moved := auxSpace shallowCopy: object]

Page 38: GC in Smalltalk

Object Closure

Host VM interface arrays

allocateArraysrememberSet 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

Page 39: GC in Smalltalk

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

Page 40: GC in Smalltalk

Demo

Page 41: GC in Smalltalk

[Audience hasQuestions] whileTrue: [self answer: Audience nextQuestion].

Audience do: [:you | self thank: you].

self returnTo: Audience