< prev index next >

src/hotspot/share/memory/metaspaceShared.cpp

Print this page


  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "classfile/classLoaderDataGraph.hpp"
  28 #include "classfile/classListParser.hpp"
  29 #include "classfile/classLoaderExt.hpp"
  30 #include "classfile/dictionary.hpp"
  31 #include "classfile/loaderConstraints.hpp"
  32 #include "classfile/javaClasses.inline.hpp"
  33 #include "classfile/placeholders.hpp"
  34 #include "classfile/symbolTable.hpp"
  35 #include "classfile/stringTable.hpp"
  36 #include "classfile/systemDictionary.hpp"
  37 #include "classfile/systemDictionaryShared.hpp"
  38 #include "code/codeCache.hpp"
  39 #include "gc/shared/softRefPolicy.hpp"
  40 #include "interpreter/bytecodeStream.hpp"
  41 #include "interpreter/bytecodes.hpp"
  42 #include "logging/log.hpp"
  43 #include "logging/logMessage.hpp"


  44 #include "memory/filemap.hpp"
  45 #include "memory/heapShared.inline.hpp"
  46 #include "memory/metaspace.hpp"
  47 #include "memory/metaspaceClosure.hpp"
  48 #include "memory/metaspaceShared.hpp"
  49 #include "memory/resourceArea.hpp"
  50 #include "memory/universe.hpp"
  51 #include "memory/dynamicArchive.hpp"
  52 #include "oops/compressedOops.inline.hpp"
  53 #include "oops/instanceClassLoaderKlass.hpp"
  54 #include "oops/instanceMirrorKlass.hpp"
  55 #include "oops/instanceRefKlass.hpp"
  56 #include "oops/methodData.hpp"
  57 #include "oops/objArrayKlass.hpp"
  58 #include "oops/objArrayOop.hpp"
  59 #include "oops/oop.inline.hpp"
  60 #include "oops/typeArrayKlass.hpp"
  61 #include "prims/jvmtiRedefineClasses.hpp"
  62 #include "runtime/handles.inline.hpp"
  63 #include "runtime/os.hpp"
  64 #include "runtime/safepointVerifiers.hpp"
  65 #include "runtime/signature.hpp"
  66 #include "runtime/timerTrace.hpp"
  67 #include "runtime/vmThread.hpp"
  68 #include "runtime/vmOperations.hpp"
  69 #include "utilities/align.hpp"
  70 #include "utilities/bitMap.hpp"
  71 #include "utilities/defaultStream.hpp"
  72 #include "utilities/hashtable.inline.hpp"
  73 #if INCLUDE_G1GC
  74 #include "gc/g1/g1CollectedHeap.hpp"
  75 #endif
  76 
  77 ReservedSpace MetaspaceShared::_shared_rs;
  78 VirtualSpace MetaspaceShared::_shared_vs;
  79 MetaspaceSharedStats MetaspaceShared::_stats;
  80 bool MetaspaceShared::_has_error_classes;
  81 bool MetaspaceShared::_archive_loading_failed = false;
  82 bool MetaspaceShared::_remapped_readwrite = false;
  83 address MetaspaceShared::_i2i_entry_code_buffers = NULL;
  84 size_t MetaspaceShared::_i2i_entry_code_buffers_size = 0;
  85 size_t MetaspaceShared::_core_spaces_size = 0;
  86 void* MetaspaceShared::_shared_metaspace_static_top = NULL;

  87 
  88 // The CDS archive is divided into the following regions:
  89 //     mc  - misc code (the method entry trampolines)
  90 //     rw  - read-write metadata
  91 //     ro  - read-only metadata and read-only tables
  92 //     md  - misc data (the c++ vtables)
  93 //
  94 //     ca0 - closed archive heap space #0
  95 //     ca1 - closed archive heap space #1 (may be empty)
  96 //     oa0 - open archive heap space #0
  97 //     oa1 - open archive heap space #1 (may be empty)
  98 //
  99 // The mc, rw, ro, and md regions are linearly allocated, starting from
 100 // SharedBaseAddress, in the order of mc->rw->ro->md. The size of these 4 regions
 101 // are page-aligned, and there's no gap between any consecutive regions.
 102 //
 103 // These 4 regions are populated in the following steps:
 104 // [1] All classes are loaded in MetaspaceShared::preload_classes(). All metadata are
 105 //     temporarily allocated outside of the shared regions. Only the method entry
 106 //     trampolines are written into the mc region.


 130     // This is just a sanity check and should not appear in any real world usage. This
 131     // happens only if you allocate more than 2GB of shared objects and would require
 132     // millions of shared classes.
 133     vm_exit_during_initialization("Out of memory in the CDS archive",
 134                                   "Please reduce the number of shared classes.");
 135   }
 136 
 137   MetaspaceShared::commit_shared_space_to(newtop);
 138   _top = newtop;
 139   return _top;
 140 }
 141 
 142 char* DumpRegion::allocate(size_t num_bytes, size_t alignment) {
 143   char* p = (char*)align_up(_top, alignment);
 144   char* newtop = p + align_up(num_bytes, alignment);
 145   expand_top_to(newtop);
 146   memset(p, 0, newtop - p);
 147   return p;
 148 }
 149 











 150 void DumpRegion::print(size_t total_bytes) const {
 151   tty->print_cr("%-3s space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used] at " INTPTR_FORMAT,
 152                 _name, used(), percent_of(used(), total_bytes), reserved(), percent_of(used(), reserved()), p2i(_base));

 153 }
 154 
 155 void DumpRegion::print_out_of_space_msg(const char* failing_region, size_t needed_bytes) {
 156   tty->print("[%-8s] " PTR_FORMAT " - " PTR_FORMAT " capacity =%9d, allocated =%9d",
 157              _name, p2i(_base), p2i(_top), int(_end - _base), int(_top - _base));
 158   if (strcmp(_name, failing_region) == 0) {
 159     tty->print_cr(" required = %d", int(needed_bytes));
 160   } else {
 161     tty->cr();
 162   }
 163 }
 164 
 165 void DumpRegion::pack(DumpRegion* next) {
 166   assert(!is_packed(), "sanity");
 167   _end = (char*)align_up(_top, Metaspace::reserve_alignment());
 168   _is_packed = true;
 169   if (next != NULL) {
 170     next->_base = next->_top = this->_end;
 171     next->_end = MetaspaceShared::shared_rs()->end();
 172   }
 173 }
 174 
 175 DumpRegion _mc_region("mc"), _ro_region("ro"), _rw_region("rw"), _md_region("md");
 176 size_t _total_closed_archive_region_size = 0, _total_open_archive_region_size = 0;
 177 
 178 void MetaspaceShared::init_shared_dump_space(DumpRegion* first_space, address first_space_bottom) {
 179   // Start with 0 committed bytes. The memory will be committed as needed by
 180   // MetaspaceShared::commit_shared_space_to().
 181   if (!_shared_vs.initialize(_shared_rs, 0)) {
 182     vm_exit_during_initialization("Unable to allocate memory for shared space");
 183   }
 184   first_space->init(&_shared_rs, (char*)first_space_bottom);
 185 }
 186 
 187 DumpRegion* MetaspaceShared::misc_code_dump_space() {
 188   return &_mc_region;
 189 }
 190 
 191 DumpRegion* MetaspaceShared::read_write_dump_space() {
 192   return &_rw_region;
 193 }
 194 
 195 DumpRegion* MetaspaceShared::read_only_dump_space() {
 196   return &_ro_region;
 197 }
 198 
 199 void MetaspaceShared::pack_dump_space(DumpRegion* current, DumpRegion* next,
 200                                       ReservedSpace* rs) {
 201   current->pack(next);
 202 }
 203 
 204 char* MetaspaceShared::misc_code_space_alloc(size_t num_bytes) {
 205   return _mc_region.allocate(num_bytes);
 206 }
 207 
 208 char* MetaspaceShared::read_only_space_alloc(size_t num_bytes) {
 209   return _ro_region.allocate(num_bytes);
 210 }
 211 
 212 void MetaspaceShared::initialize_runtime_shared_and_meta_spaces() {
 213   assert(UseSharedSpaces, "Must be called when UseSharedSpaces is enabled");
 214 
 215   // If using shared space, open the file that contains the shared space
 216   // and map in the memory before initializing the rest of metaspace (so
 217   // the addresses don't conflict)
 218   FileMapInfo* mapinfo = new FileMapInfo(true);
 219 
 220   // Open the shared archive file, read and validate the header. If
 221   // initialization fails, shared spaces [UseSharedSpaces] are
 222   // disabled and the file is closed.
 223   // Map in spaces now also
 224   if (mapinfo->initialize(true) && map_shared_spaces(mapinfo)) {
 225     size_t cds_total = core_spaces_size();
 226     address cds_address = (address)mapinfo->region_addr(0);
 227     char* cds_end = (char *)align_up(cds_address + cds_total,
 228                                      Metaspace::reserve_alignment());
 229 
 230     // Mapping the dynamic archive before allocating the class space
 231     cds_end = initialize_dynamic_runtime_shared_spaces((char*)cds_address, cds_end);
 232 
 233 #ifdef _LP64
 234     if (Metaspace::using_class_space()) {
 235       // If UseCompressedClassPointers is set then allocate the metaspace area
 236       // above the heap and above the CDS area (if it exists).
 237       Metaspace::allocate_metaspace_compressed_klass_ptrs(cds_end, cds_address);
 238       // map_heap_regions() compares the current narrow oop and klass encodings
 239       // with the archived ones, so it must be done after all encodings are determined.
 240       mapinfo->map_heap_regions();
 241     }
 242     CompressedKlassPointers::set_range(CompressedClassSpaceSize);
 243 #endif // _LP64
 244   } else {
 245     assert(!mapinfo->is_open() && !UseSharedSpaces,
 246            "archive file not closed or shared spaces not disabled.");
 247   }
 248 }
 249 
 250 char* MetaspaceShared::initialize_dynamic_runtime_shared_spaces(
 251         char* static_start, char* static_end) {
 252   assert(UseSharedSpaces, "must be runtime");
 253   char* cds_end = static_end;
 254   if (!DynamicDumpSharedSpaces) {
 255     address dynamic_top = DynamicArchive::map();
 256     if (dynamic_top != NULL) {
 257       assert(dynamic_top > (address)static_start, "Unexpected layout");
 258       MetaspaceObj::expand_shared_metaspace_range(dynamic_top);
 259       cds_end = (char *)align_up(dynamic_top, Metaspace::reserve_alignment());
 260     }
 261   }
 262   return cds_end;
 263 }
 264 
 265 ReservedSpace* MetaspaceShared::reserve_shared_rs(size_t size, size_t alignment,
 266                                                   bool large, char* requested_address) {
 267   if (requested_address != NULL) {
 268     _shared_rs = ReservedSpace(size, alignment, large, requested_address);
 269   } else {
 270     _shared_rs = ReservedSpace(size, alignment, large);
 271   }
 272   return &_shared_rs;
 273 }
 274 
 275 void MetaspaceShared::initialize_dumptime_shared_and_meta_spaces() {
 276   assert(DumpSharedSpaces, "should be called for dump time only");
 277   const size_t reserve_alignment = Metaspace::reserve_alignment();
 278   bool large_pages = false; // No large pages when dumping the CDS archive.
 279   char* shared_base = (char*)align_up((char*)SharedBaseAddress, reserve_alignment);
 280 
 281 #ifdef _LP64
 282   // On 64-bit VM, the heap and class space layout will be the same as if
 283   // you're running in -Xshare:on mode:
 284   //
 285   //                              +-- SharedBaseAddress (default = 0x800000000)
 286   //                              v
 287   // +-..---------+---------+ ... +----+----+----+----+---------------+
 288   // |    Heap    | Archive |     | MC | RW | RO | MD | class space   |
 289   // +-..---------+---------+ ... +----+----+----+----+---------------+
 290   // |<--   MaxHeapSize  -->|     |<-- UnscaledClassSpaceMax = 4GB -->|
 291   //
 292   const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);
 293   const size_t cds_total = align_down(UnscaledClassSpaceMax, reserve_alignment);
 294 #else
 295   // We don't support archives larger than 256MB on 32-bit due to limited virtual address space.
 296   size_t cds_total = align_down(256*M, reserve_alignment);
 297 #endif
 298 







 299   // First try to reserve the space at the specified SharedBaseAddress.
 300   //_shared_rs = ReservedSpace(cds_total, reserve_alignment, large_pages, shared_base);
 301   reserve_shared_rs(cds_total, reserve_alignment, large_pages, shared_base);


 302   if (_shared_rs.is_reserved()) {
 303     assert(shared_base == 0 || _shared_rs.base() == shared_base, "should match");
 304   } else {
 305     // Get a mmap region anywhere if the SharedBaseAddress fails.
 306     //_shared_rs = ReservedSpace(cds_total, reserve_alignment, large_pages);
 307     reserve_shared_rs(cds_total, reserve_alignment, large_pages, NULL);
 308   }
 309   if (!_shared_rs.is_reserved()) {
 310     vm_exit_during_initialization("Unable to reserve memory for shared space",
 311                                   err_msg(SIZE_FORMAT " bytes.", cds_total));
 312   }
 313 
 314 #ifdef _LP64
 315   // During dump time, we allocate 4GB (UnscaledClassSpaceMax) of space and split it up:
 316   // + The upper 1 GB is used as the "temporary compressed class space" -- preload_classes()
 317   //   will store Klasses into this space.
 318   // + The lower 3 GB is used for the archive -- when preload_classes() is done,
 319   //   ArchiveCompactor will copy the class metadata into this space, first the RW parts,
 320   //   then the RO parts.
 321 
 322   assert(UseCompressedOops && UseCompressedClassPointers,
 323       "UseCompressedOops and UseCompressedClassPointers must be set");
 324 
 325   size_t max_archive_size = align_down(cds_total * 3 / 4, reserve_alignment);
 326   ReservedSpace tmp_class_space = _shared_rs.last_part(max_archive_size);
 327   CompressedClassSpaceSize = align_down(tmp_class_space.size(), reserve_alignment);


 425 }
 426 
 427 void MetaspaceShared::commit_shared_space_to(char* newtop) {
 428   Arguments::assert_is_dumping_archive();
 429   char* base = _shared_rs.base();
 430   size_t need_committed_size = newtop - base;
 431   size_t has_committed_size = _shared_vs.committed_size();
 432   if (need_committed_size < has_committed_size) {
 433     return;
 434   }
 435 
 436   size_t min_bytes = need_committed_size - has_committed_size;
 437   size_t preferred_bytes = 1 * M;
 438   size_t uncommitted = _shared_vs.reserved_size() - has_committed_size;
 439 
 440   size_t commit =MAX2(min_bytes, preferred_bytes);
 441   commit = MIN2(commit, uncommitted);
 442   assert(commit <= uncommitted, "sanity");
 443 
 444   bool result = _shared_vs.expand_by(commit, false);


 445   if (!result) {
 446     vm_exit_during_initialization(err_msg("Failed to expand shared space to " SIZE_FORMAT " bytes",
 447                                           need_committed_size));
 448   }
 449 
 450   log_info(cds)("Expanding shared spaces by " SIZE_FORMAT_W(7) " bytes [total " SIZE_FORMAT_W(9)  " bytes ending at %p]",
 451                 commit, _shared_vs.actual_committed_size(), _shared_vs.high());
 452 }
 453 




 454 // Read/write a data stream for restoring/preserving metadata pointers and
 455 // miscellaneous data from/to the shared archive file.
 456 
 457 void MetaspaceShared::serialize(SerializeClosure* soc) {
 458   int tag = 0;
 459   soc->do_tag(--tag);
 460 
 461   // Verify the sizes of various metadata in the system.
 462   soc->do_tag(sizeof(Method));
 463   soc->do_tag(sizeof(ConstMethod));
 464   soc->do_tag(arrayOopDesc::base_offset_in_bytes(T_BYTE));
 465   soc->do_tag(sizeof(ConstantPool));
 466   soc->do_tag(sizeof(ConstantPoolCache));
 467   soc->do_tag(objArrayOopDesc::base_offset_in_bytes());
 468   soc->do_tag(typeArrayOopDesc::base_offset_in_bytes(T_BYTE));
 469   soc->do_tag(sizeof(Symbol));
 470 
 471   // Dump/restore miscellaneous metadata.

 472   Universe::serialize(soc);
 473   soc->do_tag(--tag);
 474 
 475   // Dump/restore references to commonly used names and signatures.
 476   vmSymbols::serialize(soc);
 477   soc->do_tag(--tag);
 478 
 479   // Dump/restore the symbol/string/subgraph_info tables
 480   SymbolTable::serialize_shared_table_header(soc);
 481   StringTable::serialize_shared_table_header(soc);
 482   HeapShared::serialize_subgraph_info_table_header(soc);
 483   SystemDictionaryShared::serialize_dictionary_headers(soc);
 484 
 485   JavaClasses::serialize_offsets(soc);
 486   InstanceMirrorKlass::serialize_offsets(soc);
 487   soc->do_tag(--tag);
 488 
 489   serialize_cloned_cpp_vtptrs(soc);
 490   soc->do_tag(--tag);
 491 
 492   soc->do_tag(666);
 493 }
 494 
 495 address MetaspaceShared::i2i_entry_code_buffers(size_t total_size) {
 496   if (DumpSharedSpaces) {
 497     if (_i2i_entry_code_buffers == NULL) {
 498       _i2i_entry_code_buffers = (address)misc_code_space_alloc(total_size);
 499       _i2i_entry_code_buffers_size = total_size;
 500     }
 501   } else if (UseSharedSpaces) {
 502     assert(_i2i_entry_code_buffers != NULL, "must already been initialized");
 503   } else {
 504     return NULL;
 505   }


 688   }
 689   static CppVtableInfo* _info;
 690 
 691   static int get_vtable_length(const char* name);
 692 
 693 public:
 694   // Allocate and initialize the C++ vtable, starting from top, but do not go past end.
 695   static intptr_t* allocate(const char* name);
 696 
 697   // Clone the vtable to ...
 698   static intptr_t* clone_vtable(const char* name, CppVtableInfo* info);
 699 
 700   static void zero_vtable_clone() {
 701     assert(DumpSharedSpaces, "dump-time only");
 702     _info->zero();
 703   }
 704 
 705   // Switch the vtable pointer to point to the cloned vtable.
 706   static void patch(Metadata* obj) {
 707     assert(DumpSharedSpaces, "dump-time only");

 708     *(void**)obj = (void*)(_info->cloned_vtable());

 709   }
 710 
 711   static bool is_valid_shared_object(const T* obj) {
 712     intptr_t* vptr = *(intptr_t**)obj;
 713     return vptr == _info->cloned_vtable();
 714   }
 715 };
 716 
 717 template <class T> CppVtableInfo* CppVtableCloner<T>::_info = NULL;
 718 
 719 template <class T>
 720 intptr_t* CppVtableCloner<T>::allocate(const char* name) {
 721   assert(is_aligned(_md_region.top(), sizeof(intptr_t)), "bad alignment");
 722   int n = get_vtable_length(name);
 723   _info = (CppVtableInfo*)_md_region.allocate(CppVtableInfo::byte_size(n), sizeof(intptr_t));
 724   _info->set_vtable_size(n);
 725 
 726   intptr_t* p = clone_vtable(name, _info);
 727   assert((char*)p == _md_region.top(), "must be");
 728 


 782 int CppVtableCloner<T>::get_vtable_length(const char* name) {
 783   CppVtableTesterA<T> a;
 784   CppVtableTesterB<T> b;
 785 
 786   intptr_t* avtable = vtable_of(a);
 787   intptr_t* bvtable = vtable_of(b);
 788 
 789   // Start at slot 1, because slot 0 may be RTTI (on Solaris/Sparc)
 790   int vtable_len = 1;
 791   for (; ; vtable_len++) {
 792     if (avtable[vtable_len] != bvtable[vtable_len]) {
 793       break;
 794     }
 795   }
 796   log_debug(cds, vtables)("Found   %3d vtable entries for %s", vtable_len, name);
 797 
 798   return vtable_len;
 799 }
 800 
 801 #define ALLOC_CPP_VTABLE_CLONE(c) \
 802   _cloned_cpp_vtptrs[c##_Kind] = CppVtableCloner<c>::allocate(#c);

 803 
 804 #define CLONE_CPP_VTABLE(c) \
 805   p = CppVtableCloner<c>::clone_vtable(#c, (CppVtableInfo*)p);
 806 
 807 #define ZERO_CPP_VTABLE(c) \
 808  CppVtableCloner<c>::zero_vtable_clone();
 809 
 810 //------------------------------ for DynamicDumpSharedSpaces - start
 811 #define DECLARE_CLONED_VTABLE_KIND(c) c ## _Kind,
 812 
 813 enum {
 814   CPP_VTABLE_PATCH_TYPES_DO(DECLARE_CLONED_VTABLE_KIND)
 815   _num_cloned_vtable_kinds
 816 };
 817 
 818 static intptr_t** _cloned_cpp_vtptrs = NULL;
 819 
 820 void MetaspaceShared::serialize_cloned_cpp_vtptrs(SerializeClosure* soc) {
 821   soc->do_ptr((void**)&_cloned_cpp_vtptrs);
 822 }


 948   assert(is_in_shared_metaspace(m), "must be");
 949   return CppVtableCloner<Method>::is_valid_shared_object(m);
 950 }
 951 
 952 void WriteClosure::do_oop(oop* o) {
 953   if (*o == NULL) {
 954     _dump_region->append_intptr_t(0);
 955   } else {
 956     assert(HeapShared::is_heap_object_archiving_allowed(),
 957            "Archiving heap object is not allowed");
 958     _dump_region->append_intptr_t(
 959       (intptr_t)CompressedOops::encode_not_null(*o));
 960   }
 961 }
 962 
 963 void WriteClosure::do_region(u_char* start, size_t size) {
 964   assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment");
 965   assert(size % sizeof(intptr_t) == 0, "bad size");
 966   do_tag((int)size);
 967   while (size > 0) {
 968     _dump_region->append_intptr_t(*(intptr_t*)start);
 969     start += sizeof(intptr_t);
 970     size -= sizeof(intptr_t);
 971   }
 972 }
 973 
 974 // This is for dumping detailed statistics for the allocations
 975 // in the shared spaces.
 976 class DumpAllocStats : public ResourceObj {
 977 public:
 978 
 979   // Here's poor man's enum inheritance
 980 #define SHAREDSPACE_OBJ_TYPES_DO(f) \
 981   METASPACE_OBJ_TYPES_DO(f) \
 982   f(SymbolHashentry) \
 983   f(SymbolBucket) \
 984   f(StringHashentry) \
 985   f(StringBucket) \
 986   f(Other)
 987 
 988   enum Type {


1112 
1113 #undef fmt_stats
1114 }
1115 
1116 // Populate the shared space.
1117 
1118 class VM_PopulateDumpSharedSpace: public VM_Operation {
1119 private:
1120   GrowableArray<MemRegion> *_closed_archive_heap_regions;
1121   GrowableArray<MemRegion> *_open_archive_heap_regions;
1122 
1123   GrowableArray<ArchiveHeapOopmapInfo> *_closed_archive_heap_oopmaps;
1124   GrowableArray<ArchiveHeapOopmapInfo> *_open_archive_heap_oopmaps;
1125 
1126   void dump_java_heap_objects() NOT_CDS_JAVA_HEAP_RETURN;
1127   void dump_archive_heap_oopmaps() NOT_CDS_JAVA_HEAP_RETURN;
1128   void dump_archive_heap_oopmaps(GrowableArray<MemRegion>* regions,
1129                                  GrowableArray<ArchiveHeapOopmapInfo>* oopmaps);
1130   void dump_symbols();
1131   char* dump_read_only_tables();

1132   void print_region_stats();

1133   void print_heap_region_stats(GrowableArray<MemRegion> *heap_mem,
1134                                const char *name, const size_t total_size);


1135 public:
1136 
1137   VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
1138   void doit();   // outline because gdb sucks
1139   static void write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region, bool read_only,  bool allow_exec) {
1140     mapinfo->write_region(region_idx, dump_region->base(), dump_region->used(), read_only, allow_exec);
1141   }
1142   bool allow_nested_vm_operations() const { return true; }
1143 }; // class VM_PopulateDumpSharedSpace
1144 
1145 class SortedSymbolClosure: public SymbolClosure {
1146   GrowableArray<Symbol*> _symbols;
1147   virtual void do_symbol(Symbol** sym) {
1148     assert((*sym)->is_permanent(), "archived symbols must be permanent");
1149     _symbols.append(*sym);
1150   }
1151   static int compare_symbols_by_address(Symbol** a, Symbol** b) {
1152     if (a[0] < b[0]) {
1153       return -1;
1154     } else if (a[0] == b[0]) {


1220       oldtop = _ro_region.top();
1221       p = _ro_region.allocate(bytes, alignment);
1222       newtop = _ro_region.top();
1223     } else {
1224       oldtop = _rw_region.top();
1225       if (ref->msotype() == MetaspaceObj::ClassType) {
1226         // Save a pointer immediate in front of an InstanceKlass, so
1227         // we can do a quick lookup from InstanceKlass* -> RunTimeSharedClassInfo*
1228         // without building another hashtable. See RunTimeSharedClassInfo::get_for()
1229         // in systemDictionaryShared.cpp.
1230         Klass* klass = (Klass*)obj;
1231         if (klass->is_instance_klass()) {
1232           SystemDictionaryShared::validate_before_archiving(InstanceKlass::cast(klass));
1233           _rw_region.allocate(sizeof(address), BytesPerWord);
1234         }
1235       }
1236       p = _rw_region.allocate(bytes, alignment);
1237       newtop = _rw_region.top();
1238     }
1239     memcpy(p, obj, bytes);

1240     assert(_new_loc_table->lookup(obj) == NULL, "each object can be relocated at most once");
1241     _new_loc_table->add(obj, (address)p);
1242     log_trace(cds)("Copy: " PTR_FORMAT " ==> " PTR_FORMAT " %d", p2i(obj), p2i(p), bytes);
1243     if (_new_loc_table->maybe_grow(MAX_TABLE_SIZE)) {
1244       log_info(cds, hashtables)("Expanded _new_loc_table to %d", _new_loc_table->table_size());
1245     }
1246     _alloc_stats->record(ref->msotype(), int(newtop - oldtop), read_only);
1247   }
1248 
1249   static address get_new_loc(MetaspaceClosure::Ref* ref) {
1250     address* pp = _new_loc_table->lookup(ref->obj());
1251     assert(pp != NULL, "must be");
1252     return *pp;
1253   }
1254 
1255 private:
1256   // Makes a shallow copy of visited MetaspaceObj's
1257   class ShallowCopier: public UniqueMetaspaceClosure {
1258     bool _read_only;
1259   public:
1260     ShallowCopier(bool read_only) : _read_only(read_only) {}
1261 
1262     virtual bool do_unique_ref(Ref* ref, bool read_only) {
1263       if (read_only == _read_only) {
1264         allocate(ref, read_only);
1265       }
1266       return true; // recurse into ref.obj()
1267     }
1268   };
1269 
1270   // Relocate embedded pointers within a MetaspaceObj's shallow copy
1271   class ShallowCopyEmbeddedRefRelocator: public UniqueMetaspaceClosure {
1272   public:
1273     virtual bool do_unique_ref(Ref* ref, bool read_only) {
1274       address new_loc = get_new_loc(ref);
1275       RefRelocator refer;
1276       ref->metaspace_pointers_do_at(&refer, new_loc);
1277       return true; // recurse into ref.obj()
1278     }









1279   };
1280 
1281   // Relocate a reference to point to its shallow copy
1282   class RefRelocator: public MetaspaceClosure {
1283   public:
1284     virtual bool do_ref(Ref* ref, bool read_only) {
1285       if (ref->not_null()) {
1286         ref->update(get_new_loc(ref));

1287       }
1288       return false; // Do not recurse.
1289     }
1290   };
1291 
1292 #ifdef ASSERT
1293   class IsRefInArchiveChecker: public MetaspaceClosure {
1294   public:
1295     virtual bool do_ref(Ref* ref, bool read_only) {
1296       if (ref->not_null()) {
1297         char* obj = (char*)ref->obj();
1298         assert(_ro_region.contains(obj) || _rw_region.contains(obj),
1299                "must be relocated to point to CDS archive");
1300       }
1301       return false; // Do not recurse.
1302     }
1303   };
1304 #endif
1305 
1306 public:


1333       ResourceMark rm;
1334       ShallowCopyEmbeddedRefRelocator emb_reloc;
1335       iterate_roots(&emb_reloc);
1336     }
1337     {
1338       tty->print_cr("Relocating external roots ... ");
1339       ResourceMark rm;
1340       RefRelocator ext_reloc;
1341       iterate_roots(&ext_reloc);
1342     }
1343 
1344 #ifdef ASSERT
1345     {
1346       tty->print_cr("Verifying external roots ... ");
1347       ResourceMark rm;
1348       IsRefInArchiveChecker checker;
1349       iterate_roots(&checker);
1350     }
1351 #endif
1352 
1353 
1354     // cleanup
1355     _ssc = NULL;
1356   }
1357 
1358   // We must relocate the System::_well_known_klasses only after we have copied the
1359   // java objects in during dump_java_heap_objects(): during the object copy, we operate on
1360   // old objects which assert that their klass is the original klass.
1361   static void relocate_well_known_klasses() {
1362     {
1363       tty->print_cr("Relocating SystemDictionary::_well_known_klasses[] ... ");
1364       ResourceMark rm;
1365       RefRelocator ext_reloc;
1366       SystemDictionary::well_known_klasses_do(&ext_reloc);
1367     }
1368     // NOTE: after this point, we shouldn't have any globals that can reach the old
1369     // objects.
1370 
1371     // We cannot use any of the objects in the heap anymore (except for the
1372     // shared strings) because their headers no longer point to valid Klasses.
1373   }


1423   }
1424   remove_java_mirror_in_classes();
1425   tty->print_cr("done. ");
1426 
1427   SystemDictionaryShared::write_to_archive();
1428 
1429   size_t vtptrs_bytes = _num_cloned_vtable_kinds * sizeof(intptr_t*);
1430   _cloned_cpp_vtptrs = (intptr_t**)_ro_region.allocate(vtptrs_bytes, sizeof(intptr_t*));
1431 
1432   // Write the other data to the output array.
1433   char* start = _ro_region.top();
1434   WriteClosure wc(&_ro_region);
1435   MetaspaceShared::serialize(&wc);
1436 
1437   // Write the bitmaps for patching the archive heap regions
1438   dump_archive_heap_oopmaps();
1439 
1440   return start;
1441 }
1442 




















































