1 /*
   2  * Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "cds/archiveBuilder.hpp"
  27 #include "cds/archiveUtils.hpp"
  28 #include "cds/cppVtables.hpp"
  29 #include "cds/dumpAllocStats.hpp"
  30 #include "cds/metaspaceShared.hpp"
  31 #include "classfile/classLoaderDataShared.hpp"
  32 #include "classfile/symbolTable.hpp"
  33 #include "classfile/systemDictionaryShared.hpp"
  34 #include "classfile/vmClasses.hpp"
  35 #include "interpreter/abstractInterpreter.hpp"
  36 #include "logging/log.hpp"
  37 #include "logging/logStream.hpp"
  38 #include "memory/allStatic.hpp"
  39 #include "memory/memRegion.hpp"
  40 #include "memory/resourceArea.hpp"
  41 #include "oops/instanceKlass.hpp"
  42 #include "oops/objArrayKlass.hpp"
  43 #include "oops/oopHandle.inline.hpp"
  44 #include "runtime/arguments.hpp"
  45 #include "runtime/globals_extension.hpp"
  46 #include "runtime/sharedRuntime.hpp"
  47 #include "runtime/thread.hpp"
  48 #include "utilities/align.hpp"
  49 #include "utilities/bitMap.inline.hpp"
  50 #include "utilities/formatBuffer.hpp"
  51 #include "utilities/hashtable.inline.hpp"
  52 
  53 ArchiveBuilder* ArchiveBuilder::_current = NULL;
  54 
  55 ArchiveBuilder::OtherROAllocMark::~OtherROAllocMark() {
  56   char* newtop = ArchiveBuilder::current()->_ro_region.top();
  57   ArchiveBuilder::alloc_stats()->record_other_type(int(newtop - _oldtop), true);
  58 }
  59 
  60 ArchiveBuilder::SourceObjList::SourceObjList() : _ptrmap(16 * K) {
  61   _total_bytes = 0;
  62   _objs = new (ResourceObj::C_HEAP, mtClassShared) GrowableArray<SourceObjInfo*>(128 * K, mtClassShared);
  63 }
  64 
  65 ArchiveBuilder::SourceObjList::~SourceObjList() {
  66   delete _objs;
  67 }
  68 
  69 void ArchiveBuilder::SourceObjList::append(MetaspaceClosure::Ref* enclosing_ref, SourceObjInfo* src_info) {
  70   // Save this source object for copying
  71   _objs->append(src_info);
  72 
  73   // Prepare for marking the pointers in this source object
  74   assert(is_aligned(_total_bytes, sizeof(address)), "must be");
  75   src_info->set_ptrmap_start(_total_bytes / sizeof(address));
  76   _total_bytes = align_up(_total_bytes + (uintx)src_info->size_in_bytes(), sizeof(address));
  77   src_info->set_ptrmap_end(_total_bytes / sizeof(address));
  78 
  79   BitMap::idx_t bitmap_size_needed = BitMap::idx_t(src_info->ptrmap_end());
  80   if (_ptrmap.size() <= bitmap_size_needed) {
  81     _ptrmap.resize((bitmap_size_needed + 1) * 2);
  82   }
  83 }
  84 
  85 void ArchiveBuilder::SourceObjList::remember_embedded_pointer(SourceObjInfo* src_info, MetaspaceClosure::Ref* ref) {
  86   // src_obj contains a pointer. Remember the location of this pointer in _ptrmap,
  87   // so that we can copy/relocate it later. E.g., if we have
  88   //    class Foo { intx scala; Bar* ptr; }
  89   //    Foo *f = 0x100;
  90   // To mark the f->ptr pointer on 64-bit platform, this function is called with
  91   //    src_info()->obj() == 0x100
  92   //    ref->addr() == 0x108
  93   address src_obj = src_info->obj();
  94   address* field_addr = ref->addr();
  95   assert(src_info->ptrmap_start() < _total_bytes, "sanity");
  96   assert(src_info->ptrmap_end() <= _total_bytes, "sanity");
  97   assert(*field_addr != NULL, "should have checked");
  98 
  99   intx field_offset_in_bytes = ((address)field_addr) - src_obj;
 100   DEBUG_ONLY(int src_obj_size = src_info->size_in_bytes();)
 101   assert(field_offset_in_bytes >= 0, "must be");
 102   assert(field_offset_in_bytes + intx(sizeof(intptr_t)) <= intx(src_obj_size), "must be");
 103   assert(is_aligned(field_offset_in_bytes, sizeof(address)), "must be");
 104 
 105   BitMap::idx_t idx = BitMap::idx_t(src_info->ptrmap_start() + (uintx)(field_offset_in_bytes / sizeof(address)));
 106   _ptrmap.set_bit(BitMap::idx_t(idx));
 107 }
 108 
 109 class RelocateEmbeddedPointers : public BitMapClosure {
 110   ArchiveBuilder* _builder;
 111   address _dumped_obj;
 112   BitMap::idx_t _start_idx;
 113 public:
 114   RelocateEmbeddedPointers(ArchiveBuilder* builder, address dumped_obj, BitMap::idx_t start_idx) :
 115     _builder(builder), _dumped_obj(dumped_obj), _start_idx(start_idx) {}
 116 
 117   bool do_bit(BitMap::idx_t bit_offset) {
 118     size_t field_offset = size_t(bit_offset - _start_idx) * sizeof(address);
 119     address* ptr_loc = (address*)(_dumped_obj + field_offset);
 120 
 121     address old_p = *ptr_loc;
 122     address new_p = _builder->get_dumped_addr(old_p);
 123 
 124     log_trace(cds)("Ref: [" PTR_FORMAT "] -> " PTR_FORMAT " => " PTR_FORMAT,
 125                    p2i(ptr_loc), p2i(old_p), p2i(new_p));
 126 
 127     ArchivePtrMarker::set_and_mark_pointer(ptr_loc, new_p);
 128     return true; // keep iterating the bitmap
 129   }
 130 };
 131 
 132 void ArchiveBuilder::SourceObjList::relocate(int i, ArchiveBuilder* builder) {
 133   SourceObjInfo* src_info = objs()->at(i);
 134   assert(src_info->should_copy(), "must be");
 135   BitMap::idx_t start = BitMap::idx_t(src_info->ptrmap_start()); // inclusive
 136   BitMap::idx_t end = BitMap::idx_t(src_info->ptrmap_end());     // exclusive
 137 
 138   RelocateEmbeddedPointers relocator(builder, src_info->dumped_addr(), start);
 139   _ptrmap.iterate(&relocator, start, end);
 140 }
 141 
 142 ArchiveBuilder::ArchiveBuilder() :
 143   _current_dump_space(NULL),
 144   _buffer_bottom(NULL),
 145   _last_verified_top(NULL),
 146   _num_dump_regions_used(0),
 147   _other_region_used_bytes(0),
 148   _requested_static_archive_bottom(NULL),
 149   _requested_static_archive_top(NULL),
 150   _requested_dynamic_archive_bottom(NULL),
 151   _requested_dynamic_archive_top(NULL),
 152   _mapped_static_archive_bottom(NULL),
 153   _mapped_static_archive_top(NULL),
 154   _buffer_to_requested_delta(0),
 155   _rw_region("rw", MAX_SHARED_DELTA),
 156   _ro_region("ro", MAX_SHARED_DELTA),
 157   _rw_src_objs(),
 158   _ro_src_objs(),
 159   _src_obj_table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE),
 160   _total_closed_heap_region_size(0),
 161   _total_open_heap_region_size(0),
 162   _estimated_metaspaceobj_bytes(0),
 163   _estimated_hashtable_bytes(0)
 164 {
 165   _klasses = new (ResourceObj::C_HEAP, mtClassShared) GrowableArray<Klass*>(4 * K, mtClassShared);
 166   _symbols = new (ResourceObj::C_HEAP, mtClassShared) GrowableArray<Symbol*>(256 * K, mtClassShared);
 167   _special_refs = new (ResourceObj::C_HEAP, mtClassShared) GrowableArray<SpecialRefInfo>(24 * K, mtClassShared);
 168 
 169   assert(_current == NULL, "must be");
 170   _current = this;
 171 }
 172 
 173 ArchiveBuilder::~ArchiveBuilder() {
 174   assert(_current == this, "must be");
 175   _current = NULL;
 176 
 177   clean_up_src_obj_table();
 178 
 179   for (int i = 0; i < _symbols->length(); i++) {
 180     _symbols->at(i)->decrement_refcount();
 181   }
 182 
 183   delete _klasses;
 184   delete _symbols;
 185   delete _special_refs;
 186   if (_shared_rs.is_reserved()) {
 187     _shared_rs.release();
 188   }
 189 }
 190 
 191 bool ArchiveBuilder::is_dumping_full_module_graph() {
 192   return DumpSharedSpaces && MetaspaceShared::use_full_module_graph();
 193 }
 194 
 195 class GatherKlassesAndSymbols : public UniqueMetaspaceClosure {
 196   ArchiveBuilder* _builder;
 197 
 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() == NULL) {
 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     }
 219     // See RunTimeClassInfo::get_for()
 220     _estimated_metaspaceobj_bytes += align_up(BytesPerWord, SharedSpaceObjectAlignment);
 221   } else if (ref->msotype() == MetaspaceObj::SymbolType) {
 222     // Make sure the symbol won't be GC'ed while we are dumping the archive.
 223     Symbol* sym = (Symbol*)ref->obj();
 224     sym->increment_refcount();
 225     _symbols->append(sym);
 226   }
 227 
 228   int bytes = ref->size() * BytesPerWord;
 229   _estimated_metaspaceobj_bytes += align_up(bytes, SharedSpaceObjectAlignment);
 230 
 231   return true; // recurse
 232 }
 233 
 234 void ArchiveBuilder::gather_klasses_and_symbols() {
 235   ResourceMark rm;
 236   log_info(cds)("Gathering classes and symbols ... ");
 237   GatherKlassesAndSymbols doit(this);
 238   iterate_roots(&doit, /*is_relocating_pointers=*/false);
 239 #if INCLUDE_CDS_JAVA_HEAP
 240   if (is_dumping_full_module_graph()) {
 241     ClassLoaderDataShared::iterate_symbols(&doit);
 242   }
 243 #endif
 244   doit.finish();
 245 
 246   if (DumpSharedSpaces) {
 247     // To ensure deterministic contents in the static archive, we need to ensure that
 248     // we iterate the MetaspaceObjs in a deterministic order. It doesn't matter where
 249     // the MetaspaceObjs are located originally, as they are copied sequentially into
 250     // the archive during the iteration.
 251     //
 252     // The only issue here is that the symbol table and the system directories may be
 253     // randomly ordered, so we copy the symbols and klasses into two arrays and sort
 254     // them deterministically.
 255     //
 256     // During -Xshare:dump, the order of Symbol creation is strictly determined by
 257     // the SharedClassListFile (class loading is done in a single thread and the JIT
 258     // is disabled). Also, Symbols are allocated in monotonically increasing addresses
 259     // (see Symbol::operator new(size_t, int)). So if we iterate the Symbols by
 260     // ascending address order, we ensure that all Symbols are copied into deterministic
 261     // locations in the archive.
 262     //
 263     // TODO: in the future, if we want to produce deterministic contents in the
 264     // dynamic archive, we might need to sort the symbols alphabetically (also see
 265     // DynamicArchiveBuilder::sort_methods()).
 266     sort_symbols_and_fix_hash();
 267     sort_klasses();
 268 
 269     // TODO -- we need a proper estimate for the archived modules, etc,
 270     // but this should be enough for now
 271     _estimated_metaspaceobj_bytes += 200 * 1024 * 1024;
 272   }
 273 }
 274 
 275 int ArchiveBuilder::compare_symbols_by_address(Symbol** a, Symbol** b) {
 276   if (a[0] < b[0]) {
 277     return -1;
 278   } else {
 279     assert(a[0] > b[0], "Duplicated symbol %s unexpected", (*a)->as_C_string());
 280     return 1;
 281   }
 282 }
 283 
 284 void ArchiveBuilder::sort_symbols_and_fix_hash() {
 285   log_info(cds)("Sorting symbols and fixing identity hash ... ");
 286   os::init_random(0x12345678);
 287   _symbols->sort(compare_symbols_by_address);
 288   for (int i = 0; i < _symbols->length(); i++) {
 289     assert(_symbols->at(i)->is_permanent(), "archived symbols must be permanent");
 290     _symbols->at(i)->update_identity_hash();
 291   }
 292 }
 293 
 294 int ArchiveBuilder::compare_klass_by_name(Klass** a, Klass** b) {
 295   return a[0]->name()->fast_compare(b[0]->name());
 296 }
 297 
 298 void ArchiveBuilder::sort_klasses() {
 299   log_info(cds)("Sorting classes ... ");
 300   _klasses->sort(compare_klass_by_name);
 301 }
 302 
 303 size_t ArchiveBuilder::estimate_archive_size() {
 304   // size of the symbol table and two dictionaries, plus the RunTimeClassInfo's
 305   size_t symbol_table_est = SymbolTable::estimate_size_for_archive();
 306   size_t dictionary_est = SystemDictionaryShared::estimate_size_for_archive();
 307   _estimated_hashtable_bytes = symbol_table_est + dictionary_est;
 308 
 309   size_t total = 0;
 310 
 311   total += _estimated_metaspaceobj_bytes;
 312   total += _estimated_hashtable_bytes;
 313 
 314   // allow fragmentation at the end of each dump region
 315   total += _total_dump_regions * MetaspaceShared::core_region_alignment();
 316 
 317   log_info(cds)("_estimated_hashtable_bytes = " SIZE_FORMAT " + " SIZE_FORMAT " = " SIZE_FORMAT,
 318                 symbol_table_est, dictionary_est, _estimated_hashtable_bytes);
 319   log_info(cds)("_estimated_metaspaceobj_bytes = " SIZE_FORMAT, _estimated_metaspaceobj_bytes);
 320   log_info(cds)("total estimate bytes = " SIZE_FORMAT, total);
 321 
 322   return align_up(total, MetaspaceShared::core_region_alignment());
 323 }
 324 
 325 address ArchiveBuilder::reserve_buffer() {
 326   size_t buffer_size = estimate_archive_size();
 327   ReservedSpace rs(buffer_size, MetaspaceShared::core_region_alignment(), os::vm_page_size());
 328   if (!rs.is_reserved()) {
 329     log_error(cds)("Failed to reserve " SIZE_FORMAT " bytes of output buffer.", buffer_size);
 330     vm_direct_exit(0);
 331   }
 332 
 333   // buffer_bottom is the lowest address of the 2 core regions (rw, ro) when
 334   // we are copying the class metadata into the buffer.
 335   address buffer_bottom = (address)rs.base();
 336   log_info(cds)("Reserved output buffer space at " PTR_FORMAT " [" SIZE_FORMAT " bytes]",
 337                 p2i(buffer_bottom), buffer_size);
 338   _shared_rs = rs;
 339 
 340   _buffer_bottom = buffer_bottom;
 341   _last_verified_top = buffer_bottom;
 342   _current_dump_space = &_rw_region;
 343   _num_dump_regions_used = 1;
 344   _other_region_used_bytes = 0;
 345   _current_dump_space->init(&_shared_rs, &_shared_vs);
 346 
 347   ArchivePtrMarker::initialize(&_ptrmap, &_shared_vs);
 348 
 349   // The bottom of the static archive should be mapped at this address by default.
 350   _requested_static_archive_bottom = (address)MetaspaceShared::requested_base_address();
 351 
 352   // The bottom of the archive (that I am writing now) should be mapped at this address by default.
 353   address my_archive_requested_bottom;
 354 
 355   if (DumpSharedSpaces) {
 356     my_archive_requested_bottom = _requested_static_archive_bottom;
 357   } else {
 358     _mapped_static_archive_bottom = (address)MetaspaceObj::shared_metaspace_base();
 359     _mapped_static_archive_top  = (address)MetaspaceObj::shared_metaspace_top();
 360     assert(_mapped_static_archive_top >= _mapped_static_archive_bottom, "must be");
 361     size_t static_archive_size = _mapped_static_archive_top - _mapped_static_archive_bottom;
 362 
 363     // At run time, we will mmap the dynamic archive at my_archive_requested_bottom
 364     _requested_static_archive_top = _requested_static_archive_bottom + static_archive_size;
 365     my_archive_requested_bottom = align_up(_requested_static_archive_top, MetaspaceShared::core_region_alignment());
 366 
 367     _requested_dynamic_archive_bottom = my_archive_requested_bottom;
 368   }
 369 
 370   _buffer_to_requested_delta = my_archive_requested_bottom - _buffer_bottom;
 371 
 372   address my_archive_requested_top = my_archive_requested_bottom + buffer_size;
 373   if (my_archive_requested_bottom <  _requested_static_archive_bottom ||
 374       my_archive_requested_top    <= _requested_static_archive_bottom) {
 375     // Size overflow.
 376     log_error(cds)("my_archive_requested_bottom = " INTPTR_FORMAT, p2i(my_archive_requested_bottom));
 377     log_error(cds)("my_archive_requested_top    = " INTPTR_FORMAT, p2i(my_archive_requested_top));
 378     log_error(cds)("SharedBaseAddress (" INTPTR_FORMAT ") is too high. "
 379                    "Please rerun java -Xshare:dump with a lower value", p2i(_requested_static_archive_bottom));
 380     vm_direct_exit(0);
 381   }
 382 
 383   if (DumpSharedSpaces) {
 384     // We don't want any valid object to be at the very bottom of the archive.
 385     // See ArchivePtrMarker::mark_pointer().
 386     rw_region()->allocate(16);
 387   }
 388 
 389   return buffer_bottom;
 390 }
 391 
 392 void ArchiveBuilder::iterate_sorted_roots(MetaspaceClosure* it, bool is_relocating_pointers) {
 393   int i;
 394 
 395   if (!is_relocating_pointers) {
 396     // Don't relocate _symbol, so we can safely call decrement_refcount on the
 397     // original symbols.
 398     int num_symbols = _symbols->length();
 399     for (i = 0; i < num_symbols; i++) {
 400       it->push(_symbols->adr_at(i));
 401     }
 402   }
 403 
 404   int num_klasses = _klasses->length();
 405   for (i = 0; i < num_klasses; i++) {
 406     it->push(_klasses->adr_at(i));
 407   }
 408 
 409   iterate_roots(it, is_relocating_pointers);
 410 }
 411 
 412 class GatherSortedSourceObjs : public MetaspaceClosure {
 413   ArchiveBuilder* _builder;
 414 
 415 public:
 416   GatherSortedSourceObjs(ArchiveBuilder* builder) : _builder(builder) {}
 417 
 418   virtual bool do_ref(Ref* ref, bool read_only) {
 419     return _builder->gather_one_source_obj(enclosing_ref(), ref, read_only);
 420   }
 421 
 422   virtual void push_special(SpecialRef type, Ref* ref, intptr_t* p) {
 423     address src_obj = ref->obj();
 424     size_t field_offset = pointer_delta(p, src_obj,  sizeof(u1));
 425     _builder->add_special_ref(type, src_obj, field_offset, ref->size() * BytesPerWord);
 426   };
 427 
 428   virtual void do_pending_ref(Ref* ref) {
 429     if (ref->obj() != NULL) {
 430       _builder->remember_embedded_pointer_in_copied_obj(enclosing_ref(), ref);
 431     }
 432   }
 433 };
 434 
 435 bool ArchiveBuilder::gather_one_source_obj(MetaspaceClosure::Ref* enclosing_ref,
 436                                            MetaspaceClosure::Ref* ref, bool read_only) {
 437   address src_obj = ref->obj();
 438   if (src_obj == NULL) {
 439     return false;
 440   }
 441   ref->set_keep_after_pushing();
 442   remember_embedded_pointer_in_copied_obj(enclosing_ref, ref);
 443 
 444   FollowMode follow_mode = get_follow_mode(ref);
 445   SourceObjInfo src_info(ref, read_only, follow_mode);
 446   bool created;
 447   SourceObjInfo* p = _src_obj_table.put_if_absent(src_obj, src_info, &created);
 448   if (created) {
 449     if (_src_obj_table.maybe_grow()) {
 450       log_info(cds, hashtables)("Expanded _src_obj_table table to %d", _src_obj_table.table_size());
 451     }
 452   }
 453 
 454   assert(p->read_only() == src_info.read_only(), "must be");
 455 
 456   if (created && src_info.should_copy()) {
 457     ref->set_user_data((void*)p);
 458     if (read_only) {
 459       _ro_src_objs.append(enclosing_ref, p);
 460     } else {
 461       _rw_src_objs.append(enclosing_ref, p);
 462     }
 463     return true; // Need to recurse into this ref only if we are copying it
 464   } else {
 465     return false;
 466   }
 467 }
 468 
 469 void ArchiveBuilder::remember_embedded_pointer_in_copied_obj(MetaspaceClosure::Ref* enclosing_ref,
 470                                                              MetaspaceClosure::Ref* ref) {
 471   assert(ref->obj() != NULL, "should have checked");
 472 
 473   if (enclosing_ref != NULL) {
 474     SourceObjInfo* src_info = (SourceObjInfo*)enclosing_ref->user_data();
 475     if (src_info == NULL) {
 476       // source objects of point_to_it/set_to_null types are not copied
 477       // so we don't need to remember their pointers.
 478     } else {
 479       if (src_info->read_only()) {
 480         _ro_src_objs.remember_embedded_pointer(src_info, ref);
 481       } else {
 482         _rw_src_objs.remember_embedded_pointer(src_info, ref);
 483       }
 484     }
 485   }
 486 }
 487 
 488 void ArchiveBuilder::gather_source_objs() {
 489   ResourceMark rm;
 490   log_info(cds)("Gathering all archivable objects ... ");
 491   gather_klasses_and_symbols();
 492   GatherSortedSourceObjs doit(this);
 493   iterate_sorted_roots(&doit, /*is_relocating_pointers=*/false);
 494   doit.finish();
 495 }
 496 
 497 bool ArchiveBuilder::is_excluded(Klass* klass) {
 498   if (klass->is_instance_klass()) {
 499     InstanceKlass* ik = InstanceKlass::cast(klass);
 500     return SystemDictionaryShared::is_excluded_class(ik);
 501   } else if (klass->is_objArray_klass()) {
 502     if (DynamicDumpSharedSpaces) {
 503       // Don't support archiving of array klasses for now (WHY???)
 504       return true;
 505     }
 506     Klass* bottom = ObjArrayKlass::cast(klass)->bottom_klass();
 507     if (bottom->is_instance_klass()) {
 508       return SystemDictionaryShared::is_excluded_class(InstanceKlass::cast(bottom));
 509     }
 510   }
 511 
 512   return false;
 513 }
 514 
 515 ArchiveBuilder::FollowMode ArchiveBuilder::get_follow_mode(MetaspaceClosure::Ref *ref) {
 516   address obj = ref->obj();
 517   if (MetaspaceShared::is_in_shared_metaspace(obj)) {
 518     // Don't dump existing shared metadata again.
 519     return point_to_it;
 520   } else if (ref->msotype() == MetaspaceObj::MethodDataType) {
 521     return set_to_null;
 522   } else {
 523     if (ref->msotype() == MetaspaceObj::ClassType) {
 524       Klass* klass = (Klass*)ref->obj();
 525       assert(klass->is_klass(), "must be");
 526       if (is_excluded(klass)) {
 527         ResourceMark rm;
 528         log_debug(cds, dynamic)("Skipping class (excluded): %s", klass->external_name());
 529         return set_to_null;
 530       }
 531     }
 532 
 533     return make_a_copy;
 534   }
 535 }
 536 
 537 void ArchiveBuilder::start_dump_space(DumpRegion* next) {
 538   address bottom = _last_verified_top;
 539   address top = (address)(current_dump_space()->top());
 540   _other_region_used_bytes += size_t(top - bottom);
 541 
 542   current_dump_space()->pack(next);
 543   _current_dump_space = next;
 544   _num_dump_regions_used ++;
 545 
 546   _last_verified_top = (address)(current_dump_space()->top());
 547 }
 548 
 549 void ArchiveBuilder::verify_estimate_size(size_t estimate, const char* which) {
 550   address bottom = _last_verified_top;
 551   address top = (address)(current_dump_space()->top());
 552   size_t used = size_t(top - bottom) + _other_region_used_bytes;
 553   int diff = int(estimate) - int(used);
 554 
 555   log_info(cds)("%s estimate = " SIZE_FORMAT " used = " SIZE_FORMAT "; diff = %d bytes", which, estimate, used, diff);
 556   assert(diff >= 0, "Estimate is too small");
 557 
 558   _last_verified_top = top;
 559   _other_region_used_bytes = 0;
 560 }
 561 
 562 void ArchiveBuilder::dump_rw_metadata() {
 563   ResourceMark rm;
 564   log_info(cds)("Allocating RW objects ... ");
 565   make_shallow_copies(&_rw_region, &_rw_src_objs);
 566 
 567 #if INCLUDE_CDS_JAVA_HEAP
 568   if (is_dumping_full_module_graph()) {
 569     // Archive the ModuleEntry's and PackageEntry's of the 3 built-in loaders
 570     char* start = rw_region()->top();
 571     ClassLoaderDataShared::allocate_archived_tables();
 572     alloc_stats()->record_modules(rw_region()->top() - start, /*read_only*/false);
 573   }
 574 #endif
 575 }
 576 
 577 void ArchiveBuilder::dump_ro_metadata() {
 578   ResourceMark rm;
 579   log_info(cds)("Allocating RO objects ... ");
 580 
 581   start_dump_space(&_ro_region);
 582   make_shallow_copies(&_ro_region, &_ro_src_objs);
 583 
 584 #if INCLUDE_CDS_JAVA_HEAP
 585   if (is_dumping_full_module_graph()) {
 586     char* start = ro_region()->top();
 587     ClassLoaderDataShared::init_archived_tables();
 588     alloc_stats()->record_modules(ro_region()->top() - start, /*read_only*/true);
 589   }
 590 #endif
 591 }
 592 
 593 void ArchiveBuilder::make_shallow_copies(DumpRegion *dump_region,
 594                                          const ArchiveBuilder::SourceObjList* src_objs) {
 595   for (int i = 0; i < src_objs->objs()->length(); i++) {
 596     make_shallow_copy(dump_region, src_objs->objs()->at(i));
 597   }
 598   log_info(cds)("done (%d objects)", src_objs->objs()->length());
 599 }
 600 
 601 void ArchiveBuilder::make_shallow_copy(DumpRegion *dump_region, SourceObjInfo* src_info) {
 602   MetaspaceClosure::Ref* ref = src_info->ref();
 603   address src = ref->obj();
 604   int bytes = src_info->size_in_bytes();
 605   char* dest;
 606   char* oldtop;
 607   char* newtop;
 608 
 609   oldtop = dump_region->top();
 610   if (ref->msotype() == MetaspaceObj::ClassType) {
 611     // Save a pointer immediate in front of an InstanceKlass, so
 612     // we can do a quick lookup from InstanceKlass* -> RunTimeClassInfo*
 613     // without building another hashtable. See RunTimeClassInfo::get_for()
 614     // in systemDictionaryShared.cpp.
 615     Klass* klass = (Klass*)src;
 616     if (klass->is_instance_klass()) {
 617       SystemDictionaryShared::validate_before_archiving(InstanceKlass::cast(klass));
 618       dump_region->allocate(sizeof(address));
 619     }
 620   }
 621   dest = dump_region->allocate(bytes);
 622   newtop = dump_region->top();
 623 
 624   memcpy(dest, src, bytes);
 625 
 626   intptr_t* archived_vtable = CppVtables::get_archived_vtable(ref->msotype(), (address)dest);
 627   if (archived_vtable != NULL) {
 628     *(address*)dest = (address)archived_vtable;
 629     ArchivePtrMarker::mark_pointer((address*)dest);
 630   }
 631 
 632   log_trace(cds)("Copy: " PTR_FORMAT " ==> " PTR_FORMAT " %d", p2i(src), p2i(dest), bytes);
 633   src_info->set_dumped_addr((address)dest);
 634 
 635   _alloc_stats.record(ref->msotype(), int(newtop - oldtop), src_info->read_only());
 636 }
 637 
 638 address ArchiveBuilder::get_dumped_addr(address src_obj) const {
 639   SourceObjInfo* p = _src_obj_table.get(src_obj);
 640   assert(p != NULL, "must be");
 641 
 642   return p->dumped_addr();
 643 }
 644 
 645 void ArchiveBuilder::relocate_embedded_pointers(ArchiveBuilder::SourceObjList* src_objs) {
 646   for (int i = 0; i < src_objs->objs()->length(); i++) {
 647     src_objs->relocate(i, this);
 648   }
 649 }
 650 
 651 void ArchiveBuilder::update_special_refs() {
 652   for (int i = 0; i < _special_refs->length(); i++) {
 653     SpecialRefInfo s = _special_refs->at(i);
 654     size_t field_offset = s.field_offset();
 655     address src_obj = s.src_obj();
 656     address dst_obj = get_dumped_addr(src_obj);
 657     intptr_t* src_p = (intptr_t*)(src_obj + field_offset);
 658     intptr_t* dst_p = (intptr_t*)(dst_obj + field_offset);
 659 
 660 
 661     MetaspaceClosure::assert_valid(s.type());
 662     switch (s.type()) {
 663     case MetaspaceClosure::_method_entry_ref:
 664       assert(*src_p == *dst_p, "must be a copy");
 665       break;
 666     case MetaspaceClosure::_internal_pointer_ref:
 667       {
 668         // *src_p points to a location inside src_obj. Let's make *dst_p point to
 669         // the same location inside dst_obj.
 670         size_t off = pointer_delta(*((address*)src_p), src_obj, sizeof(u1));
 671         assert(off < s.src_obj_size_in_bytes(), "must point to internal address");
 672         *((address*)dst_p) = dst_obj + off;
 673       }
 674       break;
 675     default:
 676       ShouldNotReachHere();
 677     }
 678     ArchivePtrMarker::mark_pointer((address*)dst_p);
 679   }
 680 }
 681 
 682 class RefRelocator: public MetaspaceClosure {
 683   ArchiveBuilder* _builder;
 684 
 685 public:
 686   RefRelocator(ArchiveBuilder* builder) : _builder(builder) {}
 687 
 688   virtual bool do_ref(Ref* ref, bool read_only) {
 689     if (ref->not_null()) {
 690       ref->update(_builder->get_dumped_addr(ref->obj()));
 691       ArchivePtrMarker::mark_pointer(ref->addr());
 692     }
 693     return false; // Do not recurse.
 694   }
 695 };
 696 
 697 void ArchiveBuilder::relocate_roots() {
 698   log_info(cds)("Relocating external roots ... ");
 699   ResourceMark rm;
 700   RefRelocator doit(this);
 701   iterate_sorted_roots(&doit, /*is_relocating_pointers=*/true);
 702   doit.finish();
 703   log_info(cds)("done");
 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   update_special_refs();
 711 }
 712 
 713 // We must relocate vmClasses::_klasses[] only after we have copied the
 714 // java objects in during dump_java_heap_objects(): during the object copy, we operate on
 715 // old objects which assert that their klass is the original klass.
 716 void ArchiveBuilder::relocate_vm_classes() {
 717   log_info(cds)("Relocating vmClasses::_klasses[] ... ");
 718   ResourceMark rm;
 719   RefRelocator doit(this);
 720   vmClasses::metaspace_pointers_do(&doit);
 721 }
 722 
 723 void ArchiveBuilder::make_klasses_shareable() {
 724   int num_instance_klasses = 0;
 725   int num_boot_klasses = 0;
 726   int num_platform_klasses = 0;
 727   int num_app_klasses = 0;
 728   int num_hidden_klasses = 0;
 729   int num_unlinked_klasses = 0;
 730   int num_unregistered_klasses = 0;
 731   int num_obj_array_klasses = 0;
 732   int num_type_array_klasses = 0;
 733 
 734   for (int i = 0; i < klasses()->length(); i++) {
 735     const char* type;
 736     const char* unlinked = "";
 737     const char* hidden = "";
 738     Klass* k = klasses()->at(i);
 739     k->remove_java_mirror();
 740     if (k->is_objArray_klass()) {
 741       // InstanceKlass and TypeArrayKlass will in turn call remove_unshareable_info
 742       // on their array classes.
 743       num_obj_array_klasses ++;
 744       type = "array";
 745     } else if (k->is_typeArray_klass()) {
 746       num_type_array_klasses ++;
 747       type = "array";
 748       k->remove_unshareable_info();
 749     } else {
 750       assert(k->is_instance_klass(), " must be");
 751       num_instance_klasses ++;
 752       InstanceKlass* ik = InstanceKlass::cast(k);
 753       if (DynamicDumpSharedSpaces) {
 754         // For static dump, class loader type are already set.
 755         ik->assign_class_loader_type();
 756       }
 757       if (ik->is_shared_boot_class()) {
 758         type = "boot";
 759         num_boot_klasses ++;
 760       } else if (ik->is_shared_platform_class()) {
 761         type = "plat";
 762         num_platform_klasses ++;
 763       } else if (ik->is_shared_app_class()) {
 764         type = "app";
 765         num_app_klasses ++;
 766       } else {
 767         assert(ik->is_shared_unregistered_class(), "must be");
 768         type = "unreg";
 769         num_unregistered_klasses ++;
 770       }
 771 
 772       if (!ik->is_linked()) {
 773         num_unlinked_klasses ++;
 774         unlinked = " ** unlinked";
 775       }
 776 
 777       if (ik->is_hidden()) {
 778         num_hidden_klasses ++;
 779         hidden = " ** hidden";
 780       }
 781 
 782       MetaspaceShared::rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread::current(), ik);
 783       ik->remove_unshareable_info();
 784     }
 785 
 786     if (log_is_enabled(Debug, cds, class)) {
 787       ResourceMark rm;
 788       log_debug(cds, class)("klasses[%5d] = " PTR_FORMAT " %-5s %s%s%s", i, p2i(to_requested(k)), type, k->external_name(), hidden, unlinked);
 789     }
 790   }
 791 
 792   log_info(cds)("Number of classes %d", num_instance_klasses + num_obj_array_klasses + num_type_array_klasses);
 793   log_info(cds)("    instance classes   = %5d", num_instance_klasses);
 794   log_info(cds)("      boot             = %5d", num_boot_klasses);
 795   log_info(cds)("      app              = %5d", num_app_klasses);
 796   log_info(cds)("      platform         = %5d", num_platform_klasses);
 797   log_info(cds)("      unregistered     = %5d", num_unregistered_klasses);
 798   log_info(cds)("      (hidden)         = %5d", num_hidden_klasses);
 799   log_info(cds)("      (unlinked)       = %5d", num_unlinked_klasses);
 800   log_info(cds)("    obj array classes  = %5d", num_obj_array_klasses);
 801   log_info(cds)("    type array classes = %5d", num_type_array_klasses);
 802   log_info(cds)("               symbols = %5d", _symbols->length());
 803 }
 804 
 805 uintx ArchiveBuilder::buffer_to_offset(address p) const {
 806   address requested_p = to_requested(p);
 807   assert(requested_p >= _requested_static_archive_bottom, "must be");
 808   return requested_p - _requested_static_archive_bottom;
 809 }
 810 
 811 uintx ArchiveBuilder::any_to_offset(address p) const {
 812   if (is_in_mapped_static_archive(p)) {
 813     assert(DynamicDumpSharedSpaces, "must be");
 814     return p - _mapped_static_archive_bottom;
 815   }
 816   return buffer_to_offset(p);
 817 }
 818 
 819 // Update a Java object to point its Klass* to the new location after
 820 // shared archive has been compacted.
 821 void ArchiveBuilder::relocate_klass_ptr(oop o) {
 822   assert(DumpSharedSpaces, "sanity");
 823   Klass* k = get_relocated_klass(o->klass());
 824   Klass* requested_k = to_requested(k);
 825   narrowKlass nk = CompressedKlassPointers::encode_not_null(requested_k, _requested_static_archive_bottom);
 826   o->set_narrow_klass(nk);
 827 }
 828 
 829 // RelocateBufferToRequested --- Relocate all the pointers in rw/ro,
 830 // so that the archive can be mapped to the "requested" location without runtime relocation.
 831 //
 832 // - See ArchiveBuilder header for the definition of "buffer", "mapped" and "requested"
 833 // - ArchivePtrMarker::ptrmap() marks all the pointers in the rw/ro regions
 834 // - Every pointer must have one of the following values:
 835 //   [a] NULL:
 836 //       No relocation is needed. Remove this pointer from ptrmap so we don't need to
 837 //       consider it at runtime.
 838 //   [b] Points into an object X which is inside the buffer:
 839 //       Adjust this pointer by _buffer_to_requested_delta, so it points to X
 840 //       when the archive is mapped at the requested location.
 841 //   [c] Points into an object Y which is inside mapped static archive:
 842 //       - This happens only during dynamic dump
 843 //       - Adjust this pointer by _mapped_to_requested_static_archive_delta,
 844 //         so it points to Y when the static archive is mapped at the requested location.
 845 template <bool STATIC_DUMP>
 846 class RelocateBufferToRequested : public BitMapClosure {
 847   ArchiveBuilder* _builder;
 848   address _buffer_bottom;
 849   intx _buffer_to_requested_delta;
 850   intx _mapped_to_requested_static_archive_delta;
 851   size_t _max_non_null_offset;
 852 
 853  public:
 854   RelocateBufferToRequested(ArchiveBuilder* builder) {
 855     _builder = builder;
 856     _buffer_bottom = _builder->buffer_bottom();
 857     _buffer_to_requested_delta = builder->buffer_to_requested_delta();
 858     _mapped_to_requested_static_archive_delta = builder->requested_static_archive_bottom() - builder->mapped_static_archive_bottom();
 859     _max_non_null_offset = 0;
 860 
 861     address bottom = _builder->buffer_bottom();
 862     address top = _builder->buffer_top();
 863     address new_bottom = bottom + _buffer_to_requested_delta;
 864     address new_top = top + _buffer_to_requested_delta;
 865     log_debug(cds)("Relocating archive from [" INTPTR_FORMAT " - " INTPTR_FORMAT "] to "
 866                    "[" INTPTR_FORMAT " - " INTPTR_FORMAT "]",
 867                    p2i(bottom), p2i(top),
 868                    p2i(new_bottom), p2i(new_top));
 869   }
 870 
 871   bool do_bit(size_t offset) {
 872     address* p = (address*)_buffer_bottom + offset;
 873     assert(_builder->is_in_buffer_space(p), "pointer must live in buffer space");
 874 
 875     if (*p == NULL) {
 876       // todo -- clear bit, etc
 877       ArchivePtrMarker::ptrmap()->clear_bit(offset);
 878     } else {
 879       if (STATIC_DUMP) {
 880         assert(_builder->is_in_buffer_space(*p), "old pointer must point inside buffer space");
 881         *p += _buffer_to_requested_delta;
 882         assert(_builder->is_in_requested_static_archive(*p), "new pointer must point inside requested archive");
 883       } else {
 884         if (_builder->is_in_buffer_space(*p)) {
 885           *p += _buffer_to_requested_delta;
 886           // assert is in requested dynamic archive
 887         } else {
 888           assert(_builder->is_in_mapped_static_archive(*p), "old pointer must point inside buffer space or mapped static archive");
 889           *p += _mapped_to_requested_static_archive_delta;
 890           assert(_builder->is_in_requested_static_archive(*p), "new pointer must point inside requested archive");
 891         }
 892       }
 893       _max_non_null_offset = offset;
 894     }
 895 
 896     return true; // keep iterating
 897   }
 898 
 899   void doit() {
 900     ArchivePtrMarker::ptrmap()->iterate(this);
 901     ArchivePtrMarker::compact(_max_non_null_offset);
 902   }
 903 };
 904 
 905 
 906 void ArchiveBuilder::relocate_to_requested() {
 907   ro_region()->pack();
 908 
 909   size_t my_archive_size = buffer_top() - buffer_bottom();
 910 
 911   if (DumpSharedSpaces) {
 912     _requested_static_archive_top = _requested_static_archive_bottom + my_archive_size;
 913     RelocateBufferToRequested<true> patcher(this);
 914     patcher.doit();
 915   } else {
 916     assert(DynamicDumpSharedSpaces, "must be");
 917     _requested_dynamic_archive_top = _requested_dynamic_archive_bottom + my_archive_size;
 918     RelocateBufferToRequested<false> patcher(this);
 919     patcher.doit();
 920   }
 921 }
 922 
 923 // Write detailed info to a mapfile to analyze contents of the archive.
 924 // static dump:
 925 //   java -Xshare:dump -Xlog:cds+map=trace:file=cds.map:none:filesize=0
 926 // dynamic dump:
 927 //   java -cp MyApp.jar -XX:ArchiveClassesAtExit=MyApp.jsa \
 928 //        -Xlog:cds+map=trace:file=cds.map:none:filesize=0 MyApp
 929 //
 930 // We need to do some address translation because the buffers used at dump time may be mapped to
 931 // a different location at runtime. At dump time, the buffers may be at arbitrary locations
 932 // picked by the OS. At runtime, we try to map at a fixed location (SharedBaseAddress). For
 933 // consistency, we log everything using runtime addresses.
 934 class ArchiveBuilder::CDSMapLogger : AllStatic {
 935   static intx buffer_to_runtime_delta() {
 936     // Translate the buffers used by the RW/RO regions to their eventual (requested) locations
 937     // at runtime.
 938     return ArchiveBuilder::current()->buffer_to_requested_delta();
 939   }
 940 
 941   // rw/ro regions only
 942   static void write_dump_region(const char* name, DumpRegion* region) {
 943     address region_base = address(region->base());
 944     address region_top  = address(region->top());
 945     write_region(name, region_base, region_top, region_base + buffer_to_runtime_delta());
 946   }
 947 
 948 #define _LOG_PREFIX PTR_FORMAT ": @@ %-17s %d"
 949 
 950   static void write_klass(Klass* k, address runtime_dest, const char* type_name, int bytes, Thread* current) {
 951     ResourceMark rm(current);
 952     log_debug(cds, map)(_LOG_PREFIX " %s",
 953                         p2i(runtime_dest), type_name, bytes, k->external_name());
 954   }
 955   static void write_method(Method* m, address runtime_dest, const char* type_name, int bytes, Thread* current) {
 956     ResourceMark rm(current);
 957     log_debug(cds, map)(_LOG_PREFIX " %s",
 958                         p2i(runtime_dest), type_name, bytes,  m->external_name());
 959   }
 960 
 961   // rw/ro regions only
 962   static void write_objects(DumpRegion* region, const ArchiveBuilder::SourceObjList* src_objs) {
 963     address last_obj_base = address(region->base());
 964     address last_obj_end  = address(region->base());
 965     address region_end    = address(region->end());
 966     Thread* current = Thread::current();
 967     for (int i = 0; i < src_objs->objs()->length(); i++) {
 968       SourceObjInfo* src_info = src_objs->at(i);
 969       address src = src_info->orig_obj();
 970       address dest = src_info->dumped_addr();
 971       write_data(last_obj_base, dest, last_obj_base + buffer_to_runtime_delta());
 972       address runtime_dest = dest + buffer_to_runtime_delta();
 973       int bytes = src_info->size_in_bytes();
 974 
 975       MetaspaceObj::Type type = src_info->msotype();
 976       const char* type_name = MetaspaceObj::type_name(type);
 977 
 978       switch (type) {
 979       case MetaspaceObj::ClassType:
 980         write_klass((Klass*)src, runtime_dest, type_name, bytes, current);
 981         break;
 982       case MetaspaceObj::ConstantPoolType:
 983         write_klass(((ConstantPool*)src)->pool_holder(),
 984                     runtime_dest, type_name, bytes, current);
 985         break;
 986       case MetaspaceObj::ConstantPoolCacheType:
 987         write_klass(((ConstantPoolCache*)src)->constant_pool()->pool_holder(),
 988                     runtime_dest, type_name, bytes, current);
 989         break;
 990       case MetaspaceObj::MethodType:
 991         write_method((Method*)src, runtime_dest, type_name, bytes, current);
 992         break;
 993       case MetaspaceObj::ConstMethodType:
 994         write_method(((ConstMethod*)src)->method(), runtime_dest, type_name, bytes, current);
 995         break;
 996       case MetaspaceObj::SymbolType:
 997         {
 998           ResourceMark rm(current);
 999           Symbol* s = (Symbol*)src;
1000           log_debug(cds, map)(_LOG_PREFIX " %s", p2i(runtime_dest), type_name, bytes,
1001                               s->as_quoted_ascii());
1002         }
1003         break;
1004       default:
1005         log_debug(cds, map)(_LOG_PREFIX, p2i(runtime_dest), type_name, bytes);
1006         break;
1007       }
1008 
1009       last_obj_base = dest;
1010       last_obj_end  = dest + bytes;
1011     }
1012 
1013     write_data(last_obj_base, last_obj_end, last_obj_base + buffer_to_runtime_delta());
1014     if (last_obj_end < region_end) {
1015       log_debug(cds, map)(PTR_FORMAT ": @@ Misc data " SIZE_FORMAT " bytes",
1016                           p2i(last_obj_end + buffer_to_runtime_delta()),
1017                           size_t(region_end - last_obj_end));
1018       write_data(last_obj_end, region_end, last_obj_end + buffer_to_runtime_delta());
1019     }
1020   }
1021 
1022 #undef _LOG_PREFIX
1023 
1024   // Write information about a region, whose address at dump time is [base .. top). At
1025   // runtime, this region will be mapped to runtime_base.  runtime_base is 0 if this
1026   // region will be mapped at os-selected addresses (such as the bitmap region), or will
1027   // be accessed with os::read (the header).
1028   static void write_region(const char* name, address base, address top, address runtime_base) {
1029     size_t size = top - base;
1030     base = runtime_base;
1031     top = runtime_base + size;
1032     log_info(cds, map)("[%-18s " PTR_FORMAT " - " PTR_FORMAT " " SIZE_FORMAT_W(9) " bytes]",
1033                        name, p2i(base), p2i(top), size);
1034   }
1035 
1036   // open and closed archive regions
1037   static void write_heap_region(const char* which, GrowableArray<MemRegion> *regions) {
1038     for (int i = 0; i < regions->length(); i++) {
1039       address start = address(regions->at(i).start());
1040       address end = address(regions->at(i).end());
1041       write_region(which, start, end, start);
1042       write_data(start, end, start);
1043     }
1044   }
1045 
1046   // Dump all the data [base...top). Pretend that the base address
1047   // will be mapped to runtime_base at run-time.
1048   static void write_data(address base, address top, address runtime_base) {
1049     assert(top >= base, "must be");
1050 
1051     LogStreamHandle(Trace, cds, map) lsh;
1052     if (lsh.is_enabled()) {
1053       os::print_hex_dump(&lsh, base, top, sizeof(address), 32, runtime_base);
1054     }
1055   }
1056 
1057   static void write_header(FileMapInfo* mapinfo) {
1058     LogStreamHandle(Info, cds, map) lsh;
1059     if (lsh.is_enabled()) {
1060       mapinfo->print(&lsh);
1061     }
1062   }
1063 
1064 public:
1065   static void write(ArchiveBuilder* builder, FileMapInfo* mapinfo,
1066              GrowableArray<MemRegion> *closed_heap_regions,
1067              GrowableArray<MemRegion> *open_heap_regions,
1068              char* bitmap, size_t bitmap_size_in_bytes) {
1069     log_info(cds, map)("%s CDS archive map for %s", DumpSharedSpaces ? "Static" : "Dynamic", mapinfo->full_path());
1070 
1071     address header = address(mapinfo->header());
1072     address header_end = header + mapinfo->header()->header_size();
1073     write_region("header", header, header_end, 0);
1074     write_header(mapinfo);
1075     write_data(header, header_end, 0);
1076 
1077     DumpRegion* rw_region = &builder->_rw_region;
1078     DumpRegion* ro_region = &builder->_ro_region;
1079 
1080     write_dump_region("rw region", rw_region);
1081     write_objects(rw_region, &builder->_rw_src_objs);
1082 
1083     write_dump_region("ro region", ro_region);
1084     write_objects(ro_region, &builder->_ro_src_objs);
1085 
1086     address bitmap_end = address(bitmap + bitmap_size_in_bytes);
1087     write_region("bitmap", address(bitmap), bitmap_end, 0);
1088     write_data(header, header_end, 0);
1089 
1090     if (closed_heap_regions != NULL) {
1091       write_heap_region("closed heap region", closed_heap_regions);
1092     }
1093     if (open_heap_regions != NULL) {
1094       write_heap_region("open heap region", open_heap_regions);
1095     }
1096 
1097     log_info(cds, map)("[End of CDS archive map]");
1098   }
1099 };
1100 
1101 void ArchiveBuilder::print_stats() {
1102   _alloc_stats.print_stats(int(_ro_region.used()), int(_rw_region.used()));
1103 }
1104 
1105 void ArchiveBuilder::clean_up_src_obj_table() {
1106   SrcObjTableCleaner cleaner;
1107   _src_obj_table.iterate(&cleaner);
1108 }
1109 
1110 void ArchiveBuilder::write_archive(FileMapInfo* mapinfo,
1111                                    GrowableArray<MemRegion>* closed_heap_regions,
1112                                    GrowableArray<MemRegion>* open_heap_regions,
1113                                    GrowableArray<ArchiveHeapOopmapInfo>* closed_heap_oopmaps,
1114                                    GrowableArray<ArchiveHeapOopmapInfo>* open_heap_oopmaps) {
1115   // Make sure NUM_CDS_REGIONS (exported in cds.h) agrees with
1116   // MetaspaceShared::n_regions (internal to hotspot).
1117   assert(NUM_CDS_REGIONS == MetaspaceShared::n_regions, "sanity");
1118 
1119   write_region(mapinfo, MetaspaceShared::rw, &_rw_region, /*read_only=*/false,/*allow_exec=*/false);
1120   write_region(mapinfo, MetaspaceShared::ro, &_ro_region, /*read_only=*/true, /*allow_exec=*/false);
1121 
1122   size_t bitmap_size_in_bytes;
1123   char* bitmap = mapinfo->write_bitmap_region(ArchivePtrMarker::ptrmap(), closed_heap_oopmaps, open_heap_oopmaps,
1124                                               bitmap_size_in_bytes);
1125 
1126   if (closed_heap_regions != NULL) {
1127     _total_closed_heap_region_size = mapinfo->write_heap_regions(
1128                                         closed_heap_regions,
1129                                         closed_heap_oopmaps,
1130                                         MetaspaceShared::first_closed_heap_region,
1131                                         MetaspaceShared::max_num_closed_heap_regions);
1132     _total_open_heap_region_size = mapinfo->write_heap_regions(
1133                                         open_heap_regions,
1134                                         open_heap_oopmaps,
1135                                         MetaspaceShared::first_open_heap_region,
1136                                         MetaspaceShared::max_num_open_heap_regions);
1137   }
1138 
1139   print_region_stats(mapinfo, closed_heap_regions, open_heap_regions);
1140 
1141   mapinfo->set_requested_base((char*)MetaspaceShared::requested_base_address());
1142   if (mapinfo->header()->magic() == CDS_DYNAMIC_ARCHIVE_MAGIC) {
1143     mapinfo->set_header_base_archive_name_size(strlen(Arguments::GetSharedArchivePath()) + 1);
1144     mapinfo->set_header_base_archive_is_default(FLAG_IS_DEFAULT(SharedArchiveFile));
1145   }
1146   mapinfo->set_header_crc(mapinfo->compute_header_crc());
1147   // After this point, we should not write any data into mapinfo->header() since this
1148   // would corrupt its checksum we have calculated before.
1149   mapinfo->write_header();
1150   mapinfo->close();
1151 
1152   if (log_is_enabled(Info, cds)) {
1153     print_stats();
1154   }
1155 
1156   if (log_is_enabled(Info, cds, map)) {
1157     CDSMapLogger::write(this, mapinfo, closed_heap_regions, open_heap_regions,
1158                         bitmap, bitmap_size_in_bytes);
1159   }
1160   FREE_C_HEAP_ARRAY(char, bitmap);
1161 }
1162 
1163 void ArchiveBuilder::write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region, bool read_only,  bool allow_exec) {
1164   mapinfo->write_region(region_idx, dump_region->base(), dump_region->used(), read_only, allow_exec);
1165 }
1166 
1167 void ArchiveBuilder::print_region_stats(FileMapInfo *mapinfo,
1168                                         GrowableArray<MemRegion>* closed_heap_regions,
1169                                         GrowableArray<MemRegion>* open_heap_regions) {
1170   // Print statistics of all the regions
1171   const size_t bitmap_used = mapinfo->space_at(MetaspaceShared::bm)->used();
1172   const size_t bitmap_reserved = mapinfo->space_at(MetaspaceShared::bm)->used_aligned();
1173   const size_t total_reserved = _ro_region.reserved()  + _rw_region.reserved() +
1174                                 bitmap_reserved +
1175                                 _total_closed_heap_region_size +
1176                                 _total_open_heap_region_size;
1177   const size_t total_bytes = _ro_region.used()  + _rw_region.used() +
1178                              bitmap_used +
1179                              _total_closed_heap_region_size +
1180                              _total_open_heap_region_size;
1181   const double total_u_perc = percent_of(total_bytes, total_reserved);
1182 
1183   _rw_region.print(total_reserved);
1184   _ro_region.print(total_reserved);
1185 
1186   print_bitmap_region_stats(bitmap_used, total_reserved);
1187 
1188   if (closed_heap_regions != NULL) {
1189     print_heap_region_stats(closed_heap_regions, "ca", total_reserved);
1190     print_heap_region_stats(open_heap_regions, "oa", total_reserved);
1191   }
1192 
1193   log_debug(cds)("total    : " SIZE_FORMAT_W(9) " [100.0%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used]",
1194                  total_bytes, total_reserved, total_u_perc);
1195 }
1196 
1197 void ArchiveBuilder::print_bitmap_region_stats(size_t size, size_t total_size) {
1198   log_debug(cds)("bm  space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [100.0%% used]",
1199                  size, size/double(total_size)*100.0, size);
1200 }
1201 
1202 void ArchiveBuilder::print_heap_region_stats(GrowableArray<MemRegion>* regions,
1203                                              const char *name, size_t total_size) {
1204   int arr_len = regions == NULL ? 0 : regions->length();
1205   for (int i = 0; i < arr_len; i++) {
1206       char* start = (char*)regions->at(i).start();
1207       size_t size = regions->at(i).byte_size();
1208       char* top = start + size;
1209       log_debug(cds)("%s%d space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [100.0%% used] at " INTPTR_FORMAT,
1210                      name, i, size, size/double(total_size)*100.0, size, p2i(start));
1211   }
1212 }
1213 
1214 void ArchiveBuilder::report_out_of_space(const char* name, size_t needed_bytes) {
1215   // This is highly unlikely to happen on 64-bits because we have reserved a 4GB space.
1216   // On 32-bit we reserve only 256MB so you could run out of space with 100,000 classes
1217   // or so.
1218   _rw_region.print_out_of_space_msg(name, needed_bytes);
1219   _ro_region.print_out_of_space_msg(name, needed_bytes);
1220 
1221   vm_exit_during_initialization(err_msg("Unable to allocate from '%s' region", name),
1222                                 "Please reduce the number of shared classes.");
1223 }
1224 
1225 
1226 #ifndef PRODUCT
1227 void ArchiveBuilder::assert_is_vm_thread() {
1228   assert(Thread::current()->is_VM_thread(), "ArchiveBuilder should be used only inside the VMThread");
1229 }
1230 #endif