< prev index next >

src/hotspot/share/cds/archiveBuilder.cpp

Print this page

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

  30 #include "cds/cppVtables.hpp"
  31 #include "cds/dumpAllocStats.hpp"
  32 #include "cds/dynamicArchive.hpp"
  33 #include "cds/heapShared.hpp"
  34 #include "cds/metaspaceShared.hpp"
  35 #include "cds/regeneratedClasses.hpp"

  36 #include "classfile/classLoaderDataShared.hpp"
  37 #include "classfile/javaClasses.hpp"
  38 #include "classfile/symbolTable.hpp"
  39 #include "classfile/systemDictionaryShared.hpp"
  40 #include "classfile/vmClasses.hpp"
  41 #include "interpreter/abstractInterpreter.hpp"
  42 #include "logging/log.hpp"
  43 #include "logging/logStream.hpp"
  44 #include "memory/allStatic.hpp"
  45 #include "memory/memRegion.hpp"
  46 #include "memory/resourceArea.hpp"
  47 #include "oops/compressedKlass.inline.hpp"
  48 #include "oops/instanceKlass.hpp"
  49 #include "oops/objArrayKlass.hpp"
  50 #include "oops/objArrayOop.inline.hpp"
  51 #include "oops/oopHandle.inline.hpp"

  52 #include "runtime/arguments.hpp"
  53 #include "runtime/fieldDescriptor.inline.hpp"
  54 #include "runtime/globals_extension.hpp"
  55 #include "runtime/javaThread.hpp"
  56 #include "runtime/sharedRuntime.hpp"
  57 #include "utilities/align.hpp"
  58 #include "utilities/bitMap.inline.hpp"
  59 #include "utilities/formatBuffer.hpp"
  60 
  61 ArchiveBuilder* ArchiveBuilder::_current = nullptr;
  62 
  63 ArchiveBuilder::OtherROAllocMark::~OtherROAllocMark() {
  64   char* newtop = ArchiveBuilder::current()->_ro_region.top();
  65   ArchiveBuilder::alloc_stats()->record_other_type(int(newtop - _oldtop), true);
  66 }
  67 
  68 ArchiveBuilder::SourceObjList::SourceObjList() : _ptrmap(16 * K, mtClassShared) {
  69   _total_bytes = 0;
  70   _objs = new (mtClassShared) GrowableArray<SourceObjInfo*>(128 * K, mtClassShared);
  71 }

 140 
 141   RelocateEmbeddedPointers relocator(builder, src_info->buffered_addr(), start);
 142   _ptrmap.iterate(&relocator, start, end);
 143 }
 144 
 145 ArchiveBuilder::ArchiveBuilder() :
 146   _current_dump_space(nullptr),
 147   _buffer_bottom(nullptr),
 148   _last_verified_top(nullptr),
 149   _num_dump_regions_used(0),
 150   _other_region_used_bytes(0),
 151   _requested_static_archive_bottom(nullptr),
 152   _requested_static_archive_top(nullptr),
 153   _requested_dynamic_archive_bottom(nullptr),
 154   _requested_dynamic_archive_top(nullptr),
 155   _mapped_static_archive_bottom(nullptr),
 156   _mapped_static_archive_top(nullptr),
 157   _buffer_to_requested_delta(0),
 158   _rw_region("rw", MAX_SHARED_DELTA),
 159   _ro_region("ro", MAX_SHARED_DELTA),

 160   _ptrmap(mtClassShared),
 161   _rw_src_objs(),
 162   _ro_src_objs(),
 163   _src_obj_table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE),
 164   _buffered_to_src_table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE),
 165   _total_heap_region_size(0),
 166   _estimated_metaspaceobj_bytes(0),
 167   _estimated_hashtable_bytes(0)
 168 {
 169   _klasses = new (mtClassShared) GrowableArray<Klass*>(4 * K, mtClassShared);
 170   _symbols = new (mtClassShared) GrowableArray<Symbol*>(256 * K, mtClassShared);
 171 
 172   assert(_current == nullptr, "must be");
 173   _current = this;
 174 }
 175 
 176 ArchiveBuilder::~ArchiveBuilder() {
 177   assert(_current == this, "must be");
 178   _current = nullptr;
 179 

 194 public:
 195   GatherKlassesAndSymbols(ArchiveBuilder* builder) : _builder(builder) {}
 196 
 197   virtual bool do_unique_ref(Ref* ref, bool read_only) {
 198     return _builder->gather_klass_and_symbol(ref, read_only);
 199   }
 200 };
 201 
 202 bool ArchiveBuilder::gather_klass_and_symbol(MetaspaceClosure::Ref* ref, bool read_only) {
 203   if (ref->obj() == nullptr) {
 204     return false;
 205   }
 206   if (get_follow_mode(ref) != make_a_copy) {
 207     return false;
 208   }
 209   if (ref->msotype() == MetaspaceObj::ClassType) {
 210     Klass* klass = (Klass*)ref->obj();
 211     assert(klass->is_klass(), "must be");
 212     if (!is_excluded(klass)) {
 213       _klasses->append(klass);



 214     }
 215     // See RunTimeClassInfo::get_for()
 216     _estimated_metaspaceobj_bytes += align_up(BytesPerWord, SharedSpaceObjectAlignment);
 217   } else if (ref->msotype() == MetaspaceObj::SymbolType) {
 218     // Make sure the symbol won't be GC'ed while we are dumping the archive.
 219     Symbol* sym = (Symbol*)ref->obj();
 220     sym->increment_refcount();
 221     _symbols->append(sym);
 222   }
 223 
 224   int bytes = ref->size() * BytesPerWord;
 225   _estimated_metaspaceobj_bytes += align_up(bytes, SharedSpaceObjectAlignment);
 226 
 227   return true; // recurse
 228 }
 229 
 230 void ArchiveBuilder::gather_klasses_and_symbols() {
 231   ResourceMark rm;
 232   log_info(cds)("Gathering classes and symbols ... ");
 233   GatherKlassesAndSymbols doit(this);

 252     // During -Xshare:dump, the order of Symbol creation is strictly determined by
 253     // the SharedClassListFile (class loading is done in a single thread and the JIT
 254     // is disabled). Also, Symbols are allocated in monotonically increasing addresses
 255     // (see Symbol::operator new(size_t, int)). So if we iterate the Symbols by
 256     // ascending address order, we ensure that all Symbols are copied into deterministic
 257     // locations in the archive.
 258     //
 259     // TODO: in the future, if we want to produce deterministic contents in the
 260     // dynamic archive, we might need to sort the symbols alphabetically (also see
 261     // DynamicArchiveBuilder::sort_methods()).
 262     log_info(cds)("Sorting symbols ... ");
 263     _symbols->sort(compare_symbols_by_address);
 264     sort_klasses();
 265 
 266     // TODO -- we need a proper estimate for the archived modules, etc,
 267     // but this should be enough for now
 268     _estimated_metaspaceobj_bytes += 200 * 1024 * 1024;
 269   }
 270 }
 271 
































 272 int ArchiveBuilder::compare_symbols_by_address(Symbol** a, Symbol** b) {
 273   if (a[0] < b[0]) {
 274     return -1;
 275   } else {
 276     assert(a[0] > b[0], "Duplicated symbol %s unexpected", (*a)->as_C_string());
 277     return 1;
 278   }
 279 }
 280 
 281 int ArchiveBuilder::compare_klass_by_name(Klass** a, Klass** b) {
 282   return a[0]->name()->fast_compare(b[0]->name());
 283 }
 284 
 285 void ArchiveBuilder::sort_klasses() {
 286   log_info(cds)("Sorting classes ... ");
 287   _klasses->sort(compare_klass_by_name);
 288 }
 289 
 290 size_t ArchiveBuilder::estimate_archive_size() {
 291   // size of the symbol table and two dictionaries, plus the RunTimeClassInfo's
 292   size_t symbol_table_est = SymbolTable::estimate_size_for_archive();
 293   size_t dictionary_est = SystemDictionaryShared::estimate_size_for_archive();
 294   _estimated_hashtable_bytes = symbol_table_est + dictionary_est;

 295 








 296   size_t total = 0;
 297 
 298   total += _estimated_metaspaceobj_bytes;
 299   total += _estimated_hashtable_bytes;
 300 
 301   // allow fragmentation at the end of each dump region
 302   total += _total_dump_regions * MetaspaceShared::core_region_alignment();
 303 
 304   log_info(cds)("_estimated_hashtable_bytes = " SIZE_FORMAT " + " SIZE_FORMAT " = " SIZE_FORMAT,
 305                 symbol_table_est, dictionary_est, _estimated_hashtable_bytes);
 306   log_info(cds)("_estimated_metaspaceobj_bytes = " SIZE_FORMAT, _estimated_metaspaceobj_bytes);
 307   log_info(cds)("total estimate bytes = " SIZE_FORMAT, total);
 308 
 309   return align_up(total, MetaspaceShared::core_region_alignment());
 310 }
 311 
 312 address ArchiveBuilder::reserve_buffer() {
 313   size_t buffer_size = estimate_archive_size();
 314   ReservedSpace rs(buffer_size, MetaspaceShared::core_region_alignment(), os::vm_page_size());
 315   if (!rs.is_reserved()) {

 394   ArchiveBuilder* _builder;
 395 
 396 public:
 397   GatherSortedSourceObjs(ArchiveBuilder* builder) : _builder(builder) {}
 398 
 399   virtual bool do_ref(Ref* ref, bool read_only) {
 400     return _builder->gather_one_source_obj(ref, read_only);
 401   }
 402 };
 403 
 404 bool ArchiveBuilder::gather_one_source_obj(MetaspaceClosure::Ref* ref, bool read_only) {
 405   address src_obj = ref->obj();
 406   if (src_obj == nullptr) {
 407     return false;
 408   }
 409   if (RegeneratedClasses::has_been_regenerated(src_obj)) {
 410     // No need to copy it. We will later relocate it to point to the regenerated klass/method.
 411     return false;
 412   }
 413   remember_embedded_pointer_in_enclosing_obj(ref);




 414 
 415   FollowMode follow_mode = get_follow_mode(ref);
 416   SourceObjInfo src_info(ref, read_only, follow_mode);
 417   bool created;
 418   SourceObjInfo* p = _src_obj_table.put_if_absent(src_obj, src_info, &created);
 419   if (created) {
 420     if (_src_obj_table.maybe_grow()) {
 421       log_info(cds, hashtables)("Expanded _src_obj_table table to %d", _src_obj_table.table_size());
 422     }
 423   }
 424 
 425 #ifdef ASSERT
 426   if (ref->msotype() == MetaspaceObj::MethodType) {
 427     Method* m = (Method*)ref->obj();
 428     assert(!RegeneratedClasses::has_been_regenerated((address)m->method_holder()),
 429            "Should not archive methods in a class that has been regenerated");
 430   }
 431 #endif
 432 
 433   assert(p->read_only() == src_info.read_only(), "must be");

 493       _rw_src_objs.remember_embedded_pointer(src_info, ref);
 494     }
 495   }
 496 }
 497 
 498 void ArchiveBuilder::gather_source_objs() {
 499   ResourceMark rm;
 500   log_info(cds)("Gathering all archivable objects ... ");
 501   gather_klasses_and_symbols();
 502   GatherSortedSourceObjs doit(this);
 503   iterate_sorted_roots(&doit);
 504   doit.finish();
 505 }
 506 
 507 bool ArchiveBuilder::is_excluded(Klass* klass) {
 508   if (klass->is_instance_klass()) {
 509     InstanceKlass* ik = InstanceKlass::cast(klass);
 510     return SystemDictionaryShared::is_excluded_class(ik);
 511   } else if (klass->is_objArray_klass()) {
 512     Klass* bottom = ObjArrayKlass::cast(klass)->bottom_klass();
 513     if (MetaspaceShared::is_shared_static(bottom)) {
 514       // The bottom class is in the static archive so it's clearly not excluded.
 515       assert(CDSConfig::is_dumping_dynamic_archive(), "sanity");
 516       return false;
 517     } else if (bottom->is_instance_klass()) {
 518       return SystemDictionaryShared::is_excluded_class(InstanceKlass::cast(bottom));
 519     }
 520   }
 521 
 522   return false;
 523 }
 524 
 525 ArchiveBuilder::FollowMode ArchiveBuilder::get_follow_mode(MetaspaceClosure::Ref *ref) {
 526   address obj = ref->obj();
 527   if (MetaspaceShared::is_in_shared_metaspace(obj)) {
 528     // Don't dump existing shared metadata again.
 529     return point_to_it;
 530   } else if (ref->msotype() == MetaspaceObj::MethodDataType ||
 531              ref->msotype() == MetaspaceObj::MethodCountersType) {
 532     return set_to_null;



 533   } else {
 534     if (ref->msotype() == MetaspaceObj::ClassType) {
 535       Klass* klass = (Klass*)ref->obj();
 536       assert(klass->is_klass(), "must be");
 537       if (is_excluded(klass)) {
 538         ResourceMark rm;
 539         log_debug(cds, dynamic)("Skipping class (excluded): %s", klass->external_name());
 540         return set_to_null;
 541       }
 542     }
 543 
 544     return make_a_copy;
 545   }
 546 }
 547 
 548 void ArchiveBuilder::start_dump_space(DumpRegion* next) {
 549   address bottom = _last_verified_top;
 550   address top = (address)(current_dump_space()->top());
 551   _other_region_used_bytes += size_t(top - bottom);
 552 

 665 
 666   log_trace(cds)("Copy: " PTR_FORMAT " ==> " PTR_FORMAT " %d", p2i(src), p2i(dest), bytes);
 667   src_info->set_buffered_addr((address)dest);
 668 
 669   _alloc_stats.record(src_info->msotype(), int(newtop - oldtop), src_info->read_only());
 670 }
 671 
 672 // This is used by code that hand-assembles data structures, such as the LambdaProxyClassKey, that are
 673 // not handled by MetaspaceClosure.
 674 void ArchiveBuilder::write_pointer_in_buffer(address* ptr_location, address src_addr) {
 675   assert(is_in_buffer_space(ptr_location), "must be");
 676   if (src_addr == nullptr) {
 677     *ptr_location = nullptr;
 678     ArchivePtrMarker::clear_pointer(ptr_location);
 679   } else {
 680     *ptr_location = get_buffered_addr(src_addr);
 681     ArchivePtrMarker::mark_pointer(ptr_location);
 682   }
 683 }
 684 








 685 address ArchiveBuilder::get_buffered_addr(address src_addr) const {
 686   SourceObjInfo* p = _src_obj_table.get(src_addr);
 687   assert(p != nullptr, "src_addr " INTPTR_FORMAT " is used but has not been archived",
 688          p2i(src_addr));
 689 
 690   return p->buffered_addr();
 691 }
 692 





 693 address ArchiveBuilder::get_source_addr(address buffered_addr) const {
 694   assert(is_in_buffer_space(buffered_addr), "must be");
 695   address* src_p = _buffered_to_src_table.get(buffered_addr);
 696   assert(src_p != nullptr && *src_p != nullptr, "must be");
 697   return *src_p;
 698 }
 699 
 700 void ArchiveBuilder::relocate_embedded_pointers(ArchiveBuilder::SourceObjList* src_objs) {
 701   for (int i = 0; i < src_objs->objs()->length(); i++) {
 702     src_objs->relocate(i, this);
 703   }
 704 }
 705 
 706 void ArchiveBuilder::relocate_metaspaceobj_embedded_pointers() {
 707   log_info(cds)("Relocating embedded pointers in core regions ... ");
 708   relocate_embedded_pointers(&_rw_src_objs);
 709   relocate_embedded_pointers(&_ro_src_objs);
 710 }
 711 








 712 void ArchiveBuilder::make_klasses_shareable() {
 713   int num_instance_klasses = 0;
 714   int num_boot_klasses = 0;
 715   int num_platform_klasses = 0;
 716   int num_app_klasses = 0;
 717   int num_hidden_klasses = 0;
 718   int num_unlinked_klasses = 0;
 719   int num_unregistered_klasses = 0;

 720   int num_obj_array_klasses = 0;
 721   int num_type_array_klasses = 0;
 722 















 723   for (int i = 0; i < klasses()->length(); i++) {
 724     const char* type;
 725     const char* unlinked = "";
 726     const char* hidden = "";
 727     const char* generated = "";
 728     Klass* k = get_buffered_addr(klasses()->at(i));
 729     k->remove_java_mirror();
 730     if (k->is_objArray_klass()) {
 731       // InstanceKlass and TypeArrayKlass will in turn call remove_unshareable_info
 732       // on their array classes.
 733       num_obj_array_klasses ++;
 734       type = "array";
 735     } else if (k->is_typeArray_klass()) {
 736       num_type_array_klasses ++;
 737       type = "array";
 738       k->remove_unshareable_info();
 739     } else {
 740       assert(k->is_instance_klass(), " must be");
 741       num_instance_klasses ++;
 742       InstanceKlass* ik = InstanceKlass::cast(k);



 743       if (CDSConfig::is_dumping_dynamic_archive()) {
 744         // For static dump, class loader type are already set.
 745         ik->assign_class_loader_type();
 746       }
 747       if (ik->is_shared_boot_class()) {















 748         type = "boot";
 749         num_boot_klasses ++;
 750       } else if (ik->is_shared_platform_class()) {
 751         type = "plat";
 752         num_platform_klasses ++;
 753       } else if (ik->is_shared_app_class()) {
 754         type = "app";
 755         num_app_klasses ++;
 756       } else {
 757         assert(ik->is_shared_unregistered_class(), "must be");
 758         type = "unreg";
 759         num_unregistered_klasses ++;




 760       }
 761 
 762       if (!ik->is_linked()) {
 763         num_unlinked_klasses ++;
 764         unlinked = " ** unlinked";









 765       }
 766 
 767       if (ik->is_hidden()) {
 768         num_hidden_klasses ++;
 769         hidden = " ** hidden";
 770       }
 771 
 772       if (ik->is_generated_shared_class()) {
 773         generated = " ** generated";
 774       }
 775       MetaspaceShared::rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread::current(), ik);
 776       ik->remove_unshareable_info();
 777     }
 778 
 779     if (log_is_enabled(Debug, cds, class)) {
 780       ResourceMark rm;
 781       log_debug(cds, class)("klasses[%5d] = " PTR_FORMAT " %-5s %s%s%s%s", i,
 782                             p2i(to_requested(k)), type, k->external_name(),
 783                             hidden, unlinked, generated);
 784     }
 785   }
 786 
 787   log_info(cds)("Number of classes %d", num_instance_klasses + num_obj_array_klasses + num_type_array_klasses);
 788   log_info(cds)("    instance classes   = %5d", num_instance_klasses);
 789   log_info(cds)("      boot             = %5d", num_boot_klasses);
 790   log_info(cds)("      app              = %5d", num_app_klasses);
 791   log_info(cds)("      platform         = %5d", num_platform_klasses);
 792   log_info(cds)("      unregistered     = %5d", num_unregistered_klasses);
 793   log_info(cds)("      (hidden)         = %5d", num_hidden_klasses);
 794   log_info(cds)("      (unlinked)       = %5d", num_unlinked_klasses);




 795   log_info(cds)("    obj array classes  = %5d", num_obj_array_klasses);
 796   log_info(cds)("    type array classes = %5d", num_type_array_klasses);
 797   log_info(cds)("               symbols = %5d", _symbols->length());
 798 
 799   DynamicArchive::make_array_klasses_shareable();
 800 }
 801 
 802 void ArchiveBuilder::serialize_dynamic_archivable_items(SerializeClosure* soc) {
 803   SymbolTable::serialize_shared_table_header(soc, false);
 804   SystemDictionaryShared::serialize_dictionary_headers(soc, false);
 805   DynamicArchive::serialize_array_klasses(soc);


 806 }
 807 
 808 uintx ArchiveBuilder::buffer_to_offset(address p) const {
 809   address requested_p = to_requested(p);
 810   assert(requested_p >= _requested_static_archive_bottom, "must be");
 811   return requested_p - _requested_static_archive_bottom;
 812 }
 813 
 814 uintx ArchiveBuilder::any_to_offset(address p) const {
 815   if (is_in_mapped_static_archive(p)) {
 816     assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
 817     return p - _mapped_static_archive_bottom;
 818   }
 819   if (!is_in_buffer_space(p)) {
 820     // p must be a "source" address
 821     p = get_buffered_addr(p);
 822   }
 823   return buffer_to_offset(p);
 824 }
 825 









 826 #if INCLUDE_CDS_JAVA_HEAP
 827 narrowKlass ArchiveBuilder::get_requested_narrow_klass(Klass* k) {
 828   assert(CDSConfig::is_dumping_heap(), "sanity");
 829   k = get_buffered_klass(k);
 830   Klass* requested_k = to_requested(k);
 831   address narrow_klass_base = _requested_static_archive_bottom; // runtime encoding base == runtime mapping start
 832   const int narrow_klass_shift = ArchiveHeapWriter::precomputed_narrow_klass_shift;
 833   return CompressedKlassPointers::encode_not_null(requested_k, narrow_klass_base, narrow_klass_shift);
 834 }
 835 #endif // INCLUDE_CDS_JAVA_HEAP
 836 
 837 // RelocateBufferToRequested --- Relocate all the pointers in rw/ro,
 838 // so that the archive can be mapped to the "requested" location without runtime relocation.
 839 //
 840 // - See ArchiveBuilder header for the definition of "buffer", "mapped" and "requested"
 841 // - ArchivePtrMarker::ptrmap() marks all the pointers in the rw/ro regions
 842 // - Every pointer must have one of the following values:
 843 //   [a] nullptr:
 844 //       No relocation is needed. Remove this pointer from ptrmap so we don't need to
 845 //       consider it at runtime.

 895         } else {
 896           assert(_builder->is_in_mapped_static_archive(*p), "old pointer must point inside buffer space or mapped static archive");
 897           *p += _mapped_to_requested_static_archive_delta;
 898           assert(_builder->is_in_requested_static_archive(*p), "new pointer must point inside requested archive");
 899         }
 900       }
 901       _max_non_null_offset = offset;
 902     }
 903 
 904     return true; // keep iterating
 905   }
 906 
 907   void doit() {
 908     ArchivePtrMarker::ptrmap()->iterate(this);
 909     ArchivePtrMarker::compact(_max_non_null_offset);
 910   }
 911 };
 912 
 913 
 914 void ArchiveBuilder::relocate_to_requested() {
 915   ro_region()->pack();


 916 
 917   size_t my_archive_size = buffer_top() - buffer_bottom();
 918 
 919   if (CDSConfig::is_dumping_static_archive()) {
 920     _requested_static_archive_top = _requested_static_archive_bottom + my_archive_size;
 921     RelocateBufferToRequested<true> patcher(this);
 922     patcher.doit();
 923   } else {
 924     assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
 925     _requested_dynamic_archive_top = _requested_dynamic_archive_bottom + my_archive_size;
 926     RelocateBufferToRequested<false> patcher(this);
 927     patcher.doit();
 928   }
 929 }
 930 
 931 // Write detailed info to a mapfile to analyze contents of the archive.
 932 // static dump:
 933 //   java -Xshare:dump -Xlog:cds+map=trace:file=cds.map:none:filesize=0
 934 // dynamic dump:
 935 //   java -cp MyApp.jar -XX:ArchiveClassesAtExit=MyApp.jsa \

