/
psParallelCompact.cpp
3442 lines (2936 loc) · 134 KB
/
psParallelCompact.cpp
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
/*
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "aot/aotLoader.hpp"
#include "classfile/classLoaderDataGraph.hpp"
#include "classfile/javaClasses.inline.hpp"
#include "classfile/stringTable.hpp"
#include "classfile/symbolTable.hpp"
#include "classfile/systemDictionary.hpp"
#include "code/codeCache.hpp"
#include "gc/parallel/parallelArguments.hpp"
#include "gc/parallel/parallelScavengeHeap.inline.hpp"
#include "gc/parallel/parMarkBitMap.inline.hpp"
#include "gc/parallel/psAdaptiveSizePolicy.hpp"
#include "gc/parallel/psCompactionManager.inline.hpp"
#include "gc/parallel/psOldGen.hpp"
#include "gc/parallel/psParallelCompact.inline.hpp"
#include "gc/parallel/psPromotionManager.inline.hpp"
#include "gc/parallel/psRootType.hpp"
#include "gc/parallel/psScavenge.hpp"
#include "gc/parallel/psYoungGen.hpp"
#include "gc/shared/gcCause.hpp"
#include "gc/shared/gcHeapSummary.hpp"
#include "gc/shared/gcId.hpp"
#include "gc/shared/gcLocker.hpp"
#include "gc/shared/gcTimer.hpp"
#include "gc/shared/gcTrace.hpp"
#include "gc/shared/gcTraceTime.inline.hpp"
#include "gc/shared/isGCActiveMark.hpp"
#include "gc/shared/referencePolicy.hpp"
#include "gc/shared/referenceProcessor.hpp"
#include "gc/shared/referenceProcessorPhaseTimes.hpp"
#include "gc/shared/spaceDecorator.inline.hpp"
#include "gc/shared/taskTerminator.hpp"
#include "gc/shared/weakProcessor.hpp"
#include "gc/shared/workerPolicy.hpp"
#include "gc/shared/workgroup.hpp"
#include "logging/log.hpp"
#include "memory/iterator.inline.hpp"
#include "memory/resourceArea.hpp"
#include "memory/universe.hpp"
#include "oops/access.inline.hpp"
#include "oops/instanceClassLoaderKlass.inline.hpp"
#include "oops/instanceKlass.inline.hpp"
#include "oops/instanceMirrorKlass.inline.hpp"
#include "oops/methodData.hpp"
#include "oops/objArrayKlass.inline.hpp"
#include "oops/oop.inline.hpp"
#include "runtime/atomic.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/safepoint.hpp"
#include "runtime/vmThread.hpp"
#include "services/management.hpp"
#include "services/memTracker.hpp"
#include "services/memoryService.hpp"
#include "utilities/align.hpp"
#include "utilities/debug.hpp"
#include "utilities/events.hpp"
#include "utilities/formatBuffer.hpp"
#include "utilities/macros.hpp"
#include "utilities/stack.inline.hpp"
#if INCLUDE_JVMCI
#include "jvmci/jvmci.hpp"
#endif
#include <math.h>
// All sizes are in HeapWords.
const size_t ParallelCompactData::Log2RegionSize = 16; // 64K words
const size_t ParallelCompactData::RegionSize = (size_t)1 << Log2RegionSize;
const size_t ParallelCompactData::RegionSizeBytes =
RegionSize << LogHeapWordSize;
const size_t ParallelCompactData::RegionSizeOffsetMask = RegionSize - 1;
const size_t ParallelCompactData::RegionAddrOffsetMask = RegionSizeBytes - 1;
const size_t ParallelCompactData::RegionAddrMask = ~RegionAddrOffsetMask;
const size_t ParallelCompactData::Log2BlockSize = 7; // 128 words
const size_t ParallelCompactData::BlockSize = (size_t)1 << Log2BlockSize;
const size_t ParallelCompactData::BlockSizeBytes =
BlockSize << LogHeapWordSize;
const size_t ParallelCompactData::BlockSizeOffsetMask = BlockSize - 1;
const size_t ParallelCompactData::BlockAddrOffsetMask = BlockSizeBytes - 1;
const size_t ParallelCompactData::BlockAddrMask = ~BlockAddrOffsetMask;
const size_t ParallelCompactData::BlocksPerRegion = RegionSize / BlockSize;
const size_t ParallelCompactData::Log2BlocksPerRegion =
Log2RegionSize - Log2BlockSize;
const ParallelCompactData::RegionData::region_sz_t
ParallelCompactData::RegionData::dc_shift = 27;
const ParallelCompactData::RegionData::region_sz_t
ParallelCompactData::RegionData::dc_mask = ~0U << dc_shift;
const ParallelCompactData::RegionData::region_sz_t
ParallelCompactData::RegionData::dc_one = 0x1U << dc_shift;
const ParallelCompactData::RegionData::region_sz_t
ParallelCompactData::RegionData::los_mask = ~dc_mask;
const ParallelCompactData::RegionData::region_sz_t
ParallelCompactData::RegionData::dc_claimed = 0x8U << dc_shift;
const ParallelCompactData::RegionData::region_sz_t
ParallelCompactData::RegionData::dc_completed = 0xcU << dc_shift;
SpaceInfo PSParallelCompact::_space_info[PSParallelCompact::last_space_id];
SpanSubjectToDiscoveryClosure PSParallelCompact::_span_based_discoverer;
ReferenceProcessor* PSParallelCompact::_ref_processor = NULL;
double PSParallelCompact::_dwl_mean;
double PSParallelCompact::_dwl_std_dev;
double PSParallelCompact::_dwl_first_term;
double PSParallelCompact::_dwl_adjustment;
#ifdef ASSERT
bool PSParallelCompact::_dwl_initialized = false;
#endif // #ifdef ASSERT
void SplitInfo::record(size_t src_region_idx, size_t partial_obj_size,
HeapWord* destination)
{
assert(src_region_idx != 0, "invalid src_region_idx");
assert(partial_obj_size != 0, "invalid partial_obj_size argument");
assert(destination != NULL, "invalid destination argument");
_src_region_idx = src_region_idx;
_partial_obj_size = partial_obj_size;
_destination = destination;
// These fields may not be updated below, so make sure they're clear.
assert(_dest_region_addr == NULL, "should have been cleared");
assert(_first_src_addr == NULL, "should have been cleared");
// Determine the number of destination regions for the partial object.
HeapWord* const last_word = destination + partial_obj_size - 1;
const ParallelCompactData& sd = PSParallelCompact::summary_data();
HeapWord* const beg_region_addr = sd.region_align_down(destination);
HeapWord* const end_region_addr = sd.region_align_down(last_word);
if (beg_region_addr == end_region_addr) {
// One destination region.
_destination_count = 1;
if (end_region_addr == destination) {
// The destination falls on a region boundary, thus the first word of the
// partial object will be the first word copied to the destination region.
_dest_region_addr = end_region_addr;
_first_src_addr = sd.region_to_addr(src_region_idx);
}
} else {
// Two destination regions. When copied, the partial object will cross a
// destination region boundary, so a word somewhere within the partial
// object will be the first word copied to the second destination region.
_destination_count = 2;
_dest_region_addr = end_region_addr;
const size_t ofs = pointer_delta(end_region_addr, destination);
assert(ofs < _partial_obj_size, "sanity");
_first_src_addr = sd.region_to_addr(src_region_idx) + ofs;
}
}
void SplitInfo::clear()
{
_src_region_idx = 0;
_partial_obj_size = 0;
_destination = NULL;
_destination_count = 0;
_dest_region_addr = NULL;
_first_src_addr = NULL;
assert(!is_valid(), "sanity");
}
#ifdef ASSERT
void SplitInfo::verify_clear()
{
assert(_src_region_idx == 0, "not clear");
assert(_partial_obj_size == 0, "not clear");
assert(_destination == NULL, "not clear");
assert(_destination_count == 0, "not clear");
assert(_dest_region_addr == NULL, "not clear");
assert(_first_src_addr == NULL, "not clear");
}
#endif // #ifdef ASSERT
void PSParallelCompact::print_on_error(outputStream* st) {
_mark_bitmap.print_on_error(st);
}
#ifndef PRODUCT
const char* PSParallelCompact::space_names[] = {
"old ", "eden", "from", "to "
};
void PSParallelCompact::print_region_ranges() {
if (!log_develop_is_enabled(Trace, gc, compaction)) {
return;
}
Log(gc, compaction) log;
ResourceMark rm;
LogStream ls(log.trace());
Universe::print_on(&ls);
log.trace("space bottom top end new_top");
log.trace("------ ---------- ---------- ---------- ----------");
for (unsigned int id = 0; id < last_space_id; ++id) {
const MutableSpace* space = _space_info[id].space();
log.trace("%u %s "
SIZE_FORMAT_W(10) " " SIZE_FORMAT_W(10) " "
SIZE_FORMAT_W(10) " " SIZE_FORMAT_W(10) " ",
id, space_names[id],
summary_data().addr_to_region_idx(space->bottom()),
summary_data().addr_to_region_idx(space->top()),
summary_data().addr_to_region_idx(space->end()),
summary_data().addr_to_region_idx(_space_info[id].new_top()));
}
}
void
print_generic_summary_region(size_t i, const ParallelCompactData::RegionData* c)
{
#define REGION_IDX_FORMAT SIZE_FORMAT_W(7)
#define REGION_DATA_FORMAT SIZE_FORMAT_W(5)
ParallelCompactData& sd = PSParallelCompact::summary_data();
size_t dci = c->destination() ? sd.addr_to_region_idx(c->destination()) : 0;
log_develop_trace(gc, compaction)(
REGION_IDX_FORMAT " " PTR_FORMAT " "
REGION_IDX_FORMAT " " PTR_FORMAT " "
REGION_DATA_FORMAT " " REGION_DATA_FORMAT " "
REGION_DATA_FORMAT " " REGION_IDX_FORMAT " %d",
i, p2i(c->data_location()), dci, p2i(c->destination()),
c->partial_obj_size(), c->live_obj_size(),
c->data_size(), c->source_region(), c->destination_count());
#undef REGION_IDX_FORMAT
#undef REGION_DATA_FORMAT
}
void
print_generic_summary_data(ParallelCompactData& summary_data,
HeapWord* const beg_addr,
HeapWord* const end_addr)
{
size_t total_words = 0;
size_t i = summary_data.addr_to_region_idx(beg_addr);
const size_t last = summary_data.addr_to_region_idx(end_addr);
HeapWord* pdest = 0;
while (i < last) {
ParallelCompactData::RegionData* c = summary_data.region(i);
if (c->data_size() != 0 || c->destination() != pdest) {
print_generic_summary_region(i, c);
total_words += c->data_size();
pdest = c->destination();
}
++i;
}
log_develop_trace(gc, compaction)("summary_data_bytes=" SIZE_FORMAT, total_words * HeapWordSize);
}
void
PSParallelCompact::print_generic_summary_data(ParallelCompactData& summary_data,
HeapWord* const beg_addr,
HeapWord* const end_addr) {
::print_generic_summary_data(summary_data,beg_addr, end_addr);
}
void
print_generic_summary_data(ParallelCompactData& summary_data,
SpaceInfo* space_info)
{
if (!log_develop_is_enabled(Trace, gc, compaction)) {
return;
}
for (unsigned int id = 0; id < PSParallelCompact::last_space_id; ++id) {
const MutableSpace* space = space_info[id].space();
print_generic_summary_data(summary_data, space->bottom(),
MAX2(space->top(), space_info[id].new_top()));
}
}
void
print_initial_summary_data(ParallelCompactData& summary_data,
const MutableSpace* space) {
if (space->top() == space->bottom()) {
return;
}
const size_t region_size = ParallelCompactData::RegionSize;
typedef ParallelCompactData::RegionData RegionData;
HeapWord* const top_aligned_up = summary_data.region_align_up(space->top());
const size_t end_region = summary_data.addr_to_region_idx(top_aligned_up);
const RegionData* c = summary_data.region(end_region - 1);
HeapWord* end_addr = c->destination() + c->data_size();
const size_t live_in_space = pointer_delta(end_addr, space->bottom());
// Print (and count) the full regions at the beginning of the space.
size_t full_region_count = 0;
size_t i = summary_data.addr_to_region_idx(space->bottom());
while (i < end_region && summary_data.region(i)->data_size() == region_size) {
ParallelCompactData::RegionData* c = summary_data.region(i);
log_develop_trace(gc, compaction)(
SIZE_FORMAT_W(5) " " PTR_FORMAT " " SIZE_FORMAT_W(5) " " SIZE_FORMAT_W(5) " " SIZE_FORMAT_W(5) " " SIZE_FORMAT_W(5) " %d",
i, p2i(c->destination()),
c->partial_obj_size(), c->live_obj_size(),
c->data_size(), c->source_region(), c->destination_count());
++full_region_count;
++i;
}
size_t live_to_right = live_in_space - full_region_count * region_size;
double max_reclaimed_ratio = 0.0;
size_t max_reclaimed_ratio_region = 0;
size_t max_dead_to_right = 0;
size_t max_live_to_right = 0;
// Print the 'reclaimed ratio' for regions while there is something live in
// the region or to the right of it. The remaining regions are empty (and
// uninteresting), and computing the ratio will result in division by 0.
while (i < end_region && live_to_right > 0) {
c = summary_data.region(i);
HeapWord* const region_addr = summary_data.region_to_addr(i);
const size_t used_to_right = pointer_delta(space->top(), region_addr);
const size_t dead_to_right = used_to_right - live_to_right;
const double reclaimed_ratio = double(dead_to_right) / live_to_right;
if (reclaimed_ratio > max_reclaimed_ratio) {
max_reclaimed_ratio = reclaimed_ratio;
max_reclaimed_ratio_region = i;
max_dead_to_right = dead_to_right;
max_live_to_right = live_to_right;
}
ParallelCompactData::RegionData* c = summary_data.region(i);
log_develop_trace(gc, compaction)(
SIZE_FORMAT_W(5) " " PTR_FORMAT " " SIZE_FORMAT_W(5) " " SIZE_FORMAT_W(5) " " SIZE_FORMAT_W(5) " " SIZE_FORMAT_W(5) " %d"
"%12.10f " SIZE_FORMAT_W(10) " " SIZE_FORMAT_W(10),
i, p2i(c->destination()),
c->partial_obj_size(), c->live_obj_size(),
c->data_size(), c->source_region(), c->destination_count(),
reclaimed_ratio, dead_to_right, live_to_right);
live_to_right -= c->data_size();
++i;
}
// Any remaining regions are empty. Print one more if there is one.
if (i < end_region) {
ParallelCompactData::RegionData* c = summary_data.region(i);
log_develop_trace(gc, compaction)(
SIZE_FORMAT_W(5) " " PTR_FORMAT " " SIZE_FORMAT_W(5) " " SIZE_FORMAT_W(5) " " SIZE_FORMAT_W(5) " " SIZE_FORMAT_W(5) " %d",
i, p2i(c->destination()),
c->partial_obj_size(), c->live_obj_size(),
c->data_size(), c->source_region(), c->destination_count());
}
log_develop_trace(gc, compaction)("max: " SIZE_FORMAT_W(4) " d2r=" SIZE_FORMAT_W(10) " l2r=" SIZE_FORMAT_W(10) " max_ratio=%14.12f",
max_reclaimed_ratio_region, max_dead_to_right, max_live_to_right, max_reclaimed_ratio);
}
void
print_initial_summary_data(ParallelCompactData& summary_data,
SpaceInfo* space_info) {
if (!log_develop_is_enabled(Trace, gc, compaction)) {
return;
}
unsigned int id = PSParallelCompact::old_space_id;
const MutableSpace* space;
do {
space = space_info[id].space();
print_initial_summary_data(summary_data, space);
} while (++id < PSParallelCompact::eden_space_id);
do {
space = space_info[id].space();
print_generic_summary_data(summary_data, space->bottom(), space->top());
} while (++id < PSParallelCompact::last_space_id);
}
#endif // #ifndef PRODUCT
#ifdef ASSERT
size_t add_obj_count;
size_t add_obj_size;
size_t mark_bitmap_count;
size_t mark_bitmap_size;
#endif // #ifdef ASSERT
ParallelCompactData::ParallelCompactData() :
_region_start(NULL),
DEBUG_ONLY(_region_end(NULL) COMMA)
_region_vspace(NULL),
_reserved_byte_size(0),
_region_data(NULL),
_region_count(0),
_block_vspace(NULL),
_block_data(NULL),
_block_count(0) {}
bool ParallelCompactData::initialize(MemRegion covered_region)
{
_region_start = covered_region.start();
const size_t region_size = covered_region.word_size();
DEBUG_ONLY(_region_end = _region_start + region_size;)
assert(region_align_down(_region_start) == _region_start,
"region start not aligned");
assert((region_size & RegionSizeOffsetMask) == 0,
"region size not a multiple of RegionSize");
bool result = initialize_region_data(region_size) && initialize_block_data();
return result;
}
PSVirtualSpace*
ParallelCompactData::create_vspace(size_t count, size_t element_size)
{
const size_t raw_bytes = count * element_size;
const size_t page_sz = os::page_size_for_region_aligned(raw_bytes, 10);
const size_t granularity = os::vm_allocation_granularity();
_reserved_byte_size = align_up(raw_bytes, MAX2(page_sz, granularity));
const size_t rs_align = page_sz == (size_t) os::vm_page_size() ? 0 :
MAX2(page_sz, granularity);
ReservedSpace rs(_reserved_byte_size, rs_align, rs_align > 0);
os::trace_page_sizes("Parallel Compact Data", raw_bytes, raw_bytes, page_sz, rs.base(),
rs.size());
MemTracker::record_virtual_memory_type((address)rs.base(), mtGC);
PSVirtualSpace* vspace = new PSVirtualSpace(rs, page_sz);
if (vspace != 0) {
if (vspace->expand_by(_reserved_byte_size)) {
return vspace;
}
delete vspace;
// Release memory reserved in the space.
rs.release();
}
return 0;
}
bool ParallelCompactData::initialize_region_data(size_t region_size)
{
const size_t count = (region_size + RegionSizeOffsetMask) >> Log2RegionSize;
_region_vspace = create_vspace(count, sizeof(RegionData));
if (_region_vspace != 0) {
_region_data = (RegionData*)_region_vspace->reserved_low_addr();
_region_count = count;
return true;
}
return false;
}
bool ParallelCompactData::initialize_block_data()
{
assert(_region_count != 0, "region data must be initialized first");
const size_t count = _region_count << Log2BlocksPerRegion;
_block_vspace = create_vspace(count, sizeof(BlockData));
if (_block_vspace != 0) {
_block_data = (BlockData*)_block_vspace->reserved_low_addr();
_block_count = count;
return true;
}
return false;
}
void ParallelCompactData::clear()
{
memset(_region_data, 0, _region_vspace->committed_size());
memset(_block_data, 0, _block_vspace->committed_size());
}
void ParallelCompactData::clear_range(size_t beg_region, size_t end_region) {
assert(beg_region <= _region_count, "beg_region out of range");
assert(end_region <= _region_count, "end_region out of range");
assert(RegionSize % BlockSize == 0, "RegionSize not a multiple of BlockSize");
const size_t region_cnt = end_region - beg_region;
memset(_region_data + beg_region, 0, region_cnt * sizeof(RegionData));
const size_t beg_block = beg_region * BlocksPerRegion;
const size_t block_cnt = region_cnt * BlocksPerRegion;
memset(_block_data + beg_block, 0, block_cnt * sizeof(BlockData));
}
HeapWord* ParallelCompactData::partial_obj_end(size_t region_idx) const
{
const RegionData* cur_cp = region(region_idx);
const RegionData* const end_cp = region(region_count() - 1);
HeapWord* result = region_to_addr(region_idx);
if (cur_cp < end_cp) {
do {
result += cur_cp->partial_obj_size();
} while (cur_cp->partial_obj_size() == RegionSize && ++cur_cp < end_cp);
}
return result;
}
void ParallelCompactData::add_obj(HeapWord* addr, size_t len)
{
const size_t obj_ofs = pointer_delta(addr, _region_start);
const size_t beg_region = obj_ofs >> Log2RegionSize;
const size_t end_region = (obj_ofs + len - 1) >> Log2RegionSize;
DEBUG_ONLY(Atomic::inc(&add_obj_count);)
DEBUG_ONLY(Atomic::add(&add_obj_size, len);)
if (beg_region == end_region) {
// All in one region.
_region_data[beg_region].add_live_obj(len);
return;
}
// First region.
const size_t beg_ofs = region_offset(addr);
_region_data[beg_region].add_live_obj(RegionSize - beg_ofs);
Klass* klass = ((oop)addr)->klass();
// Middle regions--completely spanned by this object.
for (size_t region = beg_region + 1; region < end_region; ++region) {
_region_data[region].set_partial_obj_size(RegionSize);
_region_data[region].set_partial_obj_addr(addr);
}
// Last region.
const size_t end_ofs = region_offset(addr + len - 1);
_region_data[end_region].set_partial_obj_size(end_ofs + 1);
_region_data[end_region].set_partial_obj_addr(addr);
}
void
ParallelCompactData::summarize_dense_prefix(HeapWord* beg, HeapWord* end)
{
assert(region_offset(beg) == 0, "not RegionSize aligned");
assert(region_offset(end) == 0, "not RegionSize aligned");
size_t cur_region = addr_to_region_idx(beg);
const size_t end_region = addr_to_region_idx(end);
HeapWord* addr = beg;
while (cur_region < end_region) {
_region_data[cur_region].set_destination(addr);
_region_data[cur_region].set_destination_count(0);
_region_data[cur_region].set_source_region(cur_region);
_region_data[cur_region].set_data_location(addr);
// Update live_obj_size so the region appears completely full.
size_t live_size = RegionSize - _region_data[cur_region].partial_obj_size();
_region_data[cur_region].set_live_obj_size(live_size);
++cur_region;
addr += RegionSize;
}
}
// Find the point at which a space can be split and, if necessary, record the
// split point.
//
// If the current src region (which overflowed the destination space) doesn't
// have a partial object, the split point is at the beginning of the current src
// region (an "easy" split, no extra bookkeeping required).
//
// If the current src region has a partial object, the split point is in the
// region where that partial object starts (call it the split_region). If
// split_region has a partial object, then the split point is just after that
// partial object (a "hard" split where we have to record the split data and
// zero the partial_obj_size field). With a "hard" split, we know that the
// partial_obj ends within split_region because the partial object that caused
// the overflow starts in split_region. If split_region doesn't have a partial
// obj, then the split is at the beginning of split_region (another "easy"
// split).
HeapWord*
ParallelCompactData::summarize_split_space(size_t src_region,
SplitInfo& split_info,
HeapWord* destination,
HeapWord* target_end,
HeapWord** target_next)
{
assert(destination <= target_end, "sanity");
assert(destination + _region_data[src_region].data_size() > target_end,
"region should not fit into target space");
assert(is_region_aligned(target_end), "sanity");
size_t split_region = src_region;
HeapWord* split_destination = destination;
size_t partial_obj_size = _region_data[src_region].partial_obj_size();
if (destination + partial_obj_size > target_end) {
// The split point is just after the partial object (if any) in the
// src_region that contains the start of the object that overflowed the
// destination space.
//
// Find the start of the "overflow" object and set split_region to the
// region containing it.
HeapWord* const overflow_obj = _region_data[src_region].partial_obj_addr();
split_region = addr_to_region_idx(overflow_obj);
// Clear the source_region field of all destination regions whose first word
// came from data after the split point (a non-null source_region field
// implies a region must be filled).
//
// An alternative to the simple loop below: clear during post_compact(),
// which uses memcpy instead of individual stores, and is easy to
// parallelize. (The downside is that it clears the entire RegionData
// object as opposed to just one field.)
//
// post_compact() would have to clear the summary data up to the highest
// address that was written during the summary phase, which would be
//
// max(top, max(new_top, clear_top))
//
// where clear_top is a new field in SpaceInfo. Would have to set clear_top
// to target_end.
const RegionData* const sr = region(split_region);
const size_t beg_idx =
addr_to_region_idx(region_align_up(sr->destination() +
sr->partial_obj_size()));
const size_t end_idx = addr_to_region_idx(target_end);
log_develop_trace(gc, compaction)("split: clearing source_region field in [" SIZE_FORMAT ", " SIZE_FORMAT ")", beg_idx, end_idx);
for (size_t idx = beg_idx; idx < end_idx; ++idx) {
_region_data[idx].set_source_region(0);
}
// Set split_destination and partial_obj_size to reflect the split region.
split_destination = sr->destination();
partial_obj_size = sr->partial_obj_size();
}
// The split is recorded only if a partial object extends onto the region.
if (partial_obj_size != 0) {
_region_data[split_region].set_partial_obj_size(0);
split_info.record(split_region, partial_obj_size, split_destination);
}
// Setup the continuation addresses.
*target_next = split_destination + partial_obj_size;
HeapWord* const source_next = region_to_addr(split_region) + partial_obj_size;
if (log_develop_is_enabled(Trace, gc, compaction)) {
const char * split_type = partial_obj_size == 0 ? "easy" : "hard";
log_develop_trace(gc, compaction)("%s split: src=" PTR_FORMAT " src_c=" SIZE_FORMAT " pos=" SIZE_FORMAT,
split_type, p2i(source_next), split_region, partial_obj_size);
log_develop_trace(gc, compaction)("%s split: dst=" PTR_FORMAT " dst_c=" SIZE_FORMAT " tn=" PTR_FORMAT,
split_type, p2i(split_destination),
addr_to_region_idx(split_destination),
p2i(*target_next));
if (partial_obj_size != 0) {
HeapWord* const po_beg = split_info.destination();
HeapWord* const po_end = po_beg + split_info.partial_obj_size();
log_develop_trace(gc, compaction)("%s split: po_beg=" PTR_FORMAT " " SIZE_FORMAT " po_end=" PTR_FORMAT " " SIZE_FORMAT,
split_type,
p2i(po_beg), addr_to_region_idx(po_beg),
p2i(po_end), addr_to_region_idx(po_end));
}
}
return source_next;
}
bool ParallelCompactData::summarize(SplitInfo& split_info,
HeapWord* source_beg, HeapWord* source_end,
HeapWord** source_next,
HeapWord* target_beg, HeapWord* target_end,
HeapWord** target_next)
{
HeapWord* const source_next_val = source_next == NULL ? NULL : *source_next;
log_develop_trace(gc, compaction)(
"sb=" PTR_FORMAT " se=" PTR_FORMAT " sn=" PTR_FORMAT
"tb=" PTR_FORMAT " te=" PTR_FORMAT " tn=" PTR_FORMAT,
p2i(source_beg), p2i(source_end), p2i(source_next_val),
p2i(target_beg), p2i(target_end), p2i(*target_next));
size_t cur_region = addr_to_region_idx(source_beg);
const size_t end_region = addr_to_region_idx(region_align_up(source_end));
HeapWord *dest_addr = target_beg;
while (cur_region < end_region) {
// The destination must be set even if the region has no data.
_region_data[cur_region].set_destination(dest_addr);
size_t words = _region_data[cur_region].data_size();
if (words > 0) {
// If cur_region does not fit entirely into the target space, find a point
// at which the source space can be 'split' so that part is copied to the
// target space and the rest is copied elsewhere.
if (dest_addr + words > target_end) {
assert(source_next != NULL, "source_next is NULL when splitting");
*source_next = summarize_split_space(cur_region, split_info, dest_addr,
target_end, target_next);
return false;
}
// Compute the destination_count for cur_region, and if necessary, update
// source_region for a destination region. The source_region field is
// updated if cur_region is the first (left-most) region to be copied to a
// destination region.
//
// The destination_count calculation is a bit subtle. A region that has
// data that compacts into itself does not count itself as a destination.
// This maintains the invariant that a zero count means the region is
// available and can be claimed and then filled.
uint destination_count = 0;
if (split_info.is_split(cur_region)) {
// The current region has been split: the partial object will be copied
// to one destination space and the remaining data will be copied to
// another destination space. Adjust the initial destination_count and,
// if necessary, set the source_region field if the partial object will
// cross a destination region boundary.
destination_count = split_info.destination_count();
if (destination_count == 2) {
size_t dest_idx = addr_to_region_idx(split_info.dest_region_addr());
_region_data[dest_idx].set_source_region(cur_region);
}
}
HeapWord* const last_addr = dest_addr + words - 1;
const size_t dest_region_1 = addr_to_region_idx(dest_addr);
const size_t dest_region_2 = addr_to_region_idx(last_addr);
// Initially assume that the destination regions will be the same and
// adjust the value below if necessary. Under this assumption, if
// cur_region == dest_region_2, then cur_region will be compacted
// completely into itself.
destination_count += cur_region == dest_region_2 ? 0 : 1;
if (dest_region_1 != dest_region_2) {
// Destination regions differ; adjust destination_count.
destination_count += 1;
// Data from cur_region will be copied to the start of dest_region_2.
_region_data[dest_region_2].set_source_region(cur_region);
} else if (region_offset(dest_addr) == 0) {
// Data from cur_region will be copied to the start of the destination
// region.
_region_data[dest_region_1].set_source_region(cur_region);
}
_region_data[cur_region].set_destination_count(destination_count);
_region_data[cur_region].set_data_location(region_to_addr(cur_region));
dest_addr += words;
}
++cur_region;
}
*target_next = dest_addr;
return true;
}
HeapWord* ParallelCompactData::calc_new_pointer(HeapWord* addr, ParCompactionManager* cm) {
assert(addr != NULL, "Should detect NULL oop earlier");
assert(ParallelScavengeHeap::heap()->is_in(addr), "not in heap");
assert(PSParallelCompact::mark_bitmap()->is_marked(addr), "not marked");
// Region covering the object.
RegionData* const region_ptr = addr_to_region_ptr(addr);
HeapWord* result = region_ptr->destination();
// If the entire Region is live, the new location is region->destination + the
// offset of the object within in the Region.
// Run some performance tests to determine if this special case pays off. It
// is worth it for pointers into the dense prefix. If the optimization to
// avoid pointer updates in regions that only point to the dense prefix is
// ever implemented, this should be revisited.
if (region_ptr->data_size() == RegionSize) {
result += region_offset(addr);
return result;
}
// Otherwise, the new location is region->destination + block offset + the
// number of live words in the Block that are (a) to the left of addr and (b)
// due to objects that start in the Block.
// Fill in the block table if necessary. This is unsynchronized, so multiple
// threads may fill the block table for a region (harmless, since it is
// idempotent).
if (!region_ptr->blocks_filled()) {
PSParallelCompact::fill_blocks(addr_to_region_idx(addr));
region_ptr->set_blocks_filled();
}
HeapWord* const search_start = block_align_down(addr);
const size_t block_offset = addr_to_block_ptr(addr)->offset();
const ParMarkBitMap* bitmap = PSParallelCompact::mark_bitmap();
const size_t live = bitmap->live_words_in_range(cm, search_start, oop(addr));
result += block_offset + live;
DEBUG_ONLY(PSParallelCompact::check_new_location(addr, result));
return result;
}
#ifdef ASSERT
void ParallelCompactData::verify_clear(const PSVirtualSpace* vspace)
{
const size_t* const beg = (const size_t*)vspace->committed_low_addr();
const size_t* const end = (const size_t*)vspace->committed_high_addr();
for (const size_t* p = beg; p < end; ++p) {
assert(*p == 0, "not zero");
}
}
void ParallelCompactData::verify_clear()
{
verify_clear(_region_vspace);
verify_clear(_block_vspace);
}
#endif // #ifdef ASSERT
STWGCTimer PSParallelCompact::_gc_timer;
ParallelOldTracer PSParallelCompact::_gc_tracer;
elapsedTimer PSParallelCompact::_accumulated_time;
unsigned int PSParallelCompact::_total_invocations = 0;
unsigned int PSParallelCompact::_maximum_compaction_gc_num = 0;
jlong PSParallelCompact::_time_of_last_gc = 0;
CollectorCounters* PSParallelCompact::_counters = NULL;
ParMarkBitMap PSParallelCompact::_mark_bitmap;
ParallelCompactData PSParallelCompact::_summary_data;
PSParallelCompact::IsAliveClosure PSParallelCompact::_is_alive_closure;
bool PSParallelCompact::IsAliveClosure::do_object_b(oop p) { return mark_bitmap()->is_marked(p); }
class PCReferenceProcessor: public ReferenceProcessor {
public:
PCReferenceProcessor(
BoolObjectClosure* is_subject_to_discovery,
BoolObjectClosure* is_alive_non_header) :
ReferenceProcessor(is_subject_to_discovery,
ParallelRefProcEnabled && (ParallelGCThreads > 1), // mt processing
ParallelGCThreads, // mt processing degree
true, // mt discovery
ParallelGCThreads, // mt discovery degree
true, // atomic_discovery
is_alive_non_header) {
}
template<typename T> bool discover(oop obj, ReferenceType type) {
T* referent_addr = (T*) java_lang_ref_Reference::referent_addr_raw(obj);
T heap_oop = RawAccess<>::oop_load(referent_addr);
oop referent = CompressedOops::decode_not_null(heap_oop);
return PSParallelCompact::mark_bitmap()->is_unmarked(referent)
&& ReferenceProcessor::discover_reference(obj, type);
}
virtual bool discover_reference(oop obj, ReferenceType type) {
if (UseCompressedOops) {
return discover<narrowOop>(obj, type);
} else {
return discover<oop>(obj, type);
}
}
};
void PSParallelCompact::post_initialize() {
ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
_span_based_discoverer.set_span(heap->reserved_region());
_ref_processor =
new PCReferenceProcessor(&_span_based_discoverer,
&_is_alive_closure); // non-header is alive closure
_counters = new CollectorCounters("Parallel full collection pauses", 1);
// Initialize static fields in ParCompactionManager.
ParCompactionManager::initialize(mark_bitmap());
}
bool PSParallelCompact::initialize() {
ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
MemRegion mr = heap->reserved_region();
// Was the old gen get allocated successfully?
if (!heap->old_gen()->is_allocated()) {
return false;
}
initialize_space_info();
initialize_dead_wood_limiter();
if (!_mark_bitmap.initialize(mr)) {
vm_shutdown_during_initialization(
err_msg("Unable to allocate " SIZE_FORMAT "KB bitmaps for parallel "
"garbage collection for the requested " SIZE_FORMAT "KB heap.",
_mark_bitmap.reserved_byte_size()/K, mr.byte_size()/K));
return false;
}
if (!_summary_data.initialize(mr)) {
vm_shutdown_during_initialization(
err_msg("Unable to allocate " SIZE_FORMAT "KB card tables for parallel "
"garbage collection for the requested " SIZE_FORMAT "KB heap.",
_summary_data.reserved_byte_size()/K, mr.byte_size()/K));
return false;
}
return true;
}
void PSParallelCompact::initialize_space_info()
{
memset(&_space_info, 0, sizeof(_space_info));
ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
PSYoungGen* young_gen = heap->young_gen();
_space_info[old_space_id].set_space(heap->old_gen()->object_space());
_space_info[eden_space_id].set_space(young_gen->eden_space());
_space_info[from_space_id].set_space(young_gen->from_space());
_space_info[to_space_id].set_space(young_gen->to_space());
_space_info[old_space_id].set_start_array(heap->old_gen()->start_array());
}
void PSParallelCompact::initialize_dead_wood_limiter()
{
const size_t max = 100;
_dwl_mean = double(MIN2(ParallelOldDeadWoodLimiterMean, max)) / 100.0;
_dwl_std_dev = double(MIN2(ParallelOldDeadWoodLimiterStdDev, max)) / 100.0;
_dwl_first_term = 1.0 / (sqrt(2.0 * M_PI) * _dwl_std_dev);
DEBUG_ONLY(_dwl_initialized = true;)
_dwl_adjustment = normal_distribution(1.0);
}
void
PSParallelCompact::clear_data_covering_space(SpaceId id)
{
// At this point, top is the value before GC, new_top() is the value that will
// be set at the end of GC. The marking bitmap is cleared to top; nothing
// should be marked above top. The summary data is cleared to the larger of
// top & new_top.
MutableSpace* const space = _space_info[id].space();
HeapWord* const bot = space->bottom();
HeapWord* const top = space->top();
HeapWord* const max_top = MAX2(top, _space_info[id].new_top());
const idx_t beg_bit = _mark_bitmap.addr_to_bit(bot);
const idx_t end_bit = _mark_bitmap.align_range_end(_mark_bitmap.addr_to_bit(top));
_mark_bitmap.clear_range(beg_bit, end_bit);
const size_t beg_region = _summary_data.addr_to_region_idx(bot);
const size_t end_region =
_summary_data.addr_to_region_idx(_summary_data.region_align_up(max_top));
_summary_data.clear_range(beg_region, end_region);
// Clear the data used to 'split' regions.
SplitInfo& split_info = _space_info[id].split_info();
if (split_info.is_valid()) {
split_info.clear();
}
DEBUG_ONLY(split_info.verify_clear();)
}
void PSParallelCompact::pre_compact()
{
// Update the from & to space pointers in space_info, since they are swapped
// at each young gen gc. Do the update unconditionally (even though a
// promotion failure does not swap spaces) because an unknown number of young
// collections will have swapped the spaces an unknown number of times.
GCTraceTime(Debug, gc, phases) tm("Pre Compact", &_gc_timer);
ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
_space_info[from_space_id].set_space(heap->young_gen()->from_space());
_space_info[to_space_id].set_space(heap->young_gen()->to_space());
DEBUG_ONLY(add_obj_count = add_obj_size = 0;)
DEBUG_ONLY(mark_bitmap_count = mark_bitmap_size = 0;)
// Increment the invocation count
heap->increment_total_collections(true);
// We need to track unique mark sweep invocations as well.
_total_invocations++;