/
VM_Allocator.java
3237 lines (2569 loc) · 105 KB
/
VM_Allocator.java
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
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* (C) Copyright IBM Corp. 2001
*/
//$Id$
/**
* See also: allocator/copyingGC/VM_Allocator.java
* Note: both copying and noncopying versions of VM_Allocator
* provide identical "interfaces":
* init()
* boot()
* allocateScalar()
* allocateArray()
* allocateScalarClone()
* allocateArrayClone()
* Selection of copying vs. noncopying allocators is a choice
* made at boot time by specifying appropriate directory in CLASSPATH.
*
* @author David Bacon
*/
public class VM_Allocator
extends VM_RCGC
implements VM_Constants, VM_GCConstants, VM_Uninterruptible,
VM_Callbacks.ExitMonitor
{
static final int TYPE = 12; // needed for finalization or something?
// OVERALL COLLECTOR CONTROL
static boolean gc_collect_now = false; // flag to do a collection (new logic)
static boolean gcInProgress = false; // is currently a GC happening?
static boolean needToFreeBlocks = false; // out of blocks and need to free some?
// MEMORY LAYOUT
static int bootStartAddress; // boot image
static int bootEndAddress;
static int smallHeapStartAddress; // small object heap
static int smallHeapEndAddress;
static int largeHeapStartAddress; // large object heap
static int largeHeapEndAddress;
static VM_BootRecord bootrecord; // copy of boot record
// SMALL OBJECT ALLOCATION
static VM_ProcessorLock sysLockSmall; // Small object page lock
static VM_BlockControl[] init_blocks; // VM_BlockControl's per GC-SIZES for boot, before heap setup
static int[] blocks; // VM_BlockControl's, stored as ints, 1 per heap block
static int smallHeapSize; // small object heap size, in bytes
static int num_blocks; // number of blocks in the heap
static int highest_block; // number of highest available block
static int blocks_available; // number of free blocks for small obj's
static int first_freeblock; // number of first available block
static int blocksCountDown; // when counter reaches 0, trigger GC
static final int OUT_OF_BLOCKS = -1; // End-of-list indicator for freeblock list
// LARGE OBJECT ALLOCATION
static final int LARGE_BLOCK_SIZE = 4096; // Large objects are made up of 4K blocks
static final int LOG_GC_LARGE_BLOCKSIZE = 12;
static VM_ProcessorLock sysLockLarge; // Large object page lock
static int large_last_allocated;
static int largeSpacePages;
static int largeHeapSize;
static short[] largeSpaceAlloc; // used to allocate
static short[] largeSpaceMark; // used to mark (debug only -- see GC_MARK_REACHABLE_OBJECTS)
// Constants
static final int OBJECT_ADDR_POSITION = SCALAR_HEADER_SIZE + OBJECT_HEADER_OFFSET;
// Features
static final boolean GC_CONTINUOUSLY = false; // On MP, start collection on CPU 1 as soon as finished on CPU n
static final boolean GC_FILTER_BADREFS = false;
static final boolean GC_FILTER_MALLOC_REFS = false;
static final boolean AGGRESSIVE_FREEING = true;
static final boolean GC_ON_EXIT = false; // this doesn't seem to work anymore
static final boolean COMPILE_FOR_TIMING_RUN = true; // touch heap in boot
// Statistics
static final boolean GC_COUNT_ALLOC = true;
static int allocCount; // updated every entry to allocate<x>
static int fastAllocCount; // updated every entry to allocate<x>
static int allocBytes; // total bytes allocated
static int freedCount; // number of objects freed
static final boolean RC_COUNT_EVENTS = true;
static int green; // green allocated since last cycle collect (see VM_RootBuffer)
static int black; // black allocated since last cycle collect (see VM_RootBuffer)
static int nonZeroDecs; // decrements that didn't cause count to become zero (this epoch)
static int internalDecs; // internal (implicit) decrements (this epoch)
static int totalNonZeroDecs; // decrements that didn't cause count to become zero (total)
static int totalInternalDecs; // internal (implicit) decrements (total)
static int mutationIncCount; // total increments from mutation buffers
static int mutationDecCount; // total decrements from mutation buffers
static int stackRefCount; // total increment/decrements from stack buffers
static final boolean TRACK_MEMORY_USAGE = false;
static int bytesInUseTotal;
static int bytesInUseMax;
static int gcCount; // updated every entry to gc_collect
static int[] allocated_since_lastgc; // used to count allocations between gc
static int[] countLive; // for stats - count # live objects in bin
static int[] countLargeAlloc; // - count sizes of large objects alloc'ed
static int[] countLargeLive; // - count sizes of large objects live
static int[] countSmallFree; // bytes allocated by size
static int[] countSmallBlocksAlloc; // blocks allocated by size
static int largerefs_count; // counter of large objects marked
// Timing
static final int TicksPerMicrosecond = 41*4; // Number of system ticks per microsecond (changes by machine!)
static long ticksPerUS; // computed now instead of hard coding
static double bootTime; // time when we booted VM
static final boolean GC_STATISTICS = false; // for timing parallel GC
static final boolean GC_TIMING = true; // for timing parallel GC
static final boolean TIMING_DETAILS = false; // break down components of pause times
static final boolean TIME_ALLOCATES = true; // time each allocateScalar() or allocateArray() operation
static final boolean TIME_FREEBLOCKS = false; // time each freeBlock call
static final boolean PRINT_SLOW_ALLOCATES = false;
static final long TIME_ALLOCATE_QUICK = 3000 * TicksPerMicrosecond; // report allocates slower than this (ticks)
static int allocSlowCount; // allocs above threshold for speed
static int allocLargeSlowCount; // allocs above threshold for speed
static long allocTimeTotal; // in ticks
static long allocTimeMax; // in ticks
static long allocLargeTimeMax; // in ticks
static double gcMinorTime; // for timing gc times
static double gcMajorTime; // for timing gc times
static double gcStartTime; // for timing gc times
static double gcTotalTime; // for timing gc times
// Tracing/Debugging
static final boolean GC_TRIGGERGC = false; // for knowing why GC triggered
static final boolean GC_TRACEALLOCATOR = true; // for tracing RCGC
static final boolean GC_TRACEALLOCATOR_DETAIL = false; // for detailed tracing RCGC
static final boolean GC_MARK_REACHABLE_OBJECTS = false; // check if freeing reachable obj
static final boolean GC_MARK_REACHABLE_OBJECTS_DETAIL = false; // check if freeing reachable obj
static final boolean GC_MARK_REACHABLE_OBJECTS_SOFT = false; // only soft warnings in MP mode?
static final boolean DEBUG_NEXT_SLOT = false; // verify addresses obtained from VM_SizeControl.next_slot
static final boolean DebugLink = false; // debug small object free chains
static final boolean GC_CLOBBERFREE = false;
static final boolean TRACE_LARGE = false; // trace large object alloc/dealloc
static final boolean Report = false;
static int OBJECT_GC_MARK_VALUE = 0; // changes between this and (MARK_VALUE?)
static int refToLookFor = 0; // object for tracing to use as data breakpoint
static int refToWatch = 0; // object for refcount operations to use as data breakpoint
static void
init () {
int i, ii;
if ( ! VM.BuildForConcurrentGC )
VM.sysFail("build concurrent memory manager by setting preprocessor directive RVM_WITH_CONCURRENT_GC=1");
VM_Processor st = VM_Scheduler.processors[VM_Scheduler.PRIMORDIAL_PROCESSOR_ID];
VM_CollectorThread.init(); // to alloc bootimage arrays etc
// create synchronization objects
sysLockLarge = new VM_ProcessorLock();
sysLockSmall = new VM_ProcessorLock();
allocated_since_lastgc = new int[GC_SIZES];
st.sizes = new VM_SizeControl[GC_SIZES];
init_blocks = new VM_BlockControl[GC_SIZES];
// On the jdk side, we allocate an array of VM_SizeControl Blocks,
// one for each size.
// We also allocate init_blocks array within the boot image.
// At runtime we allocate the rest of the BLOCK_CONTROLS, whose number
// depends on the heapsize, and copy the contents of init_blocks
// into the first GC_SIZES of them.
for (i = 0; i < GC_SIZES; i++) {
st.sizes[i] = new VM_SizeControl();
init_blocks[i] = new VM_BlockControl();
st.sizes[i].first_block = i; // 1 block/size initially
st.sizes[i].current_block = i;
st.sizes[i].ndx = i;
// make arrays 1 entry larger: AUTO-CHECK: TAKEOUT!!
init_blocks[i].Alloc1 = new byte[GC_BLOCKSIZE/GC_SIZEVALUES[i] ];
init_blocks[i].Alloc2 = new byte[GC_BLOCKSIZE/GC_SIZEVALUES[i] ];
init_blocks[i].alloc = init_blocks[i].Alloc1;
init_blocks[i].mark = init_blocks[i].Alloc2;
for (ii = 0; ii < GC_BLOCKSIZE/GC_SIZEVALUES[i]; ii++) {
init_blocks[i].alloc[ii] = 0;
init_blocks[i].mark[ii] = 0;
}
init_blocks[i].nextblock= 0;
init_blocks[i].slotsize = GC_SIZEVALUES[i];
}
// set up GC_INDEX_ARRAY for this Processor
st.GC_INDEX_ARRAY = new VM_SizeControl[GC_MAX_SMALL_SIZE + 1];
st.GC_INDEX_ARRAY[0] = st.sizes[0]; // for size = 0
int j = 1;
for (i = 0; i < GC_SIZES; i++)
for (; j <= GC_SIZEVALUES[i]; j++) st.GC_INDEX_ARRAY[j] = st.sizes[i];
countLive = new int[GC_SIZES];
countLargeAlloc = new int[GC_LARGE_SIZES];
countLargeLive = new int[GC_LARGE_SIZES];
countSmallFree = new int[GC_SIZES];
countSmallBlocksAlloc = new int[GC_SIZES];
for (i = 0; i < GC_LARGE_SIZES; i++) {
countLargeAlloc[i] = 0;
countLargeLive[i] = 0;
}
for (i = 0; i < GC_SIZES; i++) {
countLive[i] = 0;
allocated_since_lastgc[i] = 0;
}
largeSpaceAlloc = new short[GC_INITIAL_LARGE_SPACE_PAGES];
for (i = 0; i < GC_INITIAL_LARGE_SPACE_PAGES; i++)
largeSpaceAlloc[i] = 0;
large_last_allocated = 0;
largeSpacePages = GC_INITIAL_LARGE_SPACE_PAGES;
} // all this done in bootimagebuilder context
static void
boot (VM_BootRecord thebootrecord) {
int i;
int blocks_storage, blocks_array_storage;
VM_Processor st = VM_Scheduler.processors[VM_Scheduler.PRIMORDIAL_PROCESSOR_ID];
blocks = VM_Magic.addressAsIntArray(VM_Magic.objectAsAddress(init_blocks));
bootrecord = thebootrecord;
bootStartAddress = bootrecord.startAddress; // start of boot image
bootEndAddress = bootrecord.freeAddress; // end of boot image
smallHeapStartAddress = ((bootEndAddress + GC_BLOCKALIGNMENT - 1)/
GC_BLOCKALIGNMENT)*GC_BLOCKALIGNMENT;
smallHeapEndAddress = (bootrecord.endAddress/GC_BLOCKALIGNMENT)*
GC_BLOCKALIGNMENT;
largeHeapStartAddress = bootrecord.largeStart;
largeHeapEndAddress = bootrecord.largeStart + bootrecord.largeSize;
smallHeapSize = smallHeapEndAddress - smallHeapStartAddress;
largeHeapSize = largeHeapEndAddress - largeHeapStartAddress;
if (COMPILE_FOR_TIMING_RUN) {
for (int addr = smallHeapEndAddress - 4096; addr >= smallHeapStartAddress; addr -= 4096)
VM_Magic.setMemoryWord(addr, 0);
}
// Now set the beginning address of each block into each VM_BlockControl
// Note that init_blocks is in the boot image, but heap pages are controlled by it
for (i =0; i < GC_SIZES; i++) {
init_blocks[i].baseAddr = smallHeapStartAddress + i * GC_BLOCKSIZE;
build_list_for_new_block(init_blocks[i], st.sizes[i]);
}
// Get the three arrays that control large object space
short[] temp = new short[bootrecord.largeSize/LARGE_BLOCK_SIZE + 1];
largeSpaceMark = new short[bootrecord.largeSize/LARGE_BLOCK_SIZE + 1];
for (i = 0; i < GC_INITIAL_LARGE_SPACE_PAGES; i++)
temp[i] = largeSpaceAlloc[i];
for (int iii = 0 ; iii < largeSpacePages;) {
if (largeSpaceAlloc[iii] == 0) {
iii++;
}
else iii = iii + largeSpaceAlloc[iii]; // negative value in largeSpA
}
// At this point temp contains the up-to-now allocation information
// for large objects; so it now becomes largeSpaceAlloc
largeSpaceAlloc = temp;
largeSpacePages = bootrecord.largeSize/LARGE_BLOCK_SIZE;
if (Report) {
VM.sysWrite(bootStartAddress);
VM.sysWrite(" is the bootStartAddress \n");
VM.sysWrite(bootEndAddress);
VM.sysWrite(" is the bootEndAddress \n");
VM.sysWrite(smallHeapStartAddress);
VM.sysWrite(" is the smallHeapStartAddress \n");
VM.sysWrite(smallHeapEndAddress);
VM.sysWrite(" is the smallHeapEndAddress \n");
VM.sysWrite(largeHeapStartAddress);
VM.sysWrite(" is the largeHeapStartAddress \n");
VM.sysWrite(largeHeapEndAddress);
VM.sysWrite(" is the largeHeapEndAddress \n");
VM.sysWrite(smallHeapSize);
VM.sysWrite(" is the smallHeapSize \n");
}
// At this point it is possible to allocate
// (1 GC_BLOCKSIZE worth of )objects foreach size
if (VM_RCGC.acyclicVmClasses)
VM_RootBuffer.boot(); // mark certain special classes acyclic to reduce cycle collection costs
// Now allocate the blocks array - which will be used to allocate blocks to sizes
num_blocks = smallHeapSize/GC_BLOCKSIZE;
blocksCountDown = num_blocks >> 5;
large_last_allocated = 0;
// blocks = new VM_BlockControl[num_blocks];
// GET STORAGE FOR BLOCKS ARRAY FROM OPERATING SYSTEM
if ((blocks_array_storage = VM.sysCall1(bootrecord.sysMallocIP,
// storage for entries in blocks array: 4 bytes/ ref
num_blocks * 4 + ARRAY_HEADER_SIZE)) == 0) {
VM.sysWrite(" In VM_Allocator.boot(), call to sysMalloc returned 0 \n");
VM.sysExit(1800);
}
if ((blocks_storage = VM.sysCall1(bootrecord.sysMallocIP,
(num_blocks-GC_SIZES) * VM_BlockControl.Size)) == 0) {
VM.sysWrite(" In boot, call to sysMalloc returned 0 \n");
VM.sysExit(1900);
}
// Note: the TIB that we get should be of type int[]; if it is of type VM_BlockControl[] then things
// get very confused, since it is declared as int[].
blocks = makeArrayFromStorage(blocks_array_storage,
VM_Magic.getMemoryWord(VM_Magic.objectAsAddress(blocks) +
OBJECT_TIB_OFFSET),
num_blocks);
// index for highest page in heap
highest_block = num_blocks -1;
blocks_available = highest_block - GC_SIZES; // available to allocate
// Now fill in blocks with values from blocks_init
for (i = 0; i < GC_SIZES; i++) {
// NOTE: if blocks are identified by index, st.sizes[] need not be changed; if
// blocks are identified by address, then updates st.sizes[0-GC_SIZES] here
blocks[i] = VM_Magic.objectAsAddress(init_blocks[i]);
// make sure it survives the first collection
VM_Magic.addressAsBlockControl(blocks[i]).sticky = true;
}
// At this point we have assigned the first GC_SIZES blocks,
// 1 per, to each GC_SIZES bin
// and are prepared to allocate from such, or from large object space:
// large objects are allocated from the top of the heap downward;
// small object blocks are allocated from the bottom of the heap upward.
// VM_BlockControl blocks are not used to manage large objects -
// they are unavailable by special logic for allocation of small objs
first_freeblock = GC_SIZES; // next to be allocated
init_blocks = null; // these are currently live through blocks
// Now allocate the rest of the VM_BlockControls
for (i = GC_SIZES; i < num_blocks; i++) {
/// blocks[i] = new VM_BlockControl();
blocks[i] = makeObjectFromStorage(blocks_storage +
(i - GC_SIZES) * VM_BlockControl.Size,
VM_Magic.getMemoryWord(blocks[0]
+ OBJECT_TIB_OFFSET), VM_BlockControl.Size);
VM_Magic.addressAsBlockControl(blocks[i]).baseAddr =
smallHeapStartAddress + i * GC_BLOCKSIZE;
VM_Magic.addressAsBlockControl(blocks[i]).nextblock = i + 1;
}
VM_Magic.addressAsBlockControl(blocks[num_blocks -1]).nextblock = OUT_OF_BLOCKS;
VM_GCUtil.boot();
VM_Callbacks.addExitMonitor(new VM_Allocator());
bootTime = VM_Time.now();
} // boot()
/**
* To be called when the VM is about to exit.
* @param value the exit value
*/
public void notifyExit(int value) {
cleanup();
}
static void
setupProcessor (VM_Processor st)
{
if (GC_TRACEALLOCATOR) {
VM.sysWrite("|||| setupProcessor ");
VM.sysWrite(st.id, false);
VM.sysWrite("\n");
}
// for the PRIMORDIAL PROCESSOR, setupProcessor is called twice,
// once when building the bootimage (VM.runningVM==false) and again
// from VM.boot when the VM is booting (VM.runningVM==true)
// Allocation sturctures (size controls etc) are constructed in init().
// IncDec buffers must be allocated in the second call when booting.
//
if (st.id == VM_Scheduler.PRIMORDIAL_PROCESSOR_ID) {
if (VM.runningVM == false)
return; // nothing to do during bootimage writing
else {
VM_RCBuffers.allocateIncDecBuffer(st);
st.localEpoch = -1;
}
return;
}
VM_RCBuffers.allocateIncDecBuffer(st);
st.localEpoch = -1;
// Get VM_SizeControl array
st.sizes = new VM_SizeControl[GC_SIZES];
for (int i = 0; i < GC_SIZES; i++) {
st.sizes[i] = new VM_SizeControl();
int ii = VM_Allocator.getnewblockx(i);
st.sizes[i].first_block = ii; // 1 block/size initially
st.sizes[i].current_block = ii;
st.sizes[i].ndx = i; // to fit into old code
build_list_for_new_block(VM_Magic.addressAsBlockControl(blocks[ii]), st.sizes[i]);
}
st.GC_INDEX_ARRAY = new VM_SizeControl[GC_MAX_SMALL_SIZE + 1];
st.GC_INDEX_ARRAY[0] = st.sizes[0]; // for size = 0
// set up GC_INDEX_ARRAY for this Processor
int j = 1;
for (int i = 0; i < GC_SIZES; i++)
for (; j <= GC_SIZEVALUES[i]; j++)
st.GC_INDEX_ARRAY[j] = st.sizes[i];
}
public static void cleanup () {
double runTime = -1.0;
runTime = VM_Time.now() - bootTime;
double s = VM_Time.now();
long ts = VM_Time.cycles();
for (double d = VM_Time.now(); d-s < 1.0; d = VM_Time.now()) {}
long ticksPerSecond = VM_Time.cycles() - ts;
ticksPerUS = ticksPerSecond/1000000;
println("Ticks/us: ", (int) ticksPerUS);
if (GC_ON_EXIT) {
println("HEAP BEFORE CLEANUP"); heapInfo();
int e = VM_Scheduler.globalEpoch;
while (VM_Scheduler.globalEpoch < e+4)
collectGarbageOrAwaitCompletion("cleanup");
println("HEAP AFTER CLEANUP"); heapInfo();
}
else {
println("HEAP STATUS"); heapInfo();
}
// println("\nForcing final root buffer processing");
// VM_RootBuffer.buffer.processCycles();
print("\n\nRCGC SUMMARY\n\n");
println("Epochs: ", VM_Scheduler.globalEpoch);
println();
if (GC_COUNT_ALLOC) {
println("Objects allocated: ", allocCount);
print("Fast allocations: ", fastAllocCount); percentage(fastAllocCount, allocCount, "allocations");
print("Objects freed: ", freedCount); percentage(freedCount, allocCount, "allocations");
println("Bytes allocated: ", allocBytes);
}
if (TRACK_MEMORY_USAGE) {
println("Memory high water mark: ", bytesInUseMax);
println("Avg memory utilization: ", bytesInUseTotal/gcCount);
}
if (RC_COUNT_EVENTS) {
totalNonZeroDecs += nonZeroDecs;
totalInternalDecs += internalDecs;
VM_RootBuffer.printStatistics(allocCount, allocBytes, totalNonZeroDecs);
int totalInc = mutationIncCount + stackRefCount;
int totalDec = mutationDecCount + stackRefCount + totalInternalDecs;
print("Total increments: ", totalInc); percentage(totalInc, allocCount, "allocations(*)");
print("Total decrements: ", totalDec); percentage(totalDec, allocCount, "allocations(*)");
print("Mutator increments: ", mutationIncCount); percentage(mutationIncCount, totalInc, "increments");
print("Mutator decrements: ", mutationDecCount); percentage(mutationDecCount, totalDec, "decrements");
println("Stack inc/dec's: ", stackRefCount);
print("Internal Decrements: ", totalInternalDecs); percentage(totalInternalDecs,totalDec, "decrements");
print("Non-0 Decrements: ", totalNonZeroDecs); percentage(totalNonZeroDecs, totalDec, "decrements");
print("Max Mutation Buffers: ", VM_RCBuffers.buffersUsed);
print(" - ", (VM_RCBuffers.buffersUsed * VM_RCBuffers.INCDEC_BUFFER_SIZE)/1024); println(" KB");
if (VM_RootBuffer.ASYNC)
VM_CycleBuffer.printStatistics();
VM_RCGC.printStatistics();
}
if (TIME_ALLOCATES) {
print("Max Alloc Time: ", (int) (allocTimeMax/ticksPerUS)); println(" usecs");
if (GC_COUNT_ALLOC) {
long avgAlloc = (allocTimeTotal/((long) allocCount))/ticksPerUS;
print("Avg Alloc Time: ", (int) avgAlloc); println(" usecs");
}
else
println("Avg Alloc Time unavailable. Turn on GC_COUNT_ALLOC");
print("Allocs slower than ", ((int) (TIME_ALLOCATE_QUICK/TicksPerMicrosecond)));
println(" usec: ", allocSlowCount);
print("Max Large Alloc Time: ", (int) (allocLargeTimeMax/ticksPerUS)); println(" usecs");
print("Large allocs slower than ", ((int) (TIME_ALLOCATE_QUICK/TicksPerMicrosecond)));
println(" usec: ", allocLargeSlowCount);
}
dumpHashStats();
if (VM_RCCollectorThread.TIME_PAUSES)
VM_RCCollectorThread.printRCStatistics(freedCount);
if (RC_COUNT_EVENTS)
println("\n * Comparative only; not a true percentage");
println("\nRUN TIME: ", (int) runTime);
}
/////////////////////////////////////////////////////////////////////////////
// REFERENCE COUNTING
/////////////////////////////////////////////////////////////////////////////
// Create refcounted scalar
private static Object refcountifyScalar(Object object, Object[] tib, int size, VM_SizeControl the_size) {
int objaddr = VM_Magic.objectAsAddress(object); // get address
int rawaddr = objaddr - size + OBJECT_ADDR_POSITION; // compute base address of slot
refcountify(objaddr, rawaddr, tib, the_size); // do refcounting stuff
return object;
}
// Create refcounted array
private static Object refcountifyArray(Object object, Object[] tib, VM_SizeControl the_size) {
int objaddr = VM_Magic.objectAsAddress(object); // get address
int rawaddr = objaddr + OBJECT_HEADER_OFFSET; // compute base address of slot
refcountify(objaddr, rawaddr, tib, the_size); // do refcounting stuff
return object;
}
// Encapsulate creation of refcounted object
private static void refcountify(int objaddr, int rawaddr, Object[] tib, VM_SizeControl the_size) {
// In case of underlying allocation failure, just return
if (objaddr == 0)
return;
if (VM.VerifyAssertions && objaddr == refToWatch) {
VM.sysWrite("#### Refcountifying watched object; raw address ");
VM.sysWrite(rawaddr);
VM.sysWrite("\n");
}
// If it's a small object, mark it appropriately
if (the_size != null) {
// Update alloc byte to reflect the fact that this slot has been allocated
VM_BlockControl the_block = VM_Magic.addressAsBlockControl(blocks[the_size.current_block]);
int slotndx = (rawaddr - the_block.baseAddr) / the_block.slotsize;
the_block.alloc[slotndx] = 1;
// the_block.allocCount++; // should use atomic fetch and add on MP
VM_Synchronization.fetchAndAdd(the_block, VM_Entrypoints.allocCountOffset,1);
}
// Initialize reference count and enqueue mutation buffer operations
VM_Processor proc = VM_Processor.getCurrentProcessor();
VM_Magic.setMemoryWord(objaddr + OBJECT_REFCOUNT_OFFSET, 1);
VM_RCBuffers.addTibIncAndObjectDec(VM_Magic.objectAsAddress(tib), objaddr, proc);
// Mark acyclic objects green; others are black (0) by default [Note: change green to default?]
if (VM_Magic.objectAsType(tib[TIB_TYPE_INDEX]).acyclic) {
setColor(objaddr, GREEN);
if (RC_COUNT_EVENTS) green++;
}
else
if (RC_COUNT_EVENTS) black++;
}
// Allocate an object.
// Taken: size of object (including header), in bytes
// tib for object
// Returned: zero-filled, word aligned space for an object, with header installed
// (ready for initializer to be run on it)
//
public static Object
allocateScalar (int size, Object[] tib, boolean hasFinalizer)
throws OutOfMemoryError
{
// VM_Magic.pragmaInline(); // make sure this method is inlined
long startTime;
int allocType = 0;
Object result;
boolean mustGC = gc_collect_now;
if (TIME_ALLOCATES) startTime = VM_Time.cycles();
if (GC_COUNT_ALLOC) { allocCount++; allocBytes += size; }
if (mustGC)
gc1();
// assumption: object blocks are always a word multiple,
// so we don't need to worry about address alignment or rounding
VM_Processor st = VM_Processor.getCurrentProcessor();
if (size <= GC_MAX_SMALL_SIZE) {
VM_SizeControl the_size = st.GC_INDEX_ARRAY[size];
if (the_size.next_slot != 0) { // fastest path
int rawaddr = the_size.next_slot;
if (GC_COUNT_ALLOC) fastAllocCount++;
if (DebugLink) checkAllocation(rawaddr, the_size);
the_size.next_slot = VM_Magic.getMemoryWord(rawaddr);
if (DEBUG_NEXT_SLOT) checkNextAllocation(rawaddr, the_size);
VM_Magic.setMemoryWord(rawaddr, 0);
int objaddr = rawaddr + size - OBJECT_ADDR_POSITION;
VM_Magic.setMemoryWord(objaddr + OBJECT_TIB_OFFSET, VM_Magic.objectAsAddress(tib));
result = refcountifyScalar(VM_Magic.addressAsObject(objaddr), tib, size, the_size);
}
else {
result = refcountifyScalar(allocateScalar1(the_size, tib, size, the_size.ndx), tib, size, the_size);
allocType = 1;
}
}
else {
result = refcountifyScalar(allocateScalar1L(tib, size), tib, size, null);
allocType = 2;
}
if (TIME_ALLOCATES) {
long pauseTime = VM_Time.cycles() - startTime;
allocTimeTotal += pauseTime;
VM_Thread t = VM_Thread.getCurrentThread();
final boolean isUser = ! (t.isGCThread || t.isIdleThread);
if (isUser) {
if (pauseTime > allocTimeMax && allocType != 2) allocTimeMax = pauseTime;
if (allocType == 2 && pauseTime > allocLargeTimeMax) allocLargeTimeMax = pauseTime;
if (pauseTime > TIME_ALLOCATE_QUICK) {
if (allocType != 2) allocSlowCount++; else allocLargeSlowCount++;
}
}
if (PRINT_SLOW_ALLOCATES && pauseTime > TIME_ALLOCATE_QUICK) {
print(")))) Slow allocateScalar");
if (allocType == 1)
print("1");
else if (allocType == 2)
print("1L");
if (mustGC)
print("[gc1]");
print(" of ", size);
print(" bytes: ", (int) (pauseTime/TicksPerMicrosecond)); print(" usec");
if (! isUser) print(" [GC ALLOC]");
println();
}
}
return result;
}
static Object
allocateScalar1 (VM_SizeControl the_size, Object[] tib, int size, int ndx)
{
for (int i = 0; i < 20; i++) {
int objaddr = allocatex(the_size, tib, size, the_size.ndx);
if (objaddr != 0) {
return VM_Magic.addressAsObject(objaddr);
}
print("GCing for scalar of size ", GC_SIZEVALUES[ndx]);
print(" (iteration ", i); println(")");
collectGarbageOrAwaitCompletion("allocateScalar1");
// try fast path again
// if (the_size.next_slot != 0) {
// return VM_Magic.addressAsObject(makeScalar(the_size, tib, size));
// }
}
// failure
VM_Scheduler.trace("VM_Allocator::allocateScalar1",
"couldn't collect enough to fill a request (bytes) for ", size);
VM_Scheduler.traceback("VM_Allocator::allocateScalar1");
return null;
}
private static final boolean INSTRUMENT_ALLOC = false;
private static final long timeLimit = 3000 * TicksPerMicrosecond;
// move on to next block, or get a new block, or return 0
static int
allocatex (VM_SizeControl the_size, Object[] tib, int size, int ndx)
{
int blocksFreedCount;
int blocksBuiltCount;
int blocksSkippedCount;
long buildStartTime;
long startTime;
if (INSTRUMENT_ALLOC) startTime = VM_Time.cycles();
boolean reset = recycleBlocksIfGarbageCollected(the_size);
if (INSTRUMENT_ALLOC && VM_Time.cycles() - startTime > timeLimit) println("Slow recycling blocks");
if (the_size.next_slot != 0) {
return makeScalar(the_size, tib, size);
}
VM_Processor st = VM_Processor.getCurrentProcessor();
VM_BlockControl the_block = VM_Magic.addressAsBlockControl(blocks[the_size.current_block]);
if (INSTRUMENT_ALLOC) { blocksFreedCount = 0; blocksBuiltCount = 0; blocksSkippedCount = 0; }
final int slotsPerBlock = the_block.alloc.length;
while (the_block.nextblock != 0) {
int blockIndex = the_block.nextblock;
VM_BlockControl nextBlock = VM_Magic.addressAsBlockControl(blocks[blockIndex]);
// If the block is empty, and there are more blocks on the list, free the block to reduce
// fragmentation and keep storage utilization low.
if (AGGRESSIVE_FREEING && nextBlock.allocCount == 0 && nextBlock.nextblock != 0) {
the_block.nextblock = nextBlock.nextblock;
freeBlock(nextBlock, blockIndex);
if (INSTRUMENT_ALLOC) blocksFreedCount++;
continue;
}
// Try allocating out of this block
the_size.current_block = blockIndex;
the_block = nextBlock;
if (the_block.allocCount == slotsPerBlock) {
if (INSTRUMENT_ALLOC) blocksSkippedCount++;
the_size.next_slot = 0; // needed?
continue;
}
if (INSTRUMENT_ALLOC) { blocksBuiltCount++; buildStartTime = VM_Time.cycles(); }
if ( build_list(the_block, the_size) ) {
if (INSTRUMENT_ALLOC) {
long t = VM_Time.cycles();
if (t - startTime > timeLimit) {
print("Slow searching blocks. Freed ", blocksFreedCount);
print("; built ", blocksBuiltCount); print("; skipped ", blocksSkippedCount);
if (reset) println(" [recycled list]"); else println(" [didn't recycle]");
print("build_list() took ", (int) ((t - buildStartTime)/TicksPerMicrosecond)); println("us");
}
}
return makeScalar(the_size, tib, size);
}
} // while.... ==> need to get another block
if (getnewblock(ndx) == 0) {
the_block = VM_Magic.addressAsBlockControl(blocks[the_size.current_block]);
build_list_for_new_block(the_block, the_size);
if (INSTRUMENT_ALLOC && VM_Time.cycles() - startTime > timeLimit) {
print("Slow allocating new block. Freed ", blocksFreedCount);
print("; built ", blocksBuiltCount); print("; skipped ", blocksSkippedCount);
if (reset) println(" [recycled list]"); else println(" [didn't recycle]");
}
return makeScalar(the_size, tib, size);
}
else
return 0;
}
// make a small scalar from the free object available in next_slot
private static int makeScalar (VM_SizeControl the_size, Object[] tib, int size) {
if (VM.VerifyAssertions) VM.assert(the_size.next_slot != 0);
int objaddr = the_size.next_slot;
if (DebugLink) checkAllocation(objaddr, the_size);
the_size.next_slot = VM_Magic.getMemoryWord(objaddr);
if (DEBUG_NEXT_SLOT) checkNextAllocation(objaddr, the_size);
VM_Magic.setMemoryWord(objaddr, 0);
objaddr += size - OBJECT_ADDR_POSITION;
VM_Magic.setMemoryWord(objaddr + OBJECT_TIB_OFFSET, VM_Magic.objectAsAddress(tib));
return objaddr;
}
// Allocate an array.
// Taken: number of array elements
// size of array object (including header), in bytes
// tib for array object
// Returned: zero-filled array object with .length field set
//
public static Object
allocateArray (int numElements, int size, Object[] tib)
throws OutOfMemoryError
{
// VM_Magic.pragmaInline(); // make sure this method is inlined
Object result;
long startTime;
int allocType = 0;
int objaddr;
boolean mustGC = gc_collect_now;
if (TIME_ALLOCATES) startTime = VM_Time.cycles();
if (mustGC)
gc1();
if (GC_COUNT_ALLOC) { allocCount++; allocBytes += size; }
// note: array blocks need not be a word multiple,
// so we need to round up size to preserve alignment for future allocations
size = (size + 3) & ~3; // round up request to word multiple
if (size <= GC_MAX_SMALL_SIZE) {
VM_Processor st = VM_Processor.getCurrentProcessor();
VM_SizeControl the_size = st.GC_INDEX_ARRAY[size];
if (the_size.next_slot != 0) { // fastest path
if (GC_COUNT_ALLOC) fastAllocCount++;
objaddr = the_size.next_slot;
if (DebugLink) checkAllocation(objaddr, the_size);
the_size.next_slot = VM_Magic.getMemoryWord(objaddr);
if (DEBUG_NEXT_SLOT) checkNextAllocation(objaddr, the_size);
if (((OBJECT_HEADER_OFFSET - OBJECT_TIB_OFFSET) != 0) &&
((OBJECT_HEADER_OFFSET - ARRAY_LENGTH_OFFSET) != 0))
VM_Magic.setMemoryWord(objaddr, 0);
objaddr -= OBJECT_HEADER_OFFSET;
VM_Magic.setMemoryWord(objaddr + OBJECT_TIB_OFFSET, VM_Magic.objectAsAddress(tib));
VM_Magic.setMemoryWord(objaddr + ARRAY_LENGTH_OFFSET, numElements);
result = refcountifyArray(VM_Magic.addressAsObject(objaddr), tib, the_size);
}
else {
result = refcountifyArray(allocateArray1(the_size, tib, numElements, size, the_size.ndx), tib, the_size);
allocType = 1;
}
}
else {
result = refcountifyArray(allocateArray1L(numElements, size, tib), tib, null);
allocType = 2;
}
if (TIME_ALLOCATES) {
long pauseTime = VM_Time.cycles() - startTime;
allocTimeTotal += pauseTime;
VM_Thread t = VM_Thread.getCurrentThread();
final boolean isUser = ! (t.isGCThread || t.isIdleThread);
if (isUser) {
if (pauseTime > allocTimeMax && allocType != 2) allocTimeMax = pauseTime;
if (allocType == 2 && pauseTime > allocLargeTimeMax) allocLargeTimeMax = pauseTime;
if (pauseTime > TIME_ALLOCATE_QUICK) {
if (allocType != 2) allocSlowCount++; else allocLargeSlowCount++;
}
}
if (PRINT_SLOW_ALLOCATES && pauseTime > TIME_ALLOCATE_QUICK) {
print(")))) Slow allocateArray");
if (allocType == 1)
print("1");
else if (allocType == 2)
print("1L");
if (mustGC)
print("[gc1]");
print(" of ", size);
print(" bytes: ", (int) (pauseTime/TicksPerMicrosecond));
println(" usec");
if (! isUser) print(" [GC ALLOC]");
println();
}
}
return result;
}
static Object
allocateArray1 (VM_SizeControl the_size, Object[] tib, int numElements,
int size, int ndx)
{
for (int i = 0; i < 3; i++) {
int objaddr = allocatey(the_size, tib, numElements, size, ndx);
if (objaddr != 0) {
return VM_Magic.addressAsObject(objaddr);
}
VM.sysWrite("GCing for array of size "); VM.sysWrite(GC_SIZEVALUES[ndx], false); VM.sysWrite("\n");
collectGarbageOrAwaitCompletion("allocateArray1");
// try fast path again
// if (the_size.next_slot != 0) {
// return VM_Magic.addressAsObject(makeArray(the_size, tib, numElements));
// }
}
// failure
VM_Scheduler.trace("VM_Allocator::allocateArray1",
"couldn't collect enough to fill a request (bytes) for ", size);
VM_Scheduler.traceback("VM_Allocator::allocateArray1");
return null;
}
// move on to next block, or get a new block, or return 0
static int
allocatey (VM_SizeControl the_size, Object[] tib, int numElements, int size, int ndx) {
recycleBlocksIfGarbageCollected(the_size);
if (the_size.next_slot != 0) {
return makeArray(the_size, tib, numElements);
}
VM_Processor st = VM_Processor.getCurrentProcessor();
VM_BlockControl the_block = VM_Magic.addressAsBlockControl(blocks[the_size.current_block]);
final int slotsPerBlock = the_block.alloc.length;
while (the_block.nextblock != 0) {
int blockIndex = the_block.nextblock;