1043     base = requested_base;
1044     top = requested_base + size;
1045     log_info(cds, map)("[%-18s " PTR_FORMAT " - " PTR_FORMAT " " SIZE_FORMAT_W(9) " bytes]",
1046                        name, p2i(base), p2i(top), size);
1047   }
1048 
1049 #if INCLUDE_CDS_JAVA_HEAP
1050   static void log_heap_region(ArchiveHeapInfo* heap_info) {
1051     MemRegion r = heap_info->buffer_region();
1052     address start = address(r.start());
1053     address end = address(r.end());
1054     log_region("heap", start, end, ArchiveHeapWriter::buffered_addr_to_requested_addr(start));
1055 
1056     LogStreamHandle(Info, cds, map) st;
1057 
1058     while (start < end) {
1059       size_t byte_size;
1060       oop source_oop = ArchiveHeapWriter::buffered_addr_to_source_obj(start);
1061       address requested_start = ArchiveHeapWriter::buffered_addr_to_requested_addr(start);
1062       st.print(PTR_FORMAT ": @@ Object ", p2i(requested_start));


1063 
1064       if (source_oop != nullptr) {
1065         // This is a regular oop that got archived.
1066         print_oop_with_requested_addr_cr(&st, source_oop, false);
1067         byte_size = source_oop->size() * BytesPerWord;
1068       } else if (start == ArchiveHeapWriter::buffered_heap_roots_addr()) {
1069         // HeapShared::roots() is copied specially, so it doesn't exist in
1070         // ArchiveHeapWriter::BufferOffsetToSourceObjectTable.
1071         // See ArchiveHeapWriter::copy_roots_to_buffer().
1072         st.print_cr("HeapShared::roots[%d]", HeapShared::pending_roots()->length());
1073         byte_size = ArchiveHeapWriter::heap_roots_word_size() * BytesPerWord;
1074       } else if ((byte_size = ArchiveHeapWriter::get_filler_size_at(start)) > 0) {
1075         // We have a filler oop, which also does not exist in BufferOffsetToSourceObjectTable.
1076         st.print_cr("filler " SIZE_FORMAT " bytes", byte_size);


1077       } else {
1078         ShouldNotReachHere();
1079       }
1080 
1081       address oop_end = start + byte_size;
1082       log_as_hex(start, oop_end, requested_start, /*is_heap=*/true);
1083 
1084       if (source_oop != nullptr) {
1085         log_oop_details(heap_info, source_oop);
1086       } else if (start == ArchiveHeapWriter::buffered_heap_roots_addr()) {
1087         log_heap_roots();


1088       }
1089       start = oop_end;
1090     }
1091   }
1092 
1093   // ArchivedFieldPrinter is used to print the fields of archived objects. We can't
1094   // use _source_obj->print_on(), because we want to print the oop fields
1095   // in _source_obj with their requested addresses using print_oop_with_requested_addr_cr().
1096   class ArchivedFieldPrinter : public FieldClosure {
1097     ArchiveHeapInfo* _heap_info;
1098     outputStream* _st;
1099     oop _source_obj;
1100   public:
1101     ArchivedFieldPrinter(ArchiveHeapInfo* heap_info, outputStream* st, oop src_obj) :
1102       _heap_info(heap_info), _st(st), _source_obj(src_obj) {}
1103 
1104     void do_field(fieldDescriptor* fd) {
1105       _st->print(" - ");
1106       BasicType ft = fd->field_type();
1107       switch (ft) {

1167           print_oop_with_requested_addr_cr(&st, source_obj_array->obj_at(i));
1168         }
1169       } else {
1170         st.print_cr(" - fields (" SIZE_FORMAT " words):", source_oop->size());
1171         ArchivedFieldPrinter print_field(heap_info, &st, source_oop);
1172         InstanceKlass::cast(source_klass)->print_nonstatic_fields(&print_field);
1173       }
1174     }
1175   }
1176 
1177   static void log_heap_roots() {
1178     LogStreamHandle(Trace, cds, map, oops) st;
1179     if (st.is_enabled()) {
1180       for (int i = 0; i < HeapShared::pending_roots()->length(); i++) {
1181         st.print("roots[%4d]: ", i);
1182         print_oop_with_requested_addr_cr(&st, HeapShared::pending_roots()->at(i));
1183       }
1184     }
1185   }
1186 











