--- old/make/hotspot/lib/JvmFeatures.gmk 2020-07-31 23:33:50.687913535 -0700 +++ new/make/hotspot/lib/JvmFeatures.gmk 2020-07-31 23:33:50.495906307 -0700 @@ -116,6 +116,8 @@ ifneq ($(call check-jvm-feature, cds), true) JVM_CFLAGS_FEATURES += -DINCLUDE_CDS=0 JVM_EXCLUDE_FILES += \ + archiveBuilder.cpp \ + archiveUtils.cpp \ classListParser.cpp \ classLoaderExt.cpp \ dynamicArchive.cpp \ @@ -126,8 +128,7 @@ metaspaceShared_$(HOTSPOT_TARGET_CPU_ARCH).cpp \ sharedClassUtil.cpp \ sharedPathsMiscInfo.cpp \ - systemDictionaryShared.cpp \ - # + systemDictionaryShared.cpp endif ifneq ($(call check-jvm-feature, nmt), true) --- old/src/hotspot/share/classfile/symbolTable.cpp 2020-07-31 23:33:51.271935519 -0700 +++ new/src/hotspot/share/classfile/symbolTable.cpp 2020-07-31 23:33:51.075928141 -0700 @@ -513,13 +513,6 @@ } assert((sym == NULL) || sym->refcount() != 0, "found dead symbol"); -#if INCLUDE_CDS - if (DumpSharedSpaces) { - if (sym != NULL) { - MetaspaceShared::add_symbol(sym); - } - } -#endif return sym; } --- old/src/hotspot/share/memory/archiveUtils.cpp 2020-07-31 23:33:51.859957653 -0700 +++ new/src/hotspot/share/memory/archiveUtils.cpp 2020-07-31 23:33:51.663950275 -0700 @@ -72,11 +72,23 @@ } assert(idx < _ptrmap->size(), "must be"); _ptrmap->set_bit(idx); - //tty->print_cr("Marking pointer [%p] -> %p @ " SIZE_FORMAT_W(9), ptr_loc, *ptr_loc, idx); + //tty->print_cr("Marking pointer [" PTR_FORMAT "] -> " PTR_FORMAT " @ " SIZE_FORMAT_W(5), p2i(ptr_loc), p2i(*ptr_loc), idx); } } } +void ArchivePtrMarker::clear_pointer(address* ptr_loc) { + assert(_ptrmap != NULL, "not initialized"); + assert(!_compacted, "cannot clear anymore"); + + assert(_ptr_base <= ptr_loc && ptr_loc < _ptr_end, "must be"); + assert(uintx(ptr_loc) % sizeof(intptr_t) == 0, "pointers must be stored in aligned addresses"); + size_t idx = ptr_loc - _ptr_base; + assert(idx < _ptrmap->size(), "cannot clear pointers that have not been marked"); + _ptrmap->clear_bit(idx); + //tty->print_cr("Clearing pointer [" PTR_FORMAT "] -> " PTR_FORMAT " @ " SIZE_FORMAT_W(5), p2i(ptr_loc), p2i(*ptr_loc), idx); +} + class ArchivePtrBitmapCleaner: public BitMapClosure { CHeapBitMap* _ptrmap; address* _ptr_base; --- old/src/hotspot/share/memory/archiveUtils.hpp 2020-07-31 23:33:52.439979486 -0700 +++ new/src/hotspot/share/memory/archiveUtils.hpp 2020-07-31 23:33:52.243972108 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -44,6 +44,7 @@ public: static void initialize(CHeapBitMap* ptrmap, address* ptr_base, address* ptr_end); static void mark_pointer(address* ptr_loc); + static void clear_pointer(address* ptr_loc); static void compact(address relocatable_base, address relocatable_end); static void compact(size_t max_non_null_offset); @@ -52,6 +53,12 @@ mark_pointer((address*)ptr_loc); } + template + static void set_and_mark_pointer(T* ptr_loc, T ptr_value) { + *ptr_loc = ptr_value; + mark_pointer(ptr_loc); + } + static void expand_ptr_end(address *new_ptr_end) { assert(_ptr_end <= new_ptr_end, "must be"); _ptr_end = new_ptr_end; --- old/src/hotspot/share/memory/archiveUtils.inline.hpp 2020-07-31 23:33:53.028001620 -0700 +++ new/src/hotspot/share/memory/archiveUtils.inline.hpp 2020-07-31 23:33:52.831994242 -0700 @@ -34,7 +34,11 @@ assert(_patch_base <= p && p < _patch_end, "must be"); address old_ptr = *p; - assert(_valid_old_base <= old_ptr && old_ptr < _valid_old_end, "must be"); + if (old_ptr == NULL) { + assert(COMPACTING, "NULL pointers should not be marked when relocating at run-time"); + } else { + assert(_valid_old_base <= old_ptr && old_ptr < _valid_old_end, "must be"); + } if (COMPACTING) { // Start-up performance: use a template parameter to elide this block for run-time archive --- old/src/hotspot/share/memory/dynamicArchive.cpp 2020-07-31 23:33:53.604023302 -0700 +++ new/src/hotspot/share/memory/dynamicArchive.cpp 2020-07-31 23:33:53.408015924 -0700 @@ -26,46 +26,25 @@ #include "jvm.h" #include "classfile/classLoaderData.inline.hpp" #include "classfile/symbolTable.hpp" -#include "classfile/systemDictionary.hpp" #include "classfile/systemDictionaryShared.hpp" #include "logging/log.hpp" +#include "memory/archiveBuilder.hpp" #include "memory/archiveUtils.inline.hpp" #include "memory/dynamicArchive.hpp" -#include "memory/metadataFactory.hpp" -#include "memory/metaspace.hpp" #include "memory/metaspaceClosure.hpp" #include "memory/metaspaceShared.hpp" #include "memory/resourceArea.hpp" -#include "oops/compressedOops.hpp" -#include "oops/objArrayKlass.hpp" -#include "prims/jvmtiRedefineClasses.hpp" -#include "runtime/handles.inline.hpp" #include "runtime/os.inline.hpp" #include "runtime/sharedRuntime.hpp" #include "runtime/vmThread.hpp" #include "runtime/vmOperations.hpp" +#include "utilities/align.hpp" #include "utilities/bitMap.inline.hpp" -#ifndef O_BINARY // if defined (Win32) use binary files. -#define O_BINARY 0 // otherwise do nothing. -#endif - -class DynamicArchiveBuilder : ResourceObj { - static unsigned my_hash(const address& a) { - return primitive_hash
(a); - } - static bool my_equals(const address& a0, const address& a1) { - return primitive_equals
(a0, a1); - } - typedef ResourceHashtable< - address, address, - DynamicArchiveBuilder::my_hash, // solaris compiler doesn't like: primitive_hash
- DynamicArchiveBuilder::my_equals, // solaris compiler doesn't like: primitive_equals
- 16384, ResourceObj::C_HEAP> RelocationTable; - RelocationTable _new_loc_table; +class DynamicArchiveBuilder : public ArchiveBuilder { +public: static intx _buffer_to_target_delta; - DumpRegion* _current_dump_space; static size_t reserve_alignment() { @@ -106,23 +85,8 @@ return (T)(address(obj) + _buffer_to_target_delta); } - template T get_new_loc(T obj) { - address* pp = _new_loc_table.get((address)obj); - if (pp == NULL) { - // Excluded klasses are not copied - return NULL; - } else { - return (T)*pp; - } - } - - address get_new_loc(MetaspaceClosure::Ref* ref) { - return get_new_loc(ref->obj()); - } - - template bool has_new_loc(T obj) { - address* pp = _new_loc_table.get((address)obj); - return pp != NULL; + template T get_dumped_addr(T obj) { + return (T)ArchiveBuilder::get_dumped_addr((address)obj); } static int dynamic_dump_method_comparator(Method* a, Method* b) { @@ -147,345 +111,13 @@ return a_name->fast_compare(b_name); } -protected: - enum FollowMode { - make_a_copy, point_to_it, set_to_null - }; - public: - void copy(MetaspaceClosure::Ref* ref, bool read_only) { - int bytes = ref->size() * BytesPerWord; - address old_obj = ref->obj(); - address new_obj = copy_impl(ref, read_only, bytes); - - assert(new_obj != NULL, "must be"); - assert(new_obj != old_obj, "must be"); - bool isnew = _new_loc_table.put(old_obj, new_obj); - assert(isnew, "must be"); - } - - // Make a shallow copy of each eligible MetaspaceObj into the buffer. - class ShallowCopier: public UniqueMetaspaceClosure { - DynamicArchiveBuilder* _builder; - bool _read_only; - public: - ShallowCopier(DynamicArchiveBuilder* shuffler, bool read_only) - : _builder(shuffler), _read_only(read_only) {} - - virtual bool do_unique_ref(Ref* orig_obj, bool read_only) { - // This method gets called on each *original* object - // reachable from _builder->iterate_roots(). Each orig_obj is - // called exactly once. - FollowMode mode = _builder->follow_ref(orig_obj); - - if (mode == point_to_it) { - if (read_only == _read_only) { - log_debug(cds, dynamic)("ptr : " PTR_FORMAT " %s", p2i(orig_obj->obj()), - MetaspaceObj::type_name(orig_obj->msotype())); - address p = orig_obj->obj(); - bool isnew = _builder->_new_loc_table.put(p, p); - assert(isnew, "must be"); - } - return false; - } - - if (mode == set_to_null) { - log_debug(cds, dynamic)("nul : " PTR_FORMAT " %s", p2i(orig_obj->obj()), - MetaspaceObj::type_name(orig_obj->msotype())); - return false; - } - - if (read_only == _read_only) { - // Make a shallow copy of orig_obj in a buffer (maintained - // by copy_impl in a subclass of DynamicArchiveBuilder). - _builder->copy(orig_obj, read_only); - } - return true; - } - }; - - // Relocate all embedded pointer fields within a MetaspaceObj's shallow copy - class ShallowCopyEmbeddedRefRelocator: public UniqueMetaspaceClosure { - DynamicArchiveBuilder* _builder; - public: - ShallowCopyEmbeddedRefRelocator(DynamicArchiveBuilder* shuffler) - : _builder(shuffler) {} - - // This method gets called on each *original* object reachable - // from _builder->iterate_roots(). Each orig_obj is - // called exactly once. - virtual bool do_unique_ref(Ref* orig_ref, bool read_only) { - FollowMode mode = _builder->follow_ref(orig_ref); - - if (mode == point_to_it) { - // We did not make a copy of this object - // and we have nothing to update - assert(_builder->get_new_loc(orig_ref) == NULL || - _builder->get_new_loc(orig_ref) == orig_ref->obj(), "must be"); - return false; - } - - if (mode == set_to_null) { - // We did not make a copy of this object - // and we have nothing to update - assert(!_builder->has_new_loc(orig_ref->obj()), "must not be copied or pointed to"); - return false; - } - - // - orig_obj points to the original object. - // - new_obj points to the shallow copy (created by ShallowCopier) - // of orig_obj. new_obj is NULL if the orig_obj is excluded - address orig_obj = orig_ref->obj(); - address new_obj = _builder->get_new_loc(orig_ref); - - assert(new_obj != orig_obj, "must be"); -#ifdef ASSERT - if (new_obj == NULL) { - if (orig_ref->msotype() == MetaspaceObj::ClassType) { - Klass* k = (Klass*)orig_obj; - assert(k->is_instance_klass() && - SystemDictionaryShared::is_excluded_class(InstanceKlass::cast(k)), - "orig_obj must be excluded Class"); - } - } -#endif - - log_debug(cds, dynamic)("Relocating " PTR_FORMAT " %s", p2i(new_obj), - MetaspaceObj::type_name(orig_ref->msotype())); - if (new_obj != NULL) { - EmbeddedRefUpdater updater(_builder, orig_obj, new_obj); - orig_ref->metaspace_pointers_do(&updater); - } - - return true; // keep recursing until every object is visited exactly once. - } - - virtual void push_special(SpecialRef type, Ref* ref, intptr_t* p) { - assert(type == _method_entry_ref, "only special type allowed for now"); - address obj = ref->obj(); - address new_obj = _builder->get_new_loc(ref); - size_t offset = pointer_delta(p, obj, sizeof(u1)); - intptr_t* new_p = (intptr_t*)(new_obj + offset); - assert(*p == *new_p, "must be a copy"); - ArchivePtrMarker::mark_pointer((address*)new_p); - } - }; - - class EmbeddedRefUpdater: public MetaspaceClosure { - DynamicArchiveBuilder* _builder; - address _orig_obj; - address _new_obj; - public: - EmbeddedRefUpdater(DynamicArchiveBuilder* shuffler, address orig_obj, address new_obj) : - _builder(shuffler), _orig_obj(orig_obj), _new_obj(new_obj) {} - - // This method gets called once for each pointer field F of orig_obj. - // We update new_obj->F to point to the new location of orig_obj->F. - // - // Example: Klass* 0x100 is copied to 0x400 - // Symbol* 0x200 is copied to 0x500 - // - // Let orig_obj == 0x100; and - // new_obj == 0x400; and - // ((Klass*)orig_obj)->_name == 0x200; - // Then this function effectively assigns - // ((Klass*)new_obj)->_name = 0x500; - virtual bool do_ref(Ref* ref, bool read_only) { - address new_pointee = NULL; - - if (ref->not_null()) { - address old_pointee = ref->obj(); - - FollowMode mode = _builder->follow_ref(ref); - if (mode == point_to_it) { - new_pointee = old_pointee; - } else if (mode == set_to_null) { - new_pointee = NULL; - } else { - new_pointee = _builder->get_new_loc(old_pointee); - } - } - - const char* kind = MetaspaceObj::type_name(ref->msotype()); - // offset of this field inside the original object - intx offset = (address)ref->addr() - _orig_obj; - _builder->update_pointer((address*)(_new_obj + offset), new_pointee, kind, offset); - - // We can't mark the pointer here, because DynamicArchiveBuilder::sort_methods - // may re-layout the [iv]tables, which would change the offset(s) in an InstanceKlass - // that would contain pointers. Therefore, we must mark the pointers after - // sort_methods(), using PointerMarker. - return false; // Do not recurse. - } - }; - - class ExternalRefUpdater: public MetaspaceClosure { - DynamicArchiveBuilder* _builder; - - public: - ExternalRefUpdater(DynamicArchiveBuilder* shuffler) : _builder(shuffler) {} - - virtual bool do_ref(Ref* ref, bool read_only) { - // ref is a pointer that lives OUTSIDE of the buffer, but points to an object inside the buffer - if (ref->not_null()) { - address new_loc = _builder->get_new_loc(ref); - const char* kind = MetaspaceObj::type_name(ref->msotype()); - _builder->update_pointer(ref->addr(), new_loc, kind, 0); - _builder->mark_pointer(ref->addr()); - } - return false; // Do not recurse. - } - }; - - class PointerMarker: public UniqueMetaspaceClosure { - DynamicArchiveBuilder* _builder; - - public: - PointerMarker(DynamicArchiveBuilder* shuffler) : _builder(shuffler) {} - - virtual bool do_unique_ref(Ref* ref, bool read_only) { - if (_builder->is_in_buffer_space(ref->obj())) { - EmbeddedRefMarker ref_marker(_builder); - ref->metaspace_pointers_do(&ref_marker); - return true; // keep recursing until every buffered object is visited exactly once. - } else { - return false; - } - } - }; - - class EmbeddedRefMarker: public MetaspaceClosure { - DynamicArchiveBuilder* _builder; - - public: - EmbeddedRefMarker(DynamicArchiveBuilder* shuffler) : _builder(shuffler) {} - virtual bool do_ref(Ref* ref, bool read_only) { - if (ref->not_null()) { - _builder->mark_pointer(ref->addr()); - } - return false; // Do not recurse. - } - }; - - void update_pointer(address* addr, address value, const char* kind, uintx offset, bool is_mso_pointer=true) { - // Propagate the the mask bits to the new value -- see comments above MetaspaceClosure::obj() - if (is_mso_pointer) { - const uintx FLAG_MASK = 0x03; - uintx mask_bits = uintx(*addr) & FLAG_MASK; - value = (address)(uintx(value) | mask_bits); - } - - if (*addr != value) { - log_debug(cds, dynamic)("Update (%18s*) %3d [" PTR_FORMAT "] " PTR_FORMAT " -> " PTR_FORMAT, - kind, int(offset), p2i(addr), p2i(*addr), p2i(value)); - *addr = value; - } - } - -private: - GrowableArray* _symbols; // symbols to dump - GrowableArray* _klasses; // klasses to dump - - void append(InstanceKlass* k) { _klasses->append(k); } - void append(Symbol* s) { _symbols->append(s); } - - class GatherKlassesAndSymbols : public UniqueMetaspaceClosure { - DynamicArchiveBuilder* _builder; - bool _read_only; - - public: - GatherKlassesAndSymbols(DynamicArchiveBuilder* builder) - : _builder(builder) {} - - virtual bool do_unique_ref(Ref* ref, bool read_only) { - if (_builder->follow_ref(ref) != make_a_copy) { - return false; - } - if (ref->msotype() == MetaspaceObj::ClassType) { - Klass* klass = (Klass*)ref->obj(); - assert(klass->is_klass(), "must be"); - if (klass->is_instance_klass()) { - InstanceKlass* ik = InstanceKlass::cast(klass); - assert(!SystemDictionaryShared::is_excluded_class(ik), "must be"); - _builder->append(ik); - _builder->_estimated_metsapceobj_bytes += BytesPerWord; // See RunTimeSharedClassInfo::get_for() - } - } else if (ref->msotype() == MetaspaceObj::SymbolType) { - _builder->append((Symbol*)ref->obj()); - } - - int bytes = ref->size() * BytesPerWord; - _builder->_estimated_metsapceobj_bytes += bytes; - - return true; - } - }; - - FollowMode follow_ref(MetaspaceClosure::Ref *ref) { - address obj = ref->obj(); - if (MetaspaceShared::is_in_shared_metaspace(obj)) { - // Don't dump existing shared metadata again. - return point_to_it; - } else if (ref->msotype() == MetaspaceObj::MethodDataType) { - return set_to_null; - } else { - if (ref->msotype() == MetaspaceObj::ClassType) { - Klass* klass = (Klass*)ref->obj(); - assert(klass->is_klass(), "must be"); - if (klass->is_instance_klass()) { - InstanceKlass* ik = InstanceKlass::cast(klass); - if (SystemDictionaryShared::is_excluded_class(ik)) { - ResourceMark rm; - log_debug(cds, dynamic)("Skipping class (excluded): %s", klass->external_name()); - return set_to_null; - } - } else if (klass->is_array_klass()) { - // Don't support archiving of array klasses for now. - ResourceMark rm; - log_debug(cds, dynamic)("Skipping class (array): %s", klass->external_name()); - return set_to_null; - } - } - - return make_a_copy; - } - } - - address copy_impl(MetaspaceClosure::Ref* ref, bool read_only, int bytes) { - if (ref->msotype() == MetaspaceObj::ClassType) { - // Save a pointer immediate in front of an InstanceKlass, so - // we can do a quick lookup from InstanceKlass* -> RunTimeSharedClassInfo* - // without building another hashtable. See RunTimeSharedClassInfo::get_for() - // in systemDictionaryShared.cpp. - address obj = ref->obj(); - Klass* klass = (Klass*)obj; - if (klass->is_instance_klass()) { - SystemDictionaryShared::validate_before_archiving(InstanceKlass::cast(klass)); - current_dump_space()->allocate(sizeof(address), BytesPerWord); - } - } - address p = (address)current_dump_space()->allocate(bytes); - address obj = ref->obj(); - log_debug(cds, dynamic)("COPY: " PTR_FORMAT " ==> " PTR_FORMAT " %5d %s", - p2i(obj), p2i(p), bytes, - MetaspaceObj::type_name(ref->msotype())); - memcpy(p, obj, bytes); - intptr_t* archived_vtable = MetaspaceShared::get_archived_cpp_vtable(ref->msotype(), p); - if (archived_vtable != NULL) { - update_pointer((address*)p, (address)archived_vtable, "vtb", 0, /*is_mso_pointer*/false); - mark_pointer((address*)p); - } - - return (address)p; - } - DynamicArchiveHeader *_header; address _alloc_bottom; address _last_verified_top; size_t _other_region_used_bytes; // Conservative estimate for number of bytes needed for: - size_t _estimated_metsapceobj_bytes; // all archived MetsapceObj's. size_t _estimated_hashtable_bytes; // symbol table and dictionaries size_t _estimated_trampoline_bytes; // method entry trampolines @@ -498,7 +130,7 @@ void make_trampolines(); void make_klasses_shareable(); void sort_methods(InstanceKlass* ik) const; - void set_symbols_permanent(); + void remark_pointers_for_instance_klass(InstanceKlass* k, bool should_mark) const; void relocate_buffer_to_target(); void write_archive(char* serialized_data); @@ -520,11 +152,7 @@ } public: - DynamicArchiveBuilder() { - _klasses = new (ResourceObj::C_HEAP, mtClass) GrowableArray(100, mtClass); - _symbols = new (ResourceObj::C_HEAP, mtClass) GrowableArray(1000, mtClass); - - _estimated_metsapceobj_bytes = 0; + DynamicArchiveBuilder() : ArchiveBuilder(NULL, NULL) { _estimated_hashtable_bytes = 0; _estimated_trampoline_bytes = 0; @@ -572,19 +200,11 @@ DEBUG_ONLY(SystemDictionaryShared::NoClassLoadingMark nclm); SystemDictionaryShared::check_excluded_classes(); - { - ResourceMark rm; - GatherKlassesAndSymbols gatherer(this); - - SystemDictionaryShared::dumptime_classes_do(&gatherer); - SymbolTable::metaspace_pointers_do(&gatherer); - FileMapInfo::metaspace_pointers_do(&gatherer); - - gatherer.finish(); - } + gather_klasses_and_symbols(); // rw space starts ... address reserved_bottom = reserve_space_and_init_buffer_to_target_delta(); + set_dump_regions(MetaspaceShared::read_write_dump_space(), MetaspaceShared::read_only_dump_space()); init_header(reserved_bottom); CHeapBitMap ptrmap; @@ -593,54 +213,26 @@ reserve_buffers_for_trampolines(); verify_estimate_size(_estimated_trampoline_bytes, "Trampolines"); + gather_source_objs(); start_dump_space(MetaspaceShared::read_write_dump_space()); log_info(cds, dynamic)("Copying %d klasses and %d symbols", - _klasses->length(), _symbols->length()); + klasses()->length(), symbols()->length()); - { - assert(current_dump_space() == MetaspaceShared::read_write_dump_space(), - "Current dump space is not rw space"); - // shallow-copy RW objects, if necessary - ResourceMark rm; - ShallowCopier rw_copier(this, false); - iterate_roots(&rw_copier); - } + dump_rw_region(); // ro space starts ... DumpRegion* ro_space = MetaspaceShared::read_only_dump_space(); - { - start_dump_space(ro_space); - - // shallow-copy RO objects, if necessary - ResourceMark rm; - ShallowCopier ro_copier(this, true); - iterate_roots(&ro_copier); - } - - { - log_info(cds)("Relocating embedded pointers ... "); - ResourceMark rm; - ShallowCopyEmbeddedRefRelocator emb_reloc(this); - iterate_roots(&emb_reloc); - } - - { - log_info(cds)("Relocating external roots ... "); - ResourceMark rm; - ExternalRefUpdater ext_reloc(this); - iterate_roots(&ext_reloc); - } + start_dump_space(ro_space); + dump_ro_region(); + relocate_pointers(); verify_estimate_size(_estimated_metsapceobj_bytes, "MetaspaceObjs"); char* serialized_data; { - set_symbols_permanent(); - // Write the symbol table and system dictionaries to the RO space. - // Note that these tables still point to the *original* objects - // (because they were not processed by ExternalRefUpdater), so + // Note that these tables still point to the *original* objects, so // they would need to call DynamicArchive::original_to_target() to // get the correct addresses. assert(current_dump_space() == ro_space, "Must be RO space"); @@ -656,20 +248,15 @@ verify_estimate_size(_estimated_hashtable_bytes, "Hashtables"); make_trampolines(); + + log_info(cds)("Make classes shareable"); make_klasses_shareable(); - { - log_info(cds)("Adjust lambda proxy class dictionary"); - SystemDictionaryShared::adjust_lambda_proxy_class_dictionary(); - } + log_info(cds)("Adjust lambda proxy class dictionary"); + SystemDictionaryShared::adjust_lambda_proxy_class_dictionary(); - { - log_info(cds)("Final relocation of pointers ... "); - ResourceMark rm; - PointerMarker marker(this); - iterate_roots(&marker); - relocate_buffer_to_target(); - } + log_info(cds)("Final relocation of pointers ... "); + relocate_buffer_to_target(); write_archive(serialized_data); release_header(); @@ -678,34 +265,17 @@ verify_universe("After CDS dynamic dump"); } - void iterate_roots(MetaspaceClosure* it) { - int i; - int num_klasses = _klasses->length(); - for (i = 0; i < num_klasses; i++) { - it->push(&_klasses->at(i)); + virtual void iterate_roots(MetaspaceClosure* it, bool is_relocating_pointers) { + if (!is_relocating_pointers) { + SystemDictionaryShared::dumptime_classes_do(it); + SymbolTable::metaspace_pointers_do(it); } - - int num_symbols = _symbols->length(); - for (i = 0; i < num_symbols; i++) { - it->push(&_symbols->at(i)); - } - FileMapInfo::metaspace_pointers_do(it); - - // Do not call these again, as we have already collected all the classes and symbols - // that we want to archive. Also, these calls would corrupt the tables when - // ExternalRefUpdater is used. - // - // SystemDictionaryShared::dumptime_classes_do(it); - // SymbolTable::metaspace_pointers_do(it); - - it->finish(); } }; intx DynamicArchiveBuilder::_buffer_to_target_delta; - size_t DynamicArchiveBuilder::estimate_archive_size() { // size of the symbol table and two dictionaries, plus the RunTimeSharedClassInfo's _estimated_hashtable_bytes = 0; @@ -795,10 +365,12 @@ align_up(SharedRuntime::trampoline_size(), BytesPerWord) + align_up(sizeof(AdapterHandlerEntry*), BytesPerWord); - for (int i = 0; i < _klasses->length(); i++) { - InstanceKlass* ik = _klasses->at(i); - Array* methods = ik->methods(); - total += each_method_bytes * methods->length(); + for (int i = 0; i < klasses()->length(); i++) { + Klass* k = klasses()->at(i); + if (k->is_instance_klass()) { + Array* methods = InstanceKlass::cast(k)->methods(); + total += each_method_bytes * methods->length(); + } } if (total == 0) { // We have nothing to archive, but let's avoid having an empty region. @@ -810,8 +382,12 @@ void DynamicArchiveBuilder::make_trampolines() { DumpRegion* mc_space = MetaspaceShared::misc_code_dump_space(); char* p = mc_space->base(); - for (int i = 0; i < _klasses->length(); i++) { - InstanceKlass* ik = _klasses->at(i); + for (int i = 0; i < klasses()->length(); i++) { + Klass* k = klasses()->at(i); + if (!k->is_instance_klass()) { + continue; + } + InstanceKlass* ik = InstanceKlass::cast(k); Array* methods = ik->methods(); for (int j = 0; j < methods->length(); j++) { Method* m = methods->at(j); @@ -832,16 +408,22 @@ } void DynamicArchiveBuilder::make_klasses_shareable() { - int i, count = _klasses->length(); + int i, count = klasses()->length(); InstanceKlass::disable_method_binary_search(); for (i = 0; i < count; i++) { - InstanceKlass* ik = _klasses->at(i); - sort_methods(ik); + Klass* k = klasses()->at(i); + if (k->is_instance_klass()) { + sort_methods(InstanceKlass::cast(k)); + } } for (i = 0; i < count; i++) { - InstanceKlass* ik = _klasses->at(i); + Klass* k = klasses()->at(i); + if (!k->is_instance_klass()) { + continue; + } + InstanceKlass* ik = InstanceKlass::cast(k); ik->assign_class_loader_type(); MetaspaceShared::rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread::current(), ik); @@ -876,6 +458,11 @@ log_debug(cds, dynamic)("sorting methods for " PTR_FORMAT " %s", p2i(to_target(ik)), ik->external_name()); } + // Method sorting may re-layout the [iv]tables, which would change the offset(s) + // of the locations in an InstanceKlass that would contain pointers. Let's clear + // all the existing pointer marking bits, and re-mark the pointers after sorting. + remark_pointers_for_instance_klass(ik, false); + // Make sure all supertypes have been sorted sort_methods(ik->java_super()); Array* interfaces = ik->local_interfaces(); @@ -906,18 +493,33 @@ } ik->vtable().initialize_vtable(true, THREAD); assert(!HAS_PENDING_EXCEPTION, "cannot fail"); ik->itable().initialize_itable(true, THREAD); assert(!HAS_PENDING_EXCEPTION, "cannot fail"); -} -void DynamicArchiveBuilder::set_symbols_permanent() { - int count = _symbols->length(); - for (int i=0; iat(i); - s->set_permanent(); + // Set all the pointer marking bits after sorting. + remark_pointers_for_instance_klass(ik, true); +} - if (log_is_enabled(Trace, cds, dynamic)) { - ResourceMark rm; - log_trace(cds, dynamic)("symbols[%4i] = " PTR_FORMAT " %s", i, p2i(to_target(s)), s->as_quoted_ascii()); +template +class PointerRemarker: public MetaspaceClosure { +public: + virtual bool do_ref(Ref* ref, bool read_only) { + if (should_mark) { + ArchivePtrMarker::mark_pointer(ref->addr()); + } else { + ArchivePtrMarker::clear_pointer(ref->addr()); } + return false; // don't recurse + } +}; + +void DynamicArchiveBuilder::remark_pointers_for_instance_klass(InstanceKlass* k, bool should_mark) const { + if (should_mark) { + PointerRemarker marker; + k->metaspace_pointers_do(&marker); + marker.finish(); + } else { + PointerRemarker marker; + k->metaspace_pointers_do(&marker); + marker.finish(); } } @@ -1003,8 +605,8 @@ } void DynamicArchiveBuilder::write_archive(char* serialized_data) { - int num_klasses = _klasses->length(); - int num_symbols = _symbols->length(); + int num_klasses = klasses()->length(); + int num_symbols = symbols()->length(); _header->set_serialized_data(to_target(serialized_data)); @@ -1032,7 +634,6 @@ log_info(cds, dynamic)("%d klasses; %d symbols", num_klasses, num_symbols); } - class VM_PopulateDynamicDumpSharedSpace: public VM_Operation { DynamicArchiveBuilder* _builder; public: @@ -1070,7 +671,7 @@ address DynamicArchive::original_to_buffer_impl(address orig_obj) { assert(DynamicDumpSharedSpaces, "must be"); - address buff_obj = _builder->get_new_loc(orig_obj); + address buff_obj = _builder->get_dumped_addr(orig_obj); assert(buff_obj != NULL, "orig_obj must be used by the dynamic archive"); assert(buff_obj != orig_obj, "call this only when you know orig_obj must be copied and not just referenced"); assert(_builder->is_in_buffer_space(buff_obj), "must be"); @@ -1089,7 +690,7 @@ // This happens when the top archive points to a Symbol* in the base archive. return orig_obj; } - address buff_obj = _builder->get_new_loc(orig_obj); + address buff_obj = _builder->get_dumped_addr(orig_obj); assert(buff_obj != NULL, "orig_obj must be used by the dynamic archive"); if (buff_obj == orig_obj) { // We are storing a pointer to an original object into the dynamic buffer. E.g., --- old/src/hotspot/share/memory/metaspaceClosure.cpp 2020-07-31 23:33:54.204045889 -0700 +++ new/src/hotspot/share/memory/metaspaceClosure.cpp 2020-07-31 23:33:54.008038511 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -37,8 +37,11 @@ void MetaspaceClosure::push_impl(MetaspaceClosure::Ref* ref) { if (_nest_level < MAX_NEST_LEVEL) { do_push(ref); - delete ref; + if (!ref->keep_after_pushing()) { + delete ref; + } } else { + do_pending_ref(ref); ref->set_next(_pending_refs); _pending_refs = ref; } @@ -59,9 +62,15 @@ assert(w == _default, "must be"); read_only = ref->is_read_only_by_default(); } + if (_nest_level == 0) { + assert(_enclosing_ref == NULL, "must be"); + } _nest_level ++; if (do_ref(ref, read_only)) { // true means we want to iterate the embedded pointer in + Ref* saved = _enclosing_ref; + _enclosing_ref = ref; ref->metaspace_pointers_do(this); + _enclosing_ref = saved; } _nest_level --; } @@ -73,7 +82,9 @@ Ref* ref = _pending_refs; _pending_refs = _pending_refs->next(); do_push(ref); - delete ref; + if (!ref->keep_after_pushing()) { + delete ref; + } } } --- old/src/hotspot/share/memory/metaspaceClosure.hpp 2020-07-31 23:33:54.784067721 -0700 +++ new/src/hotspot/share/memory/metaspaceClosure.hpp 2020-07-31 23:33:54.584060193 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -108,12 +108,14 @@ // [2] All Array dimensions are statically declared. class Ref : public CHeapObj { Writability _writability; + bool _keep_after_pushing; Ref* _next; + void* _user_data; NONCOPYABLE(Ref); protected: virtual void** mpp() const = 0; - Ref(Writability w) : _writability(w), _next(NULL) {} + Ref(Writability w) : _writability(w), _keep_after_pushing(false), _next(NULL), _user_data(NULL) {} public: virtual bool not_null() const = 0; virtual int size() const = 0; @@ -137,6 +139,10 @@ void update(address new_loc) const; Writability writability() const { return _writability; }; + void set_keep_after_pushing() { _keep_after_pushing = true; } + bool keep_after_pushing() { return _keep_after_pushing; } + void set_user_data(void* data) { _user_data = data; } + void* user_data() { return _user_data; } void set_next(Ref* n) { _next = n; } Ref* next() const { return _next; } @@ -243,21 +249,43 @@ } }; - // If recursion is too deep, save the Refs in _pending_refs, and push them later using - // MetaspaceClosure::finish() + // Normally, chains of references like a->b->c->d are iterated recursively. However, + // if recursion is too deep, we save the Refs in _pending_refs, and push them later in + // MetaspaceClosure::finish(). This avoids overflowing the C stack. static const int MAX_NEST_LEVEL = 5; Ref* _pending_refs; int _nest_level; + Ref* _enclosing_ref; void push_impl(Ref* ref); void do_push(Ref* ref); public: - MetaspaceClosure(): _pending_refs(NULL), _nest_level(0) {} + MetaspaceClosure(): _pending_refs(NULL), _nest_level(0), _enclosing_ref(NULL) {} ~MetaspaceClosure(); void finish(); + // enclosing_ref() is used to compute the offset of a field in a C++ class. For example + // class Foo { intx scala; Bar* ptr; } + // Foo *f = 0x100; + // when the f->ptr field is iterated with do_ref() on 64-bit platforms, we will have + // do_ref(Ref* r) { + // r->addr() == 0x108; // == &f->ptr; + // enclosing_ref()->obj() == 0x100; // == foo + // So we know that we are iterating upon a field at offset 8 of the object at 0x100. + // + // Note that if we have stack overflow, do_pending_ref(r) will be called first and + // do_ref(r) will be called later, for the same r. In this case, enclosing_ref() is valid only + // when do_pending_ref(r) is called, and will return NULL when do_ref(r) is called. + Ref* enclosing_ref() const { + return _enclosing_ref; + } + + // This is called when a reference is placed in _pending_refs. Override this + // function if you're using enclosing_ref(). See notes above. + virtual void do_pending_ref(Ref* ref) {} + // returns true if we want to keep iterating the pointers embedded inside virtual bool do_ref(Ref* ref, bool read_only) = 0; @@ -285,7 +313,11 @@ } template void push_method_entry(T** mpp, intptr_t* p) { - push_special(_method_entry_ref, new ObjectRef(mpp, _default), (intptr_t*)p); + Ref* ref = new ObjectRef(mpp, _default); + push_special(_method_entry_ref, ref, (intptr_t*)p); + if (!ref->keep_after_pushing()) { + delete ref; + } } // This is for tagging special pointers that are not a reference to MetaspaceObj. It's currently --- old/src/hotspot/share/memory/metaspaceShared.cpp 2020-07-31 23:33:55.364089555 -0700 +++ new/src/hotspot/share/memory/metaspaceShared.cpp 2020-07-31 23:33:55.168082176 -0700 @@ -23,11 +23,9 @@ */ #include "precompiled.hpp" -#include "jvm.h" #include "classfile/classLoaderDataGraph.hpp" #include "classfile/classListParser.hpp" #include "classfile/classLoaderExt.hpp" -#include "classfile/dictionary.hpp" #include "classfile/loaderConstraints.hpp" #include "classfile/javaClasses.inline.hpp" #include "classfile/placeholders.hpp" @@ -36,11 +34,11 @@ #include "classfile/systemDictionary.hpp" #include "classfile/systemDictionaryShared.hpp" #include "code/codeCache.hpp" -#include "gc/shared/softRefPolicy.hpp" #include "interpreter/bytecodeStream.hpp" #include "interpreter/bytecodes.hpp" #include "logging/log.hpp" #include "logging/logMessage.hpp" +#include "memory/archiveBuilder.hpp" #include "memory/archiveUtils.inline.hpp" #include "memory/dynamicArchive.hpp" #include "memory/filemap.hpp" @@ -59,11 +57,9 @@ #include "oops/objArrayOop.hpp" #include "oops/oop.inline.hpp" #include "oops/typeArrayKlass.hpp" -#include "prims/jvmtiRedefineClasses.hpp" #include "runtime/handles.inline.hpp" #include "runtime/os.hpp" #include "runtime/safepointVerifiers.hpp" -#include "runtime/signature.hpp" #include "runtime/timerTrace.hpp" #include "runtime/vmThread.hpp" #include "runtime/vmOperations.hpp" @@ -624,72 +620,10 @@ // is run at a safepoint just before exit, this is the entire set of classes. static GrowableArray* _global_klass_objects; -static int global_klass_compare(Klass** a, Klass **b) { - return a[0]->name()->fast_compare(b[0]->name()); -} - GrowableArray* MetaspaceShared::collected_klasses() { return _global_klass_objects; } -static void collect_array_classes(Klass* k) { - _global_klass_objects->append_if_missing(k); - if (k->is_array_klass()) { - // Add in the array classes too - ArrayKlass* ak = ArrayKlass::cast(k); - Klass* h = ak->higher_dimension(); - if (h != NULL) { - h->array_klasses_do(collect_array_classes); - } - } -} - -class CollectClassesClosure : public KlassClosure { - void do_klass(Klass* k) { - if (k->is_instance_klass() && - SystemDictionaryShared::is_excluded_class(InstanceKlass::cast(k))) { - // Don't add to the _global_klass_objects - } else { - _global_klass_objects->append_if_missing(k); - } - if (k->is_array_klass()) { - // Add in the array classes too - ArrayKlass* ak = ArrayKlass::cast(k); - Klass* h = ak->higher_dimension(); - if (h != NULL) { - h->array_klasses_do(collect_array_classes); - } - } - } -}; - -// Global object for holding symbols that created during class loading. See SymbolTable::new_symbol -static GrowableArray* _global_symbol_objects = NULL; - -static int compare_symbols_by_address(Symbol** a, Symbol** b) { - if (a[0] < b[0]) { - return -1; - } else if (a[0] == b[0]) { - ResourceMark rm; - log_warning(cds)("Duplicated symbol %s unexpected", (*a)->as_C_string()); - return 0; - } else { - return 1; - } -} - -void MetaspaceShared::add_symbol(Symbol* sym) { - MutexLocker ml(CDSAddSymbol_lock, Mutex::_no_safepoint_check_flag); - if (_global_symbol_objects == NULL) { - _global_symbol_objects = new (ResourceObj::C_HEAP, mtSymbol) GrowableArray(2048, mtSymbol); - } - _global_symbol_objects->append(sym); -} - -GrowableArray* MetaspaceShared::collected_symbols() { - return _global_symbol_objects; -} - static void remove_unshareable_in_classes() { for (int i = 0; i < _global_klass_objects->length(); i++) { Klass* k = _global_klass_objects->at(i); @@ -1071,148 +1005,6 @@ } } -// This is for dumping detailed statistics for the allocations -// in the shared spaces. -class DumpAllocStats : public ResourceObj { -public: - - // Here's poor man's enum inheritance -#define SHAREDSPACE_OBJ_TYPES_DO(f) \ - METASPACE_OBJ_TYPES_DO(f) \ - f(SymbolHashentry) \ - f(SymbolBucket) \ - f(StringHashentry) \ - f(StringBucket) \ - f(Other) - - enum Type { - // Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc - SHAREDSPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_DECLARE) - _number_of_types - }; - - static const char * type_name(Type type) { - switch(type) { - SHAREDSPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_NAME_CASE) - default: - ShouldNotReachHere(); - return NULL; - } - } - -public: - enum { RO = 0, RW = 1 }; - - int _counts[2][_number_of_types]; - int _bytes [2][_number_of_types]; - - DumpAllocStats() { - memset(_counts, 0, sizeof(_counts)); - memset(_bytes, 0, sizeof(_bytes)); - }; - - void record(MetaspaceObj::Type type, int byte_size, bool read_only) { - assert(int(type) >= 0 && type < MetaspaceObj::_number_of_types, "sanity"); - int which = (read_only) ? RO : RW; - _counts[which][type] ++; - _bytes [which][type] += byte_size; - } - - void record_other_type(int byte_size, bool read_only) { - int which = (read_only) ? RO : RW; - _bytes [which][OtherType] += byte_size; - } - void print_stats(int ro_all, int rw_all, int mc_all); -}; - -void DumpAllocStats::print_stats(int ro_all, int rw_all, int mc_all) { - // Calculate size of data that was not allocated by Metaspace::allocate() - MetaspaceSharedStats *stats = MetaspaceShared::stats(); - - // symbols - _counts[RO][SymbolHashentryType] = stats->symbol.hashentry_count; - _bytes [RO][SymbolHashentryType] = stats->symbol.hashentry_bytes; - - _counts[RO][SymbolBucketType] = stats->symbol.bucket_count; - _bytes [RO][SymbolBucketType] = stats->symbol.bucket_bytes; - - // strings - _counts[RO][StringHashentryType] = stats->string.hashentry_count; - _bytes [RO][StringHashentryType] = stats->string.hashentry_bytes; - - _counts[RO][StringBucketType] = stats->string.bucket_count; - _bytes [RO][StringBucketType] = stats->string.bucket_bytes; - - // TODO: count things like dictionary, vtable, etc - _bytes[RW][OtherType] += mc_all; - rw_all += mc_all; // mc is mapped Read/Write - - // prevent divide-by-zero - if (ro_all < 1) { - ro_all = 1; - } - if (rw_all < 1) { - rw_all = 1; - } - - int all_ro_count = 0; - int all_ro_bytes = 0; - int all_rw_count = 0; - int all_rw_bytes = 0; - -// To make fmt_stats be a syntactic constant (for format warnings), use #define. -#define fmt_stats "%-20s: %8d %10d %5.1f | %8d %10d %5.1f | %8d %10d %5.1f" - const char *sep = "--------------------+---------------------------+---------------------------+--------------------------"; - const char *hdr = " ro_cnt ro_bytes % | rw_cnt rw_bytes % | all_cnt all_bytes %"; - - LogMessage(cds) msg; - - msg.debug("Detailed metadata info (excluding st regions; rw stats include mc regions):"); - msg.debug("%s", hdr); - msg.debug("%s", sep); - for (int type = 0; type < int(_number_of_types); type ++) { - const char *name = type_name((Type)type); - int ro_count = _counts[RO][type]; - int ro_bytes = _bytes [RO][type]; - int rw_count = _counts[RW][type]; - int rw_bytes = _bytes [RW][type]; - int count = ro_count + rw_count; - int bytes = ro_bytes + rw_bytes; - - double ro_perc = percent_of(ro_bytes, ro_all); - double rw_perc = percent_of(rw_bytes, rw_all); - double perc = percent_of(bytes, ro_all + rw_all); - - msg.debug(fmt_stats, name, - ro_count, ro_bytes, ro_perc, - rw_count, rw_bytes, rw_perc, - count, bytes, perc); - - all_ro_count += ro_count; - all_ro_bytes += ro_bytes; - all_rw_count += rw_count; - all_rw_bytes += rw_bytes; - } - - int all_count = all_ro_count + all_rw_count; - int all_bytes = all_ro_bytes + all_rw_bytes; - - double all_ro_perc = percent_of(all_ro_bytes, ro_all); - double all_rw_perc = percent_of(all_rw_bytes, rw_all); - double all_perc = percent_of(all_bytes, ro_all + rw_all); - - msg.debug("%s", sep); - msg.debug(fmt_stats, "Total", - all_ro_count, all_ro_bytes, all_ro_perc, - all_rw_count, all_rw_bytes, all_rw_perc, - all_count, all_bytes, all_perc); - - assert(all_ro_bytes == ro_all, "everything should have been counted"); - assert(all_rw_bytes == rw_all, "everything should have been counted"); - -#undef fmt_stats -} - // Populate the shared space. class VM_PopulateDumpSharedSpace: public VM_Operation { @@ -1229,7 +1021,6 @@ GrowableArray* oopmaps); void dump_symbols(); char* dump_read_only_tables(); - void print_class_stats(); void print_region_stats(FileMapInfo* map_info); void print_bitmap_region_stats(size_t size, size_t total_size); void print_heap_region_stats(GrowableArray *heap_mem, @@ -1243,278 +1034,20 @@ bool allow_nested_vm_operations() const { return true; } }; // class VM_PopulateDumpSharedSpace -// ArchiveCompactor -- -// -// This class is the central piece of shared archive compaction -- all metaspace data are -// initially allocated outside of the shared regions. ArchiveCompactor copies the -// metaspace data into their final location in the shared regions. - -class ArchiveCompactor : AllStatic { - static const int INITIAL_TABLE_SIZE = 8087; - static const int MAX_TABLE_SIZE = 1000000; - - static DumpAllocStats* _alloc_stats; - - typedef KVHashtable RelocationTable; - static RelocationTable* _new_loc_table; - +class StaticArchiveBuilder : public ArchiveBuilder { public: - static void initialize() { - _alloc_stats = new(ResourceObj::C_HEAP, mtInternal)DumpAllocStats; - _new_loc_table = new RelocationTable(INITIAL_TABLE_SIZE); - } - static DumpAllocStats* alloc_stats() { - return _alloc_stats; - } - - // Use this when you allocate space with MetaspaceShare::read_only_space_alloc() - // outside of ArchiveCompactor::allocate(). These are usually for misc tables - // that are allocated in the RO space. - class OtherROAllocMark { - char* _oldtop; - public: - OtherROAllocMark() { - _oldtop = _ro_region.top(); - } - ~OtherROAllocMark() { - char* newtop = _ro_region.top(); - ArchiveCompactor::alloc_stats()->record_other_type(int(newtop - _oldtop), true); - } - }; - - static void allocate(MetaspaceClosure::Ref* ref, bool read_only) { - address obj = ref->obj(); - int bytes = ref->size() * BytesPerWord; - char* p; - size_t alignment = BytesPerWord; - char* oldtop; - char* newtop; - - if (read_only) { - oldtop = _ro_region.top(); - p = _ro_region.allocate(bytes, alignment); - newtop = _ro_region.top(); - } else { - oldtop = _rw_region.top(); - if (ref->msotype() == MetaspaceObj::ClassType) { - // Save a pointer immediate in front of an InstanceKlass, so - // we can do a quick lookup from InstanceKlass* -> RunTimeSharedClassInfo* - // without building another hashtable. See RunTimeSharedClassInfo::get_for() - // in systemDictionaryShared.cpp. - Klass* klass = (Klass*)obj; - if (klass->is_instance_klass()) { - SystemDictionaryShared::validate_before_archiving(InstanceKlass::cast(klass)); - _rw_region.allocate(sizeof(address), BytesPerWord); - } - } - p = _rw_region.allocate(bytes, alignment); - newtop = _rw_region.top(); - } - memcpy(p, obj, bytes); - - intptr_t* archived_vtable = MetaspaceShared::get_archived_cpp_vtable(ref->msotype(), (address)p); - if (archived_vtable != NULL) { - *(address*)p = (address)archived_vtable; - ArchivePtrMarker::mark_pointer((address*)p); - } + StaticArchiveBuilder(DumpRegion* rw_region, DumpRegion* ro_region) + : ArchiveBuilder(rw_region, ro_region) {} - assert(_new_loc_table->lookup(obj) == NULL, "each object can be relocated at most once"); - _new_loc_table->add(obj, (address)p); - log_trace(cds)("Copy: " PTR_FORMAT " ==> " PTR_FORMAT " %d", p2i(obj), p2i(p), bytes); - if (_new_loc_table->maybe_grow(MAX_TABLE_SIZE)) { - log_info(cds, hashtables)("Expanded _new_loc_table to %d", _new_loc_table->table_size()); - } - _alloc_stats->record(ref->msotype(), int(newtop - oldtop), read_only); - } - - static address get_new_loc(MetaspaceClosure::Ref* ref) { - address* pp = _new_loc_table->lookup(ref->obj()); - assert(pp != NULL, "must be"); - return *pp; - } - -private: - // Makes a shallow copy of visited MetaspaceObj's - class ShallowCopier: public UniqueMetaspaceClosure { - bool _read_only; - public: - ShallowCopier(bool read_only) : _read_only(read_only) {} - - virtual bool do_unique_ref(Ref* ref, bool read_only) { - if (read_only == _read_only) { - allocate(ref, read_only); - } - return true; // recurse into ref.obj() - } - }; - - // Relocate embedded pointers within a MetaspaceObj's shallow copy - class ShallowCopyEmbeddedRefRelocator: public UniqueMetaspaceClosure { - public: - virtual bool do_unique_ref(Ref* ref, bool read_only) { - address new_loc = get_new_loc(ref); - RefRelocator refer; - ref->metaspace_pointers_do_at(&refer, new_loc); - return true; // recurse into ref.obj() - } - virtual void push_special(SpecialRef type, Ref* ref, intptr_t* p) { - assert(type == _method_entry_ref, "only special type allowed for now"); - address obj = ref->obj(); - address new_obj = get_new_loc(ref); - size_t offset = pointer_delta(p, obj, sizeof(u1)); - intptr_t* new_p = (intptr_t*)(new_obj + offset); - assert(*p == *new_p, "must be a copy"); - ArchivePtrMarker::mark_pointer((address*)new_p); - } - }; - - // Relocate a reference to point to its shallow copy - class RefRelocator: public MetaspaceClosure { - public: - virtual bool do_ref(Ref* ref, bool read_only) { - if (ref->not_null()) { - ref->update(get_new_loc(ref)); - ArchivePtrMarker::mark_pointer(ref->addr()); - } - return false; // Do not recurse. - } - }; - -#ifdef ASSERT - class IsRefInArchiveChecker: public MetaspaceClosure { - public: - virtual bool do_ref(Ref* ref, bool read_only) { - if (ref->not_null()) { - char* obj = (char*)ref->obj(); - assert(_ro_region.contains(obj) || _rw_region.contains(obj), - "must be relocated to point to CDS archive"); - } - return false; // Do not recurse. - } - }; -#endif - -public: - static void copy_and_compact() { - ResourceMark rm; - - log_info(cds)("Scanning all metaspace objects ... "); - { - // allocate and shallow-copy RW objects, immediately following the MC region - log_info(cds)("Allocating RW objects ... "); - _mc_region.pack(&_rw_region); - - ResourceMark rm; - ShallowCopier rw_copier(false); - iterate_roots(&rw_copier); - } - { - // allocate and shallow-copy of RO object, immediately following the RW region - log_info(cds)("Allocating RO objects ... "); - _rw_region.pack(&_ro_region); - - ResourceMark rm; - ShallowCopier ro_copier(true); - iterate_roots(&ro_copier); - } - { - log_info(cds)("Relocating embedded pointers ... "); - ResourceMark rm; - ShallowCopyEmbeddedRefRelocator emb_reloc; - iterate_roots(&emb_reloc); - } - { - log_info(cds)("Relocating external roots ... "); - ResourceMark rm; - RefRelocator ext_reloc; - iterate_roots(&ext_reloc); - } - { - log_info(cds)("Fixing symbol identity hash ... "); - os::init_random(0x12345678); - GrowableArray* all_symbols = MetaspaceShared::collected_symbols(); - all_symbols->sort(compare_symbols_by_address); - for (int i = 0; i < all_symbols->length(); i++) { - assert(all_symbols->at(i)->is_permanent(), "archived symbols must be permanent"); - all_symbols->at(i)->update_identity_hash(); - } - } -#ifdef ASSERT - { - log_info(cds)("Verifying external roots ... "); - ResourceMark rm; - IsRefInArchiveChecker checker; - iterate_roots(&checker); - } -#endif - } - - // We must relocate the System::_well_known_klasses only after we have copied the - // java objects in during dump_java_heap_objects(): during the object copy, we operate on - // old objects which assert that their klass is the original klass. - static void relocate_well_known_klasses() { - { - log_info(cds)("Relocating SystemDictionary::_well_known_klasses[] ... "); - ResourceMark rm; - RefRelocator ext_reloc; - SystemDictionary::well_known_klasses_do(&ext_reloc); - } - // NOTE: after this point, we shouldn't have any globals that can reach the old - // objects. - - // We cannot use any of the objects in the heap anymore (except for the - // shared strings) because their headers no longer point to valid Klasses. - } - - static void iterate_roots(MetaspaceClosure* it) { - // To ensure deterministic contents in the archive, we just need to ensure that - // we iterate the MetsapceObjs in a deterministic order. It doesn't matter where - // the MetsapceObjs are located originally, as they are copied sequentially into - // the archive during the iteration. - // - // The only issue here is that the symbol table and the system directories may be - // randomly ordered, so we copy the symbols and klasses into two arrays and sort - // them deterministically. - // - // During -Xshare:dump, the order of Symbol creation is strictly determined by - // the SharedClassListFile (class loading is done in a single thread and the JIT - // is disabled). Also, Symbols are allocated in monotonically increasing addresses - // (see Symbol::operator new(size_t, int)). So if we iterate the Symbols by - // ascending address order, we ensure that all Symbols are copied into deterministic - // locations in the archive. - GrowableArray* symbols = _global_symbol_objects; - for (int i = 0; i < symbols->length(); i++) { - it->push(symbols->adr_at(i)); - } - if (_global_klass_objects != NULL) { - // Need to fix up the pointers - for (int i = 0; i < _global_klass_objects->length(); i++) { - // NOTE -- this requires that the vtable is NOT yet patched, or else we are hosed. - it->push(_global_klass_objects->adr_at(i)); - } - } + virtual void iterate_roots(MetaspaceClosure* it, bool is_relocating_pointers) { FileMapInfo::metaspace_pointers_do(it, false); SystemDictionaryShared::dumptime_classes_do(it); Universe::metaspace_pointers_do(it); SymbolTable::metaspace_pointers_do(it); vmSymbols::metaspace_pointers_do(it); - - it->finish(); - } - - static Klass* get_relocated_klass(Klass* orig_klass) { - assert(DumpSharedSpaces, "dump time only"); - address* pp = _new_loc_table->lookup((address)orig_klass); - assert(pp != NULL, "must be"); - Klass* klass = (Klass*)(*pp); - assert(klass->is_klass(), "must be"); - return klass; } }; -DumpAllocStats* ArchiveCompactor::_alloc_stats; -ArchiveCompactor::RelocationTable* ArchiveCompactor::_new_loc_table; - void VM_PopulateDumpSharedSpace::dump_symbols() { log_info(cds)("Dumping symbol table ..."); @@ -1523,7 +1056,7 @@ } char* VM_PopulateDumpSharedSpace::dump_read_only_tables() { - ArchiveCompactor::OtherROAllocMark mark; + ArchiveBuilder::OtherROAllocMark mark; log_info(cds)("Removing java_mirror ... "); if (!HeapShared::is_heap_object_archiving_allowed()) { @@ -1547,27 +1080,6 @@ return start; } -void VM_PopulateDumpSharedSpace::print_class_stats() { - log_info(cds)("Number of classes %d", _global_klass_objects->length()); - { - int num_type_array = 0, num_obj_array = 0, num_inst = 0; - for (int i = 0; i < _global_klass_objects->length(); i++) { - Klass* k = _global_klass_objects->at(i); - if (k->is_instance_klass()) { - num_inst ++; - } else if (k->is_objArray_klass()) { - num_obj_array ++; - } else { - assert(k->is_typeArray_klass(), "sanity"); - num_type_array ++; - } - } - log_info(cds)(" instance classes = %5d", num_inst); - log_info(cds)(" obj array classes = %5d", num_obj_array); - log_info(cds)(" type array classes = %5d", num_type_array); - } -} - void VM_PopulateDumpSharedSpace::relocate_to_requested_base_address(CHeapBitMap* ptrmap) { intx addr_delta = MetaspaceShared::final_delta(); if (addr_delta == 0) { @@ -1640,12 +1152,10 @@ // Gather systemDictionary classes in a global array and do everything to // that so we don't have to walk the SystemDictionary again. SystemDictionaryShared::check_excluded_classes(); - _global_klass_objects = new GrowableArray(1000); - CollectClassesClosure collect_classes; - ClassLoaderDataGraph::loaded_classes_do(&collect_classes); - _global_klass_objects->sort(global_klass_compare); - print_class_stats(); + StaticArchiveBuilder builder(&_rw_region, &_ro_region); + builder.gather_klasses_and_symbols(); + _global_klass_objects = builder.klasses(); // Ensure the ConstMethods won't be modified at run-time log_info(cds)("Updating ConstMethods ... "); @@ -1657,12 +1167,17 @@ remove_unshareable_in_classes(); log_info(cds)("done. "); + builder.gather_source_objs(); + MetaspaceShared::allocate_cloned_cpp_vtptrs(); char* cloned_vtables = _mc_region.top(); MetaspaceShared::allocate_cpp_vtable_clones(); - ArchiveCompactor::initialize(); - ArchiveCompactor::copy_and_compact(); + _mc_region.pack(&_rw_region); + builder.dump_rw_region(); + _rw_region.pack(&_ro_region); + builder.dump_ro_region(); + builder.relocate_pointers(); dump_symbols(); @@ -1671,7 +1186,7 @@ _open_archive_heap_regions = NULL; dump_java_heap_objects(); - ArchiveCompactor::relocate_well_known_klasses(); + builder.relocate_well_known_klasses(); char* serialized_data = dump_read_only_tables(); _ro_region.pack(); @@ -1714,8 +1229,7 @@ mapinfo->close(); if (log_is_enabled(Info, cds)) { - ArchiveCompactor::alloc_stats()->print_stats(int(_ro_region.used()), int(_rw_region.used()), - int(_mc_region.used())); + builder.print_stats(int(_ro_region.used()), int(_rw_region.used()), int(_mc_region.used())); } if (PrintSystemDictionaryAtExit) { @@ -1801,13 +1315,13 @@ // shared archive has been compacted. void MetaspaceShared::relocate_klass_ptr(oop o) { assert(DumpSharedSpaces, "sanity"); - Klass* k = ArchiveCompactor::get_relocated_klass(o->klass()); + Klass* k = ArchiveBuilder::get_relocated_klass(o->klass()); o->set_klass(k); } Klass* MetaspaceShared::get_relocated_klass(Klass *k, bool is_final) { assert(DumpSharedSpaces, "sanity"); - k = ArchiveCompactor::get_relocated_klass(k); + k = ArchiveBuilder::get_relocated_klass(k); if (is_final) { k = (Klass*)(address(k) + final_delta()); } @@ -2014,7 +1528,7 @@ _open_archive_heap_regions = new GrowableArray(2); HeapShared::archive_java_heap_objects(_closed_archive_heap_regions, _open_archive_heap_regions); - ArchiveCompactor::OtherROAllocMark mark; + ArchiveBuilder::OtherROAllocMark mark; HeapShared::write_subgraph_info_table(); } --- old/src/hotspot/share/memory/metaspaceShared.hpp 2020-07-31 23:33:55.984112893 -0700 +++ new/src/hotspot/share/memory/metaspaceShared.hpp 2020-07-31 23:33:55.788105515 -0700 @@ -30,7 +30,6 @@ #include "memory/memRegion.hpp" #include "memory/virtualspace.hpp" #include "oops/oop.hpp" -#include "utilities/exceptions.hpp" #include "utilities/macros.hpp" #include "utilities/resourceHash.hpp" @@ -213,8 +212,6 @@ TRAPS) NOT_CDS_RETURN_(0); static GrowableArray* collected_klasses(); - static GrowableArray* collected_symbols(); - static void add_symbol(Symbol* sym) NOT_CDS_RETURN; static ReservedSpace* shared_rs() { CDS_ONLY(return &_shared_rs); --- old/src/hotspot/share/utilities/hashtable.hpp 2020-07-31 23:33:56.568134877 -0700 +++ new/src/hotspot/share/utilities/hashtable.hpp 2020-07-31 23:33:56.376127650 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -301,13 +301,14 @@ public: KVHashtable(int table_size) : BasicHashtable(table_size, sizeof(KVHashtableEntry)) {} - void add(K key, V value) { + V* add(K key, V value) { unsigned int hash = HASH(key); KVHashtableEntry* entry = new_entry(hash, key, value); BasicHashtable::add_entry(BasicHashtable::hash_to_index(hash), entry); + return &(entry->_value); } - V* lookup(K key) { + V* lookup(K key) const { unsigned int hash = HASH(key); int index = BasicHashtable::hash_to_index(hash); for (KVHashtableEntry* e = bucket(index); e != NULL; e = e->next()) { @@ -317,6 +318,23 @@ } return NULL; } + + int table_size() const { + return BasicHashtable::table_size(); + } + + // ITER contains bool do_entry(K, V const&), which will be + // called for each entry in the table. If do_entry() returns false, + // the iteration is cancelled. + template + void iterate(ITER* iter) const { + for (int index = 0; index < table_size(); index++) { + for (KVHashtableEntry* e = bucket(index); e != NULL; e = e->next()) { + bool cont = iter->do_entry(e->_key, &e->_value); + if (!cont) { return; } + } + } + } }; --- /dev/null 2019-11-19 22:05:02.069813242 -0800 +++ new/src/hotspot/share/memory/archiveBuilder.cpp 2020-07-31 23:33:56.896147224 -0700 @@ -0,0 +1,696 @@ +/* + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + * + */ + +#include "precompiled.hpp" +#include "classfile/systemDictionaryShared.hpp" +#include "logging/log.hpp" +#include "logging/logMessage.hpp" +#include "memory/archiveBuilder.hpp" +#include "memory/archiveUtils.hpp" +#include "memory/resourceArea.hpp" +#include "oops/instanceKlass.hpp" +#include "oops/objArrayKlass.hpp" +#include "oops/oopHandle.inline.hpp" +#include "utilities/align.hpp" + +ArchiveBuilder* ArchiveBuilder::_singleton = NULL; + +// This is for dumping detailed statistics for the allocations +// in the shared spaces. +class DumpAllocStats : public ResourceObj { +public: + + // Here's poor man's enum inheritance +#define SHAREDSPACE_OBJ_TYPES_DO(f) \ + METASPACE_OBJ_TYPES_DO(f) \ + f(SymbolHashentry) \ + f(SymbolBucket) \ + f(StringHashentry) \ + f(StringBucket) \ + f(Other) + + enum Type { + // Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc + SHAREDSPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_DECLARE) + _number_of_types + }; + + static const char* type_name(Type type) { + switch(type) { + SHAREDSPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_NAME_CASE) + default: + ShouldNotReachHere(); + return NULL; + } + } + +public: + enum { RO = 0, RW = 1 }; + + int _counts[2][_number_of_types]; + int _bytes [2][_number_of_types]; + + DumpAllocStats() { + memset(_counts, 0, sizeof(_counts)); + memset(_bytes, 0, sizeof(_bytes)); + }; + + void record(MetaspaceObj::Type type, int byte_size, bool read_only) { + assert(int(type) >= 0 && type < MetaspaceObj::_number_of_types, "sanity"); + int which = (read_only) ? RO : RW; + _counts[which][type] ++; + _bytes [which][type] += byte_size; + } + + void record_other_type(int byte_size, bool read_only) { + int which = (read_only) ? RO : RW; + _bytes [which][OtherType] += byte_size; + } + void print_stats(int ro_all, int rw_all, int mc_all); +}; + +void DumpAllocStats::print_stats(int ro_all, int rw_all, int mc_all) { + // Calculate size of data that was not allocated by Metaspace::allocate() + MetaspaceSharedStats *stats = MetaspaceShared::stats(); + + // symbols + _counts[RO][SymbolHashentryType] = stats->symbol.hashentry_count; + _bytes [RO][SymbolHashentryType] = stats->symbol.hashentry_bytes; + + _counts[RO][SymbolBucketType] = stats->symbol.bucket_count; + _bytes [RO][SymbolBucketType] = stats->symbol.bucket_bytes; + + // strings + _counts[RO][StringHashentryType] = stats->string.hashentry_count; + _bytes [RO][StringHashentryType] = stats->string.hashentry_bytes; + + _counts[RO][StringBucketType] = stats->string.bucket_count; + _bytes [RO][StringBucketType] = stats->string.bucket_bytes; + + // TODO: count things like dictionary, vtable, etc + _bytes[RW][OtherType] += mc_all; + rw_all += mc_all; // mc is mapped Read/Write + + // prevent divide-by-zero + if (ro_all < 1) { + ro_all = 1; + } + if (rw_all < 1) { + rw_all = 1; + } + + int all_ro_count = 0; + int all_ro_bytes = 0; + int all_rw_count = 0; + int all_rw_bytes = 0; + +// To make fmt_stats be a syntactic constant (for format warnings), use #define. +#define fmt_stats "%-20s: %8d %10d %5.1f | %8d %10d %5.1f | %8d %10d %5.1f" + const char *sep = "--------------------+---------------------------+---------------------------+--------------------------"; + const char *hdr = " ro_cnt ro_bytes % | rw_cnt rw_bytes % | all_cnt all_bytes %"; + + LogMessage(cds) msg; + + msg.debug("Detailed metadata info (excluding st regions; rw stats include mc regions):"); + msg.debug("%s", hdr); + msg.debug("%s", sep); + for (int type = 0; type < int(_number_of_types); type ++) { + const char *name = type_name((Type)type); + int ro_count = _counts[RO][type]; + int ro_bytes = _bytes [RO][type]; + int rw_count = _counts[RW][type]; + int rw_bytes = _bytes [RW][type]; + int count = ro_count + rw_count; + int bytes = ro_bytes + rw_bytes; + + double ro_perc = percent_of(ro_bytes, ro_all); + double rw_perc = percent_of(rw_bytes, rw_all); + double perc = percent_of(bytes, ro_all + rw_all); + + msg.debug(fmt_stats, name, + ro_count, ro_bytes, ro_perc, + rw_count, rw_bytes, rw_perc, + count, bytes, perc); + + all_ro_count += ro_count; + all_ro_bytes += ro_bytes; + all_rw_count += rw_count; + all_rw_bytes += rw_bytes; + } + + int all_count = all_ro_count + all_rw_count; + int all_bytes = all_ro_bytes + all_rw_bytes; + + double all_ro_perc = percent_of(all_ro_bytes, ro_all); + double all_rw_perc = percent_of(all_rw_bytes, rw_all); + double all_perc = percent_of(all_bytes, ro_all + rw_all); + + msg.debug("%s", sep); + msg.debug(fmt_stats, "Total", + all_ro_count, all_ro_bytes, all_ro_perc, + all_rw_count, all_rw_bytes, all_rw_perc, + all_count, all_bytes, all_perc); + + assert(all_ro_bytes == ro_all, "everything should have been counted"); + assert(all_rw_bytes == rw_all, "everything should have been counted"); + +#undef fmt_stats +} + +ArchiveBuilder::OtherROAllocMark::~OtherROAllocMark() { + char* newtop = ArchiveBuilder::singleton()->_ro_region->top(); + ArchiveBuilder::alloc_stats()->record_other_type(int(newtop - _oldtop), true); +} + +ArchiveBuilder::SourceObjList::SourceObjList() : _ptrmap(16 * K) { + _total_bytes = 0; + _objs = new (ResourceObj::C_HEAP, mtClassShared) GrowableArray(128 * K, mtClassShared); +} + +ArchiveBuilder::SourceObjList::~SourceObjList() { + delete _objs; +} + +void ArchiveBuilder::SourceObjList::append(MetaspaceClosure::Ref* enclosing_ref, SourceObjInfo* src_info) { + // Save this source object for copying + _objs->append(src_info); + + // Prepare for marking the pointers in this source object + assert(is_aligned(_total_bytes, sizeof(address)), "must be"); + src_info->set_ptrmap_start(_total_bytes / sizeof(address)); + _total_bytes = align_up(_total_bytes + (uintx)src_info->size_in_bytes(), sizeof(address)); + src_info->set_ptrmap_end(_total_bytes / sizeof(address)); + + BitMap::idx_t bitmap_size_needed = BitMap::idx_t(src_info->ptrmap_end()); + if (_ptrmap.size() <= bitmap_size_needed) { + _ptrmap.resize((bitmap_size_needed + 1) * 2); + } +} + +void ArchiveBuilder::SourceObjList::remember_embedded_pointer(SourceObjInfo* src_info, MetaspaceClosure::Ref* ref) { + // src_obj contains a pointer. Remember the location of this pointer in _ptrmap, + // so that we can copy/relocate it later. E.g., if we have + // class Foo { intx scala; Bar* ptr; } + // Foo *f = 0x100; + // To mark the f->ptr pointer on 64-bit platform, this function is called with + // src_info()->obj() == 0x100 + // ref->addr() == 0x108 + address src_obj = src_info->obj(); + address* field_addr = ref->addr(); + assert(src_info->ptrmap_start() < _total_bytes, "sanity"); + assert(src_info->ptrmap_end() <= _total_bytes, "sanity"); + assert(*field_addr != NULL, "should have checked"); + + intx field_offset_in_bytes = ((address)field_addr) - src_obj; + DEBUG_ONLY(int src_obj_size = src_info->size_in_bytes();) + assert(field_offset_in_bytes >= 0, "must be"); + assert(field_offset_in_bytes + intx(sizeof(intptr_t)) <= intx(src_obj_size), "must be"); + assert(is_aligned(field_offset_in_bytes, sizeof(address)), "must be"); + + BitMap::idx_t idx = BitMap::idx_t(src_info->ptrmap_start() + (uintx)(field_offset_in_bytes / sizeof(address))); + _ptrmap.set_bit(BitMap::idx_t(idx)); +} + +class RelocateEmbeddedPointers : public BitMapClosure { + ArchiveBuilder* _builder; + address _dumped_obj; + BitMap::idx_t _start_idx; +public: + RelocateEmbeddedPointers(ArchiveBuilder* builder, address dumped_obj, BitMap::idx_t start_idx) : + _builder(builder), _dumped_obj(dumped_obj), _start_idx(start_idx) {} + + bool do_bit(BitMap::idx_t bit_offset) { + uintx FLAG_MASK = 0x03; // See comments around MetaspaceClosure::FLAG_MASK + size_t field_offset = size_t(bit_offset - _start_idx) * sizeof(address); + address* ptr_loc = (address*)(_dumped_obj + field_offset); + + uintx old_p_and_bits = (uintx)(*ptr_loc); + uintx flag_bits = (old_p_and_bits & FLAG_MASK); + address old_p = (address)(old_p_and_bits & (~FLAG_MASK)); + address new_p = _builder->get_dumped_addr(old_p); + uintx new_p_and_bits = ((uintx)new_p) | flag_bits; + + log_trace(cds)("Ref: [" PTR_FORMAT "] -> " PTR_FORMAT " => " PTR_FORMAT, + p2i(ptr_loc), p2i(old_p), p2i(new_p)); + + ArchivePtrMarker::set_and_mark_pointer(ptr_loc, (address)(new_p_and_bits)); + return true; // keep iterating the bitmap + } +}; + +void ArchiveBuilder::SourceObjList::relocate(int i, ArchiveBuilder* builder) { + SourceObjInfo* src_info = objs()->at(i); + assert(src_info->should_copy(), "must be"); + BitMap::idx_t start = BitMap::idx_t(src_info->ptrmap_start()); // inclusive + BitMap::idx_t end = BitMap::idx_t(src_info->ptrmap_end()); // exclusive + + RelocateEmbeddedPointers relocator(builder, src_info->dumped_addr(), start); + _ptrmap.iterate(&relocator, start, end); +} + +ArchiveBuilder::ArchiveBuilder(DumpRegion* rw_region, DumpRegion* ro_region) + : _rw_src_objs(), _ro_src_objs(), _src_obj_table(INITIAL_TABLE_SIZE) { + assert(_singleton == NULL, "must be"); + _singleton = this; + + _klasses = new (ResourceObj::C_HEAP, mtClassShared) GrowableArray(4 * K, mtClassShared); + _symbols = new (ResourceObj::C_HEAP, mtClassShared) GrowableArray(256 * K, mtClassShared); + _special_refs = new (ResourceObj::C_HEAP, mtClassShared) GrowableArray(24 * K, mtClassShared); + + _num_instance_klasses = 0; + _num_obj_array_klasses = 0; + _num_type_array_klasses = 0; + _alloc_stats = new (ResourceObj::C_HEAP, mtClassShared) DumpAllocStats; + + _rw_region = rw_region; + _ro_region = ro_region; + + _estimated_metsapceobj_bytes = 0; +} + +ArchiveBuilder::~ArchiveBuilder() { + assert(_singleton == this, "must be"); + _singleton = NULL; + + clean_up_src_obj_table(); + + delete _klasses; + delete _symbols; + delete _special_refs; + delete _alloc_stats; +} + +class GatherKlassesAndSymbols : public UniqueMetaspaceClosure { + ArchiveBuilder* _builder; + +public: + GatherKlassesAndSymbols(ArchiveBuilder* builder) : _builder(builder) {} + + virtual bool do_unique_ref(Ref* ref, bool read_only) { + return _builder->gather_klass_and_symbol(ref, read_only); + } +}; + +bool ArchiveBuilder::gather_klass_and_symbol(MetaspaceClosure::Ref* ref, bool read_only) { + if (ref->obj() == NULL) { + return false; + } + if (get_follow_mode(ref) != make_a_copy) { + return false; + } + if (ref->msotype() == MetaspaceObj::ClassType) { + Klass* klass = (Klass*)ref->obj(); + assert(klass->is_klass(), "must be"); + if (!is_excluded(klass)) { + _klasses->append(klass); + if (klass->is_instance_klass()) { + _num_instance_klasses ++; + } else if (klass->is_objArray_klass()) { + _num_obj_array_klasses ++; + } else { + assert(klass->is_typeArray_klass(), "sanity"); + _num_type_array_klasses ++; + } + } + _estimated_metsapceobj_bytes += BytesPerWord; // See RunTimeSharedClassInfo::get_for() + } else if (ref->msotype() == MetaspaceObj::SymbolType) { + _symbols->append((Symbol*)ref->obj()); + } + + int bytes = ref->size() * BytesPerWord; + _estimated_metsapceobj_bytes += bytes; + + return true; // recurse +} + +void ArchiveBuilder::gather_klasses_and_symbols() { + ResourceMark rm; + log_info(cds)("Gathering classes and symbols ... "); + GatherKlassesAndSymbols doit(this); + iterate_roots(&doit, /*is_relocating_pointers=*/false); + doit.finish(); + + log_info(cds)("Number of classes %d", _num_instance_klasses + _num_obj_array_klasses + _num_type_array_klasses); + log_info(cds)(" instance classes = %5d", _num_instance_klasses); + log_info(cds)(" obj array classes = %5d", _num_obj_array_klasses); + log_info(cds)(" type array classes = %5d", _num_type_array_klasses); + + if (DumpSharedSpaces) { + // To ensure deterministic contents in the static archive, we need to ensure that + // we iterate the MetsapceObjs in a deterministic order. It doesn't matter where + // the MetsapceObjs are located originally, as they are copied sequentially into + // the archive during the iteration. + // + // The only issue here is that the symbol table and the system directories may be + // randomly ordered, so we copy the symbols and klasses into two arrays and sort + // them deterministically. + // + // During -Xshare:dump, the order of Symbol creation is strictly determined by + // the SharedClassListFile (class loading is done in a single thread and the JIT + // is disabled). Also, Symbols are allocated in monotonically increasing addresses + // (see Symbol::operator new(size_t, int)). So if we iterate the Symbols by + // ascending address order, we ensure that all Symbols are copied into deterministic + // locations in the archive. + // + // TODO: in the future, if we want to produce deterministic contents in the + // dynamic archive, we might need to sort the symbols alphabetically (also see + // DynamicArchiveBuilder::sort_methods()). + sort_symbols_and_fix_hash(); + sort_klasses(); + } +} + +int ArchiveBuilder::compare_symbols_by_address(Symbol** a, Symbol** b) { + if (a[0] < b[0]) { + return -1; + } else { + assert(a[0] > b[0], "Duplicated symbol %s unexpected", (*a)->as_C_string()); + return 1; + } +} + +void ArchiveBuilder::sort_symbols_and_fix_hash() { + log_info(cds)("Sorting symbols and fixing identity hash ... "); + os::init_random(0x12345678); + _symbols->sort(compare_symbols_by_address); + for (int i = 0; i < _symbols->length(); i++) { + assert(_symbols->at(i)->is_permanent(), "archived symbols must be permanent"); + _symbols->at(i)->update_identity_hash(); + } +} + +int ArchiveBuilder::compare_klass_by_name(Klass** a, Klass** b) { + return a[0]->name()->fast_compare(b[0]->name()); +} + +void ArchiveBuilder::sort_klasses() { + log_info(cds)("Sorting classes ... "); + _klasses->sort(compare_klass_by_name); +} + +void ArchiveBuilder::iterate_sorted_roots(MetaspaceClosure* it, bool is_relocating_pointers) { + int i; + + int num_symbols = _symbols->length(); + for (i = 0; i < num_symbols; i++) { + it->push(&_symbols->at(i)); + } + + int num_klasses = _klasses->length(); + for (i = 0; i < num_klasses; i++) { + it->push(&_klasses->at(i)); + } + + iterate_roots(it, is_relocating_pointers); +} + +class GatherSortedSourceObjs : public MetaspaceClosure { + ArchiveBuilder* _builder; + +public: + GatherSortedSourceObjs(ArchiveBuilder* builder) : _builder(builder) {} + + virtual bool do_ref(Ref* ref, bool read_only) { + return _builder->gather_one_source_obj(enclosing_ref(), ref, read_only); + } + + virtual void push_special(SpecialRef type, Ref* ref, intptr_t* p) { + assert(type == _method_entry_ref, "only special type allowed for now"); + address src_obj = ref->obj(); + size_t field_offset = pointer_delta(p, src_obj, sizeof(u1)); + _builder->add_special_ref(type, src_obj, field_offset); + }; + + virtual void do_pending_ref(Ref* ref) { + if (ref->obj() != NULL) { + _builder->remember_embedded_pointer_in_copied_obj(enclosing_ref(), ref); + } + } +}; + +bool ArchiveBuilder::gather_one_source_obj(MetaspaceClosure::Ref* enclosing_ref, + MetaspaceClosure::Ref* ref, bool read_only) { + address src_obj = ref->obj(); + if (src_obj == NULL) { + return false; + } + ref->set_keep_after_pushing(); + remember_embedded_pointer_in_copied_obj(enclosing_ref, ref); + + FollowMode follow_mode = get_follow_mode(ref); + SourceObjInfo src_info(ref, read_only, follow_mode); + bool created = false; + SourceObjInfo* p = _src_obj_table.lookup(src_obj); + if (p == NULL) { + p = _src_obj_table.add(src_obj, src_info); + if (_src_obj_table.maybe_grow(MAX_TABLE_SIZE)) { + log_info(cds, hashtables)("Expanded _src_obj_table table to %d", _src_obj_table.table_size()); + } + created = true; + } + + assert(p->read_only() == src_info.read_only(), "must be"); + + if (created && src_info.should_copy()) { + ref->set_user_data((void*)p); + if (read_only) { + _ro_src_objs.append(enclosing_ref, p); + } else { + _rw_src_objs.append(enclosing_ref, p); + } + return true; // Need to recurse into this ref only if we are copying it + } else { + return false; + } +} + +void ArchiveBuilder::add_special_ref(MetaspaceClosure::SpecialRef type, address src_obj, size_t field_offset) { + _special_refs->append(SpecialRefInfo(type, src_obj, field_offset)); +} + +void ArchiveBuilder::remember_embedded_pointer_in_copied_obj(MetaspaceClosure::Ref* enclosing_ref, + MetaspaceClosure::Ref* ref) { + assert(ref->obj() != NULL, "should have checked"); + + if (enclosing_ref != NULL) { + SourceObjInfo* src_info = (SourceObjInfo*)enclosing_ref->user_data(); + if (src_info == NULL) { + // source objects of point_to_it/set_to_null types are not copied + // so we don't need to remember their pointers. + } else { + if (src_info->read_only()) { + _ro_src_objs.remember_embedded_pointer(src_info, ref); + } else { + _rw_src_objs.remember_embedded_pointer(src_info, ref); + } + } + } +} + +void ArchiveBuilder::gather_source_objs() { + ResourceMark rm; + log_info(cds)("Gathering all archivable objects ... "); + GatherSortedSourceObjs doit(this); + iterate_sorted_roots(&doit, /*is_relocating_pointers=*/false); + doit.finish(); +} + +bool ArchiveBuilder::is_excluded(Klass* klass) { + if (klass->is_instance_klass()) { + InstanceKlass* ik = InstanceKlass::cast(klass); + return SystemDictionaryShared::is_excluded_class(ik); + } else if (klass->is_objArray_klass()) { + if (DynamicDumpSharedSpaces) { + // Don't support archiving of array klasses for now (WHY???) + return true; + } + Klass* bottom = ObjArrayKlass::cast(klass)->bottom_klass(); + if (bottom->is_instance_klass()) { + return SystemDictionaryShared::is_excluded_class(InstanceKlass::cast(bottom)); + } + } + + return false; +} + +ArchiveBuilder::FollowMode ArchiveBuilder::get_follow_mode(MetaspaceClosure::Ref *ref) { + address obj = ref->obj(); + if (MetaspaceShared::is_in_shared_metaspace(obj)) { + // Don't dump existing shared metadata again. + return point_to_it; + } else if (ref->msotype() == MetaspaceObj::MethodDataType) { + return set_to_null; + } else { + if (ref->msotype() == MetaspaceObj::ClassType) { + Klass* klass = (Klass*)ref->obj(); + assert(klass->is_klass(), "must be"); + if (is_excluded(klass)) { + ResourceMark rm; + log_debug(cds, dynamic)("Skipping class (excluded): %s", klass->external_name()); + return set_to_null; + } + } + + return make_a_copy; + } +} + +void ArchiveBuilder::dump_rw_region() { + ResourceMark rm; + log_info(cds)("Allocating RW objects ... "); + make_shallow_copies(_rw_region, &_rw_src_objs); +} + +void ArchiveBuilder::dump_ro_region() { + ResourceMark rm; + log_info(cds)("Allocating RO objects ... "); + make_shallow_copies(_ro_region, &_ro_src_objs); +} + +void ArchiveBuilder::make_shallow_copies(DumpRegion *dump_region, + const ArchiveBuilder::SourceObjList* src_objs) { + for (int i = 0; i < src_objs->objs()->length(); i++) { + make_shallow_copy(dump_region, src_objs->objs()->at(i)); + } + log_info(cds)("done (%d objects)", src_objs->objs()->length()); +} + +void ArchiveBuilder::make_shallow_copy(DumpRegion *dump_region, SourceObjInfo* src_info) { + MetaspaceClosure::Ref* ref = src_info->ref(); + address src = ref->obj(); + int bytes = src_info->size_in_bytes(); + char* dest; + size_t alignment = BytesPerWord; + char* oldtop; + char* newtop; + + oldtop = dump_region->top(); + if (ref->msotype() == MetaspaceObj::ClassType) { + // Save a pointer immediate in front of an InstanceKlass, so + // we can do a quick lookup from InstanceKlass* -> RunTimeSharedClassInfo* + // without building another hashtable. See RunTimeSharedClassInfo::get_for() + // in systemDictionaryShared.cpp. + Klass* klass = (Klass*)src; + if (klass->is_instance_klass()) { + SystemDictionaryShared::validate_before_archiving(InstanceKlass::cast(klass)); + dump_region->allocate(sizeof(address), BytesPerWord); + } + } + dest = dump_region->allocate(bytes, alignment); + newtop = dump_region->top(); + + memcpy(dest, src, bytes); + + intptr_t* archived_vtable = MetaspaceShared::get_archived_cpp_vtable(ref->msotype(), (address)dest); + if (archived_vtable != NULL) { + *(address*)dest = (address)archived_vtable; + ArchivePtrMarker::mark_pointer((address*)dest); + } + + log_trace(cds)("Copy: " PTR_FORMAT " ==> " PTR_FORMAT " %d", p2i(src), p2i(dest), bytes); + src_info->set_dumped_addr((address)dest); + + _alloc_stats->record(ref->msotype(), int(newtop - oldtop), src_info->read_only()); +} + +address ArchiveBuilder::get_dumped_addr(address src_obj) const { + SourceObjInfo* p = _src_obj_table.lookup(src_obj); + assert(p != NULL, "must be"); + + return p->dumped_addr(); +} + +void ArchiveBuilder::relocate_embedded_pointers(ArchiveBuilder::SourceObjList* src_objs) { + for (int i = 0; i < src_objs->objs()->length(); i++) { + src_objs->relocate(i, this); + } +} + +void ArchiveBuilder::update_special_refs() { + for (int i = 0; i < _special_refs->length(); i++) { + SpecialRefInfo s = _special_refs->at(i); + size_t field_offset = s.field_offset(); + address src_obj = s.src_obj(); + address dst_obj = get_dumped_addr(src_obj); + intptr_t* src_p = (intptr_t*)(src_obj + field_offset); + intptr_t* dst_p = (intptr_t*)(dst_obj + field_offset); + assert(s.type() == MetaspaceClosure::_method_entry_ref, "only special type allowed for now"); + + assert(*src_p == *dst_p, "must be a copy"); + ArchivePtrMarker::mark_pointer((address*)dst_p); + } +} + +class RefRelocator: public MetaspaceClosure { + ArchiveBuilder* _builder; + +public: + RefRelocator(ArchiveBuilder* builder) : _builder(builder) {} + + virtual bool do_ref(Ref* ref, bool read_only) { + if (ref->not_null()) { + ref->update(_builder->get_dumped_addr(ref->obj())); + ArchivePtrMarker::mark_pointer(ref->addr()); + } + return false; // Do not recurse. + } +}; + +void ArchiveBuilder::relocate_roots() { + ResourceMark rm; + RefRelocator doit(this); + iterate_sorted_roots(&doit, /*is_relocating_pointers=*/true); + doit.finish(); +} + +void ArchiveBuilder::relocate_pointers() { + log_info(cds)("Relocating embedded pointers ... "); + relocate_embedded_pointers(&_rw_src_objs); + relocate_embedded_pointers(&_ro_src_objs); + update_special_refs(); + + log_info(cds)("Relocating external roots ... "); + relocate_roots(); + + log_info(cds)("done"); +} + +// We must relocate the System::_well_known_klasses only after we have copied the +// java objects in during dump_java_heap_objects(): during the object copy, we operate on +// old objects which assert that their klass is the original klass. +void ArchiveBuilder::relocate_well_known_klasses() { + log_info(cds)("Relocating SystemDictionary::_well_known_klasses[] ... "); + ResourceMark rm; + RefRelocator doit(this); + SystemDictionary::well_known_klasses_do(&doit); +} + +void ArchiveBuilder::print_stats(int ro_all, int rw_all, int mc_all) { + _alloc_stats->print_stats(ro_all, rw_all, mc_all); +} + +void ArchiveBuilder::clean_up_src_obj_table() { + SrcObjTableCleaner cleaner; + _src_obj_table.iterate(&cleaner); +} --- /dev/null 2019-11-19 22:05:02.069813242 -0800 +++ new/src/hotspot/share/memory/archiveBuilder.hpp 2020-07-31 23:33:57.544171617 -0700 @@ -0,0 +1,235 @@ +/* + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + * + */ + +#ifndef SHARE_MEMORY_ARCHIVEBUILDER_HPP +#define SHARE_MEMORY_ARCHIVEBUILDER_HPP + +#include "memory/metaspaceClosure.hpp" +#include "utilities/bitMap.inline.hpp" +#include "utilities/growableArray.hpp" +#include "utilities/hashtable.inline.hpp" +#include "utilities/resourceHash.hpp" + +class CHeapBitMap; +class Klass; +class DumpRegion; +class Symbol; +class DumpAllocStats; + +class ArchiveBuilder : public StackObj { +public: + enum FollowMode { + make_a_copy, point_to_it, set_to_null + }; + +private: + class SpecialRefInfo { + // We have a "special pointer" of the given _type at _field_offset of _src_obj. + // See MetaspaceClosure::push_special(). + MetaspaceClosure::SpecialRef _type; + address _src_obj; + size_t _field_offset; + + public: + SpecialRefInfo() {} + SpecialRefInfo(MetaspaceClosure::SpecialRef type, address src_obj, size_t field_offset) + : _type(type), _src_obj(src_obj), _field_offset(field_offset) {} + + MetaspaceClosure::SpecialRef type() const { return _type; } + address src_obj() const { return _src_obj; } + size_t field_offset() const { return _field_offset; } + }; + + class SourceObjInfo { + MetaspaceClosure::Ref* _ref; + uintx _ptrmap_start; // The bit-offset of the start of this object (inclusive) + uintx _ptrmap_end; // The bit-offset of the end of this object (exclusive) + bool _read_only; + FollowMode _follow_mode; + address _dumped_addr; // Address this->obj(), as used by the dumped archive. + + public: + SourceObjInfo(MetaspaceClosure::Ref* ref, bool read_only, FollowMode follow_mode) : + _ref(ref), _ptrmap_start(0), _ptrmap_end(0), _read_only(read_only), _follow_mode(follow_mode) { + if (follow_mode == point_to_it) { + _dumped_addr = ref->obj(); + } else { + _dumped_addr = NULL; + } + } + + bool should_copy() const { return _follow_mode == make_a_copy; } + MetaspaceClosure::Ref* ref() const { return _ref; } + void set_dumped_addr(address dumped_addr) { + assert(should_copy(), "must be"); + assert(_dumped_addr == NULL, "cannot be copied twice"); + assert(dumped_addr != NULL, "must be a valid copy"); + _dumped_addr = dumped_addr; + } + void set_ptrmap_start(uintx v) { _ptrmap_start = v; } + void set_ptrmap_end(uintx v) { _ptrmap_end = v; } + uintx ptrmap_start() const { return _ptrmap_start; } // inclusive + uintx ptrmap_end() const { return _ptrmap_end; } // exclusive + bool read_only() const { return _read_only; } + int size_in_bytes() const { return _ref->size() * BytesPerWord; } + address dumped_addr() const { return _dumped_addr; } + + // convenience accessor + address obj() const { return ref()->obj(); } + }; + + class SourceObjList { + uintx _total_bytes; + GrowableArray* _objs; // Source objects to be archived + CHeapBitMap _ptrmap; // Marks the addresses of the pointer fields + // in the source objects + public: + SourceObjList(); + ~SourceObjList(); + + GrowableArray* objs() const { return _objs; } + + void append(MetaspaceClosure::Ref* enclosing_ref, SourceObjInfo* src_info); + void remember_embedded_pointer(SourceObjInfo* pointing_obj, MetaspaceClosure::Ref* ref); + void relocate(int i, ArchiveBuilder* builder); + + // convenience accessor + SourceObjInfo* at(int i) const { return objs()->at(i); } + }; + + class SrcObjTableCleaner { + public: + bool do_entry(address key, const SourceObjInfo* value) { + delete value->ref(); + return true; + } + }; + + static const int INITIAL_TABLE_SIZE = 15889; + static const int MAX_TABLE_SIZE = 1000000; + + DumpRegion* _rw_region; + DumpRegion* _ro_region; + + SourceObjList _rw_src_objs; // objs to put in rw region + SourceObjList _ro_src_objs; // objs to put in ro region + KVHashtable _src_obj_table; + GrowableArray* _klasses; + GrowableArray* _symbols; + GrowableArray* _special_refs; + + // statistics + int _num_instance_klasses; + int _num_obj_array_klasses; + int _num_type_array_klasses; + DumpAllocStats* _alloc_stats; + + // For global access. + static ArchiveBuilder* _singleton; + +public: + // Use this when you allocate space with MetaspaceShare::read_only_space_alloc() + // outside of ArchiveCompactor::allocate(). These are usually for misc tables + // that are allocated in the RO space. + class OtherROAllocMark { + char* _oldtop; + public: + OtherROAllocMark() { + _oldtop = _singleton->_ro_region->top(); + } + ~OtherROAllocMark(); + }; + +private: + FollowMode get_follow_mode(MetaspaceClosure::Ref *ref); + + void iterate_sorted_roots(MetaspaceClosure* it, bool is_relocating_pointers); + void sort_symbols_and_fix_hash(); + void sort_klasses(); + static int compare_symbols_by_address(Symbol** a, Symbol** b); + static int compare_klass_by_name(Klass** a, Klass** b); + + void make_shallow_copies(DumpRegion *dump_region, const SourceObjList* src_objs); + void make_shallow_copy(DumpRegion *dump_region, SourceObjInfo* src_info); + + void update_special_refs(); + void relocate_embedded_pointers(SourceObjList* src_objs); + void relocate_roots(); + + bool is_excluded(Klass* k); + void clean_up_src_obj_table(); + +protected: + virtual void iterate_roots(MetaspaceClosure* it, bool is_relocating_pointers) = 0; + + // Conservative estimate for number of bytes needed for: + size_t _estimated_metsapceobj_bytes; // all archived MetsapceObj's. + + void set_dump_regions(DumpRegion* rw_region, DumpRegion* ro_region) { + assert(_rw_region == NULL && _ro_region == NULL, "do not change"); + _rw_region = rw_region; + _ro_region = ro_region; + } + +public: + ArchiveBuilder(DumpRegion* rw_region, DumpRegion* ro_region); + ~ArchiveBuilder(); + + void gather_klasses_and_symbols(); + void gather_source_objs(); + bool gather_klass_and_symbol(MetaspaceClosure::Ref* ref, bool read_only); + bool gather_one_source_obj(MetaspaceClosure::Ref* enclosing_ref, MetaspaceClosure::Ref* ref, bool read_only); + void add_special_ref(MetaspaceClosure::SpecialRef type, address src_obj, size_t field_offset); + void remember_embedded_pointer_in_copied_obj(MetaspaceClosure::Ref* enclosing_ref, MetaspaceClosure::Ref* ref); + + void dump_rw_region(); + void dump_ro_region(); + void relocate_pointers(); + void relocate_well_known_klasses(); + + address get_dumped_addr(address src_obj) const; + + // All klasses and symbols that will be copied into the archive + GrowableArray* klasses() const { return _klasses; } + GrowableArray* symbols() const { return _symbols; } + + static ArchiveBuilder* singleton() { + assert(_singleton != NULL, "ArchiveBuilder must be active"); + return _singleton; + } + + static DumpAllocStats* alloc_stats() { + return singleton()->_alloc_stats; + } + + static Klass* get_relocated_klass(Klass* orig_klass) { + Klass* klass = (Klass*)singleton()->get_dumped_addr((address)orig_klass); + assert(klass != NULL && klass->is_klass(), "must be"); + return klass; + } + + void print_stats(int ro_all, int rw_all, int mc_all); +}; + +#endif // SHARE_MEMORY_ARCHIVEBUILDER_HPP