GC in Smalltalk

738 views

Published on

ESUG 2011, Edinburgh

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
738
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

GC in Smalltalk

  1. 1. GC in SmalltalkMarkAndCompactGCJavier Burroni gera
  2. 2. ESUG 2010 Object subclass: #GenerationalGC generational purgeRootscollect from self generational follow: object followRoots; followStack; generational moveToOldOrTo: object to rescueEphemerons; fixWeakContainers; generational flipSpaces fixReferencesOrSetTombstone: weakContainer generational addInterrupt
  3. 3. ESUG 2011Object subclass: #VMGarbageCollector VMGarbageCollector subclass: #GenerationalGC VMGarbageCollector subclass: #MarkAndCompactGC
  4. 4. ESUG 2011 VMGarbageCollector subclass: #MarkAndCompactGCcollect self old unseeWellKnownObjects; followAll; setNewPositions: oldSpace; setNewPositions: fromSpace; from prepareForCompact; compact: oldSpace; compact: fromSpace; to updateOldSpace; makeRescuedEphemeronsNonWeak; resetFrom; decommitSlack; addInterrupt
  5. 5. MarkAndCompactGC Three phases Follow (mark) Set new positions Compact (move)collect self ... followAll; setNewPositions: oldSpace; setNewPositions: fromSpace; prepareForCompact; compact: oldSpace; compact: fromSpace; ...
  6. 6. MarkAndCompactGCfollowAll Arena Precondition: all objects unseen from oldVMGarbageCollector subclass: #MarkAndCompactGC Libraries Characters true false nil
  7. 7. MarkAndCompactGCfollowAll Precondition: all objects unseenVMGarbageCollector subclass: #MarkAndCompactGC LibrariesunseeWellKnownObjects nil _beUnseenInLibrary. Characters true _beUnseenInLibrary. false _beUnseenInLibrary. true self unseeLibraryObjects; false unseeCharacters; nil unseeSKernel
  8. 8. MarkAndCompactGCfollowAll Arena Precondition: all objects unseen from oldVMGarbageCollector subclass: #MarkAndCompactGC unseen: “natural” state in Arena
  9. 9. MarkAndCompactGCfollowAll Arena Recognize & Mark all living objects from oldVMGarbageCollector subclass: #MarkAndCompactGC Libraries Characters true false nil
  10. 10. MarkAndCompactGCfollowAll Arena Recognize & Mark all living objects from oldVMGarbageCollector subclass: #MarkAndCompactGC LibrariesfollowAll self Characters followSKernel; followStack; true followExtraRoots; false rescueEphemerons; nil fixWeakContainers; followRescuedEphemerons
  11. 11. MarkAndCompactGCfollowAll Arena Recognize & Mark all living objects from oldVMGarbageCollector subclass: #MarkAndCompactGC LibrariesfollowsSKernel self Characters follow: self sKernel count: self sKernelSize true startingAt: 1 false nil
  12. 12. MarkAndCompactGCfollow: root count: size startingAt: base Arena Recognize & Mark all living objects from oldVMGarbageCollector subclass: #MarkAndCompactGCarenaIncludes: object ^(oldSpace includes: object) or: [fromSpace includes: object]
  13. 13. MarkAndCompactGCfollow: root count: size startingAt: base Recognize & Mark all living objects(self arenaIncludes: object) ifFalse: [ object _hasBeenSeenInLibrary ifFalse: [ object _beSeenInLibrary. Libraries self follow: object... Characters true false nil
  14. 14. MarkAndCompactGCfollow: root count: size startingAt: base Arena Recognize & Mark all living objects from old(self arenaIncludes: object) ifFalse: [ object _hasBeenSeenInLibrary ifFalse: [ object _beSeenInLibrary. self follow: object... ifTrue: [ object _threadWith: reference at: oldIndex. object _hasBeenSeenInSpace ifFalse: [ " object _beSeenInSpace " self follow: object...]]
  15. 15. MarkAndCompactGC follow: root count: size startingAt: base ... B A object _threadWith: reference at: oldIndex. C Z bitsMorris, F. L. 1978. A time-and space-efficient garbage compaction algorithm. Communications of the ACM. 21, 8, 662-665.
  16. 16. MarkAndCompactGC follow: root count: size startingAt: base ... B A object _threadWith: reference at: oldIndex. bits C ZMorris, F. L. 1978. A time-and space-efficient garbage compaction algorithm. Communications of the ACM. 21, 8, 662-665.
  17. 17. MarkAndCompactGCfollowAll Arena Recognize & Mark all living objects from oldVMGarbageCollector subclass: #MarkAndCompactGC LibrariesfollowExtraRoots Characters self follow: self extraRoots count: 3 startingAt: 1 truefollowRescuedEphemerons false self follow: rescuedEphemerons count:... nil
  18. 18. MarkAndCompactGCfollowAll Arena Recognize & Mark all living objects from oldVMGarbageCollector subclass: #MarkAndCompactGC LibrariesfollowStack Characters super followStack truerescueEphemerons false super rescueEphemerons nil Implemented VMGarbageCollector
  19. 19. MarkAndCompactGC collect ... setNewPositions: oldSpace;B A setNewPositions: fromSpace; bitsC Z
  20. 20. MarkAndCompactGC collect ... setNewPositions: oldSpace;B A setNewPositions: fromSpace; ZC Z bits
  21. 21. MarkAndCompactGC collect ... setNewPositions: oldSpace; setNewPositions: fromSpace;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]. object _basicAt: -1 put: headerBits; _beSeenInSpace. auxSpace nextFree: newPosition + object _byteSize]
  22. 22. MarkAndCompactGC collect Unthread references ... setNewPositions: oldSpace; setNewPositions: fromSpace;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]. object _basicAt: -1 put: headerBits; _beSeenInSpace. auxSpace nextFree: newPosition + object _byteSize]
  23. 23. MarkAndCompactGC collect ... compact: oldSpace;B A compact: fromSpace; ZC Z bits
  24. 24. MarkAndCompactGC collect ... compact: oldSpace;B A compact: fromSpace; ZC bits
  25. 25. MarkAndCompactGC collect ... compact: oldSpace; compact: fromSpace;compact: space self objectsFrom: space base to: space nextFree do: [:object | object _hasBeenSeenInSpace ifTrue: [| moved | moved := auxSpace shallowCopy: object. moved _beUnseenInSpace]]
  26. 26. MarkAndCompactGC collect ... makeRescuedEphemeronsNonWeak; updateOldSpace;makeRescuedEphemeronsNonWeak rescuedEphemerons do: [:ephemeron | ephemeron _haveNoWeaks]updateOldSpace oldSpace loadFrom: auxSpace
  27. 27. MarkAndCompactGC collectFree resources ... 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]
  28. 28. MarkAndCompactGC collectFree resources ... decommitSlack; assembleDecommit ... return := assembler pushConstant: 16r4000; pushArg; pushR; callTo: VirtualFree from: KERNEL32.DLL; convertToNative; shortJump
  29. 29. StopWe have a Smalltalk Scavenger
  30. 30. StopWe have a Smalltalk Scavenger Written in Smalltalk
  31. 31. StopWe have a Smalltalk Scavenger Written in Smalltalk Using objects
  32. 32. StopWe have a Smalltalk Scavenger Written in Smalltalk Using objects Instantiating objects
  33. 33. StopWe have a Smalltalk Scavenger Written in Smalltalk Using objects Instantiating objectsHow can it work?
  34. 34. StopWe have a Smalltalk Scavenger Written in Smalltalk Using objects Instantiating objectsHow can it work? Object Closure
  35. 35. Object ClosureWhat 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
  36. 36. Object Closure BlockClosuremakeRescuedEphemeronsNonWeak rescuedEphemerons do: [:ephemeron | ephemeron _haveNoWeaks] Environment contextcompact: space | moved | self objectsFrom: space base to: space nextFree do: [:object | object _hasBeenSeenInSpace ifTrue: [ moved := auxSpace shallowCopy: object] The VM will instantiate both at end of young space
  37. 37. Object Closure BlockClosuremakeRescuedEphemeronsNonWeak rescuedEphemerons do: [:ephemeron | ephemeron _haveNoWeaks] Environment contextcompact: space | moved | self objectsFrom: space base to: space nextFree do: [:object | object _hasBeenSeenInSpace ifTrue: [ moved := auxSpace shallowCopy: object] The VM will instantiate both at end of young space we only scan up to spaces size when the GC starts
  38. 38. Object ClosureHost VM interface arraysallocateArrays rememberSet on: oldSpace; emptyReserving: 16r100. literalsReferences emptyReserving: 16r200. classCheckReferences emptyReserving: 16r100. nativizedMethods emptyReserving: 16r100. self allocateWeakContainersArray; allocateEphemeronsArray; forgetNativeArraysallocated at end of oldSpace, just before returning
  39. 39. Object Closure Host VM interface arraysat: index ^contents _basicAt: index + 1at: index put: value ^contents _basicAt: index + 1 put: value use _primitives instead of primitives
  40. 40. Demo
  41. 41. [Audience hasQuestions] whileTrue: [ self answer: Audience nextQuestion].Audience do: [:you | self thank: you].self returnTo: Audience

×