< prev index next >

src/hotspot/share/memory/metaspaceShared.cpp

Print this page


  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "classfile/classListParser.hpp"
  28 #include "classfile/classLoaderExt.hpp"
  29 #include "classfile/dictionary.hpp"
  30 #include "classfile/loaderConstraints.hpp"
  31 #include "classfile/placeholders.hpp"
  32 #include "classfile/symbolTable.hpp"
  33 #include "classfile/stringTable.hpp"
  34 #include "classfile/systemDictionary.hpp"
  35 #include "classfile/systemDictionaryShared.hpp"
  36 #include "code/codeCache.hpp"
  37 #include "interpreter/bytecodeStream.hpp"
  38 #include "interpreter/bytecodes.hpp"
  39 #include "logging/log.hpp"
  40 #include "logging/logMessage.hpp"
  41 #include "memory/filemap.hpp"
  42 #include "memory/heapShared.hpp"
  43 #include "memory/metaspace.hpp"
  44 #include "memory/metaspaceClosure.hpp"
  45 #include "memory/metaspaceShared.hpp"
  46 #include "memory/resourceArea.hpp"
  47 #include "oops/compressedOops.inline.hpp"
  48 #include "oops/instanceClassLoaderKlass.hpp"
  49 #include "oops/instanceMirrorKlass.hpp"
  50 #include "oops/instanceRefKlass.hpp"
  51 #include "oops/objArrayKlass.hpp"
  52 #include "oops/objArrayOop.hpp"
  53 #include "oops/oop.inline.hpp"
  54 #include "oops/typeArrayKlass.hpp"
  55 #include "prims/jvmtiRedefineClasses.hpp"
  56 #include "runtime/handles.inline.hpp"
  57 #include "runtime/os.hpp"
  58 #include "runtime/safepointVerifiers.hpp"
  59 #include "runtime/signature.hpp"
  60 #include "runtime/timerTrace.hpp"
  61 #include "runtime/vmThread.hpp"
  62 #include "runtime/vm_operations.hpp"
  63 #include "utilities/align.hpp"

  64 #include "utilities/defaultStream.hpp"
  65 #include "utilities/hashtable.inline.hpp"
  66 #if INCLUDE_G1GC
  67 #include "gc/g1/g1Allocator.inline.hpp"
  68 #include "gc/g1/g1CollectedHeap.hpp"
  69 #endif
  70 
  71 ReservedSpace MetaspaceShared::_shared_rs;
  72 VirtualSpace MetaspaceShared::_shared_vs;
  73 MetaspaceSharedStats MetaspaceShared::_stats;
  74 bool MetaspaceShared::_has_error_classes;
  75 bool MetaspaceShared::_archive_loading_failed = false;
  76 bool MetaspaceShared::_remapped_readwrite = false;
  77 bool MetaspaceShared::_open_archive_heap_region_mapped = false;
  78 address MetaspaceShared::_cds_i2i_entry_code_buffers = NULL;
  79 size_t MetaspaceShared::_cds_i2i_entry_code_buffers_size = 0;
  80 size_t MetaspaceShared::_core_spaces_size = 0;
  81 
  82 // The CDS archive is divided into the following regions:
  83 //     mc  - misc code (the method entry trampolines)


 292   //   ArchiveCompactor will copy the class metadata into this space, first the RW parts,
 293   //   then the RO parts.
 294 
 295   assert(UseCompressedOops && UseCompressedClassPointers,
 296       "UseCompressedOops and UseCompressedClassPointers must be set");
 297 
 298   size_t max_archive_size = align_down(cds_total * 3 / 4, reserve_alignment);
 299   ReservedSpace tmp_class_space = _shared_rs.last_part(max_archive_size);
 300   CompressedClassSpaceSize = align_down(tmp_class_space.size(), reserve_alignment);
 301   _shared_rs = _shared_rs.first_part(max_archive_size);
 302 
 303   // Set up compress class pointers.
 304   Universe::set_narrow_klass_base((address)_shared_rs.base());
 305   // Set narrow_klass_shift to be LogKlassAlignmentInBytes. This is consistent
 306   // with AOT.
 307   Universe::set_narrow_klass_shift(LogKlassAlignmentInBytes);
 308   // Set the range of klass addresses to 4GB.
 309   Universe::set_narrow_klass_range(cds_total);
 310 
 311   Metaspace::initialize_class_space(tmp_class_space);
 312   tty->print_cr("narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
 313                 p2i(Universe::narrow_klass_base()), Universe::narrow_klass_shift());
 314 
 315   tty->print_cr("Allocated temporary class space: " SIZE_FORMAT " bytes at " PTR_FORMAT,
 316                 CompressedClassSpaceSize, p2i(tmp_class_space.base()));
 317 #endif
 318 
 319   // Start with 0 committed bytes. The memory will be committed as needed by
 320   // MetaspaceShared::commit_shared_space_to().
 321   if (!_shared_vs.initialize(_shared_rs, 0)) {
 322     vm_exit_during_initialization("Unable to allocate memory for shared space");
 323   }
 324 
 325   _mc_region.init(&_shared_rs);
 326   tty->print_cr("Allocated shared space: " SIZE_FORMAT " bytes at " PTR_FORMAT,
 327                 _shared_rs.size(), p2i(_shared_rs.base()));
 328 }
 329 
 330 // Called by universe_post_init()
 331 void MetaspaceShared::post_initialize(TRAPS) {
 332   if (UseSharedSpaces) {
 333     int size = FileMapInfo::get_number_of_shared_paths();
 334     if (size > 0) {
 335       SystemDictionaryShared::allocate_shared_data_arrays(size, THREAD);


 449   java_lang_invoke_CallSite::serialize(soc);
 450   java_lang_invoke_MethodHandleNatives_CallSiteContext::serialize(soc);
 451   java_security_AccessControlContext::serialize(soc);
 452   java_lang_reflect_AccessibleObject::serialize(soc);
 453   java_lang_reflect_Method::serialize(soc);
 454   java_lang_reflect_Constructor::serialize(soc);
 455   java_lang_reflect_Field::serialize(soc);
 456   java_nio_Buffer::serialize(soc);
 457   reflect_ConstantPool::serialize(soc);
 458   reflect_UnsafeStaticFieldAccessorImpl::serialize(soc);
 459   java_lang_reflect_Parameter::serialize(soc);
 460   java_lang_Module::serialize(soc);
 461   java_lang_StackTraceElement::serialize(soc);
 462   java_lang_StackFrameInfo::serialize(soc);
 463   java_lang_LiveStackFrameInfo::serialize(soc);
 464   java_util_concurrent_locks_AbstractOwnableSynchronizer::serialize(soc);
 465   java_util_ImmutableCollections_ListN::serialize(soc);
 466   java_util_ImmutableCollections_MapN::serialize(soc);
 467   java_util_ImmutableCollections_SetN::serialize(soc);
 468   jdk_internal_module_ArchivedModuleGraph::serialize(soc);

 469 }
 470 
 471 address MetaspaceShared::cds_i2i_entry_code_buffers(size_t total_size) {
 472   if (DumpSharedSpaces) {
 473     if (_cds_i2i_entry_code_buffers == NULL) {
 474       _cds_i2i_entry_code_buffers = (address)misc_code_space_alloc(total_size);
 475       _cds_i2i_entry_code_buffers_size = total_size;
 476     }
 477   } else if (UseSharedSpaces) {
 478     assert(_cds_i2i_entry_code_buffers != NULL, "must already been initialized");
 479   } else {
 480     return NULL;
 481   }
 482 
 483   assert(_cds_i2i_entry_code_buffers_size == total_size, "must not change");
 484   return _cds_i2i_entry_code_buffers;
 485 }
 486 
 487 // CDS code for dumping shared archive.
 488 


1040 
1041   msg.info("%s", sep);
1042   msg.info(fmt_stats, "Total",
1043                        all_ro_count, all_ro_bytes, all_ro_perc,
1044                        all_rw_count, all_rw_bytes, all_rw_perc,
1045                        all_count, all_bytes, all_perc);
1046 
1047   assert(all_ro_bytes == ro_all, "everything should have been counted");
1048   assert(all_rw_bytes == rw_all, "everything should have been counted");
1049 
1050 #undef fmt_stats
1051 }
1052 
1053 // Populate the shared space.
1054 
1055 class VM_PopulateDumpSharedSpace: public VM_Operation {
1056 private:
1057   GrowableArray<MemRegion> *_closed_archive_heap_regions;
1058   GrowableArray<MemRegion> *_open_archive_heap_regions;
1059 



1060   void dump_java_heap_objects() NOT_CDS_JAVA_HEAP_RETURN;



1061   void dump_symbols();
1062   char* dump_read_only_tables();
1063   void print_region_stats();
1064   void print_heap_region_stats(GrowableArray<MemRegion> *heap_mem,
1065                                const char *name, const size_t total_size);
1066 public:
1067 
1068   VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
1069   void doit();   // outline because gdb sucks
1070   static void write_region(FileMapInfo* mapinfo, int region, DumpRegion* space, bool read_only,  bool allow_exec);
1071   bool allow_nested_vm_operations() const { return true; }
1072 }; // class VM_PopulateDumpSharedSpace
1073 
1074 class SortedSymbolClosure: public SymbolClosure {
1075   GrowableArray<Symbol*> _symbols;
1076   virtual void do_symbol(Symbol** sym) {
1077     assert((*sym)->is_permanent(), "archived symbols must be permanent");
1078     _symbols.append(*sym);
1079   }
1080   static int compare_symbols_by_address(Symbol** a, Symbol** b) {


1352   tty->print_cr("done. ");
1353   NOT_PRODUCT(SystemDictionary::verify();)
1354 
1355   size_t buckets_bytes = SystemDictionary::count_bytes_for_buckets();
1356   char* buckets_top = _ro_region.allocate(buckets_bytes, sizeof(intptr_t));
1357   SystemDictionary::copy_buckets(buckets_top, _ro_region.top());
1358 
1359   size_t table_bytes = SystemDictionary::count_bytes_for_table();
1360   char* table_top = _ro_region.allocate(table_bytes, sizeof(intptr_t));
1361   SystemDictionary::copy_table(table_top, _ro_region.top());
1362 
1363   // Write the archived object sub-graph infos. For each klass with sub-graphs,
1364   // the info includes the static fields (sub-graph entry points) and Klasses
1365   // of objects included in the sub-graph.
1366   HeapShared::write_archived_subgraph_infos();
1367 
1368   // Write the other data to the output array.
1369   WriteClosure wc(&_ro_region);
1370   MetaspaceShared::serialize(&wc);
1371 



1372   char* newtop = _ro_region.top();
1373   ArchiveCompactor::alloc_stats()->record_other_type(int(newtop - oldtop), true);
1374   return buckets_top;
1375 }
1376 
1377 void VM_PopulateDumpSharedSpace::doit() {
1378   Thread* THREAD = VMThread::vm_thread();
1379 
1380   FileMapInfo::check_nonempty_dir_in_shared_path_table();
1381 
1382   NOT_PRODUCT(SystemDictionary::verify();)
1383   // The following guarantee is meant to ensure that no loader constraints
1384   // exist yet, since the constraints table is not shared.  This becomes
1385   // more important now that we don't re-initialize vtables/itables for
1386   // shared classes at runtime, where constraints were previously created.
1387   guarantee(SystemDictionary::constraints()->number_of_entries() == 0,
1388             "loader constraints are not saved");
1389   guarantee(SystemDictionary::placeholders()->number_of_entries() == 0,
1390           "placeholders are not saved");
1391   // Revisit and implement this if we prelink method handle call sites:


1494       // does is to update the fields in the mapinfo->_header.
1495     } else {
1496       // After the first pass, the contents of mapinfo->_header are finalized,
1497       // so we can compute the header's CRC, and write the contents of the header
1498       // and the regions into disk.
1499       mapinfo->open_for_write();
1500       mapinfo->set_header_crc(mapinfo->compute_header_crc());
1501     }
1502     mapinfo->write_header();
1503 
1504     // NOTE: md contains the trampoline code for method entries, which are patched at run time,
1505     // so it needs to be read/write.
1506     write_region(mapinfo, MetaspaceShared::mc, &_mc_region, /*read_only=*/false,/*allow_exec=*/true);
1507     write_region(mapinfo, MetaspaceShared::rw, &_rw_region, /*read_only=*/false,/*allow_exec=*/false);
1508     write_region(mapinfo, MetaspaceShared::ro, &_ro_region, /*read_only=*/true, /*allow_exec=*/false);
1509     write_region(mapinfo, MetaspaceShared::md, &_md_region, /*read_only=*/false,/*allow_exec=*/false);
1510     write_region(mapinfo, MetaspaceShared::od, &_od_region, /*read_only=*/true, /*allow_exec=*/false);
1511 
1512     _total_string_region_size = mapinfo->write_archive_heap_regions(
1513                                         _closed_archive_heap_regions,

1514                                         MetaspaceShared::first_string,
1515                                         MetaspaceShared::max_strings);
1516     _total_open_archive_region_size = mapinfo->write_archive_heap_regions(
1517                                         _open_archive_heap_regions,

1518                                         MetaspaceShared::first_open_archive_heap_region,
1519                                         MetaspaceShared::max_open_archive_heap_region);
1520   }
1521 
1522   mapinfo->close();
1523 
1524   // Restore the vtable in case we invoke any virtual methods.
1525   MetaspaceShared::clone_cpp_vtables((intptr_t*)vtbl_list);
1526 
1527   print_region_stats();
1528 
1529   if (log_is_enabled(Info, cds)) {
1530     ArchiveCompactor::alloc_stats()->print_stats(int(_ro_region.used()), int(_rw_region.used()),
1531                                                  int(_mc_region.used()), int(_md_region.used()));
1532   }
1533 
1534   if (PrintSystemDictionaryAtExit) {
1535     SystemDictionary::print();
1536   }
1537   // There may be other pending VM operations that operate on the InstanceKlasses,


1832 
1833     // Cache for recording where the archived objects are copied to
1834     MetaspaceShared::create_archive_object_cache();
1835 
1836     tty->print_cr("Dumping objects to closed archive heap region ...");
1837     NOT_PRODUCT(StringTable::verify());
1838     // The closed space has maximum two regions. See FileMapInfo::write_archive_heap_regions() for details.
1839     _closed_archive_heap_regions = new GrowableArray<MemRegion>(2);
1840     MetaspaceShared::dump_closed_archive_heap_objects(_closed_archive_heap_regions);
1841 
1842     tty->print_cr("Dumping objects to open archive heap region ...");
1843     _open_archive_heap_regions = new GrowableArray<MemRegion>(2);
1844     MetaspaceShared::dump_open_archive_heap_objects(_open_archive_heap_regions);
1845 
1846     MetaspaceShared::destroy_archive_object_cache();
1847   }
1848 
1849   G1HeapVerifier::verify_archive_regions();
1850 }
1851 





























