< prev index next >

src/hotspot/share/cds/heapShared.hpp

Print this page

  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_CDS_HEAPSHARED_HPP
 26 #define SHARE_CDS_HEAPSHARED_HPP
 27 

 28 #include "cds/dumpTimeClassInfo.hpp"
 29 #include "cds/metaspaceShared.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/resourceHash.hpp"
 41 
 42 #if INCLUDE_CDS_JAVA_HEAP
 43 class DumpedInternedStrings;
 44 class FileMapInfo;
 45 class KlassSubGraphInfo;
 46 class MetaspaceObjToOopHandleTable;
 47 class ResourceBitMap;

150   static oop scratch_java_mirror(Klass* k)        NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
151   static oop scratch_java_mirror(oop java_mirror) NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
152   static bool is_archived_boot_layer_available(JavaThread* current) NOT_CDS_JAVA_HEAP_RETURN_(false);
153 
154 private:
155 #if INCLUDE_CDS_JAVA_HEAP
156   static DumpedInternedStrings *_dumped_interned_strings;
157 
158   // statistics
159   constexpr static int ALLOC_STAT_SLOTS = 16;
160   static size_t _alloc_count[ALLOC_STAT_SLOTS];
161   static size_t _alloc_size[ALLOC_STAT_SLOTS];
162   static size_t _total_obj_count;
163   static size_t _total_obj_size; // in HeapWords
164 
165   static void count_allocation(size_t size);
166   static void print_stats();
167 public:
168   static void debug_trace();
169   static unsigned oop_hash(oop const& p);



170   static unsigned string_oop_hash(oop const& string) {
171     return java_lang_String::hash_code(string);
172   }
173 
174   class CopyKlassSubGraphInfoToArchive;
175 
176   class CachedOopInfo {
177     // Used by CDSHeapVerifier.
178     oop _orig_referrer;
179 
180     // The location of this object inside ArchiveHeapWriter::_buffer
181     size_t _buffer_offset;
182 
183     // One or more fields in this object are pointing to non-null oops.
184     bool _has_oop_pointers;
185 
186     // One or more fields in this object are pointing to MetaspaceObj
187     bool _has_native_pointers;
188   public:
189     CachedOopInfo(oop orig_referrer, bool has_oop_pointers)
190       : _orig_referrer(orig_referrer),
191         _buffer_offset(0),
192         _has_oop_pointers(has_oop_pointers),
193         _has_native_pointers(false) {}
194     oop orig_referrer()             const { return _orig_referrer;   }
195     void set_buffer_offset(size_t offset) { _buffer_offset = offset; }
196     size_t buffer_offset()          const { return _buffer_offset;   }
197     bool has_oop_pointers()         const { return _has_oop_pointers; }
198     bool has_native_pointers()      const { return _has_native_pointers; }
199     void set_has_native_pointers()        { _has_native_pointers = true; }
200   };
201 
202 private:
203   static const int INITIAL_TABLE_SIZE = 15889; // prime number
204   static const int MAX_TABLE_SIZE     = 1000000;
205   typedef ResizeableResourceHashtable<oop, CachedOopInfo,
206       AnyObj::C_HEAP,
207       mtClassShared,
208       HeapShared::oop_hash> ArchivedObjectCache;

209   static ArchivedObjectCache* _archived_object_cache;
210 
211   class DumpTimeKlassSubGraphInfoTable
212     : public ResourceHashtable<Klass*, KlassSubGraphInfo,
213                                137, // prime number
214                                AnyObj::C_HEAP,
215                                mtClassShared,
216                                DumpTimeSharedClassTable_hash> {
217   public:
218     int _count;
219   };
220 
221 public: // solaris compiler wants this for RunTimeKlassSubGraphInfoTable
222   inline static bool record_equals_compact_hashtable_entry(
223        const ArchivedKlassSubGraphInfoRecord* value, const Klass* key, int len_unused) {
224     return (value->klass() == key);
225   }
226 
227 private:
228   typedef OffsetCompactHashtable<

