SlideShare una empresa de Scribd logo
1 de 41
Descargar para leer sin conexión
GC in Smalltalk
MarkAndCompactGC




Javier Burroni   gera
ESUG 2010

   Object subclass: #GenerationalGC




                                        generational purgeRoots
collect                          from
  self                                  generational follow: object
    followRoots;
    followStack;                        generational moveToOldOrTo: object
                                  to
    rescueEphemerons;
    fixWeakContainers;                  generational
    flipSpaces                            fixReferencesOrSetTombstone:
                                          weakContainer

                                        generational addInterrupt
ESUG 2011

Object subclass: #VMGarbageCollector




         VMGarbageCollector subclass: #GenerationalGC




         VMGarbageCollector subclass: #MarkAndCompactGC
ESUG 2011

       VMGarbageCollector subclass: #MarkAndCompactGC




collect
  self                                old
    unseeWellKnownObjects;
    followAll;
    setNewPositions: oldSpace;
    setNewPositions: fromSpace;      from
    prepareForCompact;
    compact: oldSpace;
    compact: fromSpace;               to
    updateOldSpace;
    makeRescuedEphemeronsNonWeak;
    resetFrom;
    decommitSlack;
    addInterrupt
MarkAndCompactGC

      Three phases

          Follow (mark)
          Set new positions
          Compact (move)



collect
  self
    ...
    followAll;
    setNewPositions: oldSpace;
    setNewPositions: fromSpace;
    prepareForCompact;
    compact: oldSpace;
    compact: fromSpace;
    ...
MarkAndCompactGC
followAll
                                                   Arena
     Precondition: all objects unseen
                                                   from


                                                    old
VMGarbageCollector subclass: #MarkAndCompactGC

                                                 Libraries


                                                 Characters


                                                   true
                                                   false
                                                    nil
MarkAndCompactGC
followAll


     Precondition: all objects unseen




VMGarbageCollector subclass: #MarkAndCompactGC

                                                 Libraries

unseeWellKnownObjects
 nil _beUnseenInLibrary.                         Characters
 true _beUnseenInLibrary.
 false _beUnseenInLibrary.
                                                   true
 self unseeLibraryObjects;
                                                   false
   unseeCharacters;
                                                    nil
   unseeSKernel
MarkAndCompactGC
followAll
                                                 Arena
     Precondition: all objects unseen
                                                 from


                                                  old
VMGarbageCollector subclass: #MarkAndCompactGC




     unseen: “natural” state in Arena
MarkAndCompactGC
followAll
                                                   Arena
     Recognize & Mark all living objects
                                                   from


                                                    old
VMGarbageCollector subclass: #MarkAndCompactGC

                                                 Libraries


                                                 Characters


                                                   true
                                                   false
                                                    nil
MarkAndCompactGC
followAll
                                                   Arena
     Recognize & Mark all living objects
                                                   from


                                                    old
VMGarbageCollector subclass: #MarkAndCompactGC

                                                 Libraries

followAll
  self                                           Characters
    followSKernel;
    followStack;
                                                   true
    followExtraRoots;
                                                   false
    rescueEphemerons;
                                                    nil
    fixWeakContainers;
    followRescuedEphemerons
MarkAndCompactGC
followAll
                                                   Arena
     Recognize & Mark all living objects
                                                   from


                                                    old
VMGarbageCollector subclass: #MarkAndCompactGC

                                                 Libraries

followsSKernel
  self                                           Characters
    follow: self sKernel
    count: self sKernelSize                        true
    startingAt: 1                                  false
                                                    nil
MarkAndCompactGC
follow: root count: size startingAt: base
                                                                  Arena
     Recognize & Mark all living objects
                                                                  from


                                                                   old
VMGarbageCollector subclass: #MarkAndCompactGC




arenaIncludes: object
  ^(oldSpace includes: object) or: [fromSpace includes: object]
MarkAndCompactGC
follow: 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
MarkAndCompactGC
follow: 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...]]
MarkAndCompactGC
                                  follow: root count: size startingAt: base
                                    ...
 B                   A              object _threadWith: reference at: oldIndex.




 C


                            Z



                                bits


Morris, F. L. 1978. A time-and space-efficient garbage compaction algorithm.
                 Communications of the ACM. 21, 8, 662-665.
MarkAndCompactGC
                                    follow: root count: size startingAt: base
                                      ...
 B                   A                object _threadWith: reference at: oldIndex.



                         bits



 C


                                Z