1852 void MetaspaceShared::dump_closed_archive_heap_objects(
1853                                     GrowableArray<MemRegion> * closed_archive) {
1854   assert(is_heap_object_archiving_allowed(), "Cannot dump java heap objects");
1855 
1856   Thread* THREAD = Thread::current();
1857   G1CollectedHeap::heap()->begin_archive_alloc_range();
1858 
1859   // Archive interned string objects
1860   StringTable::write_to_archive();
1861 
1862   G1CollectedHeap::heap()->end_archive_alloc_range(closed_archive,
1863                                                    os::vm_allocation_granularity());
1864 }
1865 
1866 void MetaspaceShared::dump_open_archive_heap_objects(
1867                                     GrowableArray<MemRegion> * open_archive) {
1868   assert(UseG1GC, "Only support G1 GC");
1869   assert(UseCompressedOops && UseCompressedClassPointers,
1870          "Only support UseCompressedOops and UseCompressedClassPointers enabled");
1871 


1911   }
1912 
1913   int len = obj->size();
1914   if (G1CollectedHeap::heap()->is_archive_alloc_too_large(len)) {
1915     return NULL;
1916   }
1917 
1918   int hash = obj->identity_hash();
1919   oop archived_oop = (oop)G1CollectedHeap::heap()->archive_mem_allocate(len);
1920   if (archived_oop != NULL) {
1921     Copy::aligned_disjoint_words((HeapWord*)obj, (HeapWord*)archived_oop, len);
1922     relocate_klass_ptr(archived_oop);
1923     ArchivedObjectCache* cache = MetaspaceShared::archive_object_cache();
1924     cache->put(obj, archived_oop);
1925   }
1926   log_debug(cds, heap)("Archived heap object " PTR_FORMAT " ==> " PTR_FORMAT,
1927                        p2i(obj), p2i(archived_oop));
1928   return archived_oop;
1929 }
1930 
1931 oop MetaspaceShared::materialize_archived_object(oop obj) {
1932   if (obj != NULL) {

1933     return G1CollectedHeap::heap()->materialize_archived_object(obj);
1934   }
1935   return NULL;
1936 }
1937 
1938 void MetaspaceShared::archive_klass_objects(Thread* THREAD) {
1939   int i;
1940   for (i = 0; i < _global_klass_objects->length(); i++) {
1941     Klass* k = _global_klass_objects->at(i);
1942 
1943     // archive mirror object
1944     java_lang_Class::archive_mirror(k, CHECK);
1945 
1946     // archive the resolved_referenes array
1947     if (k->is_instance_klass()) {
1948       InstanceKlass* ik = InstanceKlass::cast(k);
1949       ik->constants()->archive_resolved_references(THREAD);
1950     }
1951   }
1952 }