253 
254   static KlassSubGraphInfo* init_subgraph_info(Klass *k, bool is_full_module_graph);
255   static KlassSubGraphInfo* get_subgraph_info(Klass *k);
256 
257   static void init_subgraph_entry_fields(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
258   static void init_subgraph_entry_fields(ArchivableStaticFieldInfo fields[], TRAPS);
259 
260   // UseCompressedOops only: Used by decode_from_archive
261   static address _narrow_oop_base;
262   static int     _narrow_oop_shift;
263 
264   // !UseCompressedOops only: used to relocate pointers to the archived objects
265   static ptrdiff_t _runtime_delta;
266 
267   typedef ResizeableResourceHashtable<oop, bool,
268       AnyObj::C_HEAP,
269       mtClassShared,
270       HeapShared::oop_hash> SeenObjectsTable;
271 
272   static SeenObjectsTable *_seen_objects_table;
273 
274   // The "special subgraph" contains all the archived objects that are reachable
275   // from the following roots:
276   //    - interned strings
277   //    - Klass::java_mirror() -- including aot-initialized mirrors such as those of Enum klasses.
278   //    - ConstantPool::resolved_references()
279   //    - Universe::<xxx>_exception_instance()
280   static KlassSubGraphInfo* _dump_time_special_subgraph;              // for collecting info during dump time
281   static ArchivedKlassSubGraphInfoRecord* _run_time_special_subgraph; // for initializing classes during run time.
282 
283   static GrowableArrayCHeap<oop, mtClassShared>* _pending_roots;
284   static GrowableArrayCHeap<OopHandle, mtClassShared>* _root_segments;

285   static int _root_segment_max_size_elems;
286   static OopHandle _scratch_basic_type_mirrors[T_VOID+1];
287   static MetaspaceObjToOopHandleTable* _scratch_objects_table;
288 
289   static void init_seen_objects_table() {
290     assert(_seen_objects_table == nullptr, "must be");
291     _seen_objects_table = new (mtClass)SeenObjectsTable(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE);
292   }
293   static void delete_seen_objects_table() {
294     assert(_seen_objects_table != nullptr, "must be");
295     delete _seen_objects_table;
296     _seen_objects_table = nullptr;
297   }
298 


299   // Statistics (for one round of start_recording_subgraph ... done_recording_subgraph)
300   static int _num_new_walked_objs;
301   static int _num_new_archived_objs;
302   static int _num_old_recorded_klasses;
303 
304   // Statistics (for all archived subgraphs)
305   static int _num_total_subgraph_recordings;
306   static int _num_total_walked_objs;
307   static int _num_total_archived_objs;
308   static int _num_total_recorded_klasses;
309   static int _num_total_verifications;
310 
311   static void start_recording_subgraph(InstanceKlass *k, const char* klass_name,
312                                        bool is_full_module_graph);
313   static void done_recording_subgraph(InstanceKlass *k, const char* klass_name);
314 
315   static bool has_been_seen_during_subgraph_recording(oop obj);
316   static void set_has_been_seen_during_subgraph_recording(oop obj);
317   static bool archive_object(oop obj, oop referrer, KlassSubGraphInfo* subgraph_info);
318 
319   static void resolve_classes_for_subgraphs(JavaThread* current, ArchivableStaticFieldInfo fields[]);
320   static void resolve_classes_for_subgraph_of(JavaThread* current, Klass* k);
321   static void clear_archived_roots_of(Klass* k);
322   static const ArchivedKlassSubGraphInfoRecord*
323                resolve_or_init_classes_for_subgraph_of(Klass* k, bool do_init, TRAPS);
324   static void resolve_or_init(const char* klass_name, bool do_init, TRAPS);
325   static void resolve_or_init(Klass* k, bool do_init, TRAPS);
326   static void init_archived_fields_for(Klass* k, const ArchivedKlassSubGraphInfoRecord* record);
327 
328   static int init_loaded_regions(FileMapInfo* mapinfo, LoadedArchiveHeapRegion* loaded_regions,
329                                  MemRegion& archive_space);
330   static void sort_loaded_regions(LoadedArchiveHeapRegion* loaded_regions, int num_loaded_regions,
331                                   uintptr_t buffer);
332   static bool load_regions(FileMapInfo* mapinfo, LoadedArchiveHeapRegion* loaded_regions,
333                            int num_loaded_regions, uintptr_t buffer);
334   static void init_loaded_heap_relocation(LoadedArchiveHeapRegion* reloc_info,
335                                           int num_loaded_regions);
336   static void fill_failed_loaded_region();
337   static void mark_native_pointers(oop orig_obj);
338   static bool has_been_archived(oop orig_obj);
339   static void prepare_resolved_references();
340   static void archive_strings();
341   static void archive_subgraphs();
342 
343   // PendingOop and PendingOopStack are used for recursively discovering all cacheable
344   // heap objects. The recursion is done using PendingOopStack so we won't overflow the
345   // C stack with deep reference chains.
346   class PendingOop {
347     oop _obj;
348     oop _referrer;
349     int _level;
350 
351   public:
352     PendingOop() : _obj(nullptr), _referrer(nullptr), _level(-1) {}
353     PendingOop(oop obj, oop referrer, int level) : _obj(obj), _referrer(referrer), _level(level) {}
354 
355     oop obj()      const { return _obj; }
356     oop referrer() const { return _referrer; }
357     int level()    const { return _level; }
358   };
359 
360   class OopFieldPusher;
361   using PendingOopStack = GrowableArrayCHeap<PendingOop, mtClassShared>;
362 
363   static PendingOop _object_being_archived;
364   static bool walk_one_object(PendingOopStack* stack, int level, KlassSubGraphInfo* subgraph_info,
365                               oop orig_obj, oop referrer);
366 
367  public:

368   static void reset_archived_object_states(TRAPS);
369   static void create_archived_object_cache() {
370     _archived_object_cache =
371       new (mtClass)ArchivedObjectCache(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE);
372   }
373   static void destroy_archived_object_cache() {
374     delete _archived_object_cache;
375     _archived_object_cache = nullptr;
376   }
377   static ArchivedObjectCache* archived_object_cache() {
378     return _archived_object_cache;
379   }
380 





381   static int archive_exception_instance(oop exception);
382 
383   static bool archive_reachable_objects_from(int level,
384                                              KlassSubGraphInfo* subgraph_info,
385                                              oop orig_obj);
386 
387   static void add_to_dumped_interned_strings(oop string);
388   static bool is_dumped_interned_string(oop o);
389 


390   // Scratch objects for archiving Klass::java_mirror()
391   static void set_scratch_java_mirror(Klass* k, oop mirror);
392   static void remove_scratch_objects(Klass* k);
393   static void get_pointer_info(oop src_obj, bool& has_oop_pointers, bool& has_native_pointers);
394   static void set_has_native_pointers(oop src_obj);
395 
396   // We use the HeapShared::roots() array to make sure that objects stored in the
397   // archived heap region are not prematurely collected. These roots include:
398   //
399   //    - mirrors of classes that have not yet been loaded.
400   //    - ConstantPool::resolved_references() of classes that have not yet been loaded.
401   //    - ArchivedKlassSubGraphInfoRecords that have not been initialized
402   //    - java.lang.Module objects that have not yet been added to the module graph
403   //
404   // When a mirror M becomes referenced by a newly loaded class K, M will be removed
405   // from HeapShared::roots() via clear_root(), and K will be responsible for
406   // keeping M alive.
407   //
408   // Other types of roots are also cleared similarly when they become referenced.
409 
410   // Dump-time only. Returns the index of the root, which can be used at run time to read
411   // the root using get_root(index, ...).
412   static int append_root(oop obj);
413   static GrowableArrayCHeap<oop, mtClassShared>* pending_roots() { return _pending_roots; }
414 
415   // Dump-time and runtime
416   static objArrayOop root_segment(int segment_idx);
417   static oop get_root(int index, bool clear=false);
418 
419   // Run-time only
420   static void clear_root(int index);
421 
422   static void get_segment_indexes(int index, int& segment_index, int& internal_index);
423 
424   static void setup_test_class(const char* test_class_name) PRODUCT_RETURN;
425 #endif // INCLUDE_CDS_JAVA_HEAP
426 
427  public:


428   static void write_heap(ArchiveHeapInfo* heap_info) NOT_CDS_JAVA_HEAP_RETURN;
429   static objArrayOop scratch_resolved_references(ConstantPool* src);
430   static void add_scratch_resolved_references(ConstantPool* src, objArrayOop dest) NOT_CDS_JAVA_HEAP_RETURN;
431   static void init_dumping() NOT_CDS_JAVA_HEAP_RETURN;
432   static void init_scratch_objects_for_basic_type_mirrors(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
433   static void init_box_classes(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
434   static bool is_heap_region(int idx) {
435     CDS_JAVA_HEAP_ONLY(return (idx == MetaspaceShared::hp);)
436     NOT_CDS_JAVA_HEAP_RETURN_(false);
437   }


438 
439   static void resolve_classes(JavaThread* current) NOT_CDS_JAVA_HEAP_RETURN;
440   static void initialize_from_archived_subgraph(JavaThread* current, Klass* k) NOT_CDS_JAVA_HEAP_RETURN;
441 
442   static void init_for_dumping(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
443   static void write_subgraph_info_table() NOT_CDS_JAVA_HEAP_RETURN;
444   static void add_root_segment(objArrayOop segment_oop) NOT_CDS_JAVA_HEAP_RETURN;
445   static void init_root_segment_sizes(int max_size_elems) NOT_CDS_JAVA_HEAP_RETURN;
446   static void serialize_tables(SerializeClosure* soc) NOT_CDS_JAVA_HEAP_RETURN;
447 
448 #ifndef PRODUCT
449   static bool is_a_test_class_in_unnamed_module(Klass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
450   static void initialize_test_class_from_archive(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
451 #endif
452 









453   static void initialize_java_lang_invoke(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
454   static void init_classes_for_special_subgraph(Handle loader, TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
455 
456   static bool is_lambda_form_klass(InstanceKlass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
457   static bool is_lambda_proxy_klass(InstanceKlass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
458   static bool is_string_concat_klass(InstanceKlass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
459   static bool is_archivable_hidden_klass(InstanceKlass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
460 
461   // Used by AOTArtifactFinder
462   static void start_scanning_for_oops();
463   static void end_scanning_for_oops();
464   static void scan_java_class(Klass* k);
465   static void scan_java_mirror(oop orig_mirror);
466   static void copy_and_rescan_aot_inited_mirror(InstanceKlass* ik);
467 };
468 








469 #if INCLUDE_CDS_JAVA_HEAP
470 class DumpedInternedStrings :
471   public ResizeableResourceHashtable<oop, bool,
472                            AnyObj::C_HEAP,
473                            mtClassShared,
474                            HeapShared::string_oop_hash>
475 {
476 public:
477   DumpedInternedStrings(unsigned size, unsigned max_size) :
478     ResizeableResourceHashtable<oop, bool,
479                                 AnyObj::C_HEAP,
480                                 mtClassShared,
481                                 HeapShared::string_oop_hash>(size, max_size) {}
482 };
483 #endif
484 
485 #endif // SHARE_CDS_HEAPSHARED_HPP

  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_CDS_HEAPSHARED_HPP
 26 #define SHARE_CDS_HEAPSHARED_HPP
 27 
 28 #include "cds/cds_globals.hpp"
 29 #include "cds/dumpTimeClassInfo.hpp"
 30 #include "cds/metaspaceShared.hpp"
 31 #include "classfile/compactHashtable.hpp"
 32 #include "classfile/javaClasses.hpp"
 33 #include "gc/shared/gc_globals.hpp"
 34 #include "memory/allocation.hpp"
 35 #include "memory/allStatic.hpp"
 36 #include "oops/compressedOops.hpp"
 37 #include "oops/oop.hpp"
 38 #include "oops/oopHandle.hpp"
 39 #include "oops/oopsHierarchy.hpp"
 40 #include "utilities/growableArray.hpp"
 41 #include "utilities/resourceHash.hpp"
 42 
 43 #if INCLUDE_CDS_JAVA_HEAP
 44 class DumpedInternedStrings;
 45 class FileMapInfo;
 46 class KlassSubGraphInfo;
 47 class MetaspaceObjToOopHandleTable;
 48 class ResourceBitMap;

151   static oop scratch_java_mirror(Klass* k)        NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
152   static oop scratch_java_mirror(oop java_mirror) NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
153   static bool is_archived_boot_layer_available(JavaThread* current) NOT_CDS_JAVA_HEAP_RETURN_(false);
154 
155 private:
156 #if INCLUDE_CDS_JAVA_HEAP
157   static DumpedInternedStrings *_dumped_interned_strings;
158 
159   // statistics
160   constexpr static int ALLOC_STAT_SLOTS = 16;
161   static size_t _alloc_count[ALLOC_STAT_SLOTS];
162   static size_t _alloc_size[ALLOC_STAT_SLOTS];
163   static size_t _total_obj_count;
164   static size_t _total_obj_size; // in HeapWords
165 
166   static void count_allocation(size_t size);
167   static void print_stats();
168 public:
169   static void debug_trace();
170   static unsigned oop_hash(oop const& p);
171   static unsigned oop_handle_hash(OopHandle const& oh);
172   static unsigned oop_handle_hash_raw(OopHandle const& oh);
173   static bool oop_handle_equals(const OopHandle& a, const OopHandle& b);
174   static unsigned string_oop_hash(oop const& string) {
175     return java_lang_String::hash_code(string);
176   }
177 
178   class CopyKlassSubGraphInfoToArchive;
179 
180   class CachedOopInfo {
181     // Used by CDSHeapVerifier.
182     OopHandle _orig_referrer;
183 
184     // The location of this object inside ArchiveHeapWriter::_buffer
185     size_t _buffer_offset;
186 
187     // One or more fields in this object are pointing to non-null oops.
188     bool _has_oop_pointers;
189 
190     // One or more fields in this object are pointing to MetaspaceObj
191     bool _has_native_pointers;
192   public:
193     CachedOopInfo(OopHandle orig_referrer, bool has_oop_pointers)
194       : _orig_referrer(orig_referrer),
195         _buffer_offset(0),
196         _has_oop_pointers(has_oop_pointers),
197         _has_native_pointers(false) {}
198     oop orig_referrer() const;
199     void set_buffer_offset(size_t offset) { _buffer_offset = offset; }
200     size_t buffer_offset()          const { return _buffer_offset;   }
201     bool has_oop_pointers()         const { return _has_oop_pointers; }
202     bool has_native_pointers()      const { return _has_native_pointers; }
203     void set_has_native_pointers()        { _has_native_pointers = true; }
204   };
205 
206 private:
207   static const int INITIAL_TABLE_SIZE = 15889; // prime number
208   static const int MAX_TABLE_SIZE     = 1000000;
209   typedef ResizeableResourceHashtable<OopHandle, CachedOopInfo,
210       AnyObj::C_HEAP,
211       mtClassShared,
212       HeapShared::oop_handle_hash_raw,
213       HeapShared::oop_handle_equals> ArchivedObjectCache;
214   static ArchivedObjectCache* _archived_object_cache;
215 
216   class DumpTimeKlassSubGraphInfoTable
217     : public ResourceHashtable<Klass*, KlassSubGraphInfo,
218                                137, // prime number
219                                AnyObj::C_HEAP,
220                                mtClassShared,
221                                DumpTimeSharedClassTable_hash> {
222   public:
223     int _count;
224   };
225 
226 public: // solaris compiler wants this for RunTimeKlassSubGraphInfoTable
227   inline static bool record_equals_compact_hashtable_entry(
228        const ArchivedKlassSubGraphInfoRecord* value, const Klass* key, int len_unused) {
229     return (value->klass() == key);
230   }
231 
232 private:
233   typedef OffsetCompactHashtable<

258 
259   static KlassSubGraphInfo* init_subgraph_info(Klass *k, bool is_full_module_graph);
260   static KlassSubGraphInfo* get_subgraph_info(Klass *k);
261 
262   static void init_subgraph_entry_fields(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
263   static void init_subgraph_entry_fields(ArchivableStaticFieldInfo fields[], TRAPS);
264 
265   // UseCompressedOops only: Used by decode_from_archive
266   static address _narrow_oop_base;
267   static int     _narrow_oop_shift;
268 
269   // !UseCompressedOops only: used to relocate pointers to the archived objects
270   static ptrdiff_t _runtime_delta;
271 
272   typedef ResizeableResourceHashtable<oop, bool,
273       AnyObj::C_HEAP,
274       mtClassShared,
275       HeapShared::oop_hash> SeenObjectsTable;
276 
277   static SeenObjectsTable *_seen_objects_table;

278   // The "special subgraph" contains all the archived objects that are reachable
279   // from the following roots:
280   //    - interned strings
281   //    - Klass::java_mirror() -- including aot-initialized mirrors such as those of Enum klasses.
282   //    - ConstantPool::resolved_references()
283   //    - Universe::<xxx>_exception_instance()
284   static KlassSubGraphInfo* _dump_time_special_subgraph;              // for collecting info during dump time
285   static ArchivedKlassSubGraphInfoRecord* _run_time_special_subgraph; // for initializing classes during run time.
286 
287   static GrowableArrayCHeap<OopHandle, mtClassShared>* _pending_roots;
288   static GrowableArrayCHeap<OopHandle, mtClassShared>* _root_segments;
289   static GrowableArrayCHeap<const char*, mtClassShared>* _context; // for debugging unarchivable objects
290   static int _root_segment_max_size_elems;
291   static OopHandle _scratch_basic_type_mirrors[T_VOID+1];
292   static MetaspaceObjToOopHandleTable* _scratch_objects_table;
293 
294   static void init_seen_objects_table() {
295     assert(_seen_objects_table == nullptr, "must be");
296     _seen_objects_table = new (mtClass)SeenObjectsTable(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE);
297   }
298   static void delete_seen_objects_table() {
299     assert(_seen_objects_table != nullptr, "must be");
300     delete _seen_objects_table;
301     _seen_objects_table = nullptr;
302   }
303 
304   class ContextMark;
305 
306   // Statistics (for one round of start_recording_subgraph ... done_recording_subgraph)
307   static int _num_new_walked_objs;
308   static int _num_new_archived_objs;
309   static int _num_old_recorded_klasses;
310 
311   // Statistics (for all archived subgraphs)
312   static int _num_total_subgraph_recordings;
313   static int _num_total_walked_objs;
314   static int _num_total_archived_objs;
315   static int _num_total_recorded_klasses;
316   static int _num_total_verifications;
317 
318   static void start_recording_subgraph(InstanceKlass *k, const char* klass_name,
319                                        bool is_full_module_graph);
320   static void done_recording_subgraph(InstanceKlass *k, const char* klass_name);
321 
322   static bool has_been_seen_during_subgraph_recording(oop obj);
323   static void set_has_been_seen_during_subgraph_recording(oop obj);
324   static bool archive_object(oop obj, oop referrer, KlassSubGraphInfo* subgraph_info);
325 
326   static void resolve_classes_for_subgraphs(JavaThread* current, ArchivableStaticFieldInfo fields[]);
327   static void resolve_classes_for_subgraph_of(JavaThread* current, Klass* k);
328   static void clear_archived_roots_of(Klass* k);
329   static const ArchivedKlassSubGraphInfoRecord*
330                resolve_or_init_classes_for_subgraph_of(Klass* k, bool do_init, TRAPS);
331   static void resolve_or_init(const char* klass_name, bool do_init, TRAPS);
332   static void resolve_or_init(Klass* k, bool do_init, TRAPS);
333   static void init_archived_fields_for(Klass* k, const ArchivedKlassSubGraphInfoRecord* record);
334 
335   static int init_loaded_regions(FileMapInfo* mapinfo, LoadedArchiveHeapRegion* loaded_regions,
336                                  MemRegion& archive_space);
337   static void sort_loaded_regions(LoadedArchiveHeapRegion* loaded_regions, int num_loaded_regions,
338                                   uintptr_t buffer);
339   static bool load_regions(FileMapInfo* mapinfo, LoadedArchiveHeapRegion* loaded_regions,
340                            int num_loaded_regions, uintptr_t buffer);
341   static void init_loaded_heap_relocation(LoadedArchiveHeapRegion* reloc_info,
342                                           int num_loaded_regions);
343   static void fill_failed_loaded_region();
344   static void mark_native_pointers(oop orig_obj);

345   static void prepare_resolved_references();
346   static void archive_strings();
347   static void archive_subgraphs();
348 
349   // PendingOop and PendingOopStack are used for recursively discovering all cacheable
350   // heap objects. The recursion is done using PendingOopStack so we won't overflow the
351   // C stack with deep reference chains.
352   class PendingOop {
353     oop _obj;
354     oop _referrer;
355     int _level;
356 
357   public:
358     PendingOop() : _obj(nullptr), _referrer(nullptr), _level(-1) {}
359     PendingOop(oop obj, oop referrer, int level) : _obj(obj), _referrer(referrer), _level(level) {}
360 
361     oop obj()      const { return _obj; }
362     oop referrer() const { return _referrer; }
363     int level()    const { return _level; }
364   };
365 
366   class OopFieldPusher;
367   using PendingOopStack = GrowableArrayCHeap<PendingOop, mtClassShared>;
368 
369   static PendingOop _object_being_archived;
370   static bool walk_one_object(PendingOopStack* stack, int level, KlassSubGraphInfo* subgraph_info,
371                               oop orig_obj, oop referrer);
372 
373  public:
374   static void exit_on_error();
375   static void reset_archived_object_states(TRAPS);
376   static void create_archived_object_cache() {
377     _archived_object_cache =
378       new (mtClass)ArchivedObjectCache(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE);
379   }
380   static void destroy_archived_object_cache() {
381     delete _archived_object_cache;
382     _archived_object_cache = nullptr;
383   }
384   static ArchivedObjectCache* archived_object_cache() {
385     return _archived_object_cache;
386   }
387 
388   static CachedOopInfo* get_cached_oop_info(oop orig_obj) {
389     OopHandle oh(&orig_obj);
390     return _archived_object_cache->get(oh);
391   }
392 
393   static int archive_exception_instance(oop exception);
394 
395   static bool archive_reachable_objects_from(int level,
396                                              KlassSubGraphInfo* subgraph_info,
397                                              oop orig_obj);
398 
399   static void add_to_dumped_interned_strings(oop string);
400   static bool is_dumped_interned_string(oop o);
401 
402   static void track_scratch_object(oop orig_obj, oop scratch_obj);
403 
404   // Scratch objects for archiving Klass::java_mirror()
405   static void set_scratch_java_mirror(Klass* k, oop mirror);
406   static void remove_scratch_objects(Klass* k);
407   static void get_pointer_info(oop src_obj, bool& has_oop_pointers, bool& has_native_pointers);
408   static void set_has_native_pointers(oop src_obj);
409 
410   // We use the HeapShared::roots() array to make sure that objects stored in the
411   // archived heap region are not prematurely collected. These roots include:
412   //
413   //    - mirrors of classes that have not yet been loaded.
414   //    - ConstantPool::resolved_references() of classes that have not yet been loaded.
415   //    - ArchivedKlassSubGraphInfoRecords that have not been initialized
416   //    - java.lang.Module objects that have not yet been added to the module graph
417   //
418   // When a mirror M becomes referenced by a newly loaded class K, M will be removed
419   // from HeapShared::roots() via clear_root(), and K will be responsible for
420   // keeping M alive.
421   //
422   // Other types of roots are also cleared similarly when they become referenced.
423 
424   // Dump-time only. Returns the index of the root, which can be used at run time to read
425   // the root using get_root(index, ...).
426   static int append_root(oop obj);
427   static GrowableArrayCHeap<OopHandle, mtClassShared>* pending_roots() { return _pending_roots; }
428 
429   // Dump-time and runtime
430   static objArrayOop root_segment(int segment_idx);
431   static oop get_root(int index, bool clear=false);
432 
433   // Run-time only
434   static void clear_root(int index);

435   static void get_segment_indexes(int index, int& segment_index, int& internal_index);

436   static void setup_test_class(const char* test_class_name) PRODUCT_RETURN;
437 #endif // INCLUDE_CDS_JAVA_HEAP
438 
439  public:
440   static bool has_been_archived(oop orig_obj);
441   static oop orig_to_scratch_object(oop orig_obj);
442   static void write_heap(ArchiveHeapInfo* heap_info) NOT_CDS_JAVA_HEAP_RETURN;
443   static objArrayOop scratch_resolved_references(ConstantPool* src);
444   static void add_scratch_resolved_references(ConstantPool* src, objArrayOop dest) NOT_CDS_JAVA_HEAP_RETURN;
445   static void init_dumping() NOT_CDS_JAVA_HEAP_RETURN;
446   static void init_scratch_objects_for_basic_type_mirrors(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
447   static void init_box_classes(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
448   static bool is_heap_region(int idx) {
449     CDS_JAVA_HEAP_ONLY(return (idx == MetaspaceShared::hp);)
450     NOT_CDS_JAVA_HEAP_RETURN_(false);
451   }
452   static void rehash_archived_object_cache() NOT_CDS_JAVA_HEAP_RETURN;
453   static void delete_tables_with_raw_oops() NOT_CDS_JAVA_HEAP_RETURN;
454 
455   static void resolve_classes(JavaThread* current) NOT_CDS_JAVA_HEAP_RETURN;
456   static void initialize_from_archived_subgraph(JavaThread* current, Klass* k) NOT_CDS_JAVA_HEAP_RETURN;
457 
458   static void init_for_dumping(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
459   static void write_subgraph_info_table() NOT_CDS_JAVA_HEAP_RETURN;
460   static void add_root_segment(objArrayOop segment_oop) NOT_CDS_JAVA_HEAP_RETURN;
461   static void init_root_segment_sizes(int max_size_elems) NOT_CDS_JAVA_HEAP_RETURN;
462   static void serialize_tables(SerializeClosure* soc) NOT_CDS_JAVA_HEAP_RETURN;
463 
464 #ifndef PRODUCT
465   static bool is_a_test_class_in_unnamed_module(Klass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
466   static void initialize_test_class_from_archive(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
467 #endif
468 
469   static void add_to_permanent_oop_table(oop obj, int offset);
470 
471   // AOT-compile time only: get a stable index for an archived object.
472   // Returns 0 if obj is not archived.
473   static int get_archived_object_permanent_index(oop obj) NOT_CDS_JAVA_HEAP_RETURN_(-1);
474   // Runtime only: get back the same object for an index returned by
475   // get_archived_object_permanent_index().
476   static oop get_archived_object(int permanent_index) NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
477 
478   static void initialize_java_lang_invoke(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
479   static void init_classes_for_special_subgraph(Handle loader, TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
480 
481   static bool is_lambda_form_klass(InstanceKlass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
482   static bool is_lambda_proxy_klass(InstanceKlass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
483   static bool is_string_concat_klass(InstanceKlass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
484   static bool is_archivable_hidden_klass(InstanceKlass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
485 
486   // Used by AOTArtifactFinder
487   static void start_scanning_for_oops();
488   static void end_scanning_for_oops();
489   static void scan_java_class(Klass* k);
490   static void scan_java_mirror(oop orig_mirror);
491   static void copy_and_rescan_aot_inited_mirror(InstanceKlass* ik);
492 };
493 
494 class CachedCodeDirectoryInternal {
495   int _permanent_oop_count;
496   int* _permanent_oop_offsets; // offset of each permanent object from the bottom of the archived heap
497 public:
498   void dumptime_init_internal() NOT_CDS_JAVA_HEAP_RETURN;
499   void runtime_init_internal() NOT_CDS_JAVA_HEAP_RETURN;
500 };
501 
502 #if INCLUDE_CDS_JAVA_HEAP
503 class DumpedInternedStrings :
504   public ResizeableResourceHashtable<oop, bool,
505                            AnyObj::C_HEAP,
506                            mtClassShared,
507                            HeapShared::string_oop_hash>
508 {
509 public:
510   DumpedInternedStrings(unsigned size, unsigned max_size) :
511     ResizeableResourceHashtable<oop, bool,
512                                 AnyObj::C_HEAP,
513                                 mtClassShared,
514                                 HeapShared::string_oop_hash>(size, max_size) {}
515 };
516 #endif
517 
518 #endif // SHARE_CDS_HEAPSHARED_HPP
< prev index next >