1187   // The output looks like this. The first number is the requested address. The second number is
1188   // the narrowOop version of the requested address.
1189   //     0x00000007ffc7e840 (0xfff8fd08) java.lang.Class
1190   //     0x00000007ffc000f8 (0xfff8001f) [B length: 11
1191   static void print_oop_with_requested_addr_cr(outputStream* st, oop source_oop, bool print_addr = true) {
1192     if (source_oop == nullptr) {
1193       st->print_cr("null");
1194     } else {
1195       ResourceMark rm;
1196       oop requested_obj = ArchiveHeapWriter::source_obj_to_requested_obj(source_oop);
1197       if (print_addr) {
1198         st->print(PTR_FORMAT " ", p2i(requested_obj));
1199       }
1200       if (UseCompressedOops) {
1201         st->print("(0x%08x) ", CompressedOops::narrow_oop_value(requested_obj));
1202       }
1203       if (source_oop->is_array()) {
1204         int array_len = arrayOop(source_oop)->length();
1205         st->print_cr("%s length: %d", source_oop->klass()->external_name(), array_len);
1206       } else {
1207         st->print_cr("%s", source_oop->klass()->external_name());





1208       }
1209     }
1210   }
1211 #endif // INCLUDE_CDS_JAVA_HEAP
1212 
1213   // Log all the data [base...top). Pretend that the base address
1214   // will be mapped to requested_base at run-time.
1215   static void log_as_hex(address base, address top, address requested_base, bool is_heap = false) {
1216     assert(top >= base, "must be");
1217 
1218     LogStreamHandle(Trace, cds, map) lsh;
1219     if (lsh.is_enabled()) {
1220       int unitsize = sizeof(address);
1221       if (is_heap && UseCompressedOops) {
1222         // This makes the compressed oop pointers easier to read, but
1223         // longs and doubles will be split into two words.
1224         unitsize = sizeof(narrowOop);
1225       }
1226       os::print_hex_dump(&lsh, base, top, unitsize, 32, requested_base);
1227     }

1260     if (heap_info->is_used()) {
1261       log_heap_region(heap_info);
1262     }
1263 #endif
1264 
1265     log_info(cds, map)("[End of CDS archive map]");
1266   }
1267 }; // end ArchiveBuilder::CDSMapLogger
1268 
1269 void ArchiveBuilder::print_stats() {
1270   _alloc_stats.print_stats(int(_ro_region.used()), int(_rw_region.used()));
1271 }
1272 
1273 void ArchiveBuilder::write_archive(FileMapInfo* mapinfo, ArchiveHeapInfo* heap_info) {
1274   // Make sure NUM_CDS_REGIONS (exported in cds.h) agrees with
1275   // MetaspaceShared::n_regions (internal to hotspot).
1276   assert(NUM_CDS_REGIONS == MetaspaceShared::n_regions, "sanity");
1277 
1278   write_region(mapinfo, MetaspaceShared::rw, &_rw_region, /*read_only=*/false,/*allow_exec=*/false);
1279   write_region(mapinfo, MetaspaceShared::ro, &_ro_region, /*read_only=*/true, /*allow_exec=*/false);

1280 
1281   size_t bitmap_size_in_bytes;
1282   char* bitmap = mapinfo->write_bitmap_region(ArchivePtrMarker::ptrmap(), heap_info,
1283                                               bitmap_size_in_bytes);
1284 
1285   if (heap_info->is_used()) {
1286     _total_heap_region_size = mapinfo->write_heap_region(heap_info);
1287   }
1288 
1289   print_region_stats(mapinfo, heap_info);
1290 
1291   mapinfo->set_requested_base((char*)MetaspaceShared::requested_base_address());
1292   mapinfo->set_header_crc(mapinfo->compute_header_crc());
1293   // After this point, we should not write any data into mapinfo->header() since this
1294   // would corrupt its checksum we have calculated before.
1295   mapinfo->write_header();
1296   mapinfo->close();
1297 
1298   if (log_is_enabled(Info, cds)) {
1299     print_stats();

1308 }
1309 
1310 void ArchiveBuilder::write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region, bool read_only,  bool allow_exec) {
1311   mapinfo->write_region(region_idx, dump_region->base(), dump_region->used(), read_only, allow_exec);
1312 }
1313 
1314 void ArchiveBuilder::print_region_stats(FileMapInfo *mapinfo, ArchiveHeapInfo* heap_info) {
1315   // Print statistics of all the regions
1316   const size_t bitmap_used = mapinfo->region_at(MetaspaceShared::bm)->used();
1317   const size_t bitmap_reserved = mapinfo->region_at(MetaspaceShared::bm)->used_aligned();
1318   const size_t total_reserved = _ro_region.reserved()  + _rw_region.reserved() +
1319                                 bitmap_reserved +
1320                                 _total_heap_region_size;
1321   const size_t total_bytes = _ro_region.used()  + _rw_region.used() +
1322                              bitmap_used +
1323                              _total_heap_region_size;
1324   const double total_u_perc = percent_of(total_bytes, total_reserved);
1325 
1326   _rw_region.print(total_reserved);
1327   _ro_region.print(total_reserved);

1328 
1329   print_bitmap_region_stats(bitmap_used, total_reserved);
1330 
1331   if (heap_info->is_used()) {
1332     print_heap_region_stats(heap_info, total_reserved);
1333   }
1334 
1335   log_debug(cds)("total   : " SIZE_FORMAT_W(9) " [100.0%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used]",
1336                  total_bytes, total_reserved, total_u_perc);
1337 }
1338 
1339 void ArchiveBuilder::print_bitmap_region_stats(size_t size, size_t total_size) {
1340   log_debug(cds)("bm space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [100.0%% used]",
1341                  size, size/double(total_size)*100.0, size);
1342 }
1343 
1344 void ArchiveBuilder::print_heap_region_stats(ArchiveHeapInfo *info, size_t total_size) {
1345   char* start = info->buffer_start();
1346   size_t size = info->buffer_byte_size();
1347   char* top = start + size;
1348   log_debug(cds)("hp space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [100.0%% used] at " INTPTR_FORMAT,
1349                      size, size/double(total_size)*100.0, size, p2i(start));
1350 }
1351 
1352 void ArchiveBuilder::report_out_of_space(const char* name, size_t needed_bytes) {
1353   // This is highly unlikely to happen on 64-bits because we have reserved a 4GB space.
1354   // On 32-bit we reserve only 256MB so you could run out of space with 100,000 classes
1355   // or so.
1356   _rw_region.print_out_of_space_msg(name, needed_bytes);
1357   _ro_region.print_out_of_space_msg(name, needed_bytes);
1358 
1359   log_error(cds)("Unable to allocate from '%s' region: Please reduce the number of shared classes.", name);
1360   MetaspaceShared::unrecoverable_writing_error();
1361 }
1362 
1363 
1364 #ifndef PRODUCT
1365 void ArchiveBuilder::assert_is_vm_thread() {
1366   assert(Thread::current()->is_VM_thread(), "ArchiveBuilder should be used only inside the VMThread");
1367 }
1368 #endif

  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "cds/archiveBuilder.hpp"
  27 #include "cds/archiveHeapWriter.hpp"
  28 #include "cds/archiveUtils.hpp"
  29 #include "cds/cdsConfig.hpp"
  30 #include "cds/classPrelinker.hpp"
  31 #include "cds/cppVtables.hpp"
  32 #include "cds/dumpAllocStats.hpp"
  33 #include "cds/dynamicArchive.hpp"
  34 #include "cds/heapShared.hpp"
  35 #include "cds/metaspaceShared.hpp"
  36 #include "cds/regeneratedClasses.hpp"
  37 #include "classfile/classLoader.hpp"
  38 #include "classfile/classLoaderDataShared.hpp"
  39 #include "classfile/javaClasses.hpp"
  40 #include "classfile/symbolTable.hpp"
  41 #include "classfile/systemDictionaryShared.hpp"
  42 #include "classfile/vmClasses.hpp"
  43 #include "interpreter/abstractInterpreter.hpp"
  44 #include "logging/log.hpp"
  45 #include "logging/logStream.hpp"
  46 #include "memory/allStatic.hpp"
  47 #include "memory/memRegion.hpp"
  48 #include "memory/resourceArea.hpp"
  49 #include "oops/compressedKlass.inline.hpp"
  50 #include "oops/instanceKlass.hpp"
  51 #include "oops/objArrayKlass.hpp"
  52 #include "oops/objArrayOop.inline.hpp"
  53 #include "oops/oopHandle.inline.hpp"
  54 #include "oops/trainingData.hpp"
  55 #include "runtime/arguments.hpp"
  56 #include "runtime/fieldDescriptor.inline.hpp"
  57 #include "runtime/globals_extension.hpp"
  58 #include "runtime/javaThread.hpp"
  59 #include "runtime/sharedRuntime.hpp"
  60 #include "utilities/align.hpp"
  61 #include "utilities/bitMap.inline.hpp"
  62 #include "utilities/formatBuffer.hpp"
  63 
  64 ArchiveBuilder* ArchiveBuilder::_current = nullptr;
  65 
  66 ArchiveBuilder::OtherROAllocMark::~OtherROAllocMark() {
  67   char* newtop = ArchiveBuilder::current()->_ro_region.top();
  68   ArchiveBuilder::alloc_stats()->record_other_type(int(newtop - _oldtop), true);
  69 }
  70 
  71 ArchiveBuilder::SourceObjList::SourceObjList() : _ptrmap(16 * K, mtClassShared) {
  72   _total_bytes = 0;
  73   _objs = new (mtClassShared) GrowableArray<SourceObjInfo*>(128 * K, mtClassShared);
  74 }

 143 
 144   RelocateEmbeddedPointers relocator(builder, src_info->buffered_addr(), start);
 145   _ptrmap.iterate(&relocator, start, end);
 146 }
 147 
 148 ArchiveBuilder::ArchiveBuilder() :
 149   _current_dump_space(nullptr),
 150   _buffer_bottom(nullptr),
 151   _last_verified_top(nullptr),
 152   _num_dump_regions_used(0),
 153   _other_region_used_bytes(0),
 154   _requested_static_archive_bottom(nullptr),
 155   _requested_static_archive_top(nullptr),
 156   _requested_dynamic_archive_bottom(nullptr),
 157   _requested_dynamic_archive_top(nullptr),
 158   _mapped_static_archive_bottom(nullptr),
 159   _mapped_static_archive_top(nullptr),
 160   _buffer_to_requested_delta(0),
 161   _rw_region("rw", MAX_SHARED_DELTA),
 162   _ro_region("ro", MAX_SHARED_DELTA),
 163   _cc_region("cc", MAX_SHARED_DELTA),
 164   _ptrmap(mtClassShared),
 165   _rw_src_objs(),
 166   _ro_src_objs(),
 167   _src_obj_table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE),
 168   _buffered_to_src_table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE),
 169   _total_heap_region_size(0),
 170   _estimated_metaspaceobj_bytes(0),
 171   _estimated_hashtable_bytes(0)
 172 {
 173   _klasses = new (mtClassShared) GrowableArray<Klass*>(4 * K, mtClassShared);
 174   _symbols = new (mtClassShared) GrowableArray<Symbol*>(256 * K, mtClassShared);
 175 
 176   assert(_current == nullptr, "must be");
 177   _current = this;
 178 }
 179 
 180 ArchiveBuilder::~ArchiveBuilder() {
 181   assert(_current == this, "must be");
 182   _current = nullptr;
 183 

 198 public:
 199   GatherKlassesAndSymbols(ArchiveBuilder* builder) : _builder(builder) {}
 200 
 201   virtual bool do_unique_ref(Ref* ref, bool read_only) {
 202     return _builder->gather_klass_and_symbol(ref, read_only);
 203   }
 204 };
 205 
 206 bool ArchiveBuilder::gather_klass_and_symbol(MetaspaceClosure::Ref* ref, bool read_only) {
 207   if (ref->obj() == nullptr) {
 208     return false;
 209   }
 210   if (get_follow_mode(ref) != make_a_copy) {
 211     return false;
 212   }
 213   if (ref->msotype() == MetaspaceObj::ClassType) {
 214     Klass* klass = (Klass*)ref->obj();
 215     assert(klass->is_klass(), "must be");
 216     if (!is_excluded(klass)) {
 217       _klasses->append(klass);
 218       if (klass->is_hidden() && klass->is_instance_klass()) {
 219         update_hidden_class_loader_type(InstanceKlass::cast(klass));
 220       }
 221     }
 222     // See RunTimeClassInfo::get_for()
 223     _estimated_metaspaceobj_bytes += align_up(BytesPerWord, SharedSpaceObjectAlignment);
 224   } else if (ref->msotype() == MetaspaceObj::SymbolType) {
 225     // Make sure the symbol won't be GC'ed while we are dumping the archive.
 226     Symbol* sym = (Symbol*)ref->obj();
 227     sym->increment_refcount();
 228     _symbols->append(sym);
 229   }
 230 
 231   int bytes = ref->size() * BytesPerWord;
 232   _estimated_metaspaceobj_bytes += align_up(bytes, SharedSpaceObjectAlignment);
 233 
 234   return true; // recurse
 235 }
 236 
 237 void ArchiveBuilder::gather_klasses_and_symbols() {
 238   ResourceMark rm;
 239   log_info(cds)("Gathering classes and symbols ... ");
 240   GatherKlassesAndSymbols doit(this);

 259     // During -Xshare:dump, the order of Symbol creation is strictly determined by
 260     // the SharedClassListFile (class loading is done in a single thread and the JIT
 261     // is disabled). Also, Symbols are allocated in monotonically increasing addresses
 262     // (see Symbol::operator new(size_t, int)). So if we iterate the Symbols by
 263     // ascending address order, we ensure that all Symbols are copied into deterministic
 264     // locations in the archive.
 265     //
 266     // TODO: in the future, if we want to produce deterministic contents in the
 267     // dynamic archive, we might need to sort the symbols alphabetically (also see
 268     // DynamicArchiveBuilder::sort_methods()).
 269     log_info(cds)("Sorting symbols ... ");
 270     _symbols->sort(compare_symbols_by_address);
 271     sort_klasses();
 272 
 273     // TODO -- we need a proper estimate for the archived modules, etc,
 274     // but this should be enough for now
 275     _estimated_metaspaceobj_bytes += 200 * 1024 * 1024;
 276   }
 277 }
 278 
 279 #if INCLUDE_CDS_JAVA_HEAP
 280 
 281 void ArchiveBuilder::update_hidden_class_loader_type(InstanceKlass* ik) {
 282   s2 classloader_type;
 283   if (HeapShared::is_lambda_form_klass(ik)) {
 284     assert(CDSConfig::is_dumping_invokedynamic(), "lambda form classes are archived only if ArchiveInvokeDynamic is true");
 285     classloader_type = ClassLoader::BOOT_LOADER;
 286   } else if (HeapShared::is_lambda_proxy_klass(ik)) {
 287     oop loader = ik->class_loader();
 288 
 289     if (loader == nullptr) {
 290       classloader_type = ClassLoader::BOOT_LOADER;
 291     } else if (SystemDictionary::is_platform_class_loader(loader)) {
 292       classloader_type = ClassLoader::PLATFORM_LOADER;
 293     } else if (SystemDictionary::is_system_class_loader(loader)) {
 294       classloader_type = ClassLoader::APP_LOADER;
 295     } else {
 296       ShouldNotReachHere();
 297     }
 298   } else {
 299     ShouldNotReachHere();
 300   }
 301 
 302   ik->set_shared_class_loader_type(classloader_type);
 303   if (HeapShared::is_lambda_proxy_klass(ik)) {
 304     InstanceKlass* nest_host = ik->nest_host_not_null();
 305     ik->set_shared_classpath_index(nest_host->shared_classpath_index());
 306   }
 307 }
 308 
 309 #endif //INCLUDE_CDS_JAVA_HEAP
 310 
 311 int ArchiveBuilder::compare_symbols_by_address(Symbol** a, Symbol** b) {
 312   if (a[0] < b[0]) {
 313     return -1;
 314   } else {
 315     assert(a[0] > b[0], "Duplicated symbol %s unexpected", (*a)->as_C_string());
 316     return 1;
 317   }
 318 }
 319 
 320 int ArchiveBuilder::compare_klass_by_name(Klass** a, Klass** b) {
 321   return a[0]->name()->fast_compare(b[0]->name());
 322 }
 323 
 324 void ArchiveBuilder::sort_klasses() {
 325   log_info(cds)("Sorting classes ... ");
 326   _klasses->sort(compare_klass_by_name);
 327 }
 328 
 329 size_t ArchiveBuilder::estimate_archive_size() {
 330   // size of the symbol table and two dictionaries, plus the RunTimeClassInfo's
 331   size_t symbol_table_est = SymbolTable::estimate_size_for_archive();
 332   size_t dictionary_est = SystemDictionaryShared::estimate_size_for_archive();
 333   size_t training_data_est = TrainingData::estimate_size_for_archive();
 334   _estimated_hashtable_bytes = symbol_table_est + dictionary_est + training_data_est;
 335 
 336   if (CDSConfig::is_dumping_final_static_archive()) {
 337     _estimated_hashtable_bytes += 200 * 1024 * 1024; // FIXME -- need to iterate archived symbols??
 338   }
 339 
 340   if (CDSConfig::is_dumping_dynamic_archive()) {
 341     // Some extra space for traning data. Be generous. Unused areas will be trimmed from the archive file.
 342     _estimated_hashtable_bytes += 200 * 1024 * 1024;
 343   }
 344   size_t total = 0;
 345 
 346   total += _estimated_metaspaceobj_bytes;
 347   total += _estimated_hashtable_bytes;
 348 
 349   // allow fragmentation at the end of each dump region
 350   total += _total_dump_regions * MetaspaceShared::core_region_alignment();
 351 
 352   log_info(cds)("_estimated_hashtable_bytes = " SIZE_FORMAT " + " SIZE_FORMAT " = " SIZE_FORMAT,
 353                 symbol_table_est, dictionary_est, _estimated_hashtable_bytes);
 354   log_info(cds)("_estimated_metaspaceobj_bytes = " SIZE_FORMAT, _estimated_metaspaceobj_bytes);
 355   log_info(cds)("total estimate bytes = " SIZE_FORMAT, total);
 356 
 357   return align_up(total, MetaspaceShared::core_region_alignment());
 358 }
 359 
 360 address ArchiveBuilder::reserve_buffer() {
 361   size_t buffer_size = estimate_archive_size();
 362   ReservedSpace rs(buffer_size, MetaspaceShared::core_region_alignment(), os::vm_page_size());
 363   if (!rs.is_reserved()) {

 442   ArchiveBuilder* _builder;
 443 
 444 public:
 445   GatherSortedSourceObjs(ArchiveBuilder* builder) : _builder(builder) {}
 446 
 447   virtual bool do_ref(Ref* ref, bool read_only) {
 448     return _builder->gather_one_source_obj(ref, read_only);
 449   }
 450 };
 451 
 452 bool ArchiveBuilder::gather_one_source_obj(MetaspaceClosure::Ref* ref, bool read_only) {
 453   address src_obj = ref->obj();
 454   if (src_obj == nullptr) {
 455     return false;
 456   }
 457   if (RegeneratedClasses::has_been_regenerated(src_obj)) {
 458     // No need to copy it. We will later relocate it to point to the regenerated klass/method.
 459     return false;
 460   }
 461   remember_embedded_pointer_in_enclosing_obj(ref);
 462   if (RegeneratedClasses::has_been_regenerated(src_obj)) {
 463     // No need to copy it. We will later relocate it to point to the regenerated klass/method.
 464     return false;
 465   }
 466 
 467   FollowMode follow_mode = get_follow_mode(ref);
 468   SourceObjInfo src_info(ref, read_only, follow_mode);
 469   bool created;
 470   SourceObjInfo* p = _src_obj_table.put_if_absent(src_obj, src_info, &created);
 471   if (created) {
 472     if (_src_obj_table.maybe_grow()) {
 473       log_info(cds, hashtables)("Expanded _src_obj_table table to %d", _src_obj_table.table_size());
 474     }
 475   }
 476 
 477 #ifdef ASSERT
 478   if (ref->msotype() == MetaspaceObj::MethodType) {
 479     Method* m = (Method*)ref->obj();
 480     assert(!RegeneratedClasses::has_been_regenerated((address)m->method_holder()),
 481            "Should not archive methods in a class that has been regenerated");
 482   }
 483 #endif
 484 
 485   assert(p->read_only() == src_info.read_only(), "must be");

 545       _rw_src_objs.remember_embedded_pointer(src_info, ref);
 546     }
 547   }
 548 }
 549 
 550 void ArchiveBuilder::gather_source_objs() {
 551   ResourceMark rm;
 552   log_info(cds)("Gathering all archivable objects ... ");
 553   gather_klasses_and_symbols();
 554   GatherSortedSourceObjs doit(this);
 555   iterate_sorted_roots(&doit);
 556   doit.finish();
 557 }
 558 
 559 bool ArchiveBuilder::is_excluded(Klass* klass) {
 560   if (klass->is_instance_klass()) {
 561     InstanceKlass* ik = InstanceKlass::cast(klass);
 562     return SystemDictionaryShared::is_excluded_class(ik);
 563   } else if (klass->is_objArray_klass()) {
 564     Klass* bottom = ObjArrayKlass::cast(klass)->bottom_klass();
 565     if (CDSConfig::is_dumping_dynamic_archive() && MetaspaceShared::is_shared_static(bottom)) {
 566       // The bottom class is in the static archive so it's clearly not excluded.
 567       assert(CDSConfig::is_dumping_dynamic_archive(), "sanity");
 568       return false;
 569     } else if (bottom->is_instance_klass()) {
 570       return SystemDictionaryShared::is_excluded_class(InstanceKlass::cast(bottom));
 571     }
 572   }
 573 
 574   return false;
 575 }
 576 
 577 ArchiveBuilder::FollowMode ArchiveBuilder::get_follow_mode(MetaspaceClosure::Ref *ref) {
 578   address obj = ref->obj();
 579   if (CDSConfig::is_dumping_dynamic_archive() && MetaspaceShared::is_in_shared_metaspace(obj)) {
 580     // Don't dump existing shared metadata again.
 581     return point_to_it;
 582   } else if (ref->msotype() == MetaspaceObj::MethodDataType ||
 583              ref->msotype() == MetaspaceObj::MethodCountersType ||
 584              ref->msotype() == MetaspaceObj::KlassTrainingDataType ||
 585              ref->msotype() == MetaspaceObj::MethodTrainingDataType ||
 586              ref->msotype() == MetaspaceObj::CompileTrainingDataType) {
 587       return TrainingData::need_data() ? make_a_copy : set_to_null;
 588   } else {
 589     if (ref->msotype() == MetaspaceObj::ClassType) {
 590       Klass* klass = (Klass*)ref->obj();
 591       assert(klass->is_klass(), "must be");
 592       if (is_excluded(klass)) {
 593         ResourceMark rm;
 594         log_debug(cds, dynamic)("Skipping class (excluded): %s", klass->external_name());
 595         return set_to_null;
 596       }
 597     }
 598 
 599     return make_a_copy;
 600   }
 601 }
 602 
 603 void ArchiveBuilder::start_dump_space(DumpRegion* next) {
 604   address bottom = _last_verified_top;
 605   address top = (address)(current_dump_space()->top());
 606   _other_region_used_bytes += size_t(top - bottom);
 607 

 720 
 721   log_trace(cds)("Copy: " PTR_FORMAT " ==> " PTR_FORMAT " %d", p2i(src), p2i(dest), bytes);
 722   src_info->set_buffered_addr((address)dest);
 723 
 724   _alloc_stats.record(src_info->msotype(), int(newtop - oldtop), src_info->read_only());
 725 }
 726 
 727 // This is used by code that hand-assembles data structures, such as the LambdaProxyClassKey, that are
 728 // not handled by MetaspaceClosure.
 729 void ArchiveBuilder::write_pointer_in_buffer(address* ptr_location, address src_addr) {
 730   assert(is_in_buffer_space(ptr_location), "must be");
 731   if (src_addr == nullptr) {
 732     *ptr_location = nullptr;
 733     ArchivePtrMarker::clear_pointer(ptr_location);
 734   } else {
 735     *ptr_location = get_buffered_addr(src_addr);
 736     ArchivePtrMarker::mark_pointer(ptr_location);
 737   }
 738 }
 739 
 740 void ArchiveBuilder::mark_and_relocate_to_buffered_addr(address* ptr_location) {
 741   assert(*ptr_location != nullptr, "sanity");
 742   if (!is_in_mapped_static_archive(*ptr_location)) {
 743     *ptr_location = get_buffered_addr(*ptr_location);
 744   }
 745   ArchivePtrMarker::mark_pointer(ptr_location);
 746 }
 747 
 748 address ArchiveBuilder::get_buffered_addr(address src_addr) const {
 749   SourceObjInfo* p = _src_obj_table.get(src_addr);
 750   assert(p != nullptr, "src_addr " INTPTR_FORMAT " is used but has not been archived",
 751          p2i(src_addr));
 752 
 753   return p->buffered_addr();
 754 }
 755 
 756 bool ArchiveBuilder::has_been_archived(address src_addr) const {
 757   SourceObjInfo* p = _src_obj_table.get(src_addr);
 758   return (p != nullptr);
 759 }
 760 
 761 address ArchiveBuilder::get_source_addr(address buffered_addr) const {
 762   assert(is_in_buffer_space(buffered_addr), "must be");
 763   address* src_p = _buffered_to_src_table.get(buffered_addr);
 764   assert(src_p != nullptr && *src_p != nullptr, "must be");
 765   return *src_p;
 766 }
 767 
 768 void ArchiveBuilder::relocate_embedded_pointers(ArchiveBuilder::SourceObjList* src_objs) {
 769   for (int i = 0; i < src_objs->objs()->length(); i++) {
 770     src_objs->relocate(i, this);
 771   }
 772 }
 773 
 774 void ArchiveBuilder::relocate_metaspaceobj_embedded_pointers() {
 775   log_info(cds)("Relocating embedded pointers in core regions ... ");
 776   relocate_embedded_pointers(&_rw_src_objs);
 777   relocate_embedded_pointers(&_ro_src_objs);
 778 }
 779 
 780 #define ADD_COUNT(x) \
 781   x += 1; \
 782   x ## _i += inited;
 783 
 784 #define DECLARE_INSTANCE_KLASS_COUNTER(x) \
 785   int x = 0; \
 786   int x ## _i = 0; \
 787 
 788 void ArchiveBuilder::make_klasses_shareable() {
 789   DECLARE_INSTANCE_KLASS_COUNTER(num_instance_klasses);
 790   DECLARE_INSTANCE_KLASS_COUNTER(num_boot_klasses);
 791   DECLARE_INSTANCE_KLASS_COUNTER(num_vm_klasses);
 792   DECLARE_INSTANCE_KLASS_COUNTER(num_platform_klasses);
 793   DECLARE_INSTANCE_KLASS_COUNTER(num_app_klasses);
 794   DECLARE_INSTANCE_KLASS_COUNTER(num_hidden_klasses);
 795   DECLARE_INSTANCE_KLASS_COUNTER(num_unlinked_klasses);
 796   DECLARE_INSTANCE_KLASS_COUNTER(num_unregistered_klasses);
 797   int num_obj_array_klasses = 0;
 798   int num_type_array_klasses = 0;
 799 
 800   int boot_unlinked = 0;
 801   int platform_unlinked = 0;
 802   int app_unlinked = 0;
 803   int unreg_unlinked = 0;
 804 
 805   for (int i = 0; i < klasses()->length(); i++) {
 806     // Some of the code in ConstantPool::remove_unshareable_info() requires the classes
 807     // to be in linked state, so it must be call here before the next loop, which returns
 808     // all classes to unlinked state.
 809     Klass* k = get_buffered_addr(klasses()->at(i));
 810     if (k->is_instance_klass()) {
 811       InstanceKlass::cast(k)->constants()->remove_unshareable_info();
 812     }
 813   }
 814 
 815   for (int i = 0; i < klasses()->length(); i++) {
 816     const char* type;
 817     const char* unlinked = "";
 818     const char* hidden = "";
 819     const char* generated = "";
 820     Klass* k = get_buffered_addr(klasses()->at(i));
 821     k->remove_java_mirror();
 822     if (k->is_objArray_klass()) {
 823       // InstanceKlass and TypeArrayKlass will in turn call remove_unshareable_info
 824       // on their array classes.
 825       num_obj_array_klasses ++;
 826       type = "array";
 827     } else if (k->is_typeArray_klass()) {
 828       num_type_array_klasses ++;
 829       type = "array";
 830       k->remove_unshareable_info();
 831     } else {
 832       assert(k->is_instance_klass(), " must be");

 833       InstanceKlass* ik = InstanceKlass::cast(k);
 834       InstanceKlass* src_ik = get_source_addr(ik);
 835       int inited = ik->has_preinitialized_mirror();
 836       ADD_COUNT(num_instance_klasses);
 837       if (CDSConfig::is_dumping_dynamic_archive()) {
 838         // For static dump, class loader type are already set.
 839         ik->assign_class_loader_type();
 840       }
 841       if (ik->is_hidden()) {
 842         oop loader = k->class_loader();
 843         if (loader == nullptr) {
 844           type = "boot";
 845           ADD_COUNT(num_boot_klasses);
 846         } else if (loader == SystemDictionary::java_platform_loader()) {
 847           type = "plat";
 848           ADD_COUNT(num_platform_klasses);
 849         } else if (loader == SystemDictionary::java_system_loader()) {
 850           type = "app";
 851           ADD_COUNT(num_app_klasses);
 852         } else {
 853           type = "bad";
 854           assert(0, "shouldn't happen");
 855         }
 856       } else if (ik->is_shared_boot_class()) {
 857         type = "boot";
 858         ADD_COUNT(num_boot_klasses);
 859       } else if (ik->is_shared_platform_class()) {
 860         type = "plat";
 861         ADD_COUNT(num_platform_klasses);
 862       } else if (ik->is_shared_app_class()) {
 863         type = "app";
 864         ADD_COUNT(num_app_klasses);
 865       } else {
 866         assert(ik->is_shared_unregistered_class(), "must be");
 867         type = "unreg";
 868         ADD_COUNT(num_unregistered_klasses);
 869       }
 870 
 871       if (ClassPrelinker::is_vm_class(src_ik)) {
 872         ADD_COUNT(num_vm_klasses);
 873       }
 874 
 875       if (!ik->is_linked()) {
 876         ADD_COUNT(num_unlinked_klasses);
 877         unlinked = " ** unlinked";
 878         if (ik->is_shared_boot_class()) {
 879           boot_unlinked ++;
 880         } else if (ik->is_shared_platform_class()) {
 881           platform_unlinked ++;
 882         } else if (ik->is_shared_app_class()) {
 883           app_unlinked ++;
 884         } else {
 885           unreg_unlinked ++;
 886         }
 887       }
 888 
 889       if (ik->is_hidden()) {
 890         ADD_COUNT(num_hidden_klasses);
 891         hidden = " ** hidden";
 892       }
 893 
 894       if (ik->is_generated_shared_class()) {
 895         generated = " ** generated";
 896       }
 897       MetaspaceShared::rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread::current(), ik);
 898       ik->remove_unshareable_info();
 899     }
 900 
 901     if (log_is_enabled(Debug, cds, class)) {
 902       ResourceMark rm;
 903       log_debug(cds, class)("klasses[%5d] = " PTR_FORMAT " %-5s %s%s%s%s", i,
 904                             p2i(to_requested(k)), type, k->external_name(),
 905                             hidden, unlinked, generated);
 906     }
 907   }
 908 
 909   log_info(cds)("Number of classes %d", num_instance_klasses + num_obj_array_klasses + num_type_array_klasses);
 910   log_info(cds)("    instance classes   = %5d, inited = %5d", num_instance_klasses,     num_instance_klasses_i);
 911   log_info(cds)("      boot             = %5d, inited = %5d", num_boot_klasses,         num_boot_klasses_i);
 912   log_info(cds)("       vm              = %5d, inited = %5d", num_vm_klasses,           num_vm_klasses_i);
 913   log_info(cds)("      platform         = %5d, inited = %5d", num_platform_klasses,     num_platform_klasses_i);
 914   log_info(cds)("      app              = %5d, inited = %5d", num_app_klasses,          num_app_klasses_i);
 915   log_info(cds)("      unregistered     = %5d, inited = %5d", num_unregistered_klasses, num_unregistered_klasses_i);
 916   log_info(cds)("      (hidden)         = %5d, inited = %5d", num_hidden_klasses,       num_hidden_klasses_i);
 917   log_info(cds)("      (unlinked)       = %5d, inited = %5d, boot = %d, plat = %d, app = %d, unreg = %d",
 918                                                               num_unlinked_klasses,     num_unlinked_klasses_i,
 919                                                               boot_unlinked, platform_unlinked,
 920                                                               app_unlinked, unreg_unlinked);
 921   log_info(cds)("    obj array classes  = %5d", num_obj_array_klasses);
 922   log_info(cds)("    type array classes = %5d", num_type_array_klasses);
 923   log_info(cds)("               symbols = %5d", _symbols->length());
 924 
 925   DynamicArchive::make_array_klasses_shareable();
 926 }
 927 
 928 void ArchiveBuilder::serialize_dynamic_archivable_items(SerializeClosure* soc) {
 929   SymbolTable::serialize_shared_table_header(soc, false);
 930   SystemDictionaryShared::serialize_dictionary_headers(soc, false);
 931   DynamicArchive::serialize_array_klasses(soc);
 932   ClassPrelinker::serialize(soc, false);
 933   TrainingData::serialize_training_data(soc);
 934 }
 935 
 936 uintx ArchiveBuilder::buffer_to_offset(address p) const {
 937   address requested_p = to_requested(p);
 938   assert(requested_p >= _requested_static_archive_bottom, "must be");
 939   return requested_p - _requested_static_archive_bottom;
 940 }
 941 
 942 uintx ArchiveBuilder::any_to_offset(address p) const {
 943   if (is_in_mapped_static_archive(p)) {
 944     assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
 945     return p - _mapped_static_archive_bottom;
 946   }
 947   if (!is_in_buffer_space(p)) {
 948     // p must be a "source" address
 949     p = get_buffered_addr(p);
 950   }
 951   return buffer_to_offset(p);
 952 }
 953 
 954 void ArchiveBuilder::start_cc_region() {
 955   ro_region()->pack();
 956   start_dump_space(&_cc_region);
 957 }
 958 
 959 void ArchiveBuilder::end_cc_region() {
 960   _cc_region.pack();
 961 }
 962 
 963 #if INCLUDE_CDS_JAVA_HEAP
 964 narrowKlass ArchiveBuilder::get_requested_narrow_klass(Klass* k) {
 965   assert(CDSConfig::is_dumping_heap(), "sanity");
 966   k = get_buffered_klass(k);
 967   Klass* requested_k = to_requested(k);
 968   address narrow_klass_base = _requested_static_archive_bottom; // runtime encoding base == runtime mapping start
 969   const int narrow_klass_shift = ArchiveHeapWriter::precomputed_narrow_klass_shift;
 970   return CompressedKlassPointers::encode_not_null(requested_k, narrow_klass_base, narrow_klass_shift);
 971 }
 972 #endif // INCLUDE_CDS_JAVA_HEAP
 973 
 974 // RelocateBufferToRequested --- Relocate all the pointers in rw/ro,
 975 // so that the archive can be mapped to the "requested" location without runtime relocation.
 976 //
 977 // - See ArchiveBuilder header for the definition of "buffer", "mapped" and "requested"
 978 // - ArchivePtrMarker::ptrmap() marks all the pointers in the rw/ro regions
 979 // - Every pointer must have one of the following values:
 980 //   [a] nullptr:
 981 //       No relocation is needed. Remove this pointer from ptrmap so we don't need to
 982 //       consider it at runtime.