Morris, F. L. 1978. A time-and space-efficient garbage compaction algorithm.
                 Communications of the ACM. 21, 8, 662-665.
MarkAndCompactGC
followAll
                                                          Arena
      Recognize & Mark all living objects
                                                          from


                                                           old
VMGarbageCollector subclass: #MarkAndCompactGC

                                                        Libraries

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

                                                          true
followRescuedEphemerons                                   false
  self follow: rescuedEphemerons count:...                 nil
MarkAndCompactGC
followAll
                                                   Arena
     Recognize & Mark all living objects
                                                   from


                                                    old
VMGarbageCollector subclass: #MarkAndCompactGC

                                                 Libraries

followStack                                      Characters
  super followStack

                                                   true
rescueEphemerons                                   false
  super rescueEphemerons                            nil

     Implemented VMGarbageCollector
MarkAndCompactGC
                    collect
                      ...
                        setNewPositions: oldSpace;
B    A                  setNewPositions: fromSpace;



         bits



C


                Z
MarkAndCompactGC
                    collect
                      ...
                        setNewPositions: oldSpace;
B    A                  setNewPositions: fromSpace;




                          Z'

C


         Z



             bits
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]
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]
MarkAndCompactGC
                    collect
                      ...
                        compact: oldSpace;
B    A                  compact: fromSpace;




                          Z'

C


         Z



             bits
MarkAndCompactGC
           collect
             ...
               compact: oldSpace;
B    A         compact: fromSpace;




                 Z'

C

                  bits
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]]
MarkAndCompactGC
                                collect
                                  ...
                                    makeRescuedEphemeronsNonWeak;
                                    updateOldSpace;




makeRescuedEphemeronsNonWeak
 rescuedEphemerons do: [:ephemeron | ephemeron _haveNoWeaks]



updateOldSpace
 oldSpace loadFrom: auxSpace
MarkAndCompactGC
                                     collect
Free 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]
MarkAndCompactGC
                                       collect
Free resources                           ...
                                           decommitSlack;




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

We have a Smalltalk Scavenger
Stop

We have a Smalltalk Scavenger

 Written in Smalltalk
Stop

We have a Smalltalk Scavenger

 Written in Smalltalk

  Using objects
Stop

We have a Smalltalk Scavenger

 Written in Smalltalk

  Using objects

    Instantiating objects
Stop

We have a Smalltalk Scavenger

 Written in Smalltalk

  Using objects

    Instantiating objects

How can it work?
Stop

We have a Smalltalk Scavenger

 Written in Smalltalk

  Using objects

    Instantiating objects

How can it work?


         Object Closure
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
Object Closure

 BlockClosure

makeRescuedEphemeronsNonWeak
 rescuedEphemerons do: [:ephemeron | ephemeron _haveNoWeaks]



 Environment context
compact: 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
Object Closure

 BlockClosure

makeRescuedEphemeronsNonWeak
 rescuedEphemerons do: [:ephemeron | ephemeron _haveNoWeaks]



 Environment context
compact: 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 space's size when the GC starts
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
Object Closure

 Host VM interface arrays
at: index
  ^contents _basicAt: index + 1


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




            use _primitives instead of primitives
Demo
[Audience hasQuestions] whileTrue: [
  self answer: Audience nextQuestion].

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

self returnTo: Audience

Más contenido relacionado

Más de ESUG

Workshop: Identifying concept inventories in agile programming
Workshop: Identifying concept inventories in agile programmingWorkshop: Identifying concept inventories in agile programming
Workshop: Identifying concept inventories in agile programming
ESUG
 
The Pharo Debugger and Debugging tools: Advances and Roadmap
The Pharo Debugger and Debugging tools: Advances and RoadmapThe Pharo Debugger and Debugging tools: Advances and Roadmap
The Pharo Debugger and Debugging tools: Advances and Roadmap
ESUG
 
Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...
ESUG
 
Analyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early resultsAnalyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early results
ESUG
 
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
ESUG
 
A Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test GenerationA Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test Generation
ESUG
 
Creating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic ProgrammingCreating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic Programming
ESUG
 
Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes
Threaded-Execution and CPS Provide Smooth Switching Between Execution ModesThreaded-Execution and CPS Provide Smooth Switching Between Execution Modes
Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes
ESUG
 
Exploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience ReportExploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience Report
ESUG
 
Pharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIsPharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIs
ESUG
 
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame CaseImproving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
ESUG
 
Pharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and FuturePharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and Future
ESUG
 