1988     *p = (u4)(uintx(obj));
1989   }
1990 
1991   void do_tag(int tag) {
1992     int old_tag;
1993     old_tag = (int)(intptr_t)nextPtr();
1994     // do_int(&old_tag);
1995     assert(tag == old_tag, "old tag doesn't match");
1996     FileMapInfo::assert_mark(tag == old_tag);
1997   }
1998 
1999   void do_oop(oop *p) {
2000     narrowOop o = (narrowOop)nextPtr();
2001     if (o == 0 || !MetaspaceShared::open_archive_heap_region_mapped()) {
2002       p = NULL;
2003     } else {
2004       assert(MetaspaceShared::is_heap_object_archiving_allowed(),
2005              "Archived heap object is not allowed");
2006       assert(MetaspaceShared::open_archive_heap_region_mapped(),
2007              "Open archive heap region is not mapped");
2008       *p = CompressedOops::decode_not_null(o);
2009     }
2010   }
2011 
2012   void do_region(u_char* start, size_t size) {
2013     assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment");
2014     assert(size % sizeof(intptr_t) == 0, "bad size");
2015     do_tag((int)size);
2016     while (size > 0) {
2017       *(intptr_t*)start = nextPtr();
2018       start += sizeof(intptr_t);
2019       size -= sizeof(intptr_t);
2020     }
2021   }
2022 
2023   bool reading() const { return true; }
2024 };
2025 
2026 // Return true if given address is in the misc data region
2027 bool MetaspaceShared::is_in_shared_region(const void* p, int idx) {
2028   return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_region(p, idx);
2029 }
2030 
2031 bool MetaspaceShared::is_in_trampoline_frame(address addr) {
2032   if (UseSharedSpaces && is_in_shared_region(addr, MetaspaceShared::mc)) {
2033     return true;
2034   }
2035   return false;
2036 }
2037 
2038 void MetaspaceShared::print_shared_spaces() {
2039   if (UseSharedSpaces) {
2040     FileMapInfo::current_info()->print_shared_spaces();
2041   }
2042 }
2043 
2044 
2045 // Map shared spaces at requested addresses and return if succeeded.
2046 bool MetaspaceShared::map_shared_spaces(FileMapInfo* mapinfo) {
2047   size_t image_alignment = mapinfo->alignment();
2048 
2049 #ifndef _WINDOWS
2050   // Map in the shared memory and then map the regions on top of it.
2051   // On Windows, don't map the memory here because it will cause the
2052   // mappings of the regions to fail.
2053   ReservedSpace shared_rs = mapinfo->reserve_shared_memory();
2054   if (!shared_rs.is_reserved()) return false;
2055 #endif
2056 
2057   assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces");
2058 
2059   char* ro_base = NULL; char* ro_top;
2060   char* rw_base = NULL; char* rw_top;
2061   char* mc_base = NULL; char* mc_top;
2062   char* md_base = NULL; char* md_top;
2063   char* od_base = NULL; char* od_top;
2064 


2134   buffer += sharedDictionaryLen;
2135 
2136   // The following data are the linked list elements
2137   // (HashtableEntry objects) for the shared dictionary table.
2138 
2139   int len = *(intptr_t*)buffer;     // skip over shared dictionary entries
2140   buffer += sizeof(intptr_t);
2141   buffer += len;
2142 
2143   // The table of archived java heap object sub-graph infos
2144   buffer = HeapShared::read_archived_subgraph_infos(buffer);
2145 
2146   // Verify various attributes of the archive, plus initialize the
2147   // shared string/symbol tables
2148   intptr_t* array = (intptr_t*)buffer;
2149   ReadClosure rc(&array);
2150   serialize(&rc);
2151 
2152   // Initialize the run-time symbol table.
2153   SymbolTable::create_table();


2154 
2155   // Close the mapinfo file
2156   mapinfo->close();
2157 
2158   if (PrintSharedArchiveAndExit) {
2159     if (PrintSharedDictionary) {
2160       tty->print_cr("\nShared classes:\n");
2161       SystemDictionary::print_shared(tty);
2162     }
2163     if (_archive_loading_failed) {
2164       tty->print_cr("archive is invalid");
2165       vm_exit(1);
2166     } else {
2167       tty->print_cr("archive is valid");
2168       vm_exit(0);
2169     }
2170   }
2171 }
2172 
2173 // JVM/TI RedefineClasses() support:




  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "classfile/classListParser.hpp"
  28 #include "classfile/classLoaderExt.hpp"
  29 #include "classfile/dictionary.hpp"
  30 #include "classfile/loaderConstraints.hpp"
  31 #include "classfile/placeholders.hpp"
  32 #include "classfile/symbolTable.hpp"
  33 #include "classfile/stringTable.hpp"
  34 #include "classfile/systemDictionary.hpp"
  35 #include "classfile/systemDictionaryShared.hpp"
  36 #include "code/codeCache.hpp"
  37 #include "interpreter/bytecodeStream.hpp"
  38 #include "interpreter/bytecodes.hpp"
  39 #include "logging/log.hpp"
  40 #include "logging/logMessage.hpp"
  41 #include "memory/filemap.hpp"
  42 #include "memory/heapShared.inline.hpp"
  43 #include "memory/metaspace.hpp"
  44 #include "memory/metaspaceClosure.hpp"
  45 #include "memory/metaspaceShared.hpp"
  46 #include "memory/resourceArea.hpp"
  47 #include "oops/compressedOops.inline.hpp"
  48 #include "oops/instanceClassLoaderKlass.hpp"
  49 #include "oops/instanceMirrorKlass.hpp"
  50 #include "oops/instanceRefKlass.hpp"
  51 #include "oops/objArrayKlass.hpp"
  52 #include "oops/objArrayOop.hpp"
  53 #include "oops/oop.inline.hpp"
  54 #include "oops/typeArrayKlass.hpp"
  55 #include "prims/jvmtiRedefineClasses.hpp"
  56 #include "runtime/handles.inline.hpp"
  57 #include "runtime/os.hpp"
  58 #include "runtime/safepointVerifiers.hpp"
  59 #include "runtime/signature.hpp"
  60 #include "runtime/timerTrace.hpp"
  61 #include "runtime/vmThread.hpp"
  62 #include "runtime/vm_operations.hpp"
  63 #include "utilities/align.hpp"
  64 #include "utilities/bitMap.hpp"
  65 #include "utilities/defaultStream.hpp"
  66 #include "utilities/hashtable.inline.hpp"
  67 #if INCLUDE_G1GC
  68 #include "gc/g1/g1Allocator.inline.hpp"
  69 #include "gc/g1/g1CollectedHeap.hpp"
  70 #endif
  71 
  72 ReservedSpace MetaspaceShared::_shared_rs;
  73 VirtualSpace MetaspaceShared::_shared_vs;
  74 MetaspaceSharedStats MetaspaceShared::_stats;
  75 bool MetaspaceShared::_has_error_classes;
  76 bool MetaspaceShared::_archive_loading_failed = false;
  77 bool MetaspaceShared::_remapped_readwrite = false;
  78 bool MetaspaceShared::_open_archive_heap_region_mapped = false;
  79 address MetaspaceShared::_cds_i2i_entry_code_buffers = NULL;
  80 size_t MetaspaceShared::_cds_i2i_entry_code_buffers_size = 0;
  81 size_t MetaspaceShared::_core_spaces_size = 0;
  82 
  83 // The CDS archive is divided into the following regions:
  84 //     mc  - misc code (the method entry trampolines)


 293   //   ArchiveCompactor will copy the class metadata into this space, first the RW parts,
 294   //   then the RO parts.
 295 
 296   assert(UseCompressedOops && UseCompressedClassPointers,
 297       "UseCompressedOops and UseCompressedClassPointers must be set");
 298 
 299   size_t max_archive_size = align_down(cds_total * 3 / 4, reserve_alignment);
 300   ReservedSpace tmp_class_space = _shared_rs.last_part(max_archive_size);
 301   CompressedClassSpaceSize = align_down(tmp_class_space.size(), reserve_alignment);
 302   _shared_rs = _shared_rs.first_part(max_archive_size);
 303 
 304   // Set up compress class pointers.
 305   Universe::set_narrow_klass_base((address)_shared_rs.base());
 306   // Set narrow_klass_shift to be LogKlassAlignmentInBytes. This is consistent
 307   // with AOT.
 308   Universe::set_narrow_klass_shift(LogKlassAlignmentInBytes);
 309   // Set the range of klass addresses to 4GB.
 310   Universe::set_narrow_klass_range(cds_total);
 311 
 312   Metaspace::initialize_class_space(tmp_class_space);
 313   log_info(cds)("narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
 314                 p2i(Universe::narrow_klass_base()), Universe::narrow_klass_shift());
 315 
 316   log_info(cds)("Allocated temporary class space: " SIZE_FORMAT " bytes at " PTR_FORMAT,
 317                 CompressedClassSpaceSize, p2i(tmp_class_space.base()));
 318 #endif
 319 
 320   // Start with 0 committed bytes. The memory will be committed as needed by
 321   // MetaspaceShared::commit_shared_space_to().
 322   if (!_shared_vs.initialize(_shared_rs, 0)) {
 323     vm_exit_during_initialization("Unable to allocate memory for shared space");
 324   }
 325 
 326   _mc_region.init(&_shared_rs);
 327   tty->print_cr("Allocated shared space: " SIZE_FORMAT " bytes at " PTR_FORMAT,
 328                 _shared_rs.size(), p2i(_shared_rs.base()));
 329 }
 330 
 331 // Called by universe_post_init()
 332 void MetaspaceShared::post_initialize(TRAPS) {
 333   if (UseSharedSpaces) {
 334     int size = FileMapInfo::get_number_of_shared_paths();
 335     if (size > 0) {
 336       SystemDictionaryShared::allocate_shared_data_arrays(size, THREAD);


 450   java_lang_invoke_CallSite::serialize(soc);
 451   java_lang_invoke_MethodHandleNatives_CallSiteContext::serialize(soc);
 452   java_security_AccessControlContext::serialize(soc);
 453   java_lang_reflect_AccessibleObject::serialize(soc);
 454   java_lang_reflect_Method::serialize(soc);
 455   java_lang_reflect_Constructor::serialize(soc);
 456   java_lang_reflect_Field::serialize(soc);
 457   java_nio_Buffer::serialize(soc);
 458   reflect_ConstantPool::serialize(soc);
 459   reflect_UnsafeStaticFieldAccessorImpl::serialize(soc);
 460   java_lang_reflect_Parameter::serialize(soc);
 461   java_lang_Module::serialize(soc);
 462   java_lang_StackTraceElement::serialize(soc);
 463   java_lang_StackFrameInfo::serialize(soc);
 464   java_lang_LiveStackFrameInfo::serialize(soc);
 465   java_util_concurrent_locks_AbstractOwnableSynchronizer::serialize(soc);
 466   java_util_ImmutableCollections_ListN::serialize(soc);
 467   java_util_ImmutableCollections_MapN::serialize(soc);
 468   java_util_ImmutableCollections_SetN::serialize(soc);
 469   jdk_internal_module_ArchivedModuleGraph::serialize(soc);
 470   InstanceMirrorKlass::serialize(soc);
 471 }
 472 
 473 address MetaspaceShared::cds_i2i_entry_code_buffers(size_t total_size) {
 474   if (DumpSharedSpaces) {
 475     if (_cds_i2i_entry_code_buffers == NULL) {
 476       _cds_i2i_entry_code_buffers = (address)misc_code_space_alloc(total_size);
 477       _cds_i2i_entry_code_buffers_size = total_size;
 478     }
 479   } else if (UseSharedSpaces) {
 480     assert(_cds_i2i_entry_code_buffers != NULL, "must already been initialized");
 481   } else {
 482     return NULL;
 483   }
 484 
 485   assert(_cds_i2i_entry_code_buffers_size == total_size, "must not change");
 486   return _cds_i2i_entry_code_buffers;
 487 }
 488 
 489 // CDS code for dumping shared archive.
 490 


1042 
1043   msg.info("%s", sep);
1044   msg.info(fmt_stats, "Total",
1045                        all_ro_count, all_ro_bytes, all_ro_perc,
1046                        all_rw_count, all_rw_bytes, all_rw_perc,
1047                        all_count, all_bytes, all_perc);
1048 
1049   assert(all_ro_bytes == ro_all, "everything should have been counted");
1050   assert(all_rw_bytes == rw_all, "everything should have been counted");
1051 
1052 #undef fmt_stats
1053 }
1054 
1055 // Populate the shared space.
1056 
1057 class VM_PopulateDumpSharedSpace: public VM_Operation {
1058 private:
1059   GrowableArray<MemRegion> *_closed_archive_heap_regions;
1060   GrowableArray<MemRegion> *_open_archive_heap_regions;
1061 
1062   GrowableArray<ArchiveHeapOopmapInfo> *_closed_archive_heap_oopmaps;
1063   GrowableArray<ArchiveHeapOopmapInfo> *_open_archive_heap_oopmaps;
1064 
1065   void dump_java_heap_objects() NOT_CDS_JAVA_HEAP_RETURN;
1066   void dump_archive_heap_oopmaps() NOT_CDS_JAVA_HEAP_RETURN;
1067   void dump_archive_heap_oopmaps(GrowableArray<MemRegion>* regions,
1068                                  GrowableArray<ArchiveHeapOopmapInfo>* oopmaps);
1069   void dump_symbols();
1070   char* dump_read_only_tables();
1071   void print_region_stats();
1072   void print_heap_region_stats(GrowableArray<MemRegion> *heap_mem,
1073                                const char *name, const size_t total_size);
1074 public:
1075 
1076   VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
1077   void doit();   // outline because gdb sucks
1078   static void write_region(FileMapInfo* mapinfo, int region, DumpRegion* space, bool read_only,  bool allow_exec);
1079   bool allow_nested_vm_operations() const { return true; }
1080 }; // class VM_PopulateDumpSharedSpace
1081 
1082 class SortedSymbolClosure: public SymbolClosure {
1083   GrowableArray<Symbol*> _symbols;
1084   virtual void do_symbol(Symbol** sym) {
1085     assert((*sym)->is_permanent(), "archived symbols must be permanent");
1086     _symbols.append(*sym);
1087   }
1088   static int compare_symbols_by_address(Symbol** a, Symbol** b) {


1360   tty->print_cr("done. ");
1361   NOT_PRODUCT(SystemDictionary::verify();)
1362 
1363   size_t buckets_bytes = SystemDictionary::count_bytes_for_buckets();
1364   char* buckets_top = _ro_region.allocate(buckets_bytes, sizeof(intptr_t));
1365   SystemDictionary::copy_buckets(buckets_top, _ro_region.top());
1366 
1367   size_t table_bytes = SystemDictionary::count_bytes_for_table();
1368   char* table_top = _ro_region.allocate(table_bytes, sizeof(intptr_t));
1369   SystemDictionary::copy_table(table_top, _ro_region.top());
1370 
1371   // Write the archived object sub-graph infos. For each klass with sub-graphs,
1372   // the info includes the static fields (sub-graph entry points) and Klasses
1373   // of objects included in the sub-graph.
1374   HeapShared::write_archived_subgraph_infos();
1375 
1376   // Write the other data to the output array.
1377   WriteClosure wc(&_ro_region);
1378   MetaspaceShared::serialize(&wc);
1379 
1380   // Write the bitmaps for patching the archive heap regions
1381   dump_archive_heap_oopmaps();
1382 
1383   char* newtop = _ro_region.top();
1384   ArchiveCompactor::alloc_stats()->record_other_type(int(newtop - oldtop), true);
1385   return buckets_top;
1386 }
1387 
1388 void VM_PopulateDumpSharedSpace::doit() {
1389   Thread* THREAD = VMThread::vm_thread();
1390 
1391   FileMapInfo::check_nonempty_dir_in_shared_path_table();
1392 
1393   NOT_PRODUCT(SystemDictionary::verify();)
1394   // The following guarantee is meant to ensure that no loader constraints
1395   // exist yet, since the constraints table is not shared.  This becomes
1396   // more important now that we don't re-initialize vtables/itables for
1397   // shared classes at runtime, where constraints were previously created.
1398   guarantee(SystemDictionary::constraints()->number_of_entries() == 0,
1399             "loader constraints are not saved");
1400   guarantee(SystemDictionary::placeholders()->number_of_entries() == 0,
1401           "placeholders are not saved");
1402   // Revisit and implement this if we prelink method handle call sites:


1505       // does is to update the fields in the mapinfo->_header.
1506     } else {
1507       // After the first pass, the contents of mapinfo->_header are finalized,
1508       // so we can compute the header's CRC, and write the contents of the header
1509       // and the regions into disk.
1510       mapinfo->open_for_write();
1511       mapinfo->set_header_crc(mapinfo->compute_header_crc());
1512     }
1513     mapinfo->write_header();
1514 
1515     // NOTE: md contains the trampoline code for method entries, which are patched at run time,
1516     // so it needs to be read/write.
1517     write_region(mapinfo, MetaspaceShared::mc, &_mc_region, /*read_only=*/false,/*allow_exec=*/true);
1518     write_region(mapinfo, MetaspaceShared::rw, &_rw_region, /*read_only=*/false,/*allow_exec=*/false);
1519     write_region(mapinfo, MetaspaceShared::ro, &_ro_region, /*read_only=*/true, /*allow_exec=*/false);
1520     write_region(mapinfo, MetaspaceShared::md, &_md_region, /*read_only=*/false,/*allow_exec=*/false);
1521     write_region(mapinfo, MetaspaceShared::od, &_od_region, /*read_only=*/true, /*allow_exec=*/false);
1522 
1523     _total_string_region_size = mapinfo->write_archive_heap_regions(
1524                                         _closed_archive_heap_regions,
1525                                         _closed_archive_heap_oopmaps,
1526                                         MetaspaceShared::first_string,
1527                                         MetaspaceShared::max_strings);
1528     _total_open_archive_region_size = mapinfo->write_archive_heap_regions(
1529                                         _open_archive_heap_regions,
1530                                         _open_archive_heap_oopmaps,
1531                                         MetaspaceShared::first_open_archive_heap_region,
1532                                         MetaspaceShared::max_open_archive_heap_region);
1533   }
1534 
1535   mapinfo->close();
1536 
1537   // Restore the vtable in case we invoke any virtual methods.
1538   MetaspaceShared::clone_cpp_vtables((intptr_t*)vtbl_list);
1539 
1540   print_region_stats();
1541 
1542   if (log_is_enabled(Info, cds)) {
1543     ArchiveCompactor::alloc_stats()->print_stats(int(_ro_region.used()), int(_rw_region.used()),
1544                                                  int(_mc_region.used()), int(_md_region.used()));
1545   }
1546 
1547   if (PrintSystemDictionaryAtExit) {
1548     SystemDictionary::print();
1549   }
1550   // There may be other pending VM operations that operate on the InstanceKlasses,


1845 
1846     // Cache for recording where the archived objects are copied to
1847     MetaspaceShared::create_archive_object_cache();
1848 
1849     tty->print_cr("Dumping objects to closed archive heap region ...");
1850     NOT_PRODUCT(StringTable::verify());
1851     // The closed space has maximum two regions. See FileMapInfo::write_archive_heap_regions() for details.
1852     _closed_archive_heap_regions = new GrowableArray<MemRegion>(2);
1853     MetaspaceShared::dump_closed_archive_heap_objects(_closed_archive_heap_regions);
1854 
1855     tty->print_cr("Dumping objects to open archive heap region ...");
1856     _open_archive_heap_regions = new GrowableArray<MemRegion>(2);
1857     MetaspaceShared::dump_open_archive_heap_objects(_open_archive_heap_regions);
1858 
1859     MetaspaceShared::destroy_archive_object_cache();
1860   }
1861 
1862   G1HeapVerifier::verify_archive_regions();
1863 }
1864 
1865 void VM_PopulateDumpSharedSpace::dump_archive_heap_oopmaps() {
1866   if (MetaspaceShared::is_heap_object_archiving_allowed()) {
1867     _closed_archive_heap_oopmaps = new GrowableArray<ArchiveHeapOopmapInfo>(2);
1868     dump_archive_heap_oopmaps(_closed_archive_heap_regions, _closed_archive_heap_oopmaps);
1869 
1870     _open_archive_heap_oopmaps = new GrowableArray<ArchiveHeapOopmapInfo>(2);
1871     dump_archive_heap_oopmaps(_open_archive_heap_regions, _open_archive_heap_oopmaps);
1872   }
1873 }
1874 
1875 void VM_PopulateDumpSharedSpace::dump_archive_heap_oopmaps(GrowableArray<MemRegion>* regions,
1876                                                            GrowableArray<ArchiveHeapOopmapInfo>* oopmaps) {
1877   for (int i=0; i<regions->length(); i++) {
1878     ResourceBitMap oopmap = HeapShared::calculate_oopmap(regions->at(i));
1879     size_t size_in_bits = oopmap.size();
1880     size_t size_in_bytes = oopmap.size_in_bytes();
1881     uintptr_t* buffer = (uintptr_t*)_ro_region.allocate(size_in_bytes, sizeof(intptr_t));
1882     oopmap.write_to(buffer, size_in_bytes);
1883     log_info(cds)("Oopmap = " INTPTR_FORMAT " (%6d bytes) for heap region " INTPTR_FORMAT " (%8d bytes)",
1884                   p2i(buffer), (int)(size_in_bytes),
1885                   p2i(regions->at(i).start()), (int)regions->at(i).byte_size());
1886 
1887     ArchiveHeapOopmapInfo info;
1888     info._oopmap = (address)buffer;
1889     info._oopmap_size_in_bits = size_in_bits;
1890     oopmaps->append(info);
1891   }
1892 }
1893 
1894 void MetaspaceShared::dump_closed_archive_heap_objects(
1895                                     GrowableArray<MemRegion> * closed_archive) {
1896   assert(is_heap_object_archiving_allowed(), "Cannot dump java heap objects");
1897 
1898   Thread* THREAD = Thread::current();
1899   G1CollectedHeap::heap()->begin_archive_alloc_range();
1900 
1901   // Archive interned string objects
1902   StringTable::write_to_archive();
1903 
1904   G1CollectedHeap::heap()->end_archive_alloc_range(closed_archive,
1905                                                    os::vm_allocation_granularity());
1906 }
1907 
1908 void MetaspaceShared::dump_open_archive_heap_objects(
1909                                     GrowableArray<MemRegion> * open_archive) {
1910   assert(UseG1GC, "Only support G1 GC");
1911   assert(UseCompressedOops && UseCompressedClassPointers,
1912          "Only support UseCompressedOops and UseCompressedClassPointers enabled");
1913 


1953   }
1954 
1955   int len = obj->size();
1956   if (G1CollectedHeap::heap()->is_archive_alloc_too_large(len)) {
1957     return NULL;
1958   }
1959 
1960   int hash = obj->identity_hash();
1961   oop archived_oop = (oop)G1CollectedHeap::heap()->archive_mem_allocate(len);
1962   if (archived_oop != NULL) {
1963     Copy::aligned_disjoint_words((HeapWord*)obj, (HeapWord*)archived_oop, len);
1964     relocate_klass_ptr(archived_oop);
1965     ArchivedObjectCache* cache = MetaspaceShared::archive_object_cache();
1966     cache->put(obj, archived_oop);
1967   }
1968   log_debug(cds, heap)("Archived heap object " PTR_FORMAT " ==> " PTR_FORMAT,
1969                        p2i(obj), p2i(archived_oop));
1970   return archived_oop;
1971 }
1972 
1973 oop MetaspaceShared::materialize_archived_object(narrowOop v) {
1974   if (!CompressedOops::is_null(v)) {
1975     oop obj = HeapShared::decode_with_archived_oop_encoding_mode(v);
1976     return G1CollectedHeap::heap()->materialize_archived_object(obj);
1977   }
1978   return NULL;
1979 }
1980 
1981 void MetaspaceShared::archive_klass_objects(Thread* THREAD) {
1982   int i;
1983   for (i = 0; i < _global_klass_objects->length(); i++) {
1984     Klass* k = _global_klass_objects->at(i);
1985 
1986     // archive mirror object
1987     java_lang_Class::archive_mirror(k, CHECK);
1988 
1989     // archive the resolved_referenes array
1990     if (k->is_instance_klass()) {
1991       InstanceKlass* ik = InstanceKlass::cast(k);
1992       ik->constants()->archive_resolved_references(THREAD);
1993     }
1994   }
1995 }