1032         } else {
1033           assert(_builder->is_in_mapped_static_archive(*p), "old pointer must point inside buffer space or mapped static archive");
1034           *p += _mapped_to_requested_static_archive_delta;
1035           assert(_builder->is_in_requested_static_archive(*p), "new pointer must point inside requested archive");
1036         }
1037       }
1038       _max_non_null_offset = offset;
1039     }
1040 
1041     return true; // keep iterating
1042   }
1043 
1044   void doit() {
1045     ArchivePtrMarker::ptrmap()->iterate(this);
1046     ArchivePtrMarker::compact(_max_non_null_offset);
1047   }
1048 };
1049 
1050 
1051 void ArchiveBuilder::relocate_to_requested() {
1052   if (!ro_region()->is_packed()) {
1053     ro_region()->pack();
1054   }
1055 
1056   size_t my_archive_size = buffer_top() - buffer_bottom();
1057 
1058   if (CDSConfig::is_dumping_static_archive()) {
1059     _requested_static_archive_top = _requested_static_archive_bottom + my_archive_size;
1060     RelocateBufferToRequested<true> patcher(this);
1061     patcher.doit();
1062   } else {
1063     assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
1064     _requested_dynamic_archive_top = _requested_dynamic_archive_bottom + my_archive_size;
1065     RelocateBufferToRequested<false> patcher(this);
1066     patcher.doit();
1067   }
1068 }
1069 
1070 // Write detailed info to a mapfile to analyze contents of the archive.
1071 // static dump:
1072 //   java -Xshare:dump -Xlog:cds+map=trace:file=cds.map:none:filesize=0
1073 // dynamic dump:
1074 //   java -cp MyApp.jar -XX:ArchiveClassesAtExit=MyApp.jsa \

