Skip to content

Commit

Permalink
Refactoring environment building
Browse files Browse the repository at this point in the history
  • Loading branch information
tesonep committed Feb 21, 2022
1 parent 4832058 commit 448e5fe
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -487,12 +487,6 @@ VMSimpleStackBasedCogitAbstractTest >> newJitCompiler [

]

{ #category : #running }
VMSimpleStackBasedCogitAbstractTest >> newMemory [

^ self memoryClass simulatorClass new
]

{ #category : #helpers }
VMSimpleStackBasedCogitAbstractTest >> openMachineDebugger [

Expand Down
136 changes: 136 additions & 0 deletions smalltalksrc/VMMakerTests/VMSimulatedEnvironmentBuilder.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
Class {
#name : #VMSimulatedEnvironmentBuilder,
#superclass : #Object,
#instVars : [
'interpreter',
'interpreterClass',
'initializationOptions',
'objectMemory',
'objectMemoryClass',
'wordSize',
'initialCodeSize',
'primitiveTraceLogSize'
],
#category : #'VMMakerTests-Builders'
}

{ #category : #building }
VMSimulatedEnvironmentBuilder >> build [

objectMemory := objectMemoryClass simulatorClass new.

interpreterClass
initializeWithOptions: initializationOptions
objectMemoryClass: objectMemory class.

interpreter := interpreterClass basicNew
objectMemory: objectMemory;
basicInitialize.

self doBuild
]

{ #category : #building }
VMSimulatedEnvironmentBuilder >> doBuild [

"100 k at least to put the class table in the old space.
Get total size be a multiple of 4K so Unicorn CPU emulator accepts it"
| oldSpaceSize newSpaceSize stackSpaceSize methodCacheSize rumpCStackSize initialAddress memoryManager edenSize |
oldSpaceSize := 2048 * 1024.

"Check #scavengerDenominator in SpurMemoryManager"
edenSize := 2 * 1024 + interpreter interpreterAllocationReserveBytes.
newSpaceSize := self roundToPageSize: (edenSize * 7 / 5).
stackSpaceSize := 9 * 4096.
methodCacheSize := 4096 * wordSize.
rumpCStackSize := 0.

initialAddress := 16r1000000.

"Set it to bootstrapping to allow smaller memories"
memoryManager := MachineSimulatorMemoryManager new.
memoryManager initialAddress: initialAddress.
memoryManager wordSize: wordSize.

interpreter memoryManager: memoryManager.
objectMemory createMemoryMap.

objectMemory memoryManager: memoryManager.
objectMemory coInterpreter: interpreter.

objectMemory
allocateMemoryOfSize: oldSpaceSize
newSpaceSize: newSpaceSize
stackSize: stackSpaceSize
codeSize: initialCodeSize
methodCacheSize: methodCacheSize
primitiveTraceLogSize: (self roundToPageSize: primitiveTraceLogSize)
rumpCStackSize: rumpCStackSize
initialAddress: initialAddress.

objectMemory initializePostBootstrap.
objectMemory setHeapSizeAtPreviousGC.

self assert: objectMemory objectMemory scavenger eden size > interpreter interpreterAllocationReserveBytes.
self assert: objectMemory objectMemory scavengeThreshold + interpreter interpreterAllocationReserveBytes <= objectMemory objectMemory scavenger eden limit.

objectMemory segmentManager collapseSegmentsPostSwizzle.
objectMemory segmentManager plantBridgeAtTheEndOfMemory.
objectMemory segmentManager assertBridge.

"Schedule a GC, so it does not try to schedule one"
objectMemory needGCFlag: 1.

]

{ #category : #accessing }
VMSimulatedEnvironmentBuilder >> initialCodeSize: anInteger [
initialCodeSize := anInteger
]

{ #category : #accessing }
VMSimulatedEnvironmentBuilder >> initializationOptions: aCollection [
initializationOptions := aCollection
]

{ #category : #accessing }
VMSimulatedEnvironmentBuilder >> interpreter [
^ interpreter
]

{ #category : #accessing }
VMSimulatedEnvironmentBuilder >> interpreterClass: aClass [
interpreterClass := aClass
]

{ #category : #accessing }
VMSimulatedEnvironmentBuilder >> objectMemory [
^ objectMemory
]

{ #category : #accessing }
VMSimulatedEnvironmentBuilder >> objectMemoryClass: aClass [
objectMemoryClass := aClass
]

{ #category : #accessing }
VMSimulatedEnvironmentBuilder >> primitiveTraceLogSize: anInteger [
primitiveTraceLogSize := anInteger
]

{ #category : #helpers }
VMSimulatedEnvironmentBuilder >> roundToPageSize: anInteger [

"Unicorn simulator requires mapped memory to be multiple of 4096"
| pageSize remainder |
pageSize := 4096.
remainder := anInteger \\ pageSize.
remainder = 0 ifTrue: [ ^ anInteger ].

^ anInteger + (pageSize - remainder)
]

{ #category : #accessing }
VMSimulatedEnvironmentBuilder >> wordSize: anInteger [
wordSize := anInteger
]
116 changes: 22 additions & 94 deletions smalltalksrc/VMMakerTests/VMSpurMemoryManagerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ Class {
'rumpCStackSize',
'wordSize',
'methodBuilder',
'edenSize'
'edenSize',
'environmentBuilder'
],
#pools : [
'VMBasicConstants',
Expand Down Expand Up @@ -68,12 +69,6 @@ VMSpurMemoryManagerTest >> createMethodOopFromPharoMethod: aPharoCompiledMethod
^ methodOop
]

{ #category : #accessing }
VMSpurMemoryManagerTest >> edenSize [

^ 2 * 1024 + interpreter interpreterAllocationReserveBytes
]

{ #category : #accessing }
VMSpurMemoryManagerTest >> initialCodeSize [
^ 0
Expand All @@ -86,7 +81,7 @@ VMSpurMemoryManagerTest >> initializationOptions [
#BytesPerWord.
self wordSize.
#ObjectMemory.
memory class name}
self memoryClass name}
]

{ #category : #helpers }
Expand Down Expand Up @@ -229,6 +224,14 @@ VMSpurMemoryManagerTest >> memory [
^ memory
]

{ #category : #running }
VMSpurMemoryManagerTest >> memoryClass [

^ self wordSize = 4
ifTrue: [ Spur32BitMemoryManager ]
ifFalse: [ Spur64BitMemoryManager ]
]

{ #category : #'helpers - classes' }
VMSpurMemoryManagerTest >> newClassInOldSpaceWithSlots: numberOfSlots instSpec: format [
| newClass formatWithSlots |
Expand All @@ -247,28 +250,6 @@ VMSpurMemoryManagerTest >> newClassInOldSpaceWithSlots: numberOfSlots instSpec:
^ newClass
]

{ #category : #helpers }
VMSpurMemoryManagerTest >> newInterpreter [

memory := self newMemory.

self interpreterClass
initializeWithOptions: self initializationOptions
objectMemoryClass: memory class.

^ self interpreterClass basicNew
objectMemory: memory;
basicInitialize
]

{ #category : #running }
VMSpurMemoryManagerTest >> newMemory [

^ self wordSize = 4
ifTrue: [ Spur32BitMemoryManager simulatorClass new ]
ifFalse: [ Spur64BitMemoryManager simulatorClass new ]
]

{ #category : #'helpers - methods' }
VMSpurMemoryManagerTest >> newMethodWithSmallContext: isSmall WithArguments: arguments [

Expand Down Expand Up @@ -428,18 +409,6 @@ VMSpurMemoryManagerTest >> primitiveTraceLogSize [
^ 0
]

{ #category : #helpers }
VMSpurMemoryManagerTest >> roundToPageSize: anInteger [

"Unicorn simulator requires mapped memory to be multiple of 4096"
| pageSize remainder |
pageSize := 4096.
remainder := anInteger \\ pageSize.
remainder = 0 ifTrue: [ ^ anInteger ].

^ anInteger + (pageSize - remainder)
]

{ #category : #running }
VMSpurMemoryManagerTest >> setContextClassIntoClassTable [
| aClass |
Expand Down Expand Up @@ -468,63 +437,22 @@ VMSpurMemoryManagerTest >> setMethodClassIntoClassTable [

{ #category : #running }
VMSpurMemoryManagerTest >> setUp [
| memoryManager |
super setUp.

interpreter := self newInterpreter.
environmentBuilder := VMSimulatedEnvironmentBuilder new.
environmentBuilder
interpreterClass: self interpreterClass;
objectMemoryClass: self memoryClass;
initializationOptions: self initializationOptions;
wordSize: self wordSize;
initialCodeSize: self initialCodeSize;
primitiveTraceLogSize: self primitiveTraceLogSize.

"100 k at least to put the class table in the old space.
Get total size be a multiple of 4K so Unicorn CPU emulator accepts it"
oldSpaceSize := 2048 * 1024.
edenSize := self edenSize.
environmentBuilder build.

"Check #scavengerDenominator in SpurMemoryManager"
newSpaceSize := self roundToPageSize: (edenSize * 7 / 5).
stackSpaceSize := 9 * 4096.
methodCacheSize := 4096 * self wordSize.
rumpCStackSize := 0.

initialAddress := 16r1000000.

objectHeaderSize := 8 "bytes. Always.".
emptyObjectSize := objectHeaderSize + 8 "minimum required single empty slot, to use for forwarders".

"Set it to bootstrapping to allow smaller memories"
memoryManager := MachineSimulatorMemoryManager new.
memoryManager initialAddress: initialAddress.
memoryManager wordSize: self wordSize.

interpreter memoryManager: memoryManager.
memory createMemoryMap.

memory := interpreter objectMemory.
memory memoryManager: memoryManager.
memory coInterpreter: interpreter.
interpreter objectMemory: memory.
interpreter := environmentBuilder interpreter.
memory := environmentBuilder objectMemory.

memory
allocateMemoryOfSize: oldSpaceSize
newSpaceSize: newSpaceSize
stackSize: stackSpaceSize
codeSize: self initialCodeSize
methodCacheSize: methodCacheSize
primitiveTraceLogSize: (self roundToPageSize: self primitiveTraceLogSize)
rumpCStackSize: rumpCStackSize
initialAddress: initialAddress.

memory initializePostBootstrap.
memory setHeapSizeAtPreviousGC.

self assert: memory objectMemory scavenger eden size > interpreter interpreterAllocationReserveBytes.
self assert: memory objectMemory scavengeThreshold + interpreter interpreterAllocationReserveBytes <= memory objectMemory scavenger eden limit.

memory segmentManager collapseSegmentsPostSwizzle.
memory segmentManager plantBridgeAtTheEndOfMemory.
memory segmentManager assertBridge.

"Schedule a GC, so it does not try to schedule one"
memory needGCFlag: 1.

methodBuilder := VMCompiledCodeBuilder new
interpreter: interpreter;
memory: memory;
Expand Down

0 comments on commit 448e5fe

Please sign in to comment.