1443 void VM_PopulateDumpSharedSpace::doit() {



1444   // We should no longer allocate anything from the metaspace, so that:
1445   //
1446   // (1) Metaspace::allocate might trigger GC if we have run out of
1447   //     committed metaspace, but we can't GC because we're running
1448   //     in the VM thread.
1449   // (2) ArchiveCompactor needs to work with a stable set of MetaspaceObjs.
1450   Metaspace::freeze();
1451   DEBUG_ONLY(SystemDictionaryShared::NoClassLoadingMark nclm);
1452 
1453   Thread* THREAD = VMThread::vm_thread();
1454 
1455   FileMapInfo::check_nonempty_dir_in_shared_path_table();
1456 
1457   NOT_PRODUCT(SystemDictionary::verify();)
1458   // The following guarantee is meant to ensure that no loader constraints
1459   // exist yet, since the constraints table is not shared.  This becomes
1460   // more important now that we don't re-initialize vtables/itables for
1461   // shared classes at runtime, where constraints were previously created.
1462   guarantee(SystemDictionary::constraints()->number_of_entries() == 0,
1463             "loader constraints are not saved");
1464   guarantee(SystemDictionary::placeholders()->number_of_entries() == 0,
1465           "placeholders are not saved");
1466 
1467   // At this point, many classes have been loaded.
1468   // Gather systemDictionary classes in a global array and do everything to
1469   // that so we don't have to walk the SystemDictionary again.
1470   SystemDictionaryShared::check_excluded_classes();
1471   _global_klass_objects = new GrowableArray<Klass*>(1000);
1472   CollectClassesClosure collect_classes;
1473   ClassLoaderDataGraph::loaded_classes_do(&collect_classes);
1474 
1475   tty->print_cr("Number of classes %d", _global_klass_objects->length());
1476   {
1477     int num_type_array = 0, num_obj_array = 0, num_inst = 0;
1478     for (int i = 0; i < _global_klass_objects->length(); i++) {
1479       Klass* k = _global_klass_objects->at(i);
1480       if (k->is_instance_klass()) {
1481         num_inst ++;
1482       } else if (k->is_objArray_klass()) {
1483         num_obj_array ++;
1484       } else {
1485         assert(k->is_typeArray_klass(), "sanity");
1486         num_type_array ++;
1487       }
1488     }
1489     tty->print_cr("    instance classes   = %5d", num_inst);
1490     tty->print_cr("    obj array classes  = %5d", num_obj_array);
1491     tty->print_cr("    type array classes = %5d", num_type_array);
1492   }
1493 
1494   // Ensure the ConstMethods won't be modified at run-time
1495   tty->print("Updating ConstMethods ... ");
1496   rewrite_nofast_bytecodes_and_calculate_fingerprints();
1497   tty->print_cr("done. ");
1498 
1499   // Remove all references outside the metadata
1500   tty->print("Removing unshareable information ... ");
1501   remove_unshareable_in_classes();
1502   tty->print_cr("done. ");
1503 
1504   ArchiveCompactor::initialize();
1505   ArchiveCompactor::copy_and_compact();
1506 
1507   dump_symbols();
1508 
1509   // Dump supported java heap objects
1510   _closed_archive_heap_regions = NULL;
1511   _open_archive_heap_regions = NULL;
1512   dump_java_heap_objects();
1513 
1514   ArchiveCompactor::relocate_well_known_klasses();
1515 
1516   char* serialized_data_start = dump_read_only_tables();
1517   _ro_region.pack(&_md_region);
1518 
1519   char* vtbl_list = _md_region.top();
1520   MetaspaceShared::allocate_cpp_vtable_clones();
1521   _md_region.pack();
1522 
1523   // The 4 core spaces are allocated consecutively mc->rw->ro->md, so there total size
1524   // is just the spaces between the two ends.
1525   size_t core_spaces_size = _md_region.end() - _mc_region.base();
1526   assert(core_spaces_size == (size_t)align_up(core_spaces_size, Metaspace::reserve_alignment()),
1527          "should already be aligned");
1528 
1529   // During patching, some virtual methods may be called, so at this point
1530   // the vtables must contain valid methods (as filled in by CppVtableCloner::allocate).
1531   MetaspaceShared::patch_cpp_vtable_pointers();
1532 
1533   // The vtable clones contain addresses of the current process.
1534   // We don't want to write these addresses into the archive.
1535   MetaspaceShared::zero_cpp_vtable_clones_for_writing();
1536 





1537   // Create and write the archive file that maps the shared spaces.
1538 
1539   FileMapInfo* mapinfo = new FileMapInfo(true);
1540   mapinfo->populate_header(os::vm_allocation_granularity());
1541   mapinfo->set_serialized_data_start(serialized_data_start);
1542   mapinfo->set_misc_data_patching_start(vtbl_list);
1543   mapinfo->set_i2i_entry_code_buffers(MetaspaceShared::i2i_entry_code_buffers(),
1544                                       MetaspaceShared::i2i_entry_code_buffers_size());
1545   mapinfo->set_core_spaces_size(core_spaces_size);
1546   mapinfo->open_for_write();
1547 
1548   // NOTE: md contains the trampoline code for method entries, which are patched at run time,
1549   // so it needs to be read/write.
1550   write_region(mapinfo, MetaspaceShared::mc, &_mc_region, /*read_only=*/false,/*allow_exec=*/true);
1551   write_region(mapinfo, MetaspaceShared::rw, &_rw_region, /*read_only=*/false,/*allow_exec=*/false);
1552   write_region(mapinfo, MetaspaceShared::ro, &_ro_region, /*read_only=*/true, /*allow_exec=*/false);
1553   write_region(mapinfo, MetaspaceShared::md, &_md_region, /*read_only=*/false,/*allow_exec=*/false);
1554 


1555   _total_closed_archive_region_size = mapinfo->write_archive_heap_regions(
1556                                         _closed_archive_heap_regions,
1557                                         _closed_archive_heap_oopmaps,
1558                                         MetaspaceShared::first_closed_archive_heap_region,
1559                                         MetaspaceShared::max_closed_archive_heap_region);
1560   _total_open_archive_region_size = mapinfo->write_archive_heap_regions(
1561                                         _open_archive_heap_regions,
1562                                         _open_archive_heap_oopmaps,
1563                                         MetaspaceShared::first_open_archive_heap_region,
1564                                         MetaspaceShared::max_open_archive_heap_region);
1565 

1566   mapinfo->set_header_crc(mapinfo->compute_header_crc());
1567   mapinfo->write_header();
1568   mapinfo->close();
1569 
1570   // Restore the vtable in case we invoke any virtual methods.
1571   MetaspaceShared::clone_cpp_vtables((intptr_t*)vtbl_list);
1572 
1573   print_region_stats();
1574 
1575   if (log_is_enabled(Info, cds)) {
1576     ArchiveCompactor::alloc_stats()->print_stats(int(_ro_region.used()), int(_rw_region.used()),
1577                                                  int(_mc_region.used()), int(_md_region.used()));
1578   }
1579 
1580   if (PrintSystemDictionaryAtExit) {
1581     SystemDictionary::print();
1582   }
1583 
1584   if (AllowArchivingWithJavaAgent) {
1585     warning("This archive was created with AllowArchivingWithJavaAgent. It should be used "
1586             "for testing purposes only and should not be used in a production environment");
1587   }
1588 
1589   // There may be other pending VM operations that operate on the InstanceKlasses,
1590   // which will fail because InstanceKlasses::remove_unshareable_info()
1591   // has been called. Forget these operations and exit the VM directly.
1592   vm_direct_exit(0);
1593 }
1594 
1595 void VM_PopulateDumpSharedSpace::print_region_stats() {
1596   // Print statistics of all the regions


1597   const size_t total_reserved = _ro_region.reserved()  + _rw_region.reserved() +
1598                                 _mc_region.reserved()  + _md_region.reserved() +

1599                                 _total_closed_archive_region_size +
1600                                 _total_open_archive_region_size;
1601   const size_t total_bytes = _ro_region.used()  + _rw_region.used() +
1602                              _mc_region.used()  + _md_region.used() +

1603                              _total_closed_archive_region_size +
1604                              _total_open_archive_region_size;
1605   const double total_u_perc = percent_of(total_bytes, total_reserved);
1606 
1607   _mc_region.print(total_reserved);
1608   _rw_region.print(total_reserved);
1609   _ro_region.print(total_reserved);
1610   _md_region.print(total_reserved);

1611   print_heap_region_stats(_closed_archive_heap_regions, "ca", total_reserved);
1612   print_heap_region_stats(_open_archive_heap_regions, "oa", total_reserved);
1613 
1614   tty->print_cr("total    : " SIZE_FORMAT_W(9) " [100.0%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used]",
1615                  total_bytes, total_reserved, total_u_perc);
1616 }
1617 





