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