2031     *p = (u4)(uintx(obj));
2032   }
2033 
2034   void do_tag(int tag) {
2035     int old_tag;
2036     old_tag = (int)(intptr_t)nextPtr();
2037     // do_int(&old_tag);
2038     assert(tag == old_tag, "old tag doesn't match");
2039     FileMapInfo::assert_mark(tag == old_tag);
2040   }
2041 
2042   void do_oop(oop *p) {
2043     narrowOop o = (narrowOop)nextPtr();
2044     if (o == 0 || !MetaspaceShared::open_archive_heap_region_mapped()) {
2045       p = NULL;
2046     } else {
2047       assert(MetaspaceShared::is_heap_object_archiving_allowed(),
2048              "Archived heap object is not allowed");
2049       assert(MetaspaceShared::open_archive_heap_region_mapped(),
2050              "Open archive heap region is not mapped");
2051       *p = HeapShared::decode_with_archived_oop_encoding_mode(o);
2052     }
2053   }
2054 
2055   void do_region(u_char* start, size_t size) {
2056     assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment");
2057     assert(size % sizeof(intptr_t) == 0, "bad size");
2058     do_tag((int)size);
2059     while (size > 0) {
2060       *(intptr_t*)start = nextPtr();
2061       start += sizeof(intptr_t);
2062       size -= sizeof(intptr_t);
2063     }
2064   }
2065 
2066   bool reading() const { return true; }
2067 };
2068 
2069 // Return true if given address is in the misc data region
2070 bool MetaspaceShared::is_in_shared_region(const void* p, int idx) {
2071   return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_region(p, idx);
2072 }
2073 
2074 bool MetaspaceShared::is_in_trampoline_frame(address addr) {
2075   if (UseSharedSpaces && is_in_shared_region(addr, MetaspaceShared::mc)) {
2076     return true;
2077   }
2078   return false;
2079 }
2080 







