/
heapShared.cpp
1769 lines (1548 loc) · 67.3 KB
/
heapShared.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) 2018, 2021, 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 "cds/archiveBuilder.hpp"
#include "cds/archiveUtils.hpp"
#include "cds/filemap.hpp"
#include "cds/heapShared.inline.hpp"
#include "cds/metaspaceShared.hpp"
#include "classfile/classLoaderData.hpp"
#include "classfile/classLoaderDataShared.hpp"
#include "classfile/javaClasses.inline.hpp"
#include "classfile/moduleEntry.hpp"
#include "classfile/stringTable.hpp"
#include "classfile/symbolTable.hpp"
#include "classfile/systemDictionary.hpp"
#include "classfile/systemDictionaryShared.hpp"
#include "classfile/vmClasses.hpp"
#include "classfile/vmSymbols.hpp"
#include "gc/shared/collectedHeap.hpp"
#include "gc/shared/gcLocker.hpp"
#include "gc/shared/gcVMOperations.hpp"
#include "logging/log.hpp"
#include "logging/logMessage.hpp"
#include "logging/logStream.hpp"
#include "memory/iterator.inline.hpp"
#include "memory/metadataFactory.hpp"
#include "memory/metaspaceClosure.hpp"
#include "memory/resourceArea.hpp"
#include "memory/universe.hpp"
#include "oops/compressedOops.inline.hpp"
#include "oops/fieldStreams.inline.hpp"
#include "oops/objArrayOop.hpp"
#include "oops/oop.inline.hpp"
#include "prims/jvmtiExport.hpp"
#include "runtime/fieldDescriptor.inline.hpp"
#include "runtime/globals_extension.hpp"
#include "runtime/init.hpp"
#include "runtime/java.hpp"
#include "runtime/javaCalls.hpp"
#include "runtime/safepointVerifiers.hpp"
#include "utilities/bitMap.inline.hpp"
#include "utilities/copy.hpp"
#if INCLUDE_G1GC
#include "gc/g1/g1CollectedHeap.hpp"
#endif
#if INCLUDE_CDS_JAVA_HEAP
bool HeapShared::_closed_regions_mapped = false;
bool HeapShared::_open_regions_mapped = false;
bool HeapShared::_is_loaded = false;
bool HeapShared::_disable_writing = false;
address HeapShared::_narrow_oop_base;
int HeapShared::_narrow_oop_shift;
DumpedInternedStrings *HeapShared::_dumped_interned_strings = NULL;
uintptr_t HeapShared::_loaded_heap_bottom = 0;
uintptr_t HeapShared::_loaded_heap_top = 0;
uintptr_t HeapShared::_dumptime_base_0 = UINTPTR_MAX;
uintptr_t HeapShared::_dumptime_base_1 = UINTPTR_MAX;
uintptr_t HeapShared::_dumptime_base_2 = UINTPTR_MAX;
uintptr_t HeapShared::_dumptime_base_3 = UINTPTR_MAX;
uintptr_t HeapShared::_dumptime_top = 0;
intx HeapShared::_runtime_offset_0 = 0;
intx HeapShared::_runtime_offset_1 = 0;
intx HeapShared::_runtime_offset_2 = 0;
intx HeapShared::_runtime_offset_3 = 0;
bool HeapShared::_loading_failed = false;
//
// If you add new entries to the following tables, you should know what you're doing!
//
// Entry fields for shareable subgraphs archived in the closed archive heap
// region. Warning: Objects in the subgraphs should not have reference fields
// assigned at runtime.
static ArchivableStaticFieldInfo closed_archive_subgraph_entry_fields[] = {
{"java/lang/Integer$IntegerCache", "archivedCache"},
{"java/lang/Long$LongCache", "archivedCache"},
{"java/lang/Byte$ByteCache", "archivedCache"},
{"java/lang/Short$ShortCache", "archivedCache"},
{"java/lang/Character$CharacterCache", "archivedCache"},
{"java/util/jar/Attributes$Name", "KNOWN_NAMES"},
{"sun/util/locale/BaseLocale", "constantBaseLocales"},
};
// Entry fields for subgraphs archived in the open archive heap region.
static ArchivableStaticFieldInfo open_archive_subgraph_entry_fields[] = {
{"jdk/internal/module/ArchivedModuleGraph", "archivedModuleGraph"},
{"java/util/ImmutableCollections", "archivedObjects"},
{"java/lang/ModuleLayer", "EMPTY_LAYER"},
{"java/lang/module/Configuration", "EMPTY_CONFIGURATION"},
{"jdk/internal/math/FDBigInteger", "archivedCaches"},
};
// Entry fields for subgraphs archived in the open archive heap region (full module graph).
static ArchivableStaticFieldInfo fmg_open_archive_subgraph_entry_fields[] = {
{"jdk/internal/loader/ArchivedClassLoaders", "archivedClassLoaders"},
{"jdk/internal/module/ArchivedBootLayer", "archivedBootLayer"},
{"java/lang/Module$ArchivedData", "archivedData"},
};
const static int num_closed_archive_subgraph_entry_fields =
sizeof(closed_archive_subgraph_entry_fields) / sizeof(ArchivableStaticFieldInfo);
const static int num_open_archive_subgraph_entry_fields =
sizeof(open_archive_subgraph_entry_fields) / sizeof(ArchivableStaticFieldInfo);
const static int num_fmg_open_archive_subgraph_entry_fields =
sizeof(fmg_open_archive_subgraph_entry_fields) / sizeof(ArchivableStaticFieldInfo);
GrowableArrayCHeap<oop, mtClassShared>* HeapShared::_pending_roots = NULL;
narrowOop HeapShared::_roots_narrow;
OopHandle HeapShared::_roots;
#ifdef ASSERT
bool HeapShared::is_archived_object_during_dumptime(oop p) {
assert(HeapShared::can_write(), "must be");
assert(DumpSharedSpaces, "this function is only used with -Xshare:dump");
return Universe::heap()->is_archived_object(p);
}
#endif
////////////////////////////////////////////////////////////////
//
// Java heap object archiving support
//
////////////////////////////////////////////////////////////////
void HeapShared::fixup_regions() {
FileMapInfo* mapinfo = FileMapInfo::current_info();
if (is_mapped()) {
mapinfo->fixup_mapped_heap_regions();
} else if (_loading_failed) {
fill_failed_loaded_region();
}
if (is_fully_available()) {
_roots = OopHandle(Universe::vm_global(), decode_from_archive(_roots_narrow));
if (!MetaspaceShared::use_full_module_graph()) {
// Need to remove all the archived java.lang.Module objects from HeapShared::roots().
ClassLoaderDataShared::clear_archived_oops();
}
}
SystemDictionaryShared::update_archived_mirror_native_pointers();
}
unsigned HeapShared::oop_hash(oop const& p) {
unsigned hash = (unsigned)p->identity_hash();
return hash;
}
static void reset_states(oop obj, TRAPS) {
Handle h_obj(THREAD, obj);
InstanceKlass* klass = InstanceKlass::cast(obj->klass());
TempNewSymbol method_name = SymbolTable::new_symbol("resetArchivedStates");
Symbol* method_sig = vmSymbols::void_method_signature();
while (klass != NULL) {
Method* method = klass->find_method(method_name, method_sig);
if (method != NULL) {
assert(method->is_private(), "must be");
if (log_is_enabled(Debug, cds)) {
ResourceMark rm(THREAD);
log_debug(cds)(" calling %s", method->name_and_sig_as_C_string());
}
JavaValue result(T_VOID);
JavaCalls::call_special(&result, h_obj, klass,
method_name, method_sig, CHECK);
}
klass = klass->java_super();
}
}
void HeapShared::reset_archived_object_states(TRAPS) {
assert(DumpSharedSpaces, "dump-time only");
log_debug(cds)("Resetting platform loader");
reset_states(SystemDictionary::java_platform_loader(), CHECK);
log_debug(cds)("Resetting system loader");
reset_states(SystemDictionary::java_system_loader(), CHECK);
}
HeapShared::ArchivedObjectCache* HeapShared::_archived_object_cache = NULL;
oop HeapShared::find_archived_heap_object(oop obj) {
assert(DumpSharedSpaces, "dump-time only");
ArchivedObjectCache* cache = archived_object_cache();
oop* p = cache->get(obj);
if (p != NULL) {
return *p;
} else {
return NULL;
}
}
int HeapShared::append_root(oop obj) {
assert(DumpSharedSpaces, "dump-time only");
// No GC should happen since we aren't scanning _pending_roots.
assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
if (_pending_roots == NULL) {
_pending_roots = new GrowableArrayCHeap<oop, mtClassShared>(500);
}
return _pending_roots->append(obj);
}
objArrayOop HeapShared::roots() {
if (DumpSharedSpaces) {
assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
if (!HeapShared::can_write()) {
return NULL;
}
} else {
assert(UseSharedSpaces, "must be");
}
objArrayOop roots = (objArrayOop)_roots.resolve();
assert(roots != NULL, "should have been initialized");
return roots;
}
void HeapShared::set_roots(narrowOop roots) {
assert(UseSharedSpaces, "runtime only");
assert(is_fully_available(), "must be");
_roots_narrow = roots;
}
// Returns an objArray that contains all the roots of the archived objects
oop HeapShared::get_root(int index, bool clear) {
assert(index >= 0, "sanity");
if (DumpSharedSpaces) {
assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
assert(_pending_roots != NULL, "sanity");
return _pending_roots->at(index);
} else {
assert(UseSharedSpaces, "must be");
assert(!_roots.is_empty(), "must have loaded shared heap");
oop result = roots()->obj_at(index);
if (clear) {
clear_root(index);
}
return result;
}
}
void HeapShared::clear_root(int index) {
assert(index >= 0, "sanity");
assert(UseSharedSpaces, "must be");
if (is_fully_available()) {
if (log_is_enabled(Debug, cds, heap)) {
oop old = roots()->obj_at(index);
log_debug(cds, heap)("Clearing root %d: was " PTR_FORMAT, index, p2i(old));
}
roots()->obj_at_put(index, NULL);
}
}
oop HeapShared::archive_object(oop obj) {
assert(DumpSharedSpaces, "dump-time only");
oop ao = find_archived_heap_object(obj);
if (ao != NULL) {
// already archived
return ao;
}
int len = obj->size();
if (G1CollectedHeap::heap()->is_archive_alloc_too_large(len)) {
log_debug(cds, heap)("Cannot archive, object (" PTR_FORMAT ") is too large: " SIZE_FORMAT,
p2i(obj), (size_t)obj->size());
return NULL;
}
oop archived_oop = cast_to_oop(G1CollectedHeap::heap()->archive_mem_allocate(len));
if (archived_oop != NULL) {
Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(obj), cast_from_oop<HeapWord*>(archived_oop), len);
// Reinitialize markword to remove age/marking/locking/etc.
//
// We need to retain the identity_hash, because it may have been used by some hashtables
// in the shared heap. This also has the side effect of pre-initializing the
// identity_hash for all shared objects, so they are less likely to be written
// into during run time, increasing the potential of memory sharing.
int hash_original = obj->identity_hash();
archived_oop->set_mark(markWord::prototype().copy_set_hash(hash_original));
assert(archived_oop->mark().is_unlocked(), "sanity");
DEBUG_ONLY(int hash_archived = archived_oop->identity_hash());
assert(hash_original == hash_archived, "Different hash codes: original %x, archived %x", hash_original, hash_archived);
ArchivedObjectCache* cache = archived_object_cache();
cache->put(obj, archived_oop);
if (log_is_enabled(Debug, cds, heap)) {
ResourceMark rm;
log_debug(cds, heap)("Archived heap object " PTR_FORMAT " ==> " PTR_FORMAT " : %s",
p2i(obj), p2i(archived_oop), obj->klass()->external_name());
}
} else {
log_error(cds, heap)(
"Cannot allocate space for object " PTR_FORMAT " in archived heap region",
p2i(obj));
vm_direct_exit(-1,
err_msg("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
SIZE_FORMAT "M", MaxHeapSize/M));
}
return archived_oop;
}
void HeapShared::archive_klass_objects() {
GrowableArray<Klass*>* klasses = ArchiveBuilder::current()->klasses();
assert(klasses != NULL, "sanity");
for (int i = 0; i < klasses->length(); i++) {
Klass* k = ArchiveBuilder::get_relocated_klass(klasses->at(i));
// archive mirror object
java_lang_Class::archive_mirror(k);
// archive the resolved_referenes array
if (k->is_instance_klass()) {
InstanceKlass* ik = InstanceKlass::cast(k);
ik->constants()->archive_resolved_references();
}
}
}
void HeapShared::run_full_gc_in_vm_thread() {
if (HeapShared::can_write()) {
// Avoid fragmentation while archiving heap objects.
// We do this inside a safepoint, so that no further allocation can happen after GC
// has finished.
if (GCLocker::is_active()) {
// Just checking for safety ...
// This should not happen during -Xshare:dump. If you see this, probably the Java core lib
// has been modified such that JNI code is executed in some clean up threads after
// we have finished class loading.
log_warning(cds)("GC locker is held, unable to start extra compacting GC. This may produce suboptimal results.");
} else {
log_info(cds)("Run GC ...");
Universe::heap()->collect_as_vm_thread(GCCause::_archive_time_gc);
log_info(cds)("Run GC done");
}
}
}
void HeapShared::archive_objects(GrowableArray<MemRegion>* closed_regions,
GrowableArray<MemRegion>* open_regions) {
G1HeapVerifier::verify_ready_for_archiving();
{
NoSafepointVerifier nsv;
// Cache for recording where the archived objects are copied to
create_archived_object_cache();
log_info(cds)("Heap range = [" PTR_FORMAT " - " PTR_FORMAT "]",
p2i(CompressedOops::begin()), p2i(CompressedOops::end()));
log_info(cds)("Dumping objects to closed archive heap region ...");
copy_closed_objects(closed_regions);
log_info(cds)("Dumping objects to open archive heap region ...");
copy_open_objects(open_regions);
destroy_archived_object_cache();
}
G1HeapVerifier::verify_archive_regions();
}
void HeapShared::copy_closed_objects(GrowableArray<MemRegion>* closed_regions) {
assert(HeapShared::can_write(), "must be");
G1CollectedHeap::heap()->begin_archive_alloc_range();
// Archive interned string objects
StringTable::write_to_archive(_dumped_interned_strings);
archive_object_subgraphs(closed_archive_subgraph_entry_fields,
num_closed_archive_subgraph_entry_fields,
true /* is_closed_archive */,
false /* is_full_module_graph */);
G1CollectedHeap::heap()->end_archive_alloc_range(closed_regions,
os::vm_allocation_granularity());
}
void HeapShared::copy_open_objects(GrowableArray<MemRegion>* open_regions) {
assert(HeapShared::can_write(), "must be");
G1CollectedHeap::heap()->begin_archive_alloc_range(true /* open */);
java_lang_Class::archive_basic_type_mirrors();
archive_klass_objects();
archive_object_subgraphs(open_archive_subgraph_entry_fields,
num_open_archive_subgraph_entry_fields,
false /* is_closed_archive */,
false /* is_full_module_graph */);
if (MetaspaceShared::use_full_module_graph()) {
archive_object_subgraphs(fmg_open_archive_subgraph_entry_fields,
num_fmg_open_archive_subgraph_entry_fields,
false /* is_closed_archive */,
true /* is_full_module_graph */);
ClassLoaderDataShared::init_archived_oops();
}
copy_roots();
G1CollectedHeap::heap()->end_archive_alloc_range(open_regions,
os::vm_allocation_granularity());
}
// Copy _pending_archive_roots into an objArray
void HeapShared::copy_roots() {
int length = _pending_roots != NULL ? _pending_roots->length() : 0;
size_t size = objArrayOopDesc::object_size(length);
Klass* k = Universe::objectArrayKlassObj(); // already relocated to point to archived klass
HeapWord* mem = G1CollectedHeap::heap()->archive_mem_allocate(size);
memset(mem, 0, size * BytesPerWord);
{
// This is copied from MemAllocator::finish
oopDesc::set_mark(mem, markWord::prototype());
oopDesc::release_set_klass(mem, k);
}
{
// This is copied from ObjArrayAllocator::initialize
arrayOopDesc::set_length(mem, length);
}
_roots = OopHandle(Universe::vm_global(), cast_to_oop(mem));
for (int i = 0; i < length; i++) {
roots()->obj_at_put(i, _pending_roots->at(i));
}
log_info(cds)("archived obj roots[%d] = " SIZE_FORMAT " words, klass = %p, obj = %p", length, size, k, mem);
}
void HeapShared::init_narrow_oop_decoding(address base, int shift) {
_narrow_oop_base = base;
_narrow_oop_shift = shift;
}
//
// Subgraph archiving support
//
HeapShared::DumpTimeKlassSubGraphInfoTable* HeapShared::_dump_time_subgraph_info_table = NULL;
HeapShared::RunTimeKlassSubGraphInfoTable HeapShared::_run_time_subgraph_info_table;
// Get the subgraph_info for Klass k. A new subgraph_info is created if
// there is no existing one for k. The subgraph_info records the relocated
// Klass* of the original k.
KlassSubGraphInfo* HeapShared::init_subgraph_info(Klass* k, bool is_full_module_graph) {
assert(DumpSharedSpaces, "dump time only");
bool created;
Klass* relocated_k = ArchiveBuilder::get_relocated_klass(k);
KlassSubGraphInfo* info =
_dump_time_subgraph_info_table->put_if_absent(relocated_k, KlassSubGraphInfo(relocated_k, is_full_module_graph),
&created);
assert(created, "must not initialize twice");
return info;
}
KlassSubGraphInfo* HeapShared::get_subgraph_info(Klass* k) {
assert(DumpSharedSpaces, "dump time only");
Klass* relocated_k = ArchiveBuilder::get_relocated_klass(k);
KlassSubGraphInfo* info = _dump_time_subgraph_info_table->get(relocated_k);
assert(info != NULL, "must have been initialized");
return info;
}
// Add an entry field to the current KlassSubGraphInfo.
void KlassSubGraphInfo::add_subgraph_entry_field(
int static_field_offset, oop v, bool is_closed_archive) {
assert(DumpSharedSpaces, "dump time only");
if (_subgraph_entry_fields == NULL) {
_subgraph_entry_fields =
new(ResourceObj::C_HEAP, mtClass) GrowableArray<int>(10, mtClass);
}
_subgraph_entry_fields->append(static_field_offset);
_subgraph_entry_fields->append(HeapShared::append_root(v));
}
// Add the Klass* for an object in the current KlassSubGraphInfo's subgraphs.
// Only objects of boot classes can be included in sub-graph.
void KlassSubGraphInfo::add_subgraph_object_klass(Klass* orig_k) {
assert(DumpSharedSpaces, "dump time only");
Klass* relocated_k = ArchiveBuilder::get_relocated_klass(orig_k);
if (_subgraph_object_klasses == NULL) {
_subgraph_object_klasses =
new(ResourceObj::C_HEAP, mtClass) GrowableArray<Klass*>(50, mtClass);
}
assert(ArchiveBuilder::current()->is_in_buffer_space(relocated_k), "must be a shared class");
if (_k == relocated_k) {
// Don't add the Klass containing the sub-graph to it's own klass
// initialization list.
return;
}
if (relocated_k->is_instance_klass()) {
assert(InstanceKlass::cast(relocated_k)->is_shared_boot_class(),
"must be boot class");
// vmClasses::xxx_klass() are not updated, need to check
// the original Klass*
if (orig_k == vmClasses::String_klass() ||
orig_k == vmClasses::Object_klass()) {
// Initialized early during VM initialization. No need to be added
// to the sub-graph object class list.
return;
}
} else if (relocated_k->is_objArray_klass()) {
Klass* abk = ObjArrayKlass::cast(relocated_k)->bottom_klass();
if (abk->is_instance_klass()) {
assert(InstanceKlass::cast(abk)->is_shared_boot_class(),
"must be boot class");
}
if (relocated_k == Universe::objectArrayKlassObj()) {
// Initialized early during Universe::genesis. No need to be added
// to the list.
return;
}
} else {
assert(relocated_k->is_typeArray_klass(), "must be");
// Primitive type arrays are created early during Universe::genesis.
return;
}
if (log_is_enabled(Debug, cds, heap)) {
if (!_subgraph_object_klasses->contains(relocated_k)) {
ResourceMark rm;
log_debug(cds, heap)("Adding klass %s", orig_k->external_name());
}
}
_subgraph_object_klasses->append_if_missing(relocated_k);
_has_non_early_klasses |= is_non_early_klass(orig_k);
}
bool KlassSubGraphInfo::is_non_early_klass(Klass* k) {
if (k->is_objArray_klass()) {
k = ObjArrayKlass::cast(k)->bottom_klass();
}
if (k->is_instance_klass()) {
if (!SystemDictionaryShared::is_early_klass(InstanceKlass::cast(k))) {
ResourceMark rm;
log_info(cds, heap)("non-early: %s", k->external_name());
return true;
} else {
return false;
}
} else {
return false;
}
}
// Initialize an archived subgraph_info_record from the given KlassSubGraphInfo.
void ArchivedKlassSubGraphInfoRecord::init(KlassSubGraphInfo* info) {
_k = info->klass();
_entry_field_records = NULL;
_subgraph_object_klasses = NULL;
_is_full_module_graph = info->is_full_module_graph();
if (_is_full_module_graph) {
// Consider all classes referenced by the full module graph as early -- we will be
// allocating objects of these classes during JVMTI early phase, so they cannot
// be processed by (non-early) JVMTI ClassFileLoadHook
_has_non_early_klasses = false;
} else {
_has_non_early_klasses = info->has_non_early_klasses();
}
if (_has_non_early_klasses) {
ResourceMark rm;
log_info(cds, heap)(
"Subgraph of klass %s has non-early klasses and cannot be used when JVMTI ClassFileLoadHook is enabled",
_k->external_name());
}
// populate the entry fields
GrowableArray<int>* entry_fields = info->subgraph_entry_fields();
if (entry_fields != NULL) {
int num_entry_fields = entry_fields->length();
assert(num_entry_fields % 2 == 0, "sanity");
_entry_field_records =
ArchiveBuilder::new_ro_array<int>(num_entry_fields);
for (int i = 0 ; i < num_entry_fields; i++) {
_entry_field_records->at_put(i, entry_fields->at(i));
}
}
// the Klasses of the objects in the sub-graphs
GrowableArray<Klass*>* subgraph_object_klasses = info->subgraph_object_klasses();
if (subgraph_object_klasses != NULL) {
int num_subgraphs_klasses = subgraph_object_klasses->length();
_subgraph_object_klasses =
ArchiveBuilder::new_ro_array<Klass*>(num_subgraphs_klasses);
for (int i = 0; i < num_subgraphs_klasses; i++) {
Klass* subgraph_k = subgraph_object_klasses->at(i);
if (log_is_enabled(Info, cds, heap)) {
ResourceMark rm;
log_info(cds, heap)(
"Archived object klass %s (%2d) => %s",
_k->external_name(), i, subgraph_k->external_name());
}
_subgraph_object_klasses->at_put(i, subgraph_k);
ArchivePtrMarker::mark_pointer(_subgraph_object_klasses->adr_at(i));
}
}
ArchivePtrMarker::mark_pointer(&_k);
ArchivePtrMarker::mark_pointer(&_entry_field_records);
ArchivePtrMarker::mark_pointer(&_subgraph_object_klasses);
}
struct CopyKlassSubGraphInfoToArchive : StackObj {
CompactHashtableWriter* _writer;
CopyKlassSubGraphInfoToArchive(CompactHashtableWriter* writer) : _writer(writer) {}
bool do_entry(Klass* klass, KlassSubGraphInfo& info) {
if (info.subgraph_object_klasses() != NULL || info.subgraph_entry_fields() != NULL) {
ArchivedKlassSubGraphInfoRecord* record =
(ArchivedKlassSubGraphInfoRecord*)ArchiveBuilder::ro_region_alloc(sizeof(ArchivedKlassSubGraphInfoRecord));
record->init(&info);
unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary((address)klass);
u4 delta = ArchiveBuilder::current()->any_to_offset_u4(record);
_writer->add(hash, delta);
}
return true; // keep on iterating
}
};
// Build the records of archived subgraph infos, which include:
// - Entry points to all subgraphs from the containing class mirror. The entry
// points are static fields in the mirror. For each entry point, the field
// offset, value and is_closed_archive flag are recorded in the sub-graph
// info. The value is stored back to the corresponding field at runtime.
// - A list of klasses that need to be loaded/initialized before archived
// java object sub-graph can be accessed at runtime.
void HeapShared::write_subgraph_info_table() {
// Allocate the contents of the hashtable(s) inside the RO region of the CDS archive.
DumpTimeKlassSubGraphInfoTable* d_table = _dump_time_subgraph_info_table;
CompactHashtableStats stats;
_run_time_subgraph_info_table.reset();
CompactHashtableWriter writer(d_table->_count, &stats);
CopyKlassSubGraphInfoToArchive copy(&writer);
d_table->iterate(©);
writer.dump(&_run_time_subgraph_info_table, "subgraphs");
}
void HeapShared::serialize_subgraph_info_table_header(SerializeClosure* soc) {
_run_time_subgraph_info_table.serialize_header(soc);
}
static void verify_the_heap(Klass* k, const char* which) {
if (VerifyArchivedFields > 0) {
ResourceMark rm;
log_info(cds, heap)("Verify heap %s initializing static field(s) in %s",
which, k->external_name());
VM_Verify verify_op;
VMThread::execute(&verify_op);
if (VerifyArchivedFields > 1 && is_init_completed()) {
// At this time, the oop->klass() of some archived objects in the heap may not
// have been loaded into the system dictionary yet. Nevertheless, oop->klass() should
// have enough information (object size, oop maps, etc) so that a GC can be safely
// performed.
//
// -XX:VerifyArchivedFields=2 force a GC to happen in such an early stage
// to check for GC safety.
log_info(cds, heap)("Trigger GC %s initializing static field(s) in %s",
which, k->external_name());
FlagSetting fs1(VerifyBeforeGC, true);
FlagSetting fs2(VerifyDuringGC, true);
FlagSetting fs3(VerifyAfterGC, true);
Universe::heap()->collect(GCCause::_java_lang_system_gc);
}
}
}
// Before GC can execute, we must ensure that all oops reachable from HeapShared::roots()
// have a valid klass. I.e., oopDesc::klass() must have already been resolved.
//
// Note: if a ArchivedKlassSubGraphInfoRecord contains non-early classes, and JVMTI
// ClassFileLoadHook is enabled, it's possible for this class to be dynamically replaced. In
// this case, we will not load the ArchivedKlassSubGraphInfoRecord and will clear its roots.
void HeapShared::resolve_classes(JavaThread* THREAD) {
if (!is_fully_available()) {
return; // nothing to do
}
resolve_classes_for_subgraphs(closed_archive_subgraph_entry_fields,
num_closed_archive_subgraph_entry_fields,
THREAD);
resolve_classes_for_subgraphs(open_archive_subgraph_entry_fields,
num_open_archive_subgraph_entry_fields,
THREAD);
resolve_classes_for_subgraphs(fmg_open_archive_subgraph_entry_fields,
num_fmg_open_archive_subgraph_entry_fields,
THREAD);
}
void HeapShared::resolve_classes_for_subgraphs(ArchivableStaticFieldInfo fields[],
int num, JavaThread* THREAD) {
for (int i = 0; i < num; i++) {
ArchivableStaticFieldInfo* info = &fields[i];
TempNewSymbol klass_name = SymbolTable::new_symbol(info->klass_name);
InstanceKlass* k = SystemDictionaryShared::find_builtin_class(klass_name);
assert(k != NULL && k->is_shared_boot_class(), "sanity");
resolve_classes_for_subgraph_of(k, THREAD);
}
}
void HeapShared::resolve_classes_for_subgraph_of(Klass* k, JavaThread* THREAD) {
ExceptionMark em(THREAD);
const ArchivedKlassSubGraphInfoRecord* record =
resolve_or_init_classes_for_subgraph_of(k, /*do_init=*/false, THREAD);
if (HAS_PENDING_EXCEPTION) {
CLEAR_PENDING_EXCEPTION;
}
if (record == NULL) {
clear_archived_roots_of(k);
}
}
void HeapShared::initialize_from_archived_subgraph(Klass* k, JavaThread* THREAD) {
if (!is_fully_available()) {
return; // nothing to do
}
ExceptionMark em(THREAD);
const ArchivedKlassSubGraphInfoRecord* record =
resolve_or_init_classes_for_subgraph_of(k, /*do_init=*/true, THREAD);
if (HAS_PENDING_EXCEPTION) {
CLEAR_PENDING_EXCEPTION;
// None of the field value will be set if there was an exception when initializing the classes.
// The java code will not see any of the archived objects in the
// subgraphs referenced from k in this case.
return;
}
if (record != NULL) {
init_archived_fields_for(k, record);
}
}
const ArchivedKlassSubGraphInfoRecord*
HeapShared::resolve_or_init_classes_for_subgraph_of(Klass* k, bool do_init, TRAPS) {
assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces");
if (!k->is_shared()) {
return NULL;
}
unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(k);
const ArchivedKlassSubGraphInfoRecord* record = _run_time_subgraph_info_table.lookup(k, hash, 0);
// Initialize from archived data. Currently this is done only
// during VM initialization time. No lock is needed.
if (record != NULL) {
if (record->is_full_module_graph() && !MetaspaceShared::use_full_module_graph()) {
if (log_is_enabled(Info, cds, heap)) {
ResourceMark rm(THREAD);
log_info(cds, heap)("subgraph %s cannot be used because full module graph is disabled",
k->external_name());
}
return NULL;
}
if (record->has_non_early_klasses() && JvmtiExport::should_post_class_file_load_hook()) {
if (log_is_enabled(Info, cds, heap)) {
ResourceMark rm(THREAD);
log_info(cds, heap)("subgraph %s cannot be used because JVMTI ClassFileLoadHook is enabled",
k->external_name());
}
return NULL;
}
resolve_or_init(k, do_init, CHECK_NULL);
// Load/link/initialize the klasses of the objects in the subgraph.
// NULL class loader is used.
Array<Klass*>* klasses = record->subgraph_object_klasses();
if (klasses != NULL) {
for (int i = 0; i < klasses->length(); i++) {
Klass* klass = klasses->at(i);
if (!klass->is_shared()) {
return NULL;
}
resolve_or_init(klass, do_init, CHECK_NULL);
}
}
}
return record;
}
void HeapShared::resolve_or_init(Klass* k, bool do_init, TRAPS) {
if (!do_init) {
if (k->class_loader_data() == NULL) {
Klass* resolved_k = SystemDictionary::resolve_or_null(k->name(), CHECK);
assert(resolved_k == k, "classes used by archived heap must not be replaced by JVMTI ClassFileLoadHook");
}
} else {
assert(k->class_loader_data() != NULL, "must have been resolved by HeapShared::resolve_classes");
if (k->is_instance_klass()) {
InstanceKlass* ik = InstanceKlass::cast(k);
ik->initialize(CHECK);
} else if (k->is_objArray_klass()) {
ObjArrayKlass* oak = ObjArrayKlass::cast(k);
oak->initialize(CHECK);
}
}
}
void HeapShared::init_archived_fields_for(Klass* k, const ArchivedKlassSubGraphInfoRecord* record) {
verify_the_heap(k, "before");
// Load the subgraph entry fields from the record and store them back to
// the corresponding fields within the mirror.
oop m = k->java_mirror();
Array<int>* entry_field_records = record->entry_field_records();
if (entry_field_records != NULL) {
int efr_len = entry_field_records->length();
assert(efr_len % 2 == 0, "sanity");
for (int i = 0; i < efr_len; i += 2) {
int field_offset = entry_field_records->at(i);
int root_index = entry_field_records->at(i+1);
oop v = get_root(root_index, /*clear=*/true);
m->obj_field_put(field_offset, v);
log_debug(cds, heap)(" " PTR_FORMAT " init field @ %2d = " PTR_FORMAT, p2i(k), field_offset, p2i(v));
}
// Done. Java code can see the archived sub-graphs referenced from k's
// mirror after this point.
if (log_is_enabled(Info, cds, heap)) {
ResourceMark rm;
log_info(cds, heap)("initialize_from_archived_subgraph %s " PTR_FORMAT "%s",
k->external_name(), p2i(k), JvmtiExport::is_early_phase() ? " (early)" : "");
}
}
verify_the_heap(k, "after ");
}
void HeapShared::clear_archived_roots_of(Klass* k) {
unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(k);
const ArchivedKlassSubGraphInfoRecord* record = _run_time_subgraph_info_table.lookup(k, hash, 0);
if (record != NULL) {
Array<int>* entry_field_records = record->entry_field_records();
if (entry_field_records != NULL) {
int efr_len = entry_field_records->length();
assert(efr_len % 2 == 0, "sanity");
for (int i = 0; i < efr_len; i += 2) {
int root_index = entry_field_records->at(i+1);
clear_root(root_index);
}
}
}
}
class WalkOopAndArchiveClosure: public BasicOopIterateClosure {
int _level;
bool _is_closed_archive;
bool _record_klasses_only;
KlassSubGraphInfo* _subgraph_info;
oop _orig_referencing_obj;
oop _archived_referencing_obj;
public:
WalkOopAndArchiveClosure(int level,
bool is_closed_archive,
bool record_klasses_only,
KlassSubGraphInfo* subgraph_info,
oop orig, oop archived) :
_level(level), _is_closed_archive(is_closed_archive),
_record_klasses_only(record_klasses_only),
_subgraph_info(subgraph_info),
_orig_referencing_obj(orig), _archived_referencing_obj(archived) {}
void do_oop(narrowOop *p) { WalkOopAndArchiveClosure::do_oop_work(p); }
void do_oop( oop *p) { WalkOopAndArchiveClosure::do_oop_work(p); }
protected:
template <class T> void do_oop_work(T *p) {
oop obj = RawAccess<>::oop_load(p);
if (!CompressedOops::is_null(obj)) {
assert(!HeapShared::is_archived_object_during_dumptime(obj),
"original objects must not point to archived objects");
size_t field_delta = pointer_delta(p, _orig_referencing_obj, sizeof(char));
T* new_p = (T*)(cast_from_oop<address>(_archived_referencing_obj) + field_delta);
if (!_record_klasses_only && log_is_enabled(Debug, cds, heap)) {
ResourceMark rm;
log_debug(cds, heap)("(%d) %s[" SIZE_FORMAT "] ==> " PTR_FORMAT " size " SIZE_FORMAT " %s", _level,
_orig_referencing_obj->klass()->external_name(), field_delta,
p2i(obj), obj->size() * HeapWordSize, obj->klass()->external_name());
LogTarget(Trace, cds, heap) log;
LogStream out(log);
obj->print_on(&out);
}
oop archived = HeapShared::archive_reachable_objects_from(
_level + 1, _subgraph_info, obj, _is_closed_archive);
assert(archived != NULL, "VM should have exited with unarchivable objects for _level > 1");
assert(HeapShared::is_archived_object_during_dumptime(archived), "must be");
if (!_record_klasses_only) {
// Update the reference in the archived copy of the referencing object.
log_debug(cds, heap)("(%d) updating oop @[" PTR_FORMAT "] " PTR_FORMAT " ==> " PTR_FORMAT,
_level, p2i(new_p), p2i(obj), p2i(archived));
RawAccess<IS_NOT_NULL>::oop_store(new_p, archived);
}
}
}
};
void HeapShared::check_closed_region_object(InstanceKlass* k) {
// Check fields in the object
for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
if (!fs.access_flags().is_static()) {
BasicType ft = fs.field_descriptor().field_type();
if (!fs.access_flags().is_final() && is_reference_type(ft)) {
ResourceMark rm;
log_warning(cds, heap)(
"Please check reference field in %s instance in closed archive heap region: %s %s",
k->external_name(), (fs.name())->as_C_string(),
(fs.signature())->as_C_string());
}
}
}
}
void HeapShared::check_module_oop(oop orig_module_obj) {
assert(DumpSharedSpaces, "must be");
assert(java_lang_Module::is_instance(orig_module_obj), "must be");
ModuleEntry* orig_module_ent = java_lang_Module::module_entry_raw(orig_module_obj);
if (orig_module_ent == NULL) {
// These special Module objects are created in Java code. They are not
// defined via Modules::define_module(), so they don't have a ModuleEntry:
// java.lang.Module::ALL_UNNAMED_MODULE
// java.lang.Module::EVERYONE_MODULE
// jdk.internal.loader.ClassLoaders$BootClassLoader::unnamedModule
assert(java_lang_Module::name(orig_module_obj) == NULL, "must be unnamed");
log_info(cds, heap)("Module oop with No ModuleEntry* @[" PTR_FORMAT "]", p2i(orig_module_obj));
} else {
ClassLoaderData* loader_data = orig_module_ent->loader_data();
assert(loader_data->is_builtin_class_loader_data(), "must be");
}
}
// (1) If orig_obj has not been archived yet, archive it.
// (2) If orig_obj has not been seen yet (since start_recording_subgraph() was called),
// trace all objects that are reachable from it, and make sure these objects are archived.
// (3) Record the klasses of all orig_obj and all reachable objects.
oop HeapShared::archive_reachable_objects_from(int level,
KlassSubGraphInfo* subgraph_info,
oop orig_obj,
bool is_closed_archive) {
assert(orig_obj != NULL, "must be");
assert(!is_archived_object_during_dumptime(orig_obj), "sanity");
if (!JavaClasses::is_supported_for_archiving(orig_obj)) {
// This object has injected fields that cannot be supported easily, so we disallow them for now.
// If you get an error here, you probably made a change in the JDK library that has added
// these objects that are referenced (directly or indirectly) by static fields.
ResourceMark rm;
log_error(cds, heap)("Cannot archive object of class %s", orig_obj->klass()->external_name());
vm_direct_exit(1);
}
// java.lang.Class instances cannot be included in an archived object sub-graph. We only support
// them as Klass::_archived_mirror because they need to be specially restored at run time.
//
// If you get an error here, you probably made a change in the JDK library that has added a Class
// object that is referenced (directly or indirectly) by static fields.
if (java_lang_Class::is_instance(orig_obj)) {
log_error(cds, heap)("(%d) Unknown java.lang.Class object is in the archived sub-graph", level);