-
Notifications
You must be signed in to change notification settings - Fork 71
/
CogMethodZone.class.st
963 lines (871 loc) · 32.9 KB
/
CogMethodZone.class.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
"
I am a simple allocator/deallocator for the native code zone. I also manage the youngReferers list, which contains methods that may refer to one or more young objects, and the openPICList which is a linked list of all open PICs in the zone.
Instance Variables
baseAddress: <Integer address>
coInterpreter: <CoInterpreter>
cogit: <Cogit>
limitAddress: <Integer address>
methodBytesFreedSinceLastCompaction: <Integer>
methodCount: <Integer>
mzFreeStart: <Integer address>
objectMemory: <NewCoObjectMemory|SpurCoMemoryManager>
objectRepresentation: <CogObjectRepresentation:>
openPICList: <CogMethod *|nil>
unpairedMethodList: <CogMethod *|nil>
youngReferrers: <Integer address>
baseAddress
- the lowest address in the native method zone
coInterpreter
- simulation-only
cogit
- simulation-only
limitAddress
- the address immediately following the native method zone
methodBytesFreedSinceLastCompaction
- a count of the bytes in methods freed since the last compaction of the native method zone, used to answer the used bytes in the zone
methodCount
- a count of the number of methods in the native method zone
mzFreeStart
- the start of free space in the zone
objectMemory
- simulation-only
objectRepresentation
- simulation-only
openPICList
- the head of the list of open PICs
unpairedMethodList
- the head of the list of Cog methods with no associated CompiledMethod object (Newspeak only)
youngReferrers
- the pointer to the start of an array of pointers to CogMethods that refer to young objects. May contain false positives. Occupies the top of the zone from youngReferrers up to limitAddress
"
Class {
#name : #CogMethodZone,
#superclass : #VMClass,
#instVars : [
'youngReferrers',
'unpairedMethodList',
'methodCount',
'openPICList',
'mzFreeStart',
'baseAddress',
'limitAddress',
'methodBytesFreedSinceLastCompaction',
'compactionInProgress',
'coInterpreter',
'objectRepresentation',
'cogit',
'objectMemory',
'percentageToEnsureRelease'
],
#pools : [
'CogMethodConstants',
'VMBasicConstants'
],
#category : #'VMMaker-JIT'
}
{ #category : #translation }
CogMethodZone class >> declareCVarsIn: aCCodeGenerator [
aCCodeGenerator
removeVariable: 'coInterpreter';
removeVariable: 'objectRepresentation'.
self declareC: #(youngReferrers mzFreeStart baseAddress limitAddress unpairedMethodList)
as: #usqInt
in: aCCodeGenerator
]
{ #category : #translation }
CogMethodZone class >> implicitReturnTypeFor: aSelector [
"Answer the return type for methods that don't have an explicit return."
^#void
]
{ #category : #translation }
CogMethodZone class >> isNonArgumentImplicitReceiverVariableName: aString [
^#('self' 'coInterpreter' 'objectRepresentation') includes: aString
]
{ #category : #translation }
CogMethodZone class >> typeForSelf [
^#implicit
]
{ #category : #'young referers' }
CogMethodZone >> addAllToYoungReferrers [
<api>
<returnTypeC: #void>
| cogMethod |
<var: #cogMethod type: #'CogMethod *'>
cogit enableCodeZoneWriteDuring: [
cogMethod := coInterpreter cCoerceSimple: baseAddress to: #'CogMethod *'.
[cogMethod < self limitZony] whileTrue:
[(cogMethod cmType = CMMethod
or: [cogMethod cmType = CMMegamorphicIC]) ifTrue:
[self ensureInYoungReferrers: cogMethod].
cogMethod := self methodAfter: cogMethod]]
]
{ #category : #accessing }
CogMethodZone >> addToMegamorphicICList: anOpenPIC [
<var: #anOpenPIC type: #'CogMethod *'>
self assert: anOpenPIC cmType = CMMegamorphicIC.
self assert: (openPICList == nil
or: [openPICList cmType = CMMegamorphicIC]).
anOpenPIC nextOpenPIC: openPICList asUnsignedInteger.
openPICList := anOpenPIC
]
{ #category : #'young referers' }
CogMethodZone >> addToYoungReferrers: cogMethod [
<var: #cogMethod type: #'CogMethod *'>
self assert: youngReferrers <= limitAddress.
self assert: (self occurrencesInYoungReferrers: cogMethod) = 0.
self assert: cogMethod cmRefersToYoung.
self assert: (youngReferrers <= limitAddress
and: [youngReferrers >= (limitAddress - (methodCount * objectMemory wordSize))]).
(self asserta: limitAddress - (methodCount * objectMemory wordSize) >= mzFreeStart) ifFalse:
[self error: 'no room on youngReferrers list'].
youngReferrers := youngReferrers - objectMemory wordSize.
objectMemory longAt: youngReferrers put: cogMethod asUnsignedInteger
]
{ #category : #allocating }
CogMethodZone >> allocate: numBytes [
| roundedBytes allocation |
roundedBytes := numBytes + 7 bitAnd: -8.
mzFreeStart + roundedBytes >= self allocationLimit ifTrue:
[^0].
allocation := mzFreeStart.
mzFreeStart := mzFreeStart + roundedBytes.
methodCount := methodCount + 1.
self cCode: '' inSmalltalk:
[(cogit breakPC isInteger
and: [cogit breakPC between: allocation and: mzFreeStart]) ifTrue:
[cogit singleStep: true]].
^allocation
]
{ #category : #allocating }
CogMethodZone >> allocationLimit [
<inline: true>
^limitAddress - (methodCount * objectMemory wordSize)
]
{ #category : #'as yet unclassified' }
CogMethodZone >> baseAddress [
<doNotGenerate>
^ baseAddress
]
{ #category : #'as yet unclassified' }
CogMethodZone >> baseAddress: anInteger [
<doNotGenerate>
baseAddress := anInteger
]
{ #category : #'jit - api' }
CogMethodZone >> clearCogCompiledCode [
"Free all methods"
| cogMethod |
<var: #cogMethod type: #'CogMethod *'>
cogit enableCodeZoneWriteDuring: [
cogMethod := coInterpreter cCoerceSimple: baseAddress to: #'CogMethod *'.
[cogMethod asUnsignedInteger < mzFreeStart] whileTrue:
[cogMethod cmType = CMMethod ifTrue:
[self freeMethod: cogMethod].
cogMethod := self methodAfter: cogMethod].
self manageFrom: baseAddress to: limitAddress ]
]
{ #category : #compaction }
CogMethodZone >> clearSavedPICUsageCount: cogMethod [
"For Sista, where we want PICs to last so they can be observed, we need to keep PICs unless
they are definitely unused. So we need to identify unused PICs. So in planCompact, zero the
usage counts of all PICs, saving the actual usage count in blockEntryOffset. Then in
relocateMethodsPreCompaction (actually in relocateIfCallOrMethodReference:mcpc:delta:)
restore the usage counts of used PICs. Finally in compactCompiledCode, clear the blockEntryOffset
of the unused PICs; they will then have a zero count and be reclaimed in the next code compaction."
<var: #cogMethod type: #'CogMethod *'>
<option: #SistaVM>
cogMethod cmType = CMPolymorphicIC ifTrue:
[cogMethod picUsage: 0]
]
{ #category : #printing }
CogMethodZone >> cogMethodsSelect: aBlock [
<doNotGenerate>
| cogMethod cogMethods |
cogMethods := OrderedCollection new: 256.
cogMethod := coInterpreter cCoerceSimple: baseAddress to: #'CogMethod *'.
[cogMethod < self limitZony] whileTrue:
[(aBlock value: cogMethod) ifTrue:
[cogMethods addLast: cogMethod].
cogMethod := self methodAfter: cogMethod].
^cogMethods
]
{ #category : #'simulation only' }
CogMethodZone >> cogit [
"This is for the sizeof: CogMethod hook that allows different cogit classes to use differet CogMethod variants."
<doNotGenerate>
^cogit
]
{ #category : #compaction }
CogMethodZone >> compactCompiledCode [
| objectHeaderValue source dest bytes |
<var: #source type: #'CogMethod *'>
<var: #dest type: #'CogMethod *'>
compactionInProgress := true.
objectHeaderValue := objectMemory nullHeaderForMachineCodeMethod.
source := coInterpreter cCoerceSimple: baseAddress to: #'CogMethod *'.
self voidOpenPICList. "The list will be rebuilt with the current live set"
methodCount := 0.
[source < self limitZony
and: [source cmType ~= CMFree]] whileTrue:
[self assert: (cogit cogMethodDoesntLookKosher: source) = 0.
source objectHeader: objectHeaderValue.
source cmUsageCount > 0 ifTrue:
[source cmUsageCount: source cmUsageCount // 2].
SistaVM ifTrue:
[self clearSavedPICUsageCount: source].
source cmType = CMMegamorphicIC ifTrue:
[self addToMegamorphicICList: source].
methodCount := methodCount + 1.
source := self methodAfter: source].
source >= self limitZony ifTrue:
[^self halt: 'no free methods; cannot compact.'].
dest := source.
[source < self limitZony] whileTrue:
[self assert: (cogit maybeFreeCogMethodDoesntLookKosher: source) = 0.
bytes := source blockSize.
source cmType ~= CMFree ifTrue:
[methodCount := methodCount + 1.
objectMemory memmove: dest _: source _: bytes.
dest objectHeader: objectHeaderValue.
dest cmType = CMMethod
ifTrue:
["There should be a one-to-one mapping between bytecoded and cog methods."
"Only update the original method's header if it is referring to this CogMethod."
(coInterpreter rawHeaderOf: dest methodObject) asInteger = source asInteger
ifTrue:
[coInterpreter rawHeaderOf: dest methodObject put: dest asInteger]
ifFalse:
[self assert: (cogit noAssertMethodClassAssociationOf: dest methodObject) = objectMemory nilObject]]
ifFalse:
[SistaVM ifTrue:
[self clearSavedPICUsageCount: dest].
dest cmType = CMMegamorphicIC ifTrue:
[self addToMegamorphicICList: dest]].
dest cmUsageCount > 0 ifTrue:
[dest cmUsageCount: dest cmUsageCount // 2].
dest := coInterpreter
cCoerceSimple: dest asUnsignedInteger + bytes
to: #'CogMethod *'].
source := coInterpreter
cCoerceSimple: source asUnsignedInteger + bytes
to: #'CogMethod *'].
mzFreeStart := dest asUnsignedInteger.
methodBytesFreedSinceLastCompaction := 0.
compactionInProgress := false
]
{ #category : #accessing }
CogMethodZone >> compactionInProgress [
^compactionInProgress
]
{ #category : #allocating }
CogMethodZone >> effectiveLimit [
<inline: true>
^limitAddress
]
{ #category : #'young referers' }
CogMethodZone >> ensureInYoungReferrers: cogMethod [
<var: #cogMethod type: #'CogMethod *'>
cogMethod cmRefersToYoung ifFalse:
[self assert: (self occurrencesInYoungReferrers: cogMethod) = 0.
cogMethod cmRefersToYoung: true.
self addToYoungReferrers: cogMethod]
]
{ #category : #'jit - api' }
CogMethodZone >> followForwardedLiteralsInOpenPICList [
<option: #SpurObjectMemory>
| openPIC |
<var: #openPIC type: #'CogMethod *'>
openPIC := openPICList.
[openPIC notNil] whileTrue:
[cogit followForwardedLiteralsIn: openPIC.
openPIC := self cCoerceSimple: openPIC nextOpenPIC to: #'CogMethod *'.]
]
{ #category : #compaction }
CogMethodZone >> freeMethod: cogMethod [
<api>
<var: #cogMethod type: #'CogMethod *'>
<inline: false>
self assert: cogMethod cmType ~= CMFree.
self assert: (cogit cogMethodDoesntLookKosher: cogMethod) = 0.
cogMethod cmType = CMMethod ifTrue:
["There should be a one-to-one mapping between bytecoded and cog methods."
"Only reset the original method's header if it is referring to this CogMethod."
(coInterpreter rawHeaderOf: cogMethod methodObject) asInteger = cogMethod asInteger
ifTrue:
[ coInterpreter rawHeaderOf: cogMethod methodObject put: cogMethod methodHeader ]
ifFalse:
[self cCode: [self assert: (cogit noAssertMethodClassAssociationOf: cogMethod methodObject) = objectMemory nilObject]
inSmalltalk: [self assert: ((cogit noAssertMethodClassAssociationOf: cogMethod methodObject) = objectMemory nilObject
or: [coInterpreter isKindOf: CurrentImageCoInterpreterFacade])] ].
cogit maybeFreeCountersOf: cogMethod].
cogMethod cmType = CMMegamorphicIC ifTrue:
[self removeFromOpenPICList: cogMethod].
cogMethod cmRefersToYoung: false.
cogMethod cmType: CMFree.
methodBytesFreedSinceLastCompaction := methodBytesFreedSinceLastCompaction
+ cogMethod blockSize
]
{ #category : #compaction }
CogMethodZone >> freeOlderMethodsForCompaction [
"Free methods, preferring older methods for compaction, up to some fraction, currently a quarter."
<var: #cogMethod type: #'CogMethod *'>
| zoneSize amountToFree initialFreeSpace freedSoFar freeableUsage cogMethod |
zoneSize := self effectiveLimit - baseAddress.
initialFreeSpace := self effectiveLimit - mzFreeStart
+ methodBytesFreedSinceLastCompaction.
freedSoFar := initialFreeSpace.
amountToFree := zoneSize // 4. "4 needs to be e.g. a start-up parameter"
freeableUsage := 0.
[
cogMethod := cogit cCoerceSimple: baseAddress to: #'CogMethod *'.
[
cogMethod asUnsignedInteger < mzFreeStart and: [
freedSoFar < amountToFree ] ] whileTrue: [
(self shouldFreeMethod: cogMethod given: freeableUsage) ifTrue: [
self freeMethod: cogMethod.
freedSoFar := freedSoFar + cogMethod blockSize ].
cogMethod := self methodAfter: cogMethod ].
freedSoFar < amountToFree and: [
(freeableUsage := freeableUsage + 1) < CMMaxUsageCount ] ] whileTrue
]
{ #category : #accessing }
CogMethodZone >> freeStart [
"declared as a macro so as not to conflict with the memory manager's freeStart."
<cmacro: '() mzFreeStart'>
^mzFreeStart
]
{ #category : #accessing }
CogMethodZone >> freeStart: zoneLimit [
<doNotGenerate>
^baseAddress := mzFreeStart := zoneLimit
]
{ #category : #initialization }
CogMethodZone >> initialize [
"Make youngReferrers arithmetic for addressIsInCodeZone:"
youngReferrers := methodCount := methodBytesFreedSinceLastCompaction := 0.
compactionInProgress := false
]
{ #category : #'young referers' }
CogMethodZone >> kosherYoungReferrers [
"Answer that all entries in youngReferrers are in-use and have the cmRefersToYoung flag set.
Used to check that the youngreferrers pruning routines work correctly."
| pointer cogMethod |
<var: #pointer type: #usqInt>
<var: #cogMethod type: #'CogMethod *'>
(youngReferrers > limitAddress
or: [youngReferrers < mzFreeStart]) ifTrue:
[^false].
pointer := youngReferrers.
[pointer < limitAddress] whileTrue:
[cogMethod := coInterpreter cCoerceSimple: (objectMemory longAt: pointer) to: #'CogMethod *'.
(cogMethod cmType ~= CMFree and: [cogMethod cmRefersToYoung]) ifFalse:
[^false].
pointer := pointer + objectMemory wordSize].
^true
]
{ #category : #accessing }
CogMethodZone >> limitZony [ "Let us salute Andrei Tarkovsky and his movie Stalker"
<cmacro: '() ((CogMethod *)mzFreeStart)'>
^mzFreeStart
]
{ #category : #initialization }
CogMethodZone >> manageFrom: theStartAddress to: theLimitAddress [
<returnTypeC: #void>
mzFreeStart := baseAddress := theStartAddress.
youngReferrers := limitAddress := theLimitAddress.
openPICList := nil.
methodBytesFreedSinceLastCompaction := 0.
methodCount := 0
]
{ #category : #accessing }
CogMethodZone >> methodAfter: cogMethod [
<var: #cogMethod type: #'CogMethod *'>
<inline: true>
^coInterpreter
cCoerceSimple: (self roundUpLength: cogMethod asInteger + cogMethod blockSize)
to: #'CogMethod *'
]
{ #category : #accessing }
CogMethodZone >> methodBytesFreedSinceLastCompaction [
<cmacro: '() methodBytesFreedSinceLastCompaction'>
^methodBytesFreedSinceLastCompaction
]
{ #category : #'jit - api' }
CogMethodZone >> methodFor: address [
<api>
<returnTypeC: #'CogMethod *'>
<var: #address type: #'void *'>
| cogMethod nextMethod |
<var: #cogMethod type: #'CogMethod *'>
<var: #nextMethod type: #'CogMethod *'>
cogMethod := coInterpreter cCoerceSimple: baseAddress to: #'CogMethod *'.
[cogMethod < self limitZony
and: [cogMethod asUnsignedInteger <= address asUnsignedInteger]] whileTrue:
[nextMethod := self methodAfter: cogMethod.
nextMethod = cogMethod ifTrue:
[^0].
(address asUnsignedInteger >= cogMethod asUnsignedInteger
and: [address asUnsignedInteger < nextMethod asUnsignedInteger]) ifTrue:
[^cogMethod].
cogMethod := nextMethod].
^0
]
{ #category : #'simulation only' }
CogMethodZone >> methods [
<doNotGenerate>
| methods |
methods := OrderedCollection new.
self methodsDo:
[:m| methods addLast: m].
^methods
]
{ #category : #'method zone introspection' }
CogMethodZone >> methodsCompiledToMachineCodeInto: arrayObj [
<api>
<var: #cogMethod type: #'CogMethod *'>
| cogMethod methodIndex |
methodIndex := 0.
cogMethod := cogit cCoerceSimple: baseAddress to: #'CogMethod *'.
[cogMethod < self limitZony] whileTrue:
[cogMethod cmType = CMMethod ifTrue:
[objectMemory
storePointerUnchecked: methodIndex
ofObject: arrayObj
withValue: cogMethod methodObject.
methodIndex := methodIndex + 1].
cogMethod := self methodAfter: cogMethod].
^ methodIndex
]
{ #category : #'simulation only' }
CogMethodZone >> methodsDo: aBlock [
<doNotGenerate>
| cogMethod |
cogMethod := cogit cCoerceSimple: baseAddress to: #'CogMethod *'.
[cogMethod < self limitZony] whileTrue:
[cogMethod cmType ~= CMFree ifTrue:
[aBlock value: cogMethod].
cogMethod := self methodAfter: cogMethod]
"<api>
<returnTypeC: #void>
| cogMethod |
<var: #cogMethod type: #'CogMethod *'>
cogMethod := cogit cCoerceSimple: baseAddress to: #'CogMethod *'.
[cogMethod < self limitZony] whileTrue:
[cogMethod cmType ~= CMFree ifTrue:
[aBlock value: cogMethod].
cogMethod := self methodAfter: cogMethod]"
]
{ #category : #accessing }
CogMethodZone >> numMethods [
<api>
^methodCount
]
{ #category : #'jit - api' }
CogMethodZone >> numMethodsOfType: cogMethodType [
<api>
| n cogMethod |
<var: #cogMethod type: #'CogMethod *'>
n := 0.
cogMethod := cogit cCoerceSimple: baseAddress to: #'CogMethod *'.
[cogMethod < self limitZony] whileTrue:
[cogMethod cmType = cogMethodType ifTrue:
[n := n + 1].
cogMethod := self methodAfter: cogMethod].
^n
]
{ #category : #'young referers' }
CogMethodZone >> occurrencesInYoungReferrers: cogMethod [
<var: #cogMethod type: #'CogMethod *'>
| count pointer |
<var: #pointer type: #usqInt>
self assert: youngReferrers <= limitAddress.
count := 0.
pointer := youngReferrers.
[pointer < limitAddress] whileTrue:
[cogMethod asInteger = (objectMemory longAt: pointer) ifTrue:
[count := count + 1].
pointer := pointer + objectMemory wordSize].
^count
]
{ #category : #accessing }
CogMethodZone >> openPICWithSelector: aSelector [
| openPIC |
<var: #openPIC type: #'CogMethod *'>
<returnTypeC: #'CogMethod *'>
openPIC := openPICList.
[(openPIC == nil
or: [openPIC selector = aSelector]) ifTrue:
[^openPIC].
"N.B. Use self rather than coInterpreter to avoid attempting to cast nil.
Conversion to CogMethod done in the nextOpenPIC accessor."
openPIC := self cCoerceSimple: openPIC nextOpenPIC to: #'CogMethod *'.
true] whileTrue
]
{ #category : #compaction }
CogMethodZone >> planCompaction [
"Some methods have been freed. Compute how much each survivor needs to
move during the ensuing compaction and record it in the objectHeader field.
For Sista, where we want PICs to last so they can be observed, we need to keep PICs unless
they are definitely unused. So we need to identify unused PICs. So in planCompact, zero the
usage counts of all PICs, saving the actual usage count in blockEntryOffset. Then in
relocateMethodsPreCompaction (actually in relocateIfCallOrMethodReference:mcpc:delta:)
restore the usage counts of used PICs. Finally in compactCompiledCode, clear the blockEntryOffset
of the unused PICs; they will then have a zero count and be reclaimed in the next code compaction."
| delta cogMethod |
<var: #cogMethod type: #'CogMethod *'>
delta := 0.
cogMethod := cogit cCoerceSimple: baseAddress to: #'CogMethod *'.
[cogMethod asUnsignedInteger < mzFreeStart] whileTrue:
[cogMethod cmType = CMFree
ifTrue: [delta := delta - cogMethod blockSize]
ifFalse:
[self assert: (cogit cogMethodDoesntLookKosher: cogMethod) = 0.
cogMethod objectHeader: delta.
SistaVM ifTrue:
[self savePICUsageCount: cogMethod]].
cogMethod := self methodAfter: cogMethod]
]
{ #category : #printing }
CogMethodZone >> printCogMethods [
<api>
<returnTypeC: #void>
| cogMethod nm nc no nf nu |
<var: #cogMethod type: #'CogMethod *'>
nm := nc := no := nf := nu := 0.
cogMethod := coInterpreter cCoerceSimple: baseAddress to: #'CogMethod *'.
[cogMethod < self limitZony] whileTrue:
[coInterpreter printCogMethod: cogMethod.
cogMethod cmType
caseOf: {
[CMFree] -> [nf := nf + 1].
[CMMethod] -> [nm := nm + 1].
[CMPolymorphicIC] -> [nc := nc + 1].
[CMMegamorphicIC] -> [no:= no+ 1] }
otherwise: [nu := nu + 1].
cogMethod := self methodAfter: cogMethod].
coInterpreter print: 'CMMethod '; printNum: nm; print: ' CMClosedPIC '; printNum: nc; print: ' CMOpenPIC '; printNum: no; print: ' CMFree '; printNum: nf.
nu > 0 ifTrue:
[coInterpreter print: ' UNKNOWN '; printNum: nu].
coInterpreter print: ' total '; printNum: nm+nc+no+nf+nu; cr
]
{ #category : #printing }
CogMethodZone >> printCogMethodsOfType: cmType [
<api>
<returnTypeC: #void>
| cogMethod |
<var: #cogMethod type: #'CogMethod *'>
cogMethod := coInterpreter cCoerceSimple: baseAddress to: #'CogMethod *'.
[cogMethod < self limitZony] whileTrue:
[cogMethod cmType = cmType ifTrue:
[coInterpreter printCogMethod: cogMethod].
cogMethod := self methodAfter: cogMethod]
]
{ #category : #printing }
CogMethodZone >> printCogMethodsWithMethod: methodOop [
<api>
| cogMethod |
<var: #cogMethod type: #'CogMethod *'>
cogMethod := coInterpreter cCoerceSimple: baseAddress to: #'CogMethod *'.
[cogMethod < self limitZony] whileTrue:
[(cogMethod cmType ~= CMFree
and: [cogMethod methodObject = methodOop]) ifTrue:
[coInterpreter printCogMethod: cogMethod].
cogMethod := self methodAfter: cogMethod]
]
{ #category : #printing }
CogMethodZone >> printCogMethodsWithPrimitive: primIdx [
<api>
| cogMethod |
<var: #cogMethod type: #'CogMethod *'>
cogMethod := coInterpreter cCoerceSimple: baseAddress to: #'CogMethod *'.
[cogMethod < self limitZony] whileTrue:
[(cogMethod cmType = CMMethod
and: [primIdx = (coInterpreter primitiveIndexOfMethod: cogMethod methodObject
header: cogMethod methodHeader)]) ifTrue:
[coInterpreter printCogMethod: cogMethod].
cogMethod := self methodAfter: cogMethod]
]
{ #category : #printing }
CogMethodZone >> printCogMethodsWithSelector: selectorOop [
<api>
| cogMethod |
<var: #cogMethod type: #'CogMethod *'>
cogMethod := coInterpreter cCoerceSimple: baseAddress to: #'CogMethod *'.
[cogMethod < self limitZony] whileTrue:
[(cogMethod cmType ~= CMFree
and: [cogMethod selector = selectorOop]) ifTrue:
[coInterpreter printCogMethod: cogMethod].
cogMethod := self methodAfter: cogMethod]
]
{ #category : #printing }
CogMethodZone >> printCogYoungReferrers [
<api>
<returnTypeC: #void>
| pointer cogMethod |
<var: #cogMethod type: #'CogMethod *'>
pointer := youngReferrers.
[pointer < limitAddress] whileTrue:
[cogMethod := coInterpreter cCoerceSimple: (objectMemory longAt: pointer) to: #'CogMethod *'.
cogMethod cmRefersToYoung ifFalse:
[coInterpreter print: '*'].
cogMethod cmType = CMFree ifTrue:
[coInterpreter print: '!'].
(cogMethod cmRefersToYoung and: [cogMethod cmType ~= CMFree]) ifFalse:
[coInterpreter print: ' '].
coInterpreter printCogMethod: cogMethod.
pointer := pointer + objectMemory wordSize]
]
{ #category : #accessing }
CogMethodZone >> printOpenPICList [
<api>
| n openPIC |
<var: #openPIC type: #'CogMethod *'>
n := 0.
openPIC := openPICList.
[openPIC == nil] whileFalse:
[n := n + 1.
coInterpreter printCogMethod: openPIC.
openPIC := self cCoerceSimple: openPIC nextOpenPIC to: #'CogMethod *'].
^n
]
{ #category : #'young referers' }
CogMethodZone >> pruneYoungReferrers [
| source dest next |
<var: #source type: #usqInt>
<var: #dest type: #usqInt>
<var: #next type: #usqInt>
<inline: false>
self assert: youngReferrers <= limitAddress.
youngReferrers = limitAddress ifTrue:
[^nil].
dest := limitAddress.
[next := dest - objectMemory wordSize.
next >= youngReferrers
and: [(coInterpreter cCoerceSimple: (objectMemory longAt: next) to: #'CogMethod *') cmRefersToYoung]] whileTrue:
[dest := next].
self assert: dest >= youngReferrers.
source := dest - objectMemory wordSize.
[source >= youngReferrers] whileTrue:
[(coInterpreter cCoerceSimple: (objectMemory longAt: source) to: #'CogMethod *') cmRefersToYoung ifTrue:
[self assert: source < (dest - objectMemory wordSize).
objectMemory longAt: (dest := dest - objectMemory wordSize) put: (objectMemory longAt: source)].
source := source - objectMemory wordSize].
youngReferrers := dest.
self assert: self kosherYoungReferrers
]
{ #category : #'jit - api' }
CogMethodZone >> quickClearCogCompiledCode [
"Quick, for in-image testing"
<doNotGenerate>
mzFreeStart := baseAddress.
self clearCogCompiledCode.
cogit backEnd stopsFrom: mzFreeStart to: limitAddress - 1
]
{ #category : #'young referers' }
CogMethodZone >> relocateAndPruneYoungReferrers [
"This method compacts the young referrers array, and updates the references to the methods were moved."
"Young referrers is an array that holds references to CogMethods that references objects that are in the new space.
The young referrers array is stored at the end of the code cache, it grows towards lower addresses.
The variable youngReferrers points to the lowest entry in the array, and it spans up to the limitAddress"
| source dest next cogMethod |
<var: #source type: #usqInt>
<var: #dest type: #usqInt>
<var: #next type: #usqInt>
<var: #cogMethod type: #'CogMethod *'>
<inline: false>
self assert: youngReferrers <= limitAddress.
youngReferrers = limitAddress ifTrue:
[^nil].
dest := limitAddress.
[next := dest - objectMemory wordSize.
next >= youngReferrers
and: [(cogMethod := coInterpreter cCoerceSimple: (objectMemory longAt: next) to: #'CogMethod *') cmType ~= CMFree
and: [cogMethod cmRefersToYoung]]] whileTrue:
[cogMethod objectHeader ~= 0 ifTrue:
[coInterpreter longAt: next put: cogMethod asInteger + cogMethod objectHeader].
dest := next].
self assert: dest >= youngReferrers.
source := dest - objectMemory wordSize.
[source >= youngReferrers] whileTrue:
[cogMethod := coInterpreter cCoerceSimple: (objectMemory longAt: source) to: #'CogMethod *'.
(cogMethod cmType ~= CMFree
and: [cogMethod cmRefersToYoung]) ifTrue:
[self assert: source < (dest - objectMemory wordSize).
cogMethod objectHeader ~= 0 ifTrue:
[cogMethod := coInterpreter
cCoerceSimple: cogMethod asInteger + cogMethod objectHeader asInteger
to: #'CogMethod *'].
objectMemory longAt: (dest := dest - objectMemory wordSize) put: cogMethod asInteger].
source := source - objectMemory wordSize].
youngReferrers := dest.
"this assert must be deferred until after compaction. See the end of compactCogCompiledCode"
"self assert: self kosherYoungReferrers"
]
{ #category : #compaction }
CogMethodZone >> relocateMethodsPreCompaction [
"All surviving methods have had the amount they are going to relocate by
stored in their objectHeader fields. Relocate all relative calls so that after
the compaction of both the method containing each call and the call target
the calls invoke the same target."
| cogMethod |
<var: #cogMethod type: #'CogMethod *'>
cogMethod := cogit cCoerceSimple: baseAddress to: #'CogMethod *'.
[cogMethod asUnsignedInteger < mzFreeStart] whileTrue:
[cogMethod cmType ~= CMFree ifTrue:
[cogMethod cmType = CMPolymorphicIC
ifTrue: [cogit relocateCallsInClosedPIC: cogMethod]
ifFalse: [cogit relocateCallsAndSelfReferencesInMethod: cogMethod]].
cogMethod := self methodAfter: cogMethod].
self relocateAndPruneYoungReferrers.
^true
]
{ #category : #accessing }
CogMethodZone >> removeFromOpenPICList: anOpenPIC [
<var: #anOpenPIC type: #'CogMethod *'>
| prevPIC |
<var: #prevPIC type: #'CogMethod *'>
self assert: anOpenPIC cmType = CMMegamorphicIC.
openPICList ifNil: [^nil]. "As it is when compacting or unlinking all sends"
self assert: (openPICList cmType = CMMegamorphicIC
and: [openPICList nextOpenPIC isNil
or: [(self cCoerceSimple: openPICList nextOpenPIC to: #'CogMethod *') cmType = CMMegamorphicIC]]).
anOpenPIC = openPICList ifTrue:
["N.B. Use self rather than coInterpreter to avoid attempting to cast nil.
Conversion to CogMethod done in the nextOpenPIC accessor."
openPICList := self cCoerceSimple: anOpenPIC nextOpenPIC to: #'CogMethod *'.
^nil].
prevPIC := openPICList.
[self assert: (prevPIC ~~ nil
and: [prevPIC cmType = CMMegamorphicIC]).
prevPIC nextOpenPIC = anOpenPIC asUnsignedInteger ifTrue:
[prevPIC nextOpenPIC: anOpenPIC nextOpenPIC.
^nil].
prevPIC := self cCoerceSimple: prevPIC nextOpenPIC to: #'CogMethod *'.
true] whileTrue
]
{ #category : #compaction }
CogMethodZone >> restorePICUsageCount: cogMethod [
"For Sista, where we want PICs to last so they can be observed, we need to keep PICs unless
they are definitely unused. So we need to identify unused PICs. So in planCompact, zero the
usage counts of all PICs, saving the actual usage count in blockEntryOffset. Then in
relocateMethodsPreCompaction (actually in relocateIfCallOrMethodReference:mcpc:delta:)
restore the usage counts of used PICs. Finally in compactCompiledCode, clear the blockEntryOffset
of the unused PICs; they will then have a zero count and be reclaimed in the next code compaction."
<var: #cogMethod type: #'CogMethod *'>
<option: #SistaVM>
(cogMethod cmType = CMPolymorphicIC
and: [cogMethod picUsage ~= 0]) ifTrue:
[cogMethod
cmUsageCount: cogMethod picUsage;
picUsage: 0]
]
{ #category : #accessing }
CogMethodZone >> roundUpAddress: address [
<returnTypeC: #'void *'>
<var: #address type: #'void *'>
^self cCoerce: ((self cCoerce: address to: #'usqIntptr_t') + 7 bitAnd: -8) to: #'void *'
]
{ #category : #accessing }
CogMethodZone >> roundUpLength: numBytes [
<cmacro: '(numBytes) ((numBytes) + 7 & -8)'>
^numBytes + 7 bitAnd: -8
]
{ #category : #compaction }
CogMethodZone >> savePICUsageCount: cogMethod [
"For Sista, where we want PICs to last so they can be observed, we need to keep PICs unless
they are definitely unused. So we need to identify unused PICs. So in planCompact, zero the
usage counts of all PICs, saving the actual usage count in blockEntryOffset. Then in
relocateMethodsPreCompaction (actually in relocateIfCallOrMethodReference:mcpc:delta:)
restore the usage counts of used PICs. Finally in compactCompiledCode, clear the blockEntryOffset
of the unused PICs; they will then have a zero count and be reclaimed in the next code compaction."
<var: #cogMethod type: #'CogMethod *'>
<option: #SistaVM>
cogMethod cmType = CMPolymorphicIC ifTrue:
[cogMethod
picUsage: cogMethod cmUsageCount;
cmUsageCount: 0]
]
{ #category : #initialization }
CogMethodZone >> setInterpreter: aCoInterpreter objectRepresentation: anObjectRepresentation cogit: aCogit [
<doNotGenerate>
coInterpreter := aCoInterpreter.
objectRepresentation := anObjectRepresentation.
cogit := aCogit.
objectMemory := aCoInterpreter objectMemory
]
{ #category : #compaction }
CogMethodZone >> shouldFreeMethod: cogMethod given: freeableUsage [
"Answer if cogMethod should be freed in the current pass of freeOlderMethodsForCompaction.
For Sista, where we want PICs to last so they can be observed, we need to keep PICs unless
they are definitely unused. So we need to identify unused PICs. So in planCompact, zero the
usage counts of all PICs, saving the actual usage count in blockEntryOffset. Then in
relocateMethodsPreCompaction (actually in relocateIfCallOrMethodReference:mcpc:delta:)
restore the usage counts of used PICs. Finally in compactCompiledCode, clear the blockEntryOffset
of the unused PICs; they will then have a zero count and be reclaimed in the next code compaction."
<var: #cogMethod type: #'CogMethod *'>
<inline: true>
^SistaVM
ifTrue:
[cogMethod cmType = CMMethod
ifTrue: [cogMethod cmUsageCount <= freeableUsage]
ifFalse: [cogMethod cmType ~= CMFree
and: [cogMethod cmUsageCount = 0]]]
ifFalse:
[cogMethod cmType ~= CMFree
and: [cogMethod cmUsageCount <= freeableUsage]]
]
{ #category : #accessing }
CogMethodZone >> voidOpenPICList [
openPICList := nil
]
{ #category : #'jit - api' }
CogMethodZone >> voidYoungReferrersPostTenureAll [
<var: #cogMethod type: #'CogMethod *'>
| pointer cogMethod |
<var: #pointer type: #usqInt>
<var: #cogMethod type: #'CogMethod *'>
self assert: youngReferrers <= limitAddress.
pointer := youngReferrers.
[pointer < limitAddress] whileTrue:
[cogMethod := coInterpreter cCoerceSimple: (objectMemory longAt: pointer) to: #'CogMethod *'.
cogMethod cmType ~= CMFree ifTrue:
[cogMethod cmRefersToYoung: false].
pointer := pointer + objectMemory wordSize].
youngReferrers := limitAddress
]
{ #category : #'debug printing' }
CogMethodZone >> whereIsMaybeCodeThing: anOop [
<api>
<returnTypeC: 'char *'>
(self oop: anOop isGreaterThanOrEqualTo: cogit cogCodeBase andLessThan: limitAddress) ifTrue:
[(self oop: anOop isLessThan: cogit minCogMethodAddress) ifTrue:
[^' is in generated runtime'].
(self oop: anOop isLessThan: mzFreeStart) ifTrue:
[^' is in generated methods'].
(self oop: anOop isLessThan: youngReferrers) ifTrue:
[^' is in code zone'].
^' is in young referrers'].
^nil
]
{ #category : #'young referers' }
CogMethodZone >> youngReferrers [
<cmacro: '() youngReferrers'>
^youngReferrers
]
{ #category : #accessing }
CogMethodZone >> zoneAlignment [
^8
]
{ #category : #accessing }
CogMethodZone >> zoneEnd [
^limitAddress
]
{ #category : #'simulation only' }
CogMethodZone >> zoneEnd: zoneEnd [
<doNotGenerate>
limitAddress := zoneEnd
]
{ #category : #accessing }
CogMethodZone >> zoneFree [
^mzFreeStart
]