2081 // Map shared spaces at requested addresses and return if succeeded.
2082 bool MetaspaceShared::map_shared_spaces(FileMapInfo* mapinfo) {
2083   size_t image_alignment = mapinfo->alignment();
2084 
2085 #ifndef _WINDOWS
2086   // Map in the shared memory and then map the regions on top of it.
2087   // On Windows, don't map the memory here because it will cause the
2088   // mappings of the regions to fail.
2089   ReservedSpace shared_rs = mapinfo->reserve_shared_memory();
2090   if (!shared_rs.is_reserved()) return false;
2091 #endif
2092 
2093   assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces");
2094 
2095   char* ro_base = NULL; char* ro_top;
2096   char* rw_base = NULL; char* rw_top;
2097   char* mc_base = NULL; char* mc_top;
2098   char* md_base = NULL; char* md_top;
2099   char* od_base = NULL; char* od_top;
2100 


2170   buffer += sharedDictionaryLen;
2171 
2172   // The following data are the linked list elements
2173   // (HashtableEntry objects) for the shared dictionary table.
2174 
2175   int len = *(intptr_t*)buffer;     // skip over shared dictionary entries
2176   buffer += sizeof(intptr_t);
2177   buffer += len;
2178 
2179   // The table of archived java heap object sub-graph infos
2180   buffer = HeapShared::read_archived_subgraph_infos(buffer);
2181 
2182   // Verify various attributes of the archive, plus initialize the
2183   // shared string/symbol tables
2184   intptr_t* array = (intptr_t*)buffer;
2185   ReadClosure rc(&array);
2186   serialize(&rc);
2187 
2188   // Initialize the run-time symbol table.
2189   SymbolTable::create_table();
2190 
2191   mapinfo->patch_archived_heap_embedded_pointers();
2192 
2193   // Close the mapinfo file
2194   mapinfo->close();
2195 
2196   if (PrintSharedArchiveAndExit) {
2197     if (PrintSharedDictionary) {
2198       tty->print_cr("\nShared classes:\n");
2199       SystemDictionary::print_shared(tty);
2200     }
2201     if (_archive_loading_failed) {
2202       tty->print_cr("archive is invalid");
2203       vm_exit(1);
2204     } else {
2205       tty->print_cr("archive is valid");
2206       vm_exit(0);
2207     }
2208   }
2209 }
2210 
2211 // JVM/TI RedefineClasses() support:


< prev index next >