1182     base = requested_base;
1183     top = requested_base + size;
1184     log_info(cds, map)("[%-18s " PTR_FORMAT " - " PTR_FORMAT " " SIZE_FORMAT_W(9) " bytes]",
1185                        name, p2i(base), p2i(top), size);
1186   }
1187 
1188 #if INCLUDE_CDS_JAVA_HEAP
1189   static void log_heap_region(ArchiveHeapInfo* heap_info) {
1190     MemRegion r = heap_info->buffer_region();
1191     address start = address(r.start());
1192     address end = address(r.end());
1193     log_region("heap", start, end, ArchiveHeapWriter::buffered_addr_to_requested_addr(start));
1194 
1195     LogStreamHandle(Info, cds, map) st;
1196 
1197     while (start < end) {
1198       size_t byte_size;
1199       oop source_oop = ArchiveHeapWriter::buffered_addr_to_source_obj(start);
1200       address requested_start = ArchiveHeapWriter::buffered_addr_to_requested_addr(start);
1201       st.print(PTR_FORMAT ": @@ Object ", p2i(requested_start));
1202       int permobj_segment = -1;
1203       int permobj_segment_length = -1;
1204 
1205       if (source_oop != nullptr) {
1206         // This is a regular oop that got archived.
1207         print_oop_with_requested_addr_cr(&st, source_oop, false);
1208         byte_size = source_oop->size() * BytesPerWord;
1209       } else if (start == ArchiveHeapWriter::buffered_heap_roots_addr()) {
1210         // HeapShared::roots() is copied specially, so it doesn't exist in
1211         // ArchiveHeapWriter::BufferOffsetToSourceObjectTable.
1212         // See ArchiveHeapWriter::copy_roots_to_buffer().
1213         st.print_cr("HeapShared::roots[%d]", HeapShared::pending_roots()->length());
1214         byte_size = ArchiveHeapWriter::heap_roots_word_size() * BytesPerWord;
1215       } else if ((byte_size = ArchiveHeapWriter::get_filler_size_at(start)) > 0) {
1216         // We have a filler oop, which also does not exist in BufferOffsetToSourceObjectTable.
1217         st.print_cr("filler " SIZE_FORMAT " bytes", byte_size);
1218       } else if ((permobj_segment = ArchiveHeapWriter::get_permobj_segment_at(start, &byte_size, &permobj_segment_length)) >= 0) {
1219         st.print_cr("permobj_%d[%d] %zu bytes", permobj_segment, permobj_segment_length, byte_size);
1220       } else {
1221         ShouldNotReachHere();
1222       }
1223 
1224       address oop_end = start + byte_size;
1225       log_as_hex(start, oop_end, requested_start, /*is_heap=*/true);
1226 
1227       if (source_oop != nullptr) {
1228         log_oop_details(heap_info, source_oop);
1229       } else if (start == ArchiveHeapWriter::buffered_heap_roots_addr()) {
1230         log_heap_roots();
1231       } else if (permobj_segment >= 0) {
1232         log_permobj_segment(permobj_segment, permobj_segment_length);
1233       }
1234       start = oop_end;
1235     }
1236   }
1237 
1238   // ArchivedFieldPrinter is used to print the fields of archived objects. We can't
1239   // use _source_obj->print_on(), because we want to print the oop fields
1240   // in _source_obj with their requested addresses using print_oop_with_requested_addr_cr().
1241   class ArchivedFieldPrinter : public FieldClosure {
1242     ArchiveHeapInfo* _heap_info;
1243     outputStream* _st;
1244     oop _source_obj;
1245   public:
1246     ArchivedFieldPrinter(ArchiveHeapInfo* heap_info, outputStream* st, oop src_obj) :
1247       _heap_info(heap_info), _st(st), _source_obj(src_obj) {}
1248 
1249     void do_field(fieldDescriptor* fd) {
1250       _st->print(" - ");
1251       BasicType ft = fd->field_type();
1252       switch (ft) {

1312           print_oop_with_requested_addr_cr(&st, source_obj_array->obj_at(i));
1313         }
1314       } else {
1315         st.print_cr(" - fields (" SIZE_FORMAT " words):", source_oop->size());
1316         ArchivedFieldPrinter print_field(heap_info, &st, source_oop);
1317         InstanceKlass::cast(source_klass)->print_nonstatic_fields(&print_field);
1318       }
1319     }
1320   }
1321 
1322   static void log_heap_roots() {
1323     LogStreamHandle(Trace, cds, map, oops) st;
1324     if (st.is_enabled()) {
1325       for (int i = 0; i < HeapShared::pending_roots()->length(); i++) {
1326         st.print("roots[%4d]: ", i);
1327         print_oop_with_requested_addr_cr(&st, HeapShared::pending_roots()->at(i));
1328       }
1329     }
1330   }
1331 
1332   static void log_permobj_segment(int permobj_segment, int permobj_segment_length) {
1333     LogStreamHandle(Trace, cds, map, oops) st;
1334     if (st.is_enabled()) {
1335       for (int i = 0; i < permobj_segment_length; i++) {
1336         st.print("permobj_%d[%4d]: ", permobj_segment, i);
1337         print_oop_with_requested_addr_cr(&st, ArchiveHeapWriter::get_permobj_source_addr(permobj_segment, i));
1338       }
1339     }
1340   }
1341 
1342 
1343   // The output looks like this. The first number is the requested address. The second number is
1344   // the narrowOop version of the requested address.
1345   //     0x00000007ffc7e840 (0xfff8fd08) java.lang.Class
1346   //     0x00000007ffc000f8 (0xfff8001f) [B length: 11
1347   static void print_oop_with_requested_addr_cr(outputStream* st, oop source_oop, bool print_addr = true) {
1348     if (source_oop == nullptr) {
1349       st->print_cr("null");
1350     } else {
1351       ResourceMark rm;
1352       oop requested_obj = ArchiveHeapWriter::source_obj_to_requested_obj(source_oop);
1353       if (print_addr) {
1354         st->print(PTR_FORMAT " ", p2i(requested_obj));
1355       }
1356       if (UseCompressedOops) {
1357         st->print("(0x%08x) ", CompressedOops::narrow_oop_value(requested_obj));
1358       }
1359       if (source_oop->is_array()) {
1360         int array_len = arrayOop(source_oop)->length();
1361         st->print_cr("%s length: %d", source_oop->klass()->external_name(), array_len);
1362       } else {
1363         st->print("%s", source_oop->klass()->external_name());
1364         if (java_lang_invoke_MethodType::is_instance(source_oop)) {
1365           st->print(" ");
1366           java_lang_invoke_MethodType::print_signature(source_oop, st);
1367         }
1368         st->cr();
1369       }
1370     }
1371   }
1372 #endif // INCLUDE_CDS_JAVA_HEAP
1373 
1374   // Log all the data [base...top). Pretend that the base address
1375   // will be mapped to requested_base at run-time.
1376   static void log_as_hex(address base, address top, address requested_base, bool is_heap = false) {
1377     assert(top >= base, "must be");
1378 
1379     LogStreamHandle(Trace, cds, map) lsh;
1380     if (lsh.is_enabled()) {
1381       int unitsize = sizeof(address);
1382       if (is_heap && UseCompressedOops) {
1383         // This makes the compressed oop pointers easier to read, but
1384         // longs and doubles will be split into two words.
1385         unitsize = sizeof(narrowOop);
1386       }
1387       os::print_hex_dump(&lsh, base, top, unitsize, 32, requested_base);
1388     }

1421     if (heap_info->is_used()) {
1422       log_heap_region(heap_info);
1423     }
1424 #endif
1425 
1426     log_info(cds, map)("[End of CDS archive map]");
1427   }
1428 }; // end ArchiveBuilder::CDSMapLogger
1429 
1430 void ArchiveBuilder::print_stats() {
1431   _alloc_stats.print_stats(int(_ro_region.used()), int(_rw_region.used()));
1432 }
1433 
1434 void ArchiveBuilder::write_archive(FileMapInfo* mapinfo, ArchiveHeapInfo* heap_info) {
1435   // Make sure NUM_CDS_REGIONS (exported in cds.h) agrees with
1436   // MetaspaceShared::n_regions (internal to hotspot).
1437   assert(NUM_CDS_REGIONS == MetaspaceShared::n_regions, "sanity");
1438 
1439   write_region(mapinfo, MetaspaceShared::rw, &_rw_region, /*read_only=*/false,/*allow_exec=*/false);
1440   write_region(mapinfo, MetaspaceShared::ro, &_ro_region, /*read_only=*/true, /*allow_exec=*/false);
1441   write_region(mapinfo, MetaspaceShared::cc, &_cc_region, /*read_only=*/false,/*allow_exec=*/true);
1442 
1443   size_t bitmap_size_in_bytes;
1444   char* bitmap = mapinfo->write_bitmap_region(ArchivePtrMarker::ptrmap(), heap_info,
1445                                               bitmap_size_in_bytes);
1446 
1447   if (heap_info->is_used()) {
1448     _total_heap_region_size = mapinfo->write_heap_region(heap_info);
1449   }
1450 
1451   print_region_stats(mapinfo, heap_info);
1452 
1453   mapinfo->set_requested_base((char*)MetaspaceShared::requested_base_address());
1454   mapinfo->set_header_crc(mapinfo->compute_header_crc());
1455   // After this point, we should not write any data into mapinfo->header() since this
1456   // would corrupt its checksum we have calculated before.
1457   mapinfo->write_header();
1458   mapinfo->close();
1459 
1460   if (log_is_enabled(Info, cds)) {
1461     print_stats();

1470 }
1471 
1472 void ArchiveBuilder::write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region, bool read_only,  bool allow_exec) {
1473   mapinfo->write_region(region_idx, dump_region->base(), dump_region->used(), read_only, allow_exec);
1474 }
1475 
1476 void ArchiveBuilder::print_region_stats(FileMapInfo *mapinfo, ArchiveHeapInfo* heap_info) {
1477   // Print statistics of all the regions
1478   const size_t bitmap_used = mapinfo->region_at(MetaspaceShared::bm)->used();
1479   const size_t bitmap_reserved = mapinfo->region_at(MetaspaceShared::bm)->used_aligned();
1480   const size_t total_reserved = _ro_region.reserved()  + _rw_region.reserved() +
1481                                 bitmap_reserved +
1482                                 _total_heap_region_size;
1483   const size_t total_bytes = _ro_region.used()  + _rw_region.used() +
1484                              bitmap_used +
1485                              _total_heap_region_size;
1486   const double total_u_perc = percent_of(total_bytes, total_reserved);
1487 
1488   _rw_region.print(total_reserved);
1489   _ro_region.print(total_reserved);
1490   _cc_region.print(total_reserved);
1491 
1492   print_bitmap_region_stats(bitmap_used, total_reserved);
1493 
1494   if (heap_info->is_used()) {
1495     print_heap_region_stats(heap_info, total_reserved);
1496   }
1497 
1498   log_debug(cds)("total   : " SIZE_FORMAT_W(9) " [100.0%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used]",
1499                  total_bytes, total_reserved, total_u_perc);
1500 }
1501 
1502 void ArchiveBuilder::print_bitmap_region_stats(size_t size, size_t total_size) {
1503   log_debug(cds)("bm space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [100.0%% used]",
1504                  size, size/double(total_size)*100.0, size);
1505 }
1506 
1507 void ArchiveBuilder::print_heap_region_stats(ArchiveHeapInfo *info, size_t total_size) {
1508   char* start = info->buffer_start();
1509   size_t size = info->buffer_byte_size();
1510   char* top = start + size;
1511   log_debug(cds)("hp space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [100.0%% used] at " INTPTR_FORMAT,
1512                      size, size/double(total_size)*100.0, size, p2i(start));
1513 }
1514 
1515 void ArchiveBuilder::report_out_of_space(const char* name, size_t needed_bytes) {
1516   // This is highly unlikely to happen on 64-bits because we have reserved a 4GB space.
1517   // On 32-bit we reserve only 256MB so you could run out of space with 100,000 classes
1518   // or so.
1519   _rw_region.print_out_of_space_msg(name, needed_bytes);
1520   _ro_region.print_out_of_space_msg(name, needed_bytes);
1521 
1522   log_error(cds)("Unable to allocate from '%s' region: Please reduce the number of shared classes.", name);
1523   MetaspaceShared::unrecoverable_writing_error();
1524 }







< prev index next >