1618 void VM_PopulateDumpSharedSpace::print_heap_region_stats(GrowableArray<MemRegion> *heap_mem,
1619                                                          const char *name, const size_t total_size) {
1620   int arr_len = heap_mem == NULL ? 0 : heap_mem->length();
1621   for (int i = 0; i < arr_len; i++) {
1622       char* start = (char*)heap_mem->at(i).start();
1623       size_t size = heap_mem->at(i).byte_size();
1624       char* top = start + size;
1625       tty->print_cr("%s%d space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [100.0%% used] at " INTPTR_FORMAT,
1626                     name, i, size, size/double(total_size)*100.0, size, p2i(start));
1627 
1628   }
1629 }
1630 
1631 // Update a Java object to point its Klass* to the new location after
1632 // shared archive has been compacted.
1633 void MetaspaceShared::relocate_klass_ptr(oop o) {
1634   assert(DumpSharedSpaces, "sanity");
1635   Klass* k = ArchiveCompactor::get_relocated_klass(o->klass());
1636   o->set_klass(k);
1637 }
1638 
1639 Klass* MetaspaceShared::get_relocated_klass(Klass *k) {
1640   assert(DumpSharedSpaces, "sanity");
1641   return ArchiveCompactor::get_relocated_klass(k);




1642 }
1643 
1644 class LinkSharedClassesClosure : public KlassClosure {
1645   Thread* THREAD;
1646   bool    _made_progress;
1647  public:
1648   LinkSharedClassesClosure(Thread* thread) : THREAD(thread), _made_progress(false) {}
1649 
1650   void reset()               { _made_progress = false; }
1651   bool made_progress() const { return _made_progress; }
1652 
1653   void do_klass(Klass* k) {
1654     if (k->is_instance_klass()) {
1655       InstanceKlass* ik = InstanceKlass::cast(k);
1656       // Link the class to cause the bytecodes to be rewritten and the
1657       // cpcache to be created. Class verification is done according
1658       // to -Xverify setting.
1659       _made_progress |= MetaspaceShared::try_link_class(ik, THREAD);
1660       guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1661 


1919   int old_tag;
1920   old_tag = (int)(intptr_t)nextPtr();
1921   // do_int(&old_tag);
1922   assert(tag == old_tag, "old tag doesn't match");
1923   FileMapInfo::assert_mark(tag == old_tag);
1924 }
1925 
1926 void ReadClosure::do_oop(oop *p) {
1927   narrowOop o = (narrowOop)nextPtr();
1928   if (o == 0 || !HeapShared::open_archive_heap_region_mapped()) {
1929     p = NULL;
1930   } else {
1931     assert(HeapShared::is_heap_object_archiving_allowed(),
1932            "Archived heap object is not allowed");
1933     assert(HeapShared::open_archive_heap_region_mapped(),
1934            "Open archive heap region is not mapped");
1935     *p = HeapShared::decode_from_archive(o);
1936   }
1937 }
1938 








1939 void ReadClosure::do_region(u_char* start, size_t size) {
1940   assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment");
1941   assert(size % sizeof(intptr_t) == 0, "bad size");
1942   do_tag((int)size);
1943   while (size > 0) {
1944     *(intptr_t*)start = nextPtr();
1945     start += sizeof(intptr_t);
1946     size -= sizeof(intptr_t);
1947   }
1948 }
1949 
1950 void MetaspaceShared::set_shared_metaspace_range(void* base, void* top) {
1951   _shared_metaspace_static_top = top;

1952   MetaspaceObj::set_shared_metaspace_range(base, top);
1953 }
1954 
1955 // Return true if given address is in the misc data region
1956 bool MetaspaceShared::is_in_shared_region(const void* p, int idx) {
1957   return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_region(p, idx);
1958 }
1959 
1960 bool MetaspaceShared::is_in_trampoline_frame(address addr) {
1961   if (UseSharedSpaces && is_in_shared_region(addr, MetaspaceShared::mc)) {
1962     return true;
1963   }
1964   return false;
1965 }
1966 
1967 bool MetaspaceShared::is_shared_dynamic(void* p) {
1968   if ((p < MetaspaceObj::shared_metaspace_top()) &&
1969       (p >= _shared_metaspace_static_top)) {
1970     return true;
1971   } else {
1972     return false;
1973   }
1974 }
1975 
1976 // Map shared spaces at requested addresses and return if succeeded.
1977 bool MetaspaceShared::map_shared_spaces(FileMapInfo* mapinfo) {
1978   size_t image_alignment = mapinfo->alignment();
1979 
1980 #ifndef _WINDOWS
1981   // Map in the shared memory and then map the regions on top of it.
1982   // On Windows, don't map the memory here because it will cause the
1983   // mappings of the regions to fail.
1984   ReservedSpace shared_rs = mapinfo->reserve_shared_memory();
1985   if (!shared_rs.is_reserved()) return false;
1986 #endif
1987 
1988   assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces");

1989 
1990   // Map each shared region
1991   int regions[] = {mc, rw, ro, md};
1992   size_t len = sizeof(regions)/sizeof(int);
1993   char* saved_base[] = {NULL, NULL, NULL, NULL};
1994   char* top = mapinfo->map_regions(regions, saved_base, len );
1995 
1996   if (top != NULL &&
1997       (image_alignment == (size_t)os::vm_allocation_granularity()) &&
1998       mapinfo->validate_shared_path_table()) {
1999     // Success -- set up MetaspaceObj::_shared_metaspace_{base,top} for
2000     // fast checking in MetaspaceShared::is_in_shared_metaspace() and
2001     // MetaspaceObj::is_shared().
2002     _core_spaces_size = mapinfo->core_spaces_size();
2003     set_shared_metaspace_range((void*)saved_base[0], (void*)top);
2004     return true;


2005   } else {
2006     mapinfo->unmap_regions(regions, saved_base, len);
2007 #ifndef _WINDOWS
2008     // Release the entire mapped region
2009     shared_rs.release();
2010 #endif
2011     // If -Xshare:on is specified, print out the error message and exit VM,
2012     // otherwise, set UseSharedSpaces to false and continue.
2013     if (RequireSharedSpaces || PrintSharedArchiveAndExit) {
2014       vm_exit_during_initialization("Unable to use shared archive.", "Failed map_region for using -Xshare:on.");
2015     } else {
2016       FLAG_SET_DEFAULT(UseSharedSpaces, false);








































































































































































































2017     }
2018     return false;



























































2019   }
2020 }
2021 
2022 // Read the miscellaneous data from the shared file, and
2023 // serialize it out to its various destinations.
2024 
2025 void MetaspaceShared::initialize_shared_spaces() {
2026   FileMapInfo *mapinfo = FileMapInfo::current_info();
2027   _i2i_entry_code_buffers = mapinfo->i2i_entry_code_buffers();
2028   _i2i_entry_code_buffers_size = mapinfo->i2i_entry_code_buffers_size();
2029   // _core_spaces_size is loaded from the shared archive immediatelly after mapping
2030   assert(_core_spaces_size == mapinfo->core_spaces_size(), "sanity");
2031   char* buffer = mapinfo->misc_data_patching_start();
2032   clone_cpp_vtables((intptr_t*)buffer);
2033 
2034   // Verify various attributes of the archive, plus initialize the
2035   // shared string/symbol tables
2036   buffer = mapinfo->serialized_data_start();
2037   intptr_t* array = (intptr_t*)buffer;
2038   ReadClosure rc(&array);
2039   serialize(&rc);
2040 
2041   // Initialize the run-time symbol table.
2042   SymbolTable::create_table();
2043 
2044   mapinfo->patch_archived_heap_embedded_pointers();
2045 
2046   // Close the mapinfo file
2047   mapinfo->close();









2048 
2049   if (PrintSharedArchiveAndExit) {
2050     if (PrintSharedDictionary) {
2051       tty->print_cr("\nShared classes:\n");
2052       SystemDictionaryShared::print_on(tty);
2053     }
2054     if (_archive_loading_failed) {
2055       tty->print_cr("archive is invalid");
2056       vm_exit(1);
2057     } else {
2058       tty->print_cr("archive is valid");
2059       vm_exit(0);
2060     }
2061   }
2062 }
2063 
2064 // JVM/TI RedefineClasses() support:
2065 bool MetaspaceShared::remap_shared_readonly_as_readwrite() {
2066   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2067 
2068   if (UseSharedSpaces) {
2069     // remap the shared readonly space to shared readwrite, private
2070     FileMapInfo* mapinfo = FileMapInfo::current_info();
2071     if (!mapinfo->remap_shared_readonly_as_readwrite()) {
2072       return false;
2073     }
2074     if (FileMapInfo::dynamic_info() != NULL) {


2076       if (!mapinfo->remap_shared_readonly_as_readwrite()) {
2077         return false;
2078       }
2079     }
2080     _remapped_readwrite = true;
2081   }
2082   return true;
2083 }
2084 
2085 void MetaspaceShared::report_out_of_space(const char* name, size_t needed_bytes) {
2086   // This is highly unlikely to happen on 64-bits because we have reserved a 4GB space.
2087   // On 32-bit we reserve only 256MB so you could run out of space with 100,000 classes
2088   // or so.
2089   _mc_region.print_out_of_space_msg(name, needed_bytes);
2090   _rw_region.print_out_of_space_msg(name, needed_bytes);
2091   _ro_region.print_out_of_space_msg(name, needed_bytes);
2092   _md_region.print_out_of_space_msg(name, needed_bytes);
2093 
2094   vm_exit_during_initialization(err_msg("Unable to allocate from '%s' region", name),
2095                                 "Please reduce the number of shared classes.");





2096 }


  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "classfile/classLoaderDataGraph.hpp"
  28 #include "classfile/classListParser.hpp"
  29 #include "classfile/classLoaderExt.hpp"
  30 #include "classfile/dictionary.hpp"
  31 #include "classfile/loaderConstraints.hpp"
  32 #include "classfile/javaClasses.inline.hpp"
  33 #include "classfile/placeholders.hpp"
  34 #include "classfile/symbolTable.hpp"
  35 #include "classfile/stringTable.hpp"
  36 #include "classfile/systemDictionary.hpp"
  37 #include "classfile/systemDictionaryShared.hpp"
  38 #include "code/codeCache.hpp"
  39 #include "gc/shared/softRefPolicy.hpp"
  40 #include "interpreter/bytecodeStream.hpp"
  41 #include "interpreter/bytecodes.hpp"
  42 #include "logging/log.hpp"
  43 #include "logging/logMessage.hpp"
  44 #include "memory/archiveUtils.hpp"
  45 #include "memory/dynamicArchive.hpp"
  46 #include "memory/filemap.hpp"
  47 #include "memory/heapShared.inline.hpp"
  48 #include "memory/metaspace.hpp"
  49 #include "memory/metaspaceClosure.hpp"
  50 #include "memory/metaspaceShared.hpp"
  51 #include "memory/resourceArea.hpp"
  52 #include "memory/universe.hpp"

  53 #include "oops/compressedOops.inline.hpp"
  54 #include "oops/instanceClassLoaderKlass.hpp"
  55 #include "oops/instanceMirrorKlass.hpp"
  56 #include "oops/instanceRefKlass.hpp"
  57 #include "oops/methodData.hpp"
  58 #include "oops/objArrayKlass.hpp"
  59 #include "oops/objArrayOop.hpp"
  60 #include "oops/oop.inline.hpp"
  61 #include "oops/typeArrayKlass.hpp"
  62 #include "prims/jvmtiRedefineClasses.hpp"
  63 #include "runtime/handles.inline.hpp"
  64 #include "runtime/os.hpp"
  65 #include "runtime/safepointVerifiers.hpp"
  66 #include "runtime/signature.hpp"
  67 #include "runtime/timerTrace.hpp"
  68 #include "runtime/vmThread.hpp"
  69 #include "runtime/vmOperations.hpp"
  70 #include "utilities/align.hpp"
  71 #include "utilities/bitMap.inline.hpp"
  72 #include "utilities/defaultStream.hpp"
  73 #include "utilities/hashtable.inline.hpp"
  74 #if INCLUDE_G1GC
  75 #include "gc/g1/g1CollectedHeap.hpp"
  76 #endif
  77 
  78 ReservedSpace MetaspaceShared::_shared_rs;
  79 VirtualSpace MetaspaceShared::_shared_vs;
  80 MetaspaceSharedStats MetaspaceShared::_stats;
  81 bool MetaspaceShared::_has_error_classes;
  82 bool MetaspaceShared::_archive_loading_failed = false;
  83 bool MetaspaceShared::_remapped_readwrite = false;
  84 address MetaspaceShared::_i2i_entry_code_buffers = NULL;
  85 size_t MetaspaceShared::_i2i_entry_code_buffers_size = 0;

  86 void* MetaspaceShared::_shared_metaspace_static_top = NULL;
  87 intx MetaspaceShared::_mapping_delta;
  88 
  89 // The CDS archive is divided into the following regions:
  90 //     mc  - misc code (the method entry trampolines)
  91 //     rw  - read-write metadata
  92 //     ro  - read-only metadata and read-only tables
  93 //     md  - misc data (the c++ vtables)
  94 //
  95 //     ca0 - closed archive heap space #0
  96 //     ca1 - closed archive heap space #1 (may be empty)
  97 //     oa0 - open archive heap space #0
  98 //     oa1 - open archive heap space #1 (may be empty)
  99 //
 100 // The mc, rw, ro, and md regions are linearly allocated, starting from
 101 // SharedBaseAddress, in the order of mc->rw->ro->md. The size of these 4 regions
 102 // are page-aligned, and there's no gap between any consecutive regions.
 103 //
 104 // These 4 regions are populated in the following steps:
 105 // [1] All classes are loaded in MetaspaceShared::preload_classes(). All metadata are
 106 //     temporarily allocated outside of the shared regions. Only the method entry
 107 //     trampolines are written into the mc region.


 131     // This is just a sanity check and should not appear in any real world usage. This
 132     // happens only if you allocate more than 2GB of shared objects and would require
 133     // millions of shared classes.
 134     vm_exit_during_initialization("Out of memory in the CDS archive",
 135                                   "Please reduce the number of shared classes.");
 136   }
 137 
 138   MetaspaceShared::commit_shared_space_to(newtop);
 139   _top = newtop;
 140   return _top;
 141 }
 142 
 143 char* DumpRegion::allocate(size_t num_bytes, size_t alignment) {
 144   char* p = (char*)align_up(_top, alignment);
 145   char* newtop = p + align_up(num_bytes, alignment);
 146   expand_top_to(newtop);
 147   memset(p, 0, newtop - p);
 148   return p;
 149 }
 150 
 151 void DumpRegion::append_intptr_t(intptr_t n, bool need_to_mark) {
 152   assert(is_aligned(_top, sizeof(intptr_t)), "bad alignment");
 153   intptr_t *p = (intptr_t*)_top;
 154   char* newtop = _top + sizeof(intptr_t);
 155   expand_top_to(newtop);
 156   *p = n;
 157   if (need_to_mark) {
 158     ArchivePtrMarker::mark_pointer(p);
 159   }
 160 }
 161 
 162 void DumpRegion::print(size_t total_bytes) const {
 163   tty->print_cr("%-3s space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used] at " INTPTR_FORMAT,
 164                 _name, used(), percent_of(used(), total_bytes), reserved(), percent_of(used(), reserved()),
 165                 p2i(_base + MetaspaceShared::final_delta()));
 166 }
 167 
 168 void DumpRegion::print_out_of_space_msg(const char* failing_region, size_t needed_bytes) {
 169   tty->print("[%-8s] " PTR_FORMAT " - " PTR_FORMAT " capacity =%9d, allocated =%9d",
 170              _name, p2i(_base), p2i(_top), int(_end - _base), int(_top - _base));
 171   if (strcmp(_name, failing_region) == 0) {
 172     tty->print_cr(" required = %d", int(needed_bytes));
 173   } else {
 174     tty->cr();
 175   }
 176 }
 177 
 178 void DumpRegion::pack(DumpRegion* next) {
 179   assert(!is_packed(), "sanity");
 180   _end = (char*)align_up(_top, Metaspace::reserve_alignment());
 181   _is_packed = true;
 182   if (next != NULL) {
 183     next->_base = next->_top = this->_end;
 184     next->_end = MetaspaceShared::shared_rs()->end();
 185   }
 186 }
 187 
 188 static DumpRegion _mc_region("mc"), _ro_region("ro"), _rw_region("rw"), _md_region("md");
 189 static size_t _total_closed_archive_region_size = 0, _total_open_archive_region_size = 0;
 190 
 191 void MetaspaceShared::init_shared_dump_space(DumpRegion* first_space, address first_space_bottom) {
 192   // Start with 0 committed bytes. The memory will be committed as needed by
 193   // MetaspaceShared::commit_shared_space_to().
 194   if (!_shared_vs.initialize(_shared_rs, 0)) {
 195     fatal("Unable to allocate memory for shared space");
 196   }
 197   first_space->init(&_shared_rs, (char*)first_space_bottom);
 198 }
 199 
 200 DumpRegion* MetaspaceShared::misc_code_dump_space() {
 201   return &_mc_region;
 202 }
 203 
 204 DumpRegion* MetaspaceShared::read_write_dump_space() {
 205   return &_rw_region;
 206 }
 207 
 208 DumpRegion* MetaspaceShared::read_only_dump_space() {
 209   return &_ro_region;
 210 }
 211 
 212 void MetaspaceShared::pack_dump_space(DumpRegion* current, DumpRegion* next,
 213                                       ReservedSpace* rs) {
 214   current->pack(next);
 215 }
 216 
 217 char* MetaspaceShared::misc_code_space_alloc(size_t num_bytes) {
 218   return _mc_region.allocate(num_bytes);
 219 }
 220 
 221 char* MetaspaceShared::read_only_space_alloc(size_t num_bytes) {
 222   return _ro_region.allocate(num_bytes);
 223 }
 224 
 225 // When reserving an address range using ReservedSpace, we need an alignment that satisfies both:
 226 // os::vm_allocation_granularity() -- so that we can sub-divide this range into multiple mmap regions,
 227 //                                    while keeping the first range at offset 0 of this range.
 228 // Metaspace::reserve_alignment()  -- so we can pass the region to
 229 //                                    Metaspace::allocate_metaspace_compressed_klass_ptrs.
 230 size_t MetaspaceShared::reserved_space_alignment() {
 231   size_t os_align = os::vm_allocation_granularity();
 232   size_t ms_align = Metaspace::reserve_alignment();
 233   if (os_align >= ms_align) {
 234     assert(os_align % ms_align == 0, "must be a multiple");
 235     return os_align;
 236   } else {
 237     assert(ms_align % os_align == 0, "must be a multiple");
 238     return ms_align;



































 239   }

 240 }
 241 
 242 ReservedSpace MetaspaceShared::reserve_shared_space(size_t size, char* requested_address) {
 243   bool large_pages = false; // Don't use large pages for the CDS archive.
 244   assert(is_aligned(requested_address, reserved_space_alignment()), "must be");
 245   return ReservedSpace(size, reserved_space_alignment(), large_pages, requested_address);




 246 }
 247 
 248 void MetaspaceShared::initialize_dumptime_shared_and_meta_spaces() {
 249   assert(DumpSharedSpaces, "should be called for dump time only");
 250   const size_t reserve_alignment = reserved_space_alignment();

 251   char* shared_base = (char*)align_up((char*)SharedBaseAddress, reserve_alignment);
 252 
 253 #ifdef _LP64
 254   // On 64-bit VM, the heap and class space layout will be the same as if
 255   // you're running in -Xshare:on mode:
 256   //
 257   //                              +-- SharedBaseAddress (default = 0x800000000)
 258   //                              v
 259   // +-..---------+---------+ ... +----+----+----+----+---------------+
 260   // |    Heap    | Archive |     | MC | RW | RO | MD | class space   |
 261   // +-..---------+---------+ ... +----+----+----+----+---------------+
 262   // |<--   MaxHeapSize  -->|     |<-- UnscaledClassSpaceMax = 4GB -->|
 263   //
 264   const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);
 265   const size_t cds_total = align_down(UnscaledClassSpaceMax, reserve_alignment);
 266 #else
 267   // We don't support archives larger than 256MB on 32-bit due to limited virtual address space.
 268   size_t cds_total = align_down(256*M, reserve_alignment);
 269 #endif
 270 
 271   bool use_requested_base = true;
 272   DEBUG_ONLY(
 273     if (SharedBaseAddress == 0) {
 274       log_info(cds)("SharedBaseAddress == 0: always allocate class space at an alternative address");
 275       use_requested_base = false;
 276     })
 277 
 278   // First try to reserve the space at the specified SharedBaseAddress.
 279   assert(!_shared_rs.is_reserved(), "must be");
 280   if (use_requested_base) {
 281     _shared_rs = reserve_shared_space(cds_total, shared_base);
 282   }
 283   if (_shared_rs.is_reserved()) {
 284     assert(shared_base == 0 || _shared_rs.base() == shared_base, "should match");
 285   } else {
 286     // Get a mmap region anywhere if the SharedBaseAddress fails.
 287     _shared_rs = reserve_shared_space(cds_total);

 288   }
 289   if (!_shared_rs.is_reserved()) {
 290     vm_exit_during_initialization("Unable to reserve memory for shared space",
 291                                   err_msg(SIZE_FORMAT " bytes.", cds_total));
 292   }
 293 
 294 #ifdef _LP64
 295   // During dump time, we allocate 4GB (UnscaledClassSpaceMax) of space and split it up:
 296   // + The upper 1 GB is used as the "temporary compressed class space" -- preload_classes()
 297   //   will store Klasses into this space.
 298   // + The lower 3 GB is used for the archive -- when preload_classes() is done,
 299   //   ArchiveCompactor will copy the class metadata into this space, first the RW parts,
 300   //   then the RO parts.
 301 
 302   assert(UseCompressedOops && UseCompressedClassPointers,
 303       "UseCompressedOops and UseCompressedClassPointers must be set");
 304 
 305   size_t max_archive_size = align_down(cds_total * 3 / 4, reserve_alignment);
 306   ReservedSpace tmp_class_space = _shared_rs.last_part(max_archive_size);
 307   CompressedClassSpaceSize = align_down(tmp_class_space.size(), reserve_alignment);


 405 }
 406 
 407 void MetaspaceShared::commit_shared_space_to(char* newtop) {
 408   Arguments::assert_is_dumping_archive();
 409   char* base = _shared_rs.base();
 410   size_t need_committed_size = newtop - base;
 411   size_t has_committed_size = _shared_vs.committed_size();
 412   if (need_committed_size < has_committed_size) {
 413     return;
 414   }
 415 
 416   size_t min_bytes = need_committed_size - has_committed_size;
 417   size_t preferred_bytes = 1 * M;
 418   size_t uncommitted = _shared_vs.reserved_size() - has_committed_size;
 419 
 420   size_t commit =MAX2(min_bytes, preferred_bytes);
 421   commit = MIN2(commit, uncommitted);
 422   assert(commit <= uncommitted, "sanity");
 423 
 424   bool result = _shared_vs.expand_by(commit, false);
 425   ArchivePtrMarker::expand_ptr_end((address*)_shared_vs.high());
 426 
 427   if (!result) {
 428     vm_exit_during_initialization(err_msg("Failed to expand shared space to " SIZE_FORMAT " bytes",
 429                                           need_committed_size));
 430   }
 431 
 432   log_info(cds)("Expanding shared spaces by " SIZE_FORMAT_W(7) " bytes [total " SIZE_FORMAT_W(9)  " bytes ending at %p]",
 433                 commit, _shared_vs.actual_committed_size(), _shared_vs.high());
 434 }
 435 
 436 void MetaspaceShared::initialize_ptr_marker(CHeapBitMap* ptrmap) {
 437   ArchivePtrMarker::initialize(ptrmap, (address*)_shared_vs.low(), (address*)_shared_vs.high());
 438 }
 439 
 440 // Read/write a data stream for restoring/preserving metadata pointers and
 441 // miscellaneous data from/to the shared archive file.
 442 
 443 void MetaspaceShared::serialize(SerializeClosure* soc) {
 444   int tag = 0;
 445   soc->do_tag(--tag);
 446 
 447   // Verify the sizes of various metadata in the system.
 448   soc->do_tag(sizeof(Method));
 449   soc->do_tag(sizeof(ConstMethod));
 450   soc->do_tag(arrayOopDesc::base_offset_in_bytes(T_BYTE));
 451   soc->do_tag(sizeof(ConstantPool));
 452   soc->do_tag(sizeof(ConstantPoolCache));
 453   soc->do_tag(objArrayOopDesc::base_offset_in_bytes());
 454   soc->do_tag(typeArrayOopDesc::base_offset_in_bytes(T_BYTE));
 455   soc->do_tag(sizeof(Symbol));
 456 
 457   // Dump/restore miscellaneous metadata.
 458   JavaClasses::serialize_offsets(soc);
 459   Universe::serialize(soc);
 460   soc->do_tag(--tag);
 461 
 462   // Dump/restore references to commonly used names and signatures.
 463   vmSymbols::serialize(soc);
 464   soc->do_tag(--tag);
 465 
 466   // Dump/restore the symbol/string/subgraph_info tables
 467   SymbolTable::serialize_shared_table_header(soc);
 468   StringTable::serialize_shared_table_header(soc);
 469   HeapShared::serialize_subgraph_info_table_header(soc);
 470   SystemDictionaryShared::serialize_dictionary_headers(soc);
 471 

 472   InstanceMirrorKlass::serialize_offsets(soc);
 473   soc->do_tag(--tag);
 474 
 475   serialize_cloned_cpp_vtptrs(soc);
 476   soc->do_tag(--tag);
 477 
 478   soc->do_tag(666);
 479 }
 480 
 481 address MetaspaceShared::i2i_entry_code_buffers(size_t total_size) {
 482   if (DumpSharedSpaces) {
 483     if (_i2i_entry_code_buffers == NULL) {
 484       _i2i_entry_code_buffers = (address)misc_code_space_alloc(total_size);
 485       _i2i_entry_code_buffers_size = total_size;
 486     }
 487   } else if (UseSharedSpaces) {
 488     assert(_i2i_entry_code_buffers != NULL, "must already been initialized");
 489   } else {
 490     return NULL;
 491   }


 674   }
 675   static CppVtableInfo* _info;
 676 
 677   static int get_vtable_length(const char* name);
 678 
 679 public:
 680   // Allocate and initialize the C++ vtable, starting from top, but do not go past end.
 681   static intptr_t* allocate(const char* name);
 682 
 683   // Clone the vtable to ...
 684   static intptr_t* clone_vtable(const char* name, CppVtableInfo* info);
 685 
 686   static void zero_vtable_clone() {
 687     assert(DumpSharedSpaces, "dump-time only");
 688     _info->zero();
 689   }
 690 
 691   // Switch the vtable pointer to point to the cloned vtable.
 692   static void patch(Metadata* obj) {
 693     assert(DumpSharedSpaces, "dump-time only");
 694     assert(MetaspaceShared::is_in_output_space(obj), "must be");
 695     *(void**)obj = (void*)(_info->cloned_vtable());
 696     ArchivePtrMarker::mark_pointer(obj);
 697   }
 698 
 699   static bool is_valid_shared_object(const T* obj) {
 700     intptr_t* vptr = *(intptr_t**)obj;
 701     return vptr == _info->cloned_vtable();
 702   }
 703 };
 704 
 705 template <class T> CppVtableInfo* CppVtableCloner<T>::_info = NULL;
 706 
 707 template <class T>
 708 intptr_t* CppVtableCloner<T>::allocate(const char* name) {
 709   assert(is_aligned(_md_region.top(), sizeof(intptr_t)), "bad alignment");
 710   int n = get_vtable_length(name);
 711   _info = (CppVtableInfo*)_md_region.allocate(CppVtableInfo::byte_size(n), sizeof(intptr_t));
 712   _info->set_vtable_size(n);
 713 
 714   intptr_t* p = clone_vtable(name, _info);
 715   assert((char*)p == _md_region.top(), "must be");
 716 


 770 int CppVtableCloner<T>::get_vtable_length(const char* name) {
 771   CppVtableTesterA<T> a;
 772   CppVtableTesterB<T> b;
 773 
 774   intptr_t* avtable = vtable_of(a);
 775   intptr_t* bvtable = vtable_of(b);
 776 
 777   // Start at slot 1, because slot 0 may be RTTI (on Solaris/Sparc)
 778   int vtable_len = 1;
 779   for (; ; vtable_len++) {
 780     if (avtable[vtable_len] != bvtable[vtable_len]) {
 781       break;
 782     }
 783   }
 784   log_debug(cds, vtables)("Found   %3d vtable entries for %s", vtable_len, name);
 785 
 786   return vtable_len;
 787 }
 788 
 789 #define ALLOC_CPP_VTABLE_CLONE(c) \
 790   _cloned_cpp_vtptrs[c##_Kind] = CppVtableCloner<c>::allocate(#c); \
 791   ArchivePtrMarker::mark_pointer(&_cloned_cpp_vtptrs[c##_Kind]);
 792 
 793 #define CLONE_CPP_VTABLE(c) \
 794   p = CppVtableCloner<c>::clone_vtable(#c, (CppVtableInfo*)p);
 795 
 796 #define ZERO_CPP_VTABLE(c) \
 797  CppVtableCloner<c>::zero_vtable_clone();
 798 
 799 //------------------------------ for DynamicDumpSharedSpaces - start
 800 #define DECLARE_CLONED_VTABLE_KIND(c) c ## _Kind,
 801 
 802 enum {
 803   CPP_VTABLE_PATCH_TYPES_DO(DECLARE_CLONED_VTABLE_KIND)
 804   _num_cloned_vtable_kinds
 805 };
 806 
 807 static intptr_t** _cloned_cpp_vtptrs = NULL;
 808 
 809 void MetaspaceShared::serialize_cloned_cpp_vtptrs(SerializeClosure* soc) {
 810   soc->do_ptr((void**)&_cloned_cpp_vtptrs);
 811 }


 937   assert(is_in_shared_metaspace(m), "must be");
 938   return CppVtableCloner<Method>::is_valid_shared_object(m);
 939 }
 940 
 941 void WriteClosure::do_oop(oop* o) {
 942   if (*o == NULL) {
 943     _dump_region->append_intptr_t(0);
 944   } else {
 945     assert(HeapShared::is_heap_object_archiving_allowed(),
 946            "Archiving heap object is not allowed");
 947     _dump_region->append_intptr_t(
 948       (intptr_t)CompressedOops::encode_not_null(*o));
 949   }
 950 }
 951 
 952 void WriteClosure::do_region(u_char* start, size_t size) {
 953   assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment");
 954   assert(size % sizeof(intptr_t) == 0, "bad size");
 955   do_tag((int)size);
 956   while (size > 0) {
 957     _dump_region->append_intptr_t(*(intptr_t*)start, true);
 958     start += sizeof(intptr_t);
 959     size -= sizeof(intptr_t);
 960   }
 961 }
 962 
 963 // This is for dumping detailed statistics for the allocations
 964 // in the shared spaces.
 965 class DumpAllocStats : public ResourceObj {
 966 public:
 967 
 968   // Here's poor man's enum inheritance
 969 #define SHAREDSPACE_OBJ_TYPES_DO(f) \
 970   METASPACE_OBJ_TYPES_DO(f) \
 971   f(SymbolHashentry) \
 972   f(SymbolBucket) \
 973   f(StringHashentry) \
 974   f(StringBucket) \
 975   f(Other)
 976 
 977   enum Type {


1101 
1102 #undef fmt_stats
1103 }
1104 
1105 // Populate the shared space.
1106 
1107 class VM_PopulateDumpSharedSpace: public VM_Operation {
1108 private:
1109   GrowableArray<MemRegion> *_closed_archive_heap_regions;
1110   GrowableArray<MemRegion> *_open_archive_heap_regions;
1111 
1112   GrowableArray<ArchiveHeapOopmapInfo> *_closed_archive_heap_oopmaps;
1113   GrowableArray<ArchiveHeapOopmapInfo> *_open_archive_heap_oopmaps;
1114 
1115   void dump_java_heap_objects() NOT_CDS_JAVA_HEAP_RETURN;
1116   void dump_archive_heap_oopmaps() NOT_CDS_JAVA_HEAP_RETURN;
1117   void dump_archive_heap_oopmaps(GrowableArray<MemRegion>* regions,
1118                                  GrowableArray<ArchiveHeapOopmapInfo>* oopmaps);
1119   void dump_symbols();
1120   char* dump_read_only_tables();
1121   void print_class_stats();
1122   void print_region_stats();
1123   void print_bitmap_region_stats(size_t size, size_t total_size);
1124   void print_heap_region_stats(GrowableArray<MemRegion> *heap_mem,
1125                                const char *name, size_t total_size);
1126   void relocate_to_default_base_address(CHeapBitMap* ptrmap);
1127 
1128 public:
1129 
1130   VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
1131   void doit();   // outline because gdb sucks
1132   static void write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region, bool read_only,  bool allow_exec) {
1133     mapinfo->write_region(region_idx, dump_region->base(), dump_region->used(), read_only, allow_exec);
1134   }
1135   bool allow_nested_vm_operations() const { return true; }
1136 }; // class VM_PopulateDumpSharedSpace
1137 
1138 class SortedSymbolClosure: public SymbolClosure {
1139   GrowableArray<Symbol*> _symbols;
1140   virtual void do_symbol(Symbol** sym) {
1141     assert((*sym)->is_permanent(), "archived symbols must be permanent");
1142     _symbols.append(*sym);
1143   }
1144   static int compare_symbols_by_address(Symbol** a, Symbol** b) {
1145     if (a[0] < b[0]) {
1146       return -1;
1147     } else if (a[0] == b[0]) {


1213       oldtop = _ro_region.top();
1214       p = _ro_region.allocate(bytes, alignment);
1215       newtop = _ro_region.top();
1216     } else {
1217       oldtop = _rw_region.top();
1218       if (ref->msotype() == MetaspaceObj::ClassType) {
1219         // Save a pointer immediate in front of an InstanceKlass, so
1220         // we can do a quick lookup from InstanceKlass* -> RunTimeSharedClassInfo*
1221         // without building another hashtable. See RunTimeSharedClassInfo::get_for()
1222         // in systemDictionaryShared.cpp.
1223         Klass* klass = (Klass*)obj;
1224         if (klass->is_instance_klass()) {
1225           SystemDictionaryShared::validate_before_archiving(InstanceKlass::cast(klass));
1226           _rw_region.allocate(sizeof(address), BytesPerWord);
1227         }
1228       }
1229       p = _rw_region.allocate(bytes, alignment);
1230       newtop = _rw_region.top();
1231     }
1232     memcpy(p, obj, bytes);
1233 
1234     assert(_new_loc_table->lookup(obj) == NULL, "each object can be relocated at most once");
1235     _new_loc_table->add(obj, (address)p);
1236     log_trace(cds)("Copy: " PTR_FORMAT " ==> " PTR_FORMAT " %d", p2i(obj), p2i(p), bytes);
1237     if (_new_loc_table->maybe_grow(MAX_TABLE_SIZE)) {
1238       log_info(cds, hashtables)("Expanded _new_loc_table to %d", _new_loc_table->table_size());
1239     }
1240     _alloc_stats->record(ref->msotype(), int(newtop - oldtop), read_only);
1241   }
1242 
1243   static address get_new_loc(MetaspaceClosure::Ref* ref) {
1244     address* pp = _new_loc_table->lookup(ref->obj());
1245     assert(pp != NULL, "must be");
1246     return *pp;
1247   }
1248 
1249 private:
1250   // Makes a shallow copy of visited MetaspaceObj's
1251   class ShallowCopier: public UniqueMetaspaceClosure {
1252     bool _read_only;
1253   public:
1254     ShallowCopier(bool read_only) : _read_only(read_only) {}
1255 
1256     virtual bool do_unique_ref(Ref* ref, bool read_only) {
1257       if (read_only == _read_only) {
1258         allocate(ref, read_only);
1259       }
1260       return true; // recurse into ref.obj()
1261     }
1262   };
1263 
1264   // Relocate embedded pointers within a MetaspaceObj's shallow copy
1265   class ShallowCopyEmbeddedRefRelocator: public UniqueMetaspaceClosure {
1266   public:
1267     virtual bool do_unique_ref(Ref* ref, bool read_only) {
1268       address new_loc = get_new_loc(ref);
1269       RefRelocator refer;
1270       ref->metaspace_pointers_do_at(&refer, new_loc);
1271       return true; // recurse into ref.obj()
1272     }
1273     virtual void push_special(SpecialRef type, Ref* ref, intptr_t* p) {
1274       assert(type == _method_entry_ref, "only special type allowed for now");
1275       address obj = ref->obj();
1276       address new_obj = get_new_loc(ref);
1277       size_t offset = pointer_delta(p, obj,  sizeof(u1));
1278       intptr_t* new_p = (intptr_t*)(new_obj + offset);
1279       assert(*p == *new_p, "must be a copy");
1280       ArchivePtrMarker::mark_pointer((address*)new_p);
1281     }
1282   };
1283 
1284   // Relocate a reference to point to its shallow copy
1285   class RefRelocator: public MetaspaceClosure {
1286   public:
1287     virtual bool do_ref(Ref* ref, bool read_only) {
1288       if (ref->not_null()) {
1289         ref->update(get_new_loc(ref));
1290         ArchivePtrMarker::mark_pointer(ref->addr());
1291       }
1292       return false; // Do not recurse.
1293     }
1294   };
1295 
1296 #ifdef ASSERT
1297   class IsRefInArchiveChecker: public MetaspaceClosure {
1298   public:
1299     virtual bool do_ref(Ref* ref, bool read_only) {
1300       if (ref->not_null()) {
1301         char* obj = (char*)ref->obj();
1302         assert(_ro_region.contains(obj) || _rw_region.contains(obj),
1303                "must be relocated to point to CDS archive");
1304       }
1305       return false; // Do not recurse.
1306     }
1307   };
1308 #endif
1309 
1310 public:


1337       ResourceMark rm;
1338       ShallowCopyEmbeddedRefRelocator emb_reloc;
1339       iterate_roots(&emb_reloc);
1340     }
1341     {
1342       tty->print_cr("Relocating external roots ... ");
1343       ResourceMark rm;
1344       RefRelocator ext_reloc;
1345       iterate_roots(&ext_reloc);
1346     }
1347 
1348 #ifdef ASSERT
1349     {
1350       tty->print_cr("Verifying external roots ... ");
1351       ResourceMark rm;
1352       IsRefInArchiveChecker checker;
1353       iterate_roots(&checker);
1354     }
1355 #endif
1356 

1357     // cleanup
1358     _ssc = NULL;
1359   }
1360 
1361   // We must relocate the System::_well_known_klasses only after we have copied the
1362   // java objects in during dump_java_heap_objects(): during the object copy, we operate on
1363   // old objects which assert that their klass is the original klass.
1364   static void relocate_well_known_klasses() {
1365     {
1366       tty->print_cr("Relocating SystemDictionary::_well_known_klasses[] ... ");
1367       ResourceMark rm;
1368       RefRelocator ext_reloc;
1369       SystemDictionary::well_known_klasses_do(&ext_reloc);
1370     }
1371     // NOTE: after this point, we shouldn't have any globals that can reach the old
1372     // objects.
1373 
1374     // We cannot use any of the objects in the heap anymore (except for the
1375     // shared strings) because their headers no longer point to valid Klasses.
1376   }


1426   }
1427   remove_java_mirror_in_classes();
1428   tty->print_cr("done. ");
1429 
1430   SystemDictionaryShared::write_to_archive();
1431 
1432   size_t vtptrs_bytes = _num_cloned_vtable_kinds * sizeof(intptr_t*);
1433   _cloned_cpp_vtptrs = (intptr_t**)_ro_region.allocate(vtptrs_bytes, sizeof(intptr_t*));
1434 
1435   // Write the other data to the output array.
1436   char* start = _ro_region.top();
1437   WriteClosure wc(&_ro_region);
1438   MetaspaceShared::serialize(&wc);
1439 
1440   // Write the bitmaps for patching the archive heap regions
1441   dump_archive_heap_oopmaps();
1442 
1443   return start;
1444 }
1445 
1446 void VM_PopulateDumpSharedSpace::print_class_stats() {
1447   tty->print_cr("Number of classes %d", _global_klass_objects->length());
1448   {
1449     int num_type_array = 0, num_obj_array = 0, num_inst = 0;
1450     for (int i = 0; i < _global_klass_objects->length(); i++) {
1451       Klass* k = _global_klass_objects->at(i);
1452       if (k->is_instance_klass()) {
1453         num_inst ++;
1454       } else if (k->is_objArray_klass()) {
1455         num_obj_array ++;
1456       } else {
1457         assert(k->is_typeArray_klass(), "sanity");
1458         num_type_array ++;
1459       }
1460     }
1461     tty->print_cr("    instance classes   = %5d", num_inst);
1462     tty->print_cr("    obj array classes  = %5d", num_obj_array);
1463     tty->print_cr("    type array classes = %5d", num_type_array);
1464   }
1465 }
1466 
1467 void VM_PopulateDumpSharedSpace::relocate_to_default_base_address(CHeapBitMap* ptrmap) {
1468   intx addr_delta = MetaspaceShared::final_delta();
1469   if (addr_delta == 0) {
1470     return;
1471   }
1472 
1473   // Patch all pointers that are marked by ptrmap within this region,
1474   // where we have just dumped all the metaspace data.
1475   address patch_base = (address)SharedBaseAddress;
1476   address patch_end  = (address)_md_region.top();
1477   size_t size = patch_end - patch_base;
1478 
1479   // debug only -- the current value of the pointers to be patched must be within this
1480   // range (i.e., must point to valid metaspace objects)
1481   address valid_old_base = patch_base;
1482   address valid_old_end  = patch_end;
1483 
1484   // debug only -- after patching, the pointers must point inside this range
1485   // (the requested location of the archive, as mapped at runtime).
1486   address valid_new_base = (address)Arguments::default_SharedBaseAddress();
1487   address valid_new_end  = valid_new_base + size;
1488 
1489   log_debug(cds)("Relocating archive from [" INTPTR_FORMAT " - " INTPTR_FORMAT " ] to "
1490                  "[" INTPTR_FORMAT " - " INTPTR_FORMAT " ]", p2i(patch_base), p2i(patch_end),
1491                  p2i(valid_new_base), p2i(valid_new_end));
1492 
1493   SharedDataRelocator patcher((address*)patch_base, (address*)patch_end, valid_old_base, valid_old_end,
1494                               valid_new_base, valid_new_end, addr_delta);
1495   ptrmap->iterate(&patcher);
1496 }
1497 
1498 void VM_PopulateDumpSharedSpace::doit() {
1499   CHeapBitMap ptrmap;
1500   MetaspaceShared::initialize_ptr_marker(&ptrmap);
1501 
1502   // We should no longer allocate anything from the metaspace, so that:
1503   //
1504   // (1) Metaspace::allocate might trigger GC if we have run out of
1505   //     committed metaspace, but we can't GC because we're running
1506   //     in the VM thread.
1507   // (2) ArchiveCompactor needs to work with a stable set of MetaspaceObjs.
1508   Metaspace::freeze();
1509   DEBUG_ONLY(SystemDictionaryShared::NoClassLoadingMark nclm);
1510 
1511   Thread* THREAD = VMThread::vm_thread();
1512 
1513   FileMapInfo::check_nonempty_dir_in_shared_path_table();
1514 
1515   NOT_PRODUCT(SystemDictionary::verify();)
1516   // The following guarantee is meant to ensure that no loader constraints
1517   // exist yet, since the constraints table is not shared.  This becomes
1518   // more important now that we don't re-initialize vtables/itables for
1519   // shared classes at runtime, where constraints were previously created.
1520   guarantee(SystemDictionary::constraints()->number_of_entries() == 0,
1521             "loader constraints are not saved");
1522   guarantee(SystemDictionary::placeholders()->number_of_entries() == 0,
1523           "placeholders are not saved");
1524 
1525   // At this point, many classes have been loaded.
1526   // Gather systemDictionary classes in a global array and do everything to
1527   // that so we don't have to walk the SystemDictionary again.
1528   SystemDictionaryShared::check_excluded_classes();
1529   _global_klass_objects = new GrowableArray<Klass*>(1000);
1530   CollectClassesClosure collect_classes;
1531   ClassLoaderDataGraph::loaded_classes_do(&collect_classes);
1532 
1533   print_class_stats();

















1534 
1535   // Ensure the ConstMethods won't be modified at run-time
1536   tty->print("Updating ConstMethods ... ");
1537   rewrite_nofast_bytecodes_and_calculate_fingerprints();
1538   tty->print_cr("done. ");
1539 
1540   // Remove all references outside the metadata
1541   tty->print("Removing unshareable information ... ");
1542   remove_unshareable_in_classes();
1543   tty->print_cr("done. ");
1544 
1545   ArchiveCompactor::initialize();
1546   ArchiveCompactor::copy_and_compact();
1547 
1548   dump_symbols();
1549 
1550   // Dump supported java heap objects
1551   _closed_archive_heap_regions = NULL;
1552   _open_archive_heap_regions = NULL;
1553   dump_java_heap_objects();
1554 
1555   ArchiveCompactor::relocate_well_known_klasses();
1556 
1557   char* serialized_data_start = dump_read_only_tables();
1558   _ro_region.pack(&_md_region);
1559 
1560   char* vtbl_list = _md_region.top();
1561   MetaspaceShared::allocate_cpp_vtable_clones();
1562   _md_region.pack();
1563 






1564   // During patching, some virtual methods may be called, so at this point
1565   // the vtables must contain valid methods (as filled in by CppVtableCloner::allocate).
1566   MetaspaceShared::patch_cpp_vtable_pointers();
1567 
1568   // The vtable clones contain addresses of the current process.
1569   // We don't want to write these addresses into the archive.
1570   MetaspaceShared::zero_cpp_vtable_clones_for_writing();
1571 
1572   // relocate the data so that it can be mapped to Arguments::default_SharedBaseAddress
1573   // without runtime relocation.
1574   ArchivePtrMarker::compact((address)SharedBaseAddress, (address)_md_region.top());
1575   relocate_to_default_base_address(&ptrmap);
1576 
1577   // Create and write the archive file that maps the shared spaces.
1578 
1579   FileMapInfo* mapinfo = new FileMapInfo(true);
1580   mapinfo->populate_header(os::vm_allocation_granularity());
1581   mapinfo->set_serialized_data_start(serialized_data_start);
1582   mapinfo->set_misc_data_patching_start(vtbl_list);
1583   mapinfo->set_i2i_entry_code_buffers(MetaspaceShared::i2i_entry_code_buffers(),
1584                                       MetaspaceShared::i2i_entry_code_buffers_size());

1585   mapinfo->open_for_write();
1586 
1587   // NOTE: md contains the trampoline code for method entries, which are patched at run time,
1588   // so it needs to be read/write.
1589   write_region(mapinfo, MetaspaceShared::mc, &_mc_region, /*read_only=*/false,/*allow_exec=*/true);
1590   write_region(mapinfo, MetaspaceShared::rw, &_rw_region, /*read_only=*/false,/*allow_exec=*/false);
1591   write_region(mapinfo, MetaspaceShared::ro, &_ro_region, /*read_only=*/true, /*allow_exec=*/false);
1592   write_region(mapinfo, MetaspaceShared::md, &_md_region, /*read_only=*/false,/*allow_exec=*/false);
1593 
1594   mapinfo->write_bitmap_region(ArchivePtrMarker::ptrmap());
1595 
1596   _total_closed_archive_region_size = mapinfo->write_archive_heap_regions(
1597                                         _closed_archive_heap_regions,
1598                                         _closed_archive_heap_oopmaps,
1599                                         MetaspaceShared::first_closed_archive_heap_region,
1600                                         MetaspaceShared::max_closed_archive_heap_region);
1601   _total_open_archive_region_size = mapinfo->write_archive_heap_regions(
1602                                         _open_archive_heap_regions,
1603                                         _open_archive_heap_oopmaps,
1604                                         MetaspaceShared::first_open_archive_heap_region,
1605                                         MetaspaceShared::max_open_archive_heap_region);
1606 
1607   mapinfo->set_final_requested_base((char*)Arguments::default_SharedBaseAddress());
1608   mapinfo->set_header_crc(mapinfo->compute_header_crc());
1609   mapinfo->write_header();
1610   mapinfo->close();
1611 
1612   // Restore the vtable in case we invoke any virtual methods.
1613   MetaspaceShared::clone_cpp_vtables((intptr_t*)vtbl_list);
1614 
1615   print_region_stats();
1616 
1617   if (log_is_enabled(Info, cds)) {
1618     ArchiveCompactor::alloc_stats()->print_stats(int(_ro_region.used()), int(_rw_region.used()),
1619                                                  int(_mc_region.used()), int(_md_region.used()));
1620   }
1621 
1622   if (PrintSystemDictionaryAtExit) {
1623     SystemDictionary::print();
1624   }
1625 
1626   if (AllowArchivingWithJavaAgent) {
1627     warning("This archive was created with AllowArchivingWithJavaAgent. It should be used "
1628             "for testing purposes only and should not be used in a production environment");
1629   }
1630 
1631   // There may be other pending VM operations that operate on the InstanceKlasses,
1632   // which will fail because InstanceKlasses::remove_unshareable_info()
1633   // has been called. Forget these operations and exit the VM directly.
1634   vm_direct_exit(0);
1635 }
1636 
1637 void VM_PopulateDumpSharedSpace::print_region_stats() {
1638   // Print statistics of all the regions
1639   const size_t bitmap_used = ArchivePtrMarker::ptrmap()->size_in_bytes();
1640   const size_t bitmap_reserved = align_up(bitmap_used, Metaspace::reserve_alignment());
1641   const size_t total_reserved = _ro_region.reserved()  + _rw_region.reserved() +
1642                                 _mc_region.reserved()  + _md_region.reserved() +
1643                                 bitmap_reserved +
1644                                 _total_closed_archive_region_size +
1645                                 _total_open_archive_region_size;
1646   const size_t total_bytes = _ro_region.used()  + _rw_region.used() +
1647                              _mc_region.used()  + _md_region.used() +
1648                              bitmap_used +
1649                              _total_closed_archive_region_size +
1650                              _total_open_archive_region_size;
1651   const double total_u_perc = percent_of(total_bytes, total_reserved);
1652 
1653   _mc_region.print(total_reserved);
1654   _rw_region.print(total_reserved);
1655   _ro_region.print(total_reserved);
1656   _md_region.print(total_reserved);
1657   print_bitmap_region_stats(bitmap_reserved, total_reserved);
1658   print_heap_region_stats(_closed_archive_heap_regions, "ca", total_reserved);
1659   print_heap_region_stats(_open_archive_heap_regions, "oa", total_reserved);
1660 
1661   tty->print_cr("total    : " SIZE_FORMAT_W(9) " [100.0%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used]",
1662                  total_bytes, total_reserved, total_u_perc);
1663 }
1664 
1665 void VM_PopulateDumpSharedSpace::print_bitmap_region_stats(size_t size, size_t total_size) {
1666   tty->print_cr("bm  space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [100.0%% used] at " INTPTR_FORMAT,
1667                 size, size/double(total_size)*100.0, size, p2i(NULL));
1668 }
1669 
1670 void VM_PopulateDumpSharedSpace::print_heap_region_stats(GrowableArray<MemRegion> *heap_mem,
1671                                                          const char *name, size_t total_size) {
1672   int arr_len = heap_mem == NULL ? 0 : heap_mem->length();
1673   for (int i = 0; i < arr_len; i++) {
1674       char* start = (char*)heap_mem->at(i).start();
1675       size_t size = heap_mem->at(i).byte_size();
1676       char* top = start + size;
1677       tty->print_cr("%s%d space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [100.0%% used] at " INTPTR_FORMAT,
1678                     name, i, size, size/double(total_size)*100.0, size, p2i(start));
1679 
1680   }
1681 }
1682 
1683 // Update a Java object to point its Klass* to the new location after
1684 // shared archive has been compacted.
1685 void MetaspaceShared::relocate_klass_ptr(oop o) {
1686   assert(DumpSharedSpaces, "sanity");
1687   Klass* k = ArchiveCompactor::get_relocated_klass(o->klass());
1688   o->set_klass(k);
1689 }
1690 
1691 Klass* MetaspaceShared::get_relocated_klass(Klass *k, bool is_final) {
1692   assert(DumpSharedSpaces, "sanity");
1693   k = ArchiveCompactor::get_relocated_klass(k);
1694   if (is_final) {
1695     k = (Klass*)(address(k) + final_delta());
1696   }
1697   return k;
1698 }
1699 
1700 class LinkSharedClassesClosure : public KlassClosure {
1701   Thread* THREAD;
1702   bool    _made_progress;
1703  public:
1704   LinkSharedClassesClosure(Thread* thread) : THREAD(thread), _made_progress(false) {}
1705 
1706   void reset()               { _made_progress = false; }
1707   bool made_progress() const { return _made_progress; }
1708 
1709   void do_klass(Klass* k) {
1710     if (k->is_instance_klass()) {
1711       InstanceKlass* ik = InstanceKlass::cast(k);
1712       // Link the class to cause the bytecodes to be rewritten and the
1713       // cpcache to be created. Class verification is done according
1714       // to -Xverify setting.
1715       _made_progress |= MetaspaceShared::try_link_class(ik, THREAD);
1716       guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1717 


1975   int old_tag;
1976   old_tag = (int)(intptr_t)nextPtr();
1977   // do_int(&old_tag);
1978   assert(tag == old_tag, "old tag doesn't match");
1979   FileMapInfo::assert_mark(tag == old_tag);
1980 }
1981 
1982 void ReadClosure::do_oop(oop *p) {
1983   narrowOop o = (narrowOop)nextPtr();
1984   if (o == 0 || !HeapShared::open_archive_heap_region_mapped()) {
1985     p = NULL;
1986   } else {
1987     assert(HeapShared::is_heap_object_archiving_allowed(),
1988            "Archived heap object is not allowed");
1989     assert(HeapShared::open_archive_heap_region_mapped(),
1990            "Open archive heap region is not mapped");
1991     *p = HeapShared::decode_from_archive(o);
1992   }
1993 }
1994 
1995 void ReadClosure::do_mirror_oop(oop *p) {
1996   do_oop(p);
1997   oop mirror = *p;
1998   if (mirror != NULL) {
1999     java_lang_Class::update_archived_mirror_native_pointers(mirror);
2000   }
2001 }
2002 
2003 void ReadClosure::do_region(u_char* start, size_t size) {
2004   assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment");
2005   assert(size % sizeof(intptr_t) == 0, "bad size");
2006   do_tag((int)size);
2007   while (size > 0) {
2008     *(intptr_t*)start = nextPtr();
2009     start += sizeof(intptr_t);
2010     size -= sizeof(intptr_t);
2011   }
2012 }
2013 
2014 void MetaspaceShared::set_shared_metaspace_range(void* base, void *static_top, void* top) {
2015   assert(base <= static_top && static_top <= top, "must be");
2016   _shared_metaspace_static_top = static_top;
2017   MetaspaceObj::set_shared_metaspace_range(base, top);
2018 }
2019 
2020 // Return true if given address is in the misc data region
2021 bool MetaspaceShared::is_in_shared_region(const void* p, int idx) {
2022   return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_region(p, idx);
2023 }
2024 
2025 bool MetaspaceShared::is_in_trampoline_frame(address addr) {
2026   if (UseSharedSpaces && is_in_shared_region(addr, MetaspaceShared::mc)) {
2027     return true;
2028   }
2029   return false;
2030 }
2031 
2032 bool MetaspaceShared::is_shared_dynamic(void* p) {
2033   if ((p < MetaspaceObj::shared_metaspace_top()) &&
2034       (p >= _shared_metaspace_static_top)) {
2035     return true;
2036   } else {
2037     return false;
2038   }
2039 }
2040 
2041 void MetaspaceShared::initialize_runtime_shared_and_meta_spaces() {
2042   assert(UseSharedSpaces, "Must be called when UseSharedSpaces is enabled");
2043   MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
2044   FileMapInfo* static_mapinfo = open_static_archive();
2045   FileMapInfo* dynamic_mapinfo = NULL;






2046 
2047   if (static_mapinfo != NULL) {
2048     dynamic_mapinfo = open_dynamic_archive();
2049 
2050     // First try to map at the requested address
2051     result = map_archives(static_mapinfo, dynamic_mapinfo, true);
2052     if (result == MAP_ARCHIVE_MMAP_FAILURE) {
2053       // Mapping has failed (probably due to ASLR). Let's map at an address chosen
2054       // by the OS.
2055       result = map_archives(static_mapinfo, dynamic_mapinfo, false);
2056     }
2057   }
2058 
2059   if (result == MAP_ARCHIVE_SUCCESS) {
2060     bool dynamic_mapped = (dynamic_mapinfo != NULL && dynamic_mapinfo->is_mapped());
2061     char* cds_base = static_mapinfo->mapped_base();
2062     char* cds_end =  dynamic_mapped ? dynamic_mapinfo->mapped_end() : static_mapinfo->mapped_end();
2063     set_shared_metaspace_range(cds_base, static_mapinfo->mapped_end(), cds_end);
2064     _mapping_delta = static_mapinfo->mapping_delta();
2065     if (dynamic_mapped) {
2066       FileMapInfo::set_shared_path_table(dynamic_mapinfo);
2067     } else {
2068       FileMapInfo::set_shared_path_table(static_mapinfo);
2069     }







2070   } else {
2071     set_shared_metaspace_range(NULL, NULL, NULL);
2072     UseSharedSpaces = false;
2073     FileMapInfo::fail_continue("Unable to map shared spaces");
2074     if (PrintSharedArchiveAndExit) {
2075       vm_exit_during_initialization("Unable to use shared archive.");
2076     }
2077   }
2078 
2079   if (static_mapinfo != NULL && !static_mapinfo->is_mapped()) {
2080     delete static_mapinfo;
2081   }
2082   if (dynamic_mapinfo != NULL && !dynamic_mapinfo->is_mapped()) {
2083     delete dynamic_mapinfo;
2084   }
2085 }
2086 
2087 FileMapInfo* MetaspaceShared::open_static_archive() {
2088   FileMapInfo* mapinfo = new FileMapInfo(true);
2089   if (!mapinfo->initialize()) {
2090     delete(mapinfo);
2091     return NULL;
2092   }
2093   return mapinfo;
2094 }
2095 
2096 FileMapInfo* MetaspaceShared::open_dynamic_archive() {
2097   if (DynamicDumpSharedSpaces) {
2098     return NULL;
2099   }
2100   if (Arguments::GetSharedDynamicArchivePath() == NULL) {
2101     return NULL;
2102   }
2103 
2104   FileMapInfo* mapinfo = new FileMapInfo(false);
2105   if (!mapinfo->initialize()) {
2106     delete(mapinfo);
2107     return NULL;
2108   }
2109   return mapinfo;
2110 }
2111 
2112 MapArchiveResult MetaspaceShared::map_archives(FileMapInfo* static_mapinfo, FileMapInfo* dynamic_mapinfo,
2113                                                bool use_requested_addr) {
2114   // Uncomment the next line to benchmark mapping at alternative locations.
2115   // if (SharedBaseAddress == 0 && use_requested_addr) { return MAP_ARCHIVE_MMAP_FAILURE; }
2116 
2117   if (dynamic_mapinfo != NULL) {
2118     // Ensure that the OS won't be able to allocate new memory spaces between the two
2119     // archives, or else it would mess up the simple comparision in MetaspaceObj::is_shared().
2120     assert(static_mapinfo->mapping_end_offset() == dynamic_mapinfo->mapping_base_offset(), "no gap");
2121   }
2122 
2123   ReservedSpace main_rs, archive_space_rs, class_space_rs;
2124   MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
2125   char* mapped_base_address = reserve_address_space_for_archives(static_mapinfo, dynamic_mapinfo,
2126                                                                  use_requested_addr, main_rs, archive_space_rs,
2127                                                                  class_space_rs);
2128   if (mapped_base_address == NULL) {
2129     result = MAP_ARCHIVE_MMAP_FAILURE;
2130   } else {
2131     log_debug(cds)("Reserved archive_space_rs     [" INTPTR_FORMAT " - " INTPTR_FORMAT "] (" SIZE_FORMAT ") bytes",
2132                    p2i(archive_space_rs.base()), p2i(archive_space_rs.end()), archive_space_rs.size());
2133     log_debug(cds)("Reserved class_space_rs [" INTPTR_FORMAT " - " INTPTR_FORMAT "] (" SIZE_FORMAT ") bytes",
2134                    p2i(class_space_rs.base()), p2i(class_space_rs.end()), class_space_rs.size());
2135     MapArchiveResult static_result = map_archive(static_mapinfo, mapped_base_address, archive_space_rs);
2136     MapArchiveResult dynamic_result = (static_result == MAP_ARCHIVE_SUCCESS) ?
2137                                      map_archive(dynamic_mapinfo, mapped_base_address, archive_space_rs) : MAP_ARCHIVE_OTHER_FAILURE;
2138 
2139     if (static_result == MAP_ARCHIVE_SUCCESS) {
2140       if (dynamic_result == MAP_ARCHIVE_SUCCESS) {
2141         result = MAP_ARCHIVE_SUCCESS;
2142       } else if (dynamic_result == MAP_ARCHIVE_OTHER_FAILURE) {
2143         assert(dynamic_mapinfo != NULL && !dynamic_mapinfo->is_mapped(), "must have failed");
2144         // No need to retry mapping the dynamic archive again, as it will never succeed
2145         // (bad file, etc) -- just keep the base archive.
2146         log_warning(cds, dynamic)("Unable to use shared archive. The top archive failed to load: %s",
2147                                   dynamic_mapinfo->full_path());
2148         result = MAP_ARCHIVE_SUCCESS;
2149         // FIXME: reduce archive space end ....
2150       } else {
2151         result = MAP_ARCHIVE_MMAP_FAILURE;
2152       }
2153     } else if (static_result == MAP_ARCHIVE_OTHER_FAILURE) {
2154       result = MAP_ARCHIVE_OTHER_FAILURE;
2155     } else {
2156       result = MAP_ARCHIVE_MMAP_FAILURE;
2157     }
2158   }
2159 
2160   if (result == MAP_ARCHIVE_SUCCESS) {
2161     if (!main_rs.is_reserved() && class_space_rs.is_reserved()) {
2162       MemTracker::record_virtual_memory_type((address)class_space_rs.base(), mtClass);
2163     }
2164     SharedBaseAddress = (size_t)mapped_base_address;
2165     LP64_ONLY({
2166         if (Metaspace::using_class_space()) {
2167           assert(class_space_rs.is_reserved(), "must be");
2168           char* cds_base = static_mapinfo->mapped_base();
2169           Metaspace::allocate_metaspace_compressed_klass_ptrs(class_space_rs, NULL, (address)cds_base);
2170           // map_heap_regions() compares the current narrow oop and klass encodings
2171           // with the archived ones, so it must be done after all encodings are determined.
2172           static_mapinfo->map_heap_regions();
2173         }
2174         CompressedKlassPointers::set_range(CompressedClassSpaceSize);
2175       });
2176   } else {
2177     unmap_archive(static_mapinfo);
2178     unmap_archive(dynamic_mapinfo);
2179     release_reserved_spaces(main_rs, archive_space_rs, class_space_rs);
2180   }
2181 
2182   return result;
2183 }
2184 
2185 char* MetaspaceShared::reserve_address_space_for_archives(FileMapInfo* static_mapinfo,
2186                                                           FileMapInfo* dynamic_mapinfo,
2187                                                           bool use_requested_addr,
2188                                                           ReservedSpace& main_rs,
2189                                                           ReservedSpace& archive_space_rs,
2190                                                           ReservedSpace& class_space_rs) {
2191   const bool use_klass_space = NOT_LP64(false) LP64_ONLY(Metaspace::using_class_space());
2192   const size_t class_space_size = NOT_LP64(0) LP64_ONLY(Metaspace::compressed_class_space_size());
2193 
2194   if (use_klass_space) {
2195     assert(class_space_size > 0, "CompressedClassSpaceSize must have been validated");
2196   }
2197   if (use_requested_addr && !is_aligned(static_mapinfo->requested_base_address(), reserved_space_alignment())) {
2198     return NULL;
2199   }
2200 
2201   // Size and requested location of the archive_space_rs (for both static and dynamic archives)
2202   size_t base_offset = static_mapinfo->mapping_base_offset();
2203   size_t end_offset  = (dynamic_mapinfo == NULL) ? static_mapinfo->mapping_end_offset() : dynamic_mapinfo->mapping_end_offset();
2204   assert(base_offset == 0, "must be");
2205   assert(is_aligned(end_offset,  os::vm_allocation_granularity()), "must be");
2206   assert(is_aligned(base_offset, os::vm_allocation_granularity()), "must be");
2207 
2208   // In case reserved_space_alignment() != os::vm_allocation_granularity()
2209   assert((size_t)os::vm_allocation_granularity() <= reserved_space_alignment(), "must be");
2210   end_offset = align_up(end_offset, reserved_space_alignment()); 
2211 
2212   size_t archive_space_size = end_offset - base_offset;
2213 
2214   // Special handling for Windows because it cannot mmap into a reserved space:
2215   //    use_requested_addr: We just map each region individually, and give up if any one of them fails.
2216   //   !use_requested_addr: We reserve the space first, and then os::read in all the regions (instead of mmap).
2217   //                        We're going to patch all the pointers anyway so there's no benefit for mmap.
2218 
2219   if (use_requested_addr) {
2220     char* archive_space_base = static_mapinfo->requested_base_address() + base_offset;
2221     char* archive_space_end  = archive_space_base + archive_space_size;
2222     if (!MetaspaceShared::use_windows_memory_mapping()) {
2223       archive_space_rs = reserve_shared_space(archive_space_size, archive_space_base);
2224       if (!archive_space_rs.is_reserved()) {
2225         return NULL;
2226       }
2227     }
2228     if (use_klass_space) {
2229       // Make sure we can map the klass space immediately following the archive_space space
2230       char* class_space_base = archive_space_end;
2231       class_space_rs = reserve_shared_space(class_space_size, class_space_base);
2232       if (!class_space_rs.is_reserved()) {
2233         return NULL;
2234       }
2235     }
2236     return static_mapinfo->requested_base_address();
2237   } else {
2238     if (use_klass_space) {
2239       main_rs = reserve_shared_space(archive_space_size + class_space_size);
2240       if (main_rs.is_reserved()) {
2241         archive_space_rs = main_rs.first_part(archive_space_size, reserved_space_alignment(), /*split=*/true);
2242         class_space_rs = main_rs.last_part(archive_space_size);
2243       }
2244     } else {
2245       main_rs = reserve_shared_space(archive_space_size);
2246       archive_space_rs = main_rs;
2247     }
2248     if (archive_space_rs.is_reserved()) {
2249       return archive_space_rs.base();
2250     } else {
2251       return NULL;
2252     }
2253   }
2254 }
2255 
2256 void MetaspaceShared::release_reserved_spaces(ReservedSpace& main_rs,
2257                                               ReservedSpace& archive_space_rs,
2258                                               ReservedSpace& class_space_rs) {
2259   if (main_rs.is_reserved()) {
2260     assert(main_rs.contains(archive_space_rs.base()), "must be");
2261     assert(main_rs.contains(class_space_rs.base()), "must be");
2262     log_debug(cds)("Released shared space (archive+classes) " INTPTR_FORMAT, p2i(main_rs.base()));
2263     main_rs.release();
2264   } else {
2265     if (archive_space_rs.is_reserved()) {
2266       log_debug(cds)("Released shared space (archive) " INTPTR_FORMAT, p2i(archive_space_rs.base()));
2267       archive_space_rs.release();
2268     }
2269     if (class_space_rs.is_reserved()) {
2270       log_debug(cds)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
2271       class_space_rs.release();
2272     }
2273   }
2274 }
2275 
2276 static int static_regions[]  = {MetaspaceShared::mc,
2277                                 MetaspaceShared::rw,
2278                                 MetaspaceShared::ro,
2279                                 MetaspaceShared::md};
2280 static int dynamic_regions[] = {MetaspaceShared::rw,
2281                                 MetaspaceShared::ro,
2282                                 MetaspaceShared::mc};
2283 static int static_regions_count  = 4;
2284 static int dynamic_regions_count = 3;
2285 
2286 MapArchiveResult MetaspaceShared::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) {
2287   assert(UseSharedSpaces, "must be runtime");
2288   if (mapinfo == NULL) {
2289     return MAP_ARCHIVE_SUCCESS; // no error has happeed -- trivially succeeded.
2290   }
2291 
2292   mapinfo->set_is_mapped(false);
2293 
2294   if (mapinfo->alignment() != (size_t)os::vm_allocation_granularity()) { // FIXME
2295     // FIXME log
2296     return MAP_ARCHIVE_OTHER_FAILURE;
2297   }
2298 
2299   MapArchiveResult result = mapinfo->is_static() ?
2300     mapinfo->map_regions(static_regions, static_regions_count, mapped_base_address, rs) :
2301     mapinfo->map_regions(dynamic_regions, dynamic_regions_count, mapped_base_address, rs);
2302 
2303   if (result != MAP_ARCHIVE_SUCCESS) {
2304     unmap_archive(mapinfo);
2305     return result;
2306   }
2307 
2308   if (mapinfo->is_static()) {
2309     if (!mapinfo->validate_shared_path_table()) {
2310       unmap_archive(mapinfo);
2311       return MAP_ARCHIVE_OTHER_FAILURE;
2312     }
2313   } else {
2314     if (!DynamicArchive::validate(mapinfo)) {
2315       unmap_archive(mapinfo);
2316       return MAP_ARCHIVE_OTHER_FAILURE;
2317     }
2318   }
2319 
2320   mapinfo->set_is_mapped(true);
2321   return MAP_ARCHIVE_SUCCESS;
2322 }
2323 
2324 void MetaspaceShared::unmap_archive(FileMapInfo* mapinfo) {
2325   assert(UseSharedSpaces, "must be runtime");
2326   if (mapinfo != NULL) {
2327     if (mapinfo->is_static()) {
2328       mapinfo->unmap_regions(static_regions, static_regions_count);
2329     } else {
2330       mapinfo->unmap_regions(dynamic_regions, dynamic_regions_count);
2331     }
2332     mapinfo->set_is_mapped(false);
2333   }
2334 }
2335 
2336 // Read the miscellaneous data from the shared file, and
2337 // serialize it out to its various destinations.
2338 
2339 void MetaspaceShared::initialize_shared_spaces() {
2340   FileMapInfo *static_mapinfo = FileMapInfo::current_info();
2341   _i2i_entry_code_buffers = static_mapinfo->i2i_entry_code_buffers();
2342   _i2i_entry_code_buffers_size = static_mapinfo->i2i_entry_code_buffers_size();
2343   char* buffer = static_mapinfo->misc_data_patching_start();


2344   clone_cpp_vtables((intptr_t*)buffer);
2345 
2346   // Verify various attributes of the archive, plus initialize the
2347   // shared string/symbol tables
2348   buffer = static_mapinfo->serialized_data_start();
2349   intptr_t* array = (intptr_t*)buffer;
2350   ReadClosure rc(&array);
2351   serialize(&rc);
2352 
2353   // Initialize the run-time symbol table.
2354   SymbolTable::create_table();
2355 
2356   static_mapinfo->patch_archived_heap_embedded_pointers();
2357 
2358   // Close the mapinfo file
2359   static_mapinfo->close();
2360 
2361   FileMapInfo *dynamic_mapinfo = FileMapInfo::dynamic_info();
2362   if (dynamic_mapinfo != NULL) {
2363     intptr_t* buffer = (intptr_t*)dynamic_mapinfo->serialized_data_start();
2364     ReadClosure rc(&buffer);
2365     SymbolTable::serialize_shared_table_header(&rc, false);
2366     SystemDictionaryShared::serialize_dictionary_headers(&rc, false);
2367     dynamic_mapinfo->close();
2368   }
2369 
2370   if (PrintSharedArchiveAndExit) {
2371     if (PrintSharedDictionary) {
2372       tty->print_cr("\nShared classes:\n");
2373       SystemDictionaryShared::print_on(tty);
2374     }
2375     if (FileMapInfo::current_info() == NULL || _archive_loading_failed) {
2376       tty->print_cr("archive is invalid");
2377       vm_exit(1);
2378     } else {
2379       tty->print_cr("archive is valid");
2380       vm_exit(0);
2381     }
2382   }
2383 }
2384 
2385 // JVM/TI RedefineClasses() support:
2386 bool MetaspaceShared::remap_shared_readonly_as_readwrite() {
2387   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2388 
2389   if (UseSharedSpaces) {
2390     // remap the shared readonly space to shared readwrite, private
2391     FileMapInfo* mapinfo = FileMapInfo::current_info();
2392     if (!mapinfo->remap_shared_readonly_as_readwrite()) {
2393       return false;
2394     }
2395     if (FileMapInfo::dynamic_info() != NULL) {


2397       if (!mapinfo->remap_shared_readonly_as_readwrite()) {
2398         return false;
2399       }
2400     }
2401     _remapped_readwrite = true;
2402   }
2403   return true;
2404 }
2405 
2406 void MetaspaceShared::report_out_of_space(const char* name, size_t needed_bytes) {
2407   // This is highly unlikely to happen on 64-bits because we have reserved a 4GB space.
2408   // On 32-bit we reserve only 256MB so you could run out of space with 100,000 classes
2409   // or so.
2410   _mc_region.print_out_of_space_msg(name, needed_bytes);
2411   _rw_region.print_out_of_space_msg(name, needed_bytes);
2412   _ro_region.print_out_of_space_msg(name, needed_bytes);
2413   _md_region.print_out_of_space_msg(name, needed_bytes);
2414 
2415   vm_exit_during_initialization(err_msg("Unable to allocate from '%s' region", name),
2416                                 "Please reduce the number of shared classes.");
2417 }
2418 
2419 intx MetaspaceShared::final_delta() { // FIXME rename
2420   return intx(Arguments::default_SharedBaseAddress())  // We want the archive to be mapped to here at runtime
2421        - intx(SharedBaseAddress);   // .. but the archive is mapped at here at dump time
2422 }
< prev index next >