A New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and TransformationsA New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and Transformations
ESUG
 

Más de ESUG (20)

Workshop: Identifying concept inventories in agile programming
Workshop: Identifying concept inventories in agile programmingWorkshop: Identifying concept inventories in agile programming
Workshop: Identifying concept inventories in agile programming
 
Technical documentation support in Pharo
Technical documentation support in PharoTechnical documentation support in Pharo
Technical documentation support in Pharo
 
The Pharo Debugger and Debugging tools: Advances and Roadmap
The Pharo Debugger and Debugging tools: Advances and RoadmapThe Pharo Debugger and Debugging tools: Advances and Roadmap
The Pharo Debugger and Debugging tools: Advances and Roadmap
 
Sequence: Pipeline modelling in Pharo
Sequence: Pipeline modelling in PharoSequence: Pipeline modelling in Pharo
Sequence: Pipeline modelling in Pharo
 
Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...
 
Analyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early resultsAnalyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early results
 
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
 
A Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test GenerationA Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test Generation
 
Creating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic ProgrammingCreating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic Programming
 
Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes
Threaded-Execution and CPS Provide Smooth Switching Between Execution ModesThreaded-Execution and CPS Provide Smooth Switching Between Execution Modes
Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes
 
Exploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience ReportExploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience Report
 
Pharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIsPharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIs
 
Garbage Collector Tuning
Garbage Collector TuningGarbage Collector Tuning
Garbage Collector Tuning
 
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame CaseImproving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
 
Pharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and FuturePharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and Future
 
thisContext in the Debugger
thisContext in the DebuggerthisContext in the Debugger
thisContext in the Debugger
 
Websockets for Fencing Score
Websockets for Fencing ScoreWebsockets for Fencing Score
Websockets for Fencing Score
 
ShowUs: PharoJS.org Develop in Pharo, Run on JavaScript
ShowUs: PharoJS.org Develop in Pharo, Run on JavaScriptShowUs: PharoJS.org Develop in Pharo, Run on JavaScript
ShowUs: PharoJS.org Develop in Pharo, Run on JavaScript
 
Advanced Object- Oriented Design Mooc
Advanced Object- Oriented Design MoocAdvanced Object- Oriented Design Mooc
Advanced Object- Oriented Design Mooc
 
A New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and TransformationsA New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and Transformations
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Último (20)

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

