23 */
24
25 #ifndef SHARE_CDS_HEAPSHARED_HPP
26 #define SHARE_CDS_HEAPSHARED_HPP
27
28 #include "cds/aotMetaspace.hpp"
29 #include "cds/dumpTimeClassInfo.hpp"
30 #include "classfile/compactHashtable.hpp"
31 #include "classfile/javaClasses.hpp"
32 #include "gc/shared/gc_globals.hpp"
33 #include "memory/allocation.hpp"
34 #include "memory/allStatic.hpp"
35 #include "oops/compressedOops.hpp"
36 #include "oops/oop.hpp"
37 #include "oops/oopHandle.hpp"
38 #include "oops/oopsHierarchy.hpp"
39 #include "utilities/growableArray.hpp"
40 #include "utilities/hashTable.hpp"
41
42 #if INCLUDE_CDS_JAVA_HEAP
43 class DumpedInternedStrings;
44 class FileMapInfo;
45 class KlassSubGraphInfo;
46 class MetaspaceObjToOopHandleTable;
47 class ResourceBitMap;
48
49 struct ArchivableStaticFieldInfo;
50
51 #define ARCHIVED_BOOT_LAYER_CLASS "jdk/internal/module/ArchivedBootLayer"
52 #define ARCHIVED_BOOT_LAYER_FIELD "archivedBootLayer"
53
54 // A dump time sub-graph info for Klass _k. It includes the entry points
55 // (static fields in _k's mirror) of the archived sub-graphs reachable
56 // from _k's mirror. It also contains a list of Klasses of the objects
57 // within the sub-graphs.
58 class KlassSubGraphInfo: public CHeapObj<mtClass> {
59 private:
60 // The class that contains the static field(s) as the entry point(s)
61 // of archived object sub-graph(s).
62 Klass* _k;
63 // A list of classes need to be loaded and initialized before the archived
159 inline static bool is_writing_streaming_mode() NOT_CDS_JAVA_HEAP_RETURN_(false);
160 inline static bool is_writing_mapping_mode() NOT_CDS_JAVA_HEAP_RETURN_(false);
161
162 static bool is_subgraph_root_class(InstanceKlass* ik);
163
164 // Scratch objects for archiving Klass::java_mirror()
165 static oop scratch_java_mirror(BasicType t) NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
166 static oop scratch_java_mirror(Klass* k) NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
167 static oop scratch_java_mirror(oop java_mirror) NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
168 static bool is_archived_boot_layer_available(JavaThread* current) NOT_CDS_JAVA_HEAP_RETURN_(false);
169
170 static bool is_archived_heap_in_use() NOT_CDS_JAVA_HEAP_RETURN_(false);
171 static bool can_use_archived_heap() NOT_CDS_JAVA_HEAP_RETURN_(false);
172 static bool is_too_large_to_archive(size_t size);
173 static bool is_string_too_large_to_archive(oop string);
174 static bool is_too_large_to_archive(oop obj);
175
176 static void initialize_streaming() NOT_CDS_JAVA_HEAP_RETURN;
177 static void enable_gc() NOT_CDS_JAVA_HEAP_RETURN;
178 static void materialize_thread_object() NOT_CDS_JAVA_HEAP_RETURN;
179 static void add_to_dumped_interned_strings(oop string) NOT_CDS_JAVA_HEAP_RETURN;
180 static void finalize_initialization(FileMapInfo* static_mapinfo) NOT_CDS_JAVA_HEAP_RETURN;
181
182 private:
183 #if INCLUDE_CDS_JAVA_HEAP
184 static HeapArchiveMode _heap_load_mode;
185 static HeapArchiveMode _heap_write_mode;
186
187 // statistics
188 constexpr static int ALLOC_STAT_SLOTS = 16;
189 static size_t _alloc_count[ALLOC_STAT_SLOTS];
190 static size_t _alloc_size[ALLOC_STAT_SLOTS];
191 static size_t _total_obj_count;
192 static size_t _total_obj_size; // in HeapWords
193
194 static void count_allocation(size_t size);
195 static void print_stats();
196 public:
197 static void debug_trace();
198 static unsigned oop_hash(oop const& p);
199 static unsigned oop_handle_hash(OopHandle const& oh);
200 static unsigned oop_handle_hash_raw(OopHandle const& oh);
201 static bool oop_handle_equals(const OopHandle& a, const OopHandle& b);
202 static unsigned string_oop_hash(oop const& string) {
203 return java_lang_String::hash_code(string);
204 }
205
206 class CopyKlassSubGraphInfoToArchive;
207
208 class CachedOopInfo {
209 // Used by CDSHeapVerifier.
210 OopHandle _orig_referrer;
211
212 // The location of this object inside {AOTMappedHeapWriter, AOTStreamedHeapWriter}::_buffer
213 size_t _buffer_offset;
214
215 // One or more fields in this object are pointing to non-null oops.
216 bool _has_oop_pointers;
217
218 // One or more fields in this object are pointing to MetaspaceObj
219 bool _has_native_pointers;
220 public:
221 CachedOopInfo(OopHandle orig_referrer, bool has_oop_pointers)
222 : _orig_referrer(orig_referrer),
223 _buffer_offset(0),
224 _has_oop_pointers(has_oop_pointers),
225 _has_native_pointers(false) {}
226 oop orig_referrer() const;
227 void set_buffer_offset(size_t offset) { _buffer_offset = offset; }
228 size_t buffer_offset() const { return _buffer_offset; }
229 bool has_oop_pointers() const { return _has_oop_pointers; }
230 bool has_native_pointers() const { return _has_native_pointers; }
231 void set_has_native_pointers() { _has_native_pointers = true; }
232 };
233
234 private:
235 static const int INITIAL_TABLE_SIZE = 15889; // prime number
236 static const int MAX_TABLE_SIZE = 1000000;
237 typedef ResizeableHashTable<OopHandle, CachedOopInfo,
238 AnyObj::C_HEAP,
239 mtClassShared,
240 HeapShared::oop_handle_hash_raw,
241 HeapShared::oop_handle_equals> ArchivedObjectCache;
242 static ArchivedObjectCache* _archived_object_cache;
243
244 class DumpTimeKlassSubGraphInfoTable
245 : public HashTable<Klass*, KlassSubGraphInfo,
246 137, // prime number
247 AnyObj::C_HEAP,
248 mtClassShared,
249 DumpTimeSharedClassTable_hash> {};
250
251 public: // solaris compiler wants this for RunTimeKlassSubGraphInfoTable
252 inline static bool record_equals_compact_hashtable_entry(
253 const ArchivedKlassSubGraphInfoRecord* value, const Klass* key, int len_unused) {
254 return (value->klass() == key);
255 }
256
257 private:
258 typedef OffsetCompactHashtable<
259 const Klass*,
260 const ArchivedKlassSubGraphInfoRecord*,
377
378 class OopFieldPusher;
379 using PendingOopStack = GrowableArrayCHeap<PendingOop, mtClassShared>;
380
381 static PendingOop _object_being_archived;
382 static bool walk_one_object(PendingOopStack* stack, int level, KlassSubGraphInfo* subgraph_info,
383 oop orig_obj, oop referrer);
384
385 static void reset_archived_object_states(TRAPS);
386 static void ensure_determinism(TRAPS);
387 public:
388 static void prepare_for_archiving(TRAPS);
389 static void create_archived_object_cache() {
390 _archived_object_cache =
391 new (mtClass)ArchivedObjectCache(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE);
392 }
393 static void destroy_archived_object_cache() {
394 delete _archived_object_cache;
395 _archived_object_cache = nullptr;
396 }
397 static ArchivedObjectCache* archived_object_cache() {
398 return _archived_object_cache;
399 }
400
401 static CachedOopInfo* get_cached_oop_info(oop orig_obj);
402
403 static int archive_exception_instance(oop exception);
404
405 static bool archive_reachable_objects_from(int level,
406 KlassSubGraphInfo* subgraph_info,
407 oop orig_obj);
408
409 static bool is_dumped_interned_string(oop o);
410
411 // Scratch objects for archiving Klass::java_mirror()
412 static void set_scratch_java_mirror(Klass* k, oop mirror);
413 static void remove_scratch_objects(Klass* k);
414 static bool is_metadata_field(oop src_obj, int offset);
415 template <typename T> static void do_metadata_offsets(oop src_obj, T callback);
416 static void remap_dumped_metadata(oop src_obj, address archived_object);
417 inline static void remap_loaded_metadata(oop obj);
418 inline static oop maybe_remap_referent(bool is_java_lang_ref, size_t field_offset, oop referent);
419 static void get_pointer_info(oop src_obj, bool& has_oop_pointers, bool& has_native_pointers);
420 static void set_has_native_pointers(oop src_obj);
421 static uintptr_t archive_location(oop src_obj);
422
423 // We use the HeapShared::roots() array to make sure that objects stored in the
424 // archived heap region are not prematurely collected. These roots include:
425 //
426 // - mirrors of classes that have not yet been loaded.
427 // - ConstantPool::resolved_references() of classes that have not yet been loaded.
428 // - ArchivedKlassSubGraphInfoRecords that have not been initialized
429 // - java.lang.Module objects that have not yet been added to the module graph
430 //
431 // When a mirror M becomes referenced by a newly loaded class K, M will be removed
432 // from HeapShared::roots() via clear_root(), and K will be responsible for
433 // keeping M alive.
434 //
435 // Other types of roots are also cleared similarly when they become referenced.
436
437 // Dump-time only. Returns the index of the root, which can be used at run time to read
438 // the root using get_root(index, ...).
439 static int append_root(oop obj);
440 static GrowableArrayCHeap<oop, mtClassShared>* pending_roots() { return _pending_roots; }
441
442 // Dump-time and runtime
443 static objArrayOop root_segment(int segment_idx);
444 static oop get_root(int index, bool clear=false);
445
446 // Run-time only
447 static void clear_root(int index);
448
449 static void get_segment_indexes(int index, int& segment_index, int& internal_index);
450
451 static void setup_test_class(const char* test_class_name) PRODUCT_RETURN;
452 #endif // INCLUDE_CDS_JAVA_HEAP
453
454 public:
455 static void finish_materialize_objects() NOT_CDS_JAVA_HEAP_RETURN;
456
457 static void write_heap(AOTMappedHeapInfo* mapped_heap_info, AOTStreamedHeapInfo* streamed_heap_info) NOT_CDS_JAVA_HEAP_RETURN;
458 static objArrayOop scratch_resolved_references(ConstantPool* src);
459 static void add_scratch_resolved_references(ConstantPool* src, objArrayOop dest) NOT_CDS_JAVA_HEAP_RETURN;
460 static void init_dumping() NOT_CDS_JAVA_HEAP_RETURN;
461 static void init_scratch_objects_for_basic_type_mirrors(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
462 static void init_box_classes(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
463 static bool is_heap_region(int idx) {
464 CDS_JAVA_HEAP_ONLY(return (idx == AOTMetaspace::hp);)
465 NOT_CDS_JAVA_HEAP_RETURN_(false);
466 }
467 static void delete_tables_with_raw_oops() NOT_CDS_JAVA_HEAP_RETURN;
468
469 static void resolve_classes(JavaThread* current) NOT_CDS_JAVA_HEAP_RETURN;
470 static void initialize_from_archived_subgraph(JavaThread* current, Klass* k) NOT_CDS_JAVA_HEAP_RETURN;
|
23 */
24
25 #ifndef SHARE_CDS_HEAPSHARED_HPP
26 #define SHARE_CDS_HEAPSHARED_HPP
27
28 #include "cds/aotMetaspace.hpp"
29 #include "cds/dumpTimeClassInfo.hpp"
30 #include "classfile/compactHashtable.hpp"
31 #include "classfile/javaClasses.hpp"
32 #include "gc/shared/gc_globals.hpp"
33 #include "memory/allocation.hpp"
34 #include "memory/allStatic.hpp"
35 #include "oops/compressedOops.hpp"
36 #include "oops/oop.hpp"
37 #include "oops/oopHandle.hpp"
38 #include "oops/oopsHierarchy.hpp"
39 #include "utilities/growableArray.hpp"
40 #include "utilities/hashTable.hpp"
41
42 #if INCLUDE_CDS_JAVA_HEAP
43 class FileMapInfo;
44 class KlassSubGraphInfo;
45 class MetaspaceObjToOopHandleTable;
46 class ResourceBitMap;
47
48 struct ArchivableStaticFieldInfo;
49
50 #define ARCHIVED_BOOT_LAYER_CLASS "jdk/internal/module/ArchivedBootLayer"
51 #define ARCHIVED_BOOT_LAYER_FIELD "archivedBootLayer"
52
53 // A dump time sub-graph info for Klass _k. It includes the entry points
54 // (static fields in _k's mirror) of the archived sub-graphs reachable
55 // from _k's mirror. It also contains a list of Klasses of the objects
56 // within the sub-graphs.
57 class KlassSubGraphInfo: public CHeapObj<mtClass> {
58 private:
59 // The class that contains the static field(s) as the entry point(s)
60 // of archived object sub-graph(s).
61 Klass* _k;
62 // A list of classes need to be loaded and initialized before the archived
158 inline static bool is_writing_streaming_mode() NOT_CDS_JAVA_HEAP_RETURN_(false);
159 inline static bool is_writing_mapping_mode() NOT_CDS_JAVA_HEAP_RETURN_(false);
160
161 static bool is_subgraph_root_class(InstanceKlass* ik);
162
163 // Scratch objects for archiving Klass::java_mirror()
164 static oop scratch_java_mirror(BasicType t) NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
165 static oop scratch_java_mirror(Klass* k) NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
166 static oop scratch_java_mirror(oop java_mirror) NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
167 static bool is_archived_boot_layer_available(JavaThread* current) NOT_CDS_JAVA_HEAP_RETURN_(false);
168
169 static bool is_archived_heap_in_use() NOT_CDS_JAVA_HEAP_RETURN_(false);
170 static bool can_use_archived_heap() NOT_CDS_JAVA_HEAP_RETURN_(false);
171 static bool is_too_large_to_archive(size_t size);
172 static bool is_string_too_large_to_archive(oop string);
173 static bool is_too_large_to_archive(oop obj);
174
175 static void initialize_streaming() NOT_CDS_JAVA_HEAP_RETURN;
176 static void enable_gc() NOT_CDS_JAVA_HEAP_RETURN;
177 static void materialize_thread_object() NOT_CDS_JAVA_HEAP_RETURN;
178 static void archive_interned_string(oop string);
179 static void finalize_initialization(FileMapInfo* static_mapinfo) NOT_CDS_JAVA_HEAP_RETURN;
180
181 private:
182 #if INCLUDE_CDS_JAVA_HEAP
183 static HeapArchiveMode _heap_load_mode;
184 static HeapArchiveMode _heap_write_mode;
185
186 // statistics
187 constexpr static int ALLOC_STAT_SLOTS = 16;
188 static size_t _alloc_count[ALLOC_STAT_SLOTS];
189 static size_t _alloc_size[ALLOC_STAT_SLOTS];
190 static size_t _total_obj_count;
191 static size_t _total_obj_size; // in HeapWords
192
193 static void count_allocation(size_t size);
194 static void print_stats();
195 public:
196 static void debug_trace();
197 static unsigned oop_hash(oop const& p);
198 static bool oop_handle_equals(const OopHandle& a, const OopHandle& b);
199
200 class CopyKlassSubGraphInfoToArchive;
201
202 class CachedOopInfo {
203 // Used by CDSHeapVerifier.
204 OopHandle _orig_referrer;
205
206 // The location of this object inside {AOTMappedHeapWriter, AOTStreamedHeapWriter}::_buffer
207 size_t _buffer_offset;
208
209 // One or more fields in this object are pointing to non-null oops.
210 bool _has_oop_pointers;
211
212 // One or more fields in this object are pointing to MetaspaceObj
213 bool _has_native_pointers;
214
215 // >= 0 if this oop has been append to the list of roots
216 int _root_index;
217 public:
218 CachedOopInfo(OopHandle orig_referrer, bool has_oop_pointers)
219 : _orig_referrer(orig_referrer),
220 _buffer_offset(0),
221 _has_oop_pointers(has_oop_pointers),
222 _has_native_pointers(false),
223 _root_index(-1) {}
224 oop orig_referrer() const;
225 void set_buffer_offset(size_t offset) { _buffer_offset = offset; }
226 size_t buffer_offset() const { return _buffer_offset; }
227 bool has_oop_pointers() const { return _has_oop_pointers; }
228 bool has_native_pointers() const { return _has_native_pointers; }
229 void set_has_native_pointers() { _has_native_pointers = true; }
230 int root_index() const { return _root_index; }
231 void set_root_index(int i) { _root_index = i; }
232 };
233
234 private:
235 static const int INITIAL_TABLE_SIZE = 15889; // prime number
236 static const int MAX_TABLE_SIZE = 1000000;
237 static bool _use_identity_hash_for_archived_object_cache;
238
239 static unsigned archived_object_cache_hash(OopHandle const& oh);
240
241 typedef ResizeableHashTable<OopHandle, CachedOopInfo,
242 AnyObj::C_HEAP,
243 mtClassShared,
244 HeapShared::archived_object_cache_hash,
245 HeapShared::oop_handle_equals> ArchivedObjectCache;
246 static ArchivedObjectCache* _archived_object_cache;
247
248 class DumpTimeKlassSubGraphInfoTable
249 : public HashTable<Klass*, KlassSubGraphInfo,
250 137, // prime number
251 AnyObj::C_HEAP,
252 mtClassShared,
253 DumpTimeSharedClassTable_hash> {};
254
255 public: // solaris compiler wants this for RunTimeKlassSubGraphInfoTable
256 inline static bool record_equals_compact_hashtable_entry(
257 const ArchivedKlassSubGraphInfoRecord* value, const Klass* key, int len_unused) {
258 return (value->klass() == key);
259 }
260
261 private:
262 typedef OffsetCompactHashtable<
263 const Klass*,
264 const ArchivedKlassSubGraphInfoRecord*,
381
382 class OopFieldPusher;
383 using PendingOopStack = GrowableArrayCHeap<PendingOop, mtClassShared>;
384
385 static PendingOop _object_being_archived;
386 static bool walk_one_object(PendingOopStack* stack, int level, KlassSubGraphInfo* subgraph_info,
387 oop orig_obj, oop referrer);
388
389 static void reset_archived_object_states(TRAPS);
390 static void ensure_determinism(TRAPS);
391 public:
392 static void prepare_for_archiving(TRAPS);
393 static void create_archived_object_cache() {
394 _archived_object_cache =
395 new (mtClass)ArchivedObjectCache(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE);
396 }
397 static void destroy_archived_object_cache() {
398 delete _archived_object_cache;
399 _archived_object_cache = nullptr;
400 }
401 static void make_archived_object_cache_gc_safe();
402 static ArchivedObjectCache* archived_object_cache() {
403 return _archived_object_cache;
404 }
405
406 static CachedOopInfo* get_cached_oop_info(oop orig_obj);
407
408 static int archive_exception_instance(oop exception);
409
410 static bool archive_reachable_objects_from(int level,
411 KlassSubGraphInfo* subgraph_info,
412 oop orig_obj);
413
414 static bool is_interned_string(oop obj);
415 static bool is_dumped_interned_string(oop o);
416
417 // Scratch objects for archiving Klass::java_mirror()
418 static void set_scratch_java_mirror(Klass* k, oop mirror);
419 static void remove_scratch_objects(Klass* k);
420 static bool is_metadata_field(oop src_obj, int offset);
421 template <typename T> static void do_metadata_offsets(oop src_obj, T callback);
422 static void remap_dumped_metadata(oop src_obj, address archived_object);
423 inline static void remap_loaded_metadata(oop obj);
424 inline static oop maybe_remap_referent(bool is_java_lang_ref, size_t field_offset, oop referent);
425 static void get_pointer_info(oop src_obj, bool& has_oop_pointers, bool& has_native_pointers);
426 static void set_has_native_pointers(oop src_obj);
427 static uintptr_t archive_location(oop src_obj);
428
429 // We use the HeapShared::roots() array to make sure that objects stored in the
430 // archived heap region are not prematurely collected. These roots include:
431 //
432 // - mirrors of classes that have not yet been loaded.
433 // - ConstantPool::resolved_references() of classes that have not yet been loaded.
434 // - ArchivedKlassSubGraphInfoRecords that have not been initialized
435 // - java.lang.Module objects that have not yet been added to the module graph
436 //
437 // When a mirror M becomes referenced by a newly loaded class K, M will be removed
438 // from HeapShared::roots() via clear_root(), and K will be responsible for
439 // keeping M alive.
440 //
441 // Other types of roots are also cleared similarly when they become referenced.
442
443 // Dump-time only. Returns the index of the root, which can be used at run time to read
444 // the root using get_root(index, ...).
445 static int append_root(oop obj);
446
447 // AOT-compile time only.
448 // Returns -1 if obj is not in the heap root set.
449 static int get_root_index(oop obj) NOT_CDS_JAVA_HEAP_RETURN_(-1);
450
451 static GrowableArrayCHeap<oop, mtClassShared>* pending_roots() { return _pending_roots; }
452
453 // Dump-time and runtime
454 static objArrayOop root_segment(int segment_idx);
455 static oop get_root(int index, bool clear=false);
456
457 // Run-time only
458 static void clear_root(int index);
459 static void get_segment_indexes(int index, int& segment_index, int& internal_index);
460 static void setup_test_class(const char* test_class_name) PRODUCT_RETURN;
461 #endif // INCLUDE_CDS_JAVA_HEAP
462
463 public:
464 static void finish_materialize_objects() NOT_CDS_JAVA_HEAP_RETURN;
465
466 static void write_heap(AOTMappedHeapInfo* mapped_heap_info, AOTStreamedHeapInfo* streamed_heap_info) NOT_CDS_JAVA_HEAP_RETURN;
467 static objArrayOop scratch_resolved_references(ConstantPool* src);
468 static void add_scratch_resolved_references(ConstantPool* src, objArrayOop dest) NOT_CDS_JAVA_HEAP_RETURN;
469 static void init_dumping() NOT_CDS_JAVA_HEAP_RETURN;
470 static void init_scratch_objects_for_basic_type_mirrors(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
471 static void init_box_classes(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
472 static bool is_heap_region(int idx) {
473 CDS_JAVA_HEAP_ONLY(return (idx == AOTMetaspace::hp);)
474 NOT_CDS_JAVA_HEAP_RETURN_(false);
475 }
476 static void delete_tables_with_raw_oops() NOT_CDS_JAVA_HEAP_RETURN;
477
478 static void resolve_classes(JavaThread* current) NOT_CDS_JAVA_HEAP_RETURN;
479 static void initialize_from_archived_subgraph(JavaThread* current, Klass* k) NOT_CDS_JAVA_HEAP_RETURN;
|