GC in Smalltalk

  • 2. ESUG 2010 Object subclass: #GenerationalGC generational purgeRoots collect from self generational follow: object followRoots; followStack; generational moveToOldOrTo: object to rescueEphemerons; fixWeakContainers; generational flipSpaces fixReferencesOrSetTombstone: weakContainer generational addInterrupt
  • 3. ESUG 2011 Object subclass: #VMGarbageCollector VMGarbageCollector subclass: #GenerationalGC VMGarbageCollector subclass: #MarkAndCompactGC
  • 4. ESUG 2011 VMGarbageCollector subclass: #MarkAndCompactGC collect self old unseeWellKnownObjects; followAll; setNewPositions: oldSpace; setNewPositions: fromSpace; from prepareForCompact; compact: oldSpace; compact: fromSpace; to updateOldSpace; makeRescuedEphemeronsNonWeak; resetFrom; decommitSlack; addInterrupt
  • 5. MarkAndCompactGC Three phases Follow (mark) Set new positions Compact (move) collect self ... followAll; setNewPositions: oldSpace; setNewPositions: fromSpace; prepareForCompact; compact: oldSpace; compact: fromSpace; ...
  • 6. MarkAndCompactGC followAll Arena Precondition: all objects unseen from old VMGarbageCollector subclass: #MarkAndCompactGC Libraries Characters true false nil
  • 7. MarkAndCompactGC followAll Precondition: all objects unseen VMGarbageCollector subclass: #MarkAndCompactGC Libraries unseeWellKnownObjects nil _beUnseenInLibrary. Characters true _beUnseenInLibrary. false _beUnseenInLibrary. true self unseeLibraryObjects; false unseeCharacters; nil unseeSKernel
  • 8. MarkAndCompactGC followAll Arena Precondition: all objects unseen from old VMGarbageCollector subclass: #MarkAndCompactGC unseen: “natural” state in Arena
  • 9. MarkAndCompactGC followAll Arena Recognize & Mark all living objects from old VMGarbageCollector subclass: #MarkAndCompactGC Libraries Characters true false nil
  • 10. MarkAndCompactGC followAll Arena Recognize & Mark all living objects from old VMGarbageCollector subclass: #MarkAndCompactGC Libraries followAll self Characters followSKernel; followStack; true followExtraRoots; false rescueEphemerons; nil fixWeakContainers; followRescuedEphemerons
  • 11. MarkAndCompactGC followAll Arena Recognize & Mark all living objects from old VMGarbageCollector subclass: #MarkAndCompactGC Libraries followsSKernel self Characters follow: self sKernel count: self sKernelSize true startingAt: 1 false nil
  • 12. MarkAndCompactGC follow: root count: size startingAt: base Arena Recognize & Mark all living objects from old VMGarbageCollector subclass: #MarkAndCompactGC arenaIncludes: object ^(oldSpace includes: object) or: [fromSpace includes: object]
  • 13. MarkAndCompactGC follow: 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. MarkAndCompactGC follow: 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. MarkAndCompactGC follow: root count: size startingAt: base ... B A object _threadWith: reference at: oldIndex. C Z bits Morris, F. L. 1978. A time-and space-efficient garbage compaction algorithm. Communications of the ACM. 21, 8, 662-665.
  • 16. MarkAndCompactGC follow: root count: size startingAt: base ... B A object _threadWith: reference at: oldIndex. bits C Z Morris, F. L. 1978. A time-and space-efficient garbage compaction algorithm. Communications of the ACM. 21, 8, 662-665.
  • 17. MarkAndCompactGC followAll Arena Recognize & Mark all living objects from old VMGarbageCollector subclass: #MarkAndCompactGC Libraries followExtraRoots Characters self follow: self extraRoots count: 3 startingAt: 1 true followRescuedEphemerons false self follow: rescuedEphemerons count:... nil
  • 18. MarkAndCompactGC followAll Arena Recognize & Mark all living objects from old VMGarbageCollector subclass: #MarkAndCompactGC Libraries followStack Characters super followStack true rescueEphemerons false super rescueEphemerons nil Implemented VMGarbageCollector
  • 19. MarkAndCompactGC collect ... setNewPositions: oldSpace; B A setNewPositions: fromSpace; bits C Z
  • 20. MarkAndCompactGC collect ... setNewPositions: oldSpace; B A setNewPositions: fromSpace; Z' C Z bits
  • 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. 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. MarkAndCompactGC collect ... compact: oldSpace; B A compact: fromSpace; Z' C Z bits
  • 24. MarkAndCompactGC collect ... compact: oldSpace; B A compact: fromSpace; Z' C bits
  • 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. MarkAndCompactGC collect ... makeRescuedEphemeronsNonWeak; updateOldSpace; makeRescuedEphemeronsNonWeak rescuedEphemerons do: [:ephemeron | ephemeron _haveNoWeaks] updateOldSpace oldSpace loadFrom: auxSpace
  • 27. MarkAndCompactGC collect Free 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. MarkAndCompactGC collect Free resources ... decommitSlack; assembleDecommit ... return := assembler pushConstant: 16r4000; pushArg; pushR; callTo: 'VirtualFree' from: 'KERNEL32.DLL'; convertToNative; shortJump
  • 29. Stop We have a Smalltalk Scavenger
  • 30. Stop We have a Smalltalk Scavenger Written in Smalltalk
  • 31. Stop We have a Smalltalk Scavenger Written in Smalltalk Using objects
  • 32. Stop We have a Smalltalk Scavenger Written in Smalltalk Using objects Instantiating objects
  • 33. Stop We have a Smalltalk Scavenger Written in Smalltalk Using objects Instantiating objects How can it work?
  • 34. Stop We have a Smalltalk Scavenger Written in Smalltalk Using objects Instantiating objects How can it work? Object Closure
  • 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
  • 36. Object Closure BlockClosure makeRescuedEphemeronsNonWeak rescuedEphemerons do: [:ephemeron | ephemeron _haveNoWeaks] Environment context compact: 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. Object Closure BlockClosure makeRescuedEphemeronsNonWeak rescuedEphemerons do: [:ephemeron | ephemeron _haveNoWeaks] Environment context compact: 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 space's size when the GC starts
  • 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
  • 39. Object Closure Host VM interface arrays at: index ^contents _basicAt: index + 1 at: index put: value ^contents _basicAt: index + 1 put: value use _primitives instead of primitives
  • 40. Demo
  • 41. [Audience hasQuestions] whileTrue: [ self answer: Audience nextQuestion]. Audience do: [:you | self thank: you]. self returnTo: Audience