1 /*
2 * Copyright (c) 2020, 2025, 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 "cds/aotArtifactFinder.hpp"
26 #include "cds/aotClassLinker.hpp"
27 #include "cds/aotLinkedClassBulkLoader.hpp"
28 #include "cds/archiveBuilder.hpp"
29 #include "cds/archiveHeapWriter.hpp"
30 #include "cds/archiveUtils.hpp"
31 #include "cds/cdsConfig.hpp"
32 #include "cds/cppVtables.hpp"
33 #include "cds/dumpAllocStats.hpp"
34 #include "cds/dynamicArchive.hpp"
35 #include "cds/heapShared.hpp"
36 #include "cds/metaspaceShared.hpp"
37 #include "cds/regeneratedClasses.hpp"
38 #include "classfile/classLoader.hpp"
39 #include "classfile/classLoaderDataShared.hpp"
40 #include "classfile/classLoaderExt.hpp"
41 #include "classfile/javaClasses.hpp"
42 #include "classfile/symbolTable.hpp"
43 #include "classfile/systemDictionaryShared.hpp"
44 #include "classfile/vmClasses.hpp"
45 #include "code/aotCodeCache.hpp"
46 #include "interpreter/abstractInterpreter.hpp"
47 #include "jvm.h"
48 #include "logging/log.hpp"
49 #include "logging/logStream.hpp"
50 #include "memory/allStatic.hpp"
51 #include "memory/memoryReserver.hpp"
52 #include "memory/memRegion.hpp"
53 #include "memory/resourceArea.hpp"
54 #include "oops/compressedKlass.inline.hpp"
55 #include "oops/instanceKlass.hpp"
56 #include "oops/objArrayKlass.hpp"
57 #include "oops/objArrayOop.inline.hpp"
58 #include "oops/oopHandle.inline.hpp"
59 #include "runtime/arguments.hpp"
60 #include "runtime/fieldDescriptor.inline.hpp"
61 #include "runtime/globals_extension.hpp"
62 #include "runtime/javaThread.hpp"
63 #include "runtime/sharedRuntime.hpp"
64 #include "utilities/align.hpp"
65 #include "utilities/bitMap.inline.hpp"
66 #include "utilities/formatBuffer.hpp"
67
68 ArchiveBuilder* ArchiveBuilder::_current = nullptr;
69
70 ArchiveBuilder::OtherROAllocMark::~OtherROAllocMark() {
71 char* newtop = ArchiveBuilder::current()->_ro_region.top();
72 ArchiveBuilder::alloc_stats()->record_other_type(int(newtop - _oldtop), true);
73 }
74
75 ArchiveBuilder::SourceObjList::SourceObjList() : _ptrmap(16 * K, mtClassShared) {
76 _total_bytes = 0;
77 _objs = new (mtClassShared) GrowableArray<SourceObjInfo*>(128 * K, mtClassShared);
78 }
79
80 ArchiveBuilder::SourceObjList::~SourceObjList() {
81 delete _objs;
82 }
83
84 void ArchiveBuilder::SourceObjList::append(SourceObjInfo* src_info) {
85 // Save this source object for copying
86 src_info->set_id(_objs->length());
87 _objs->append(src_info);
88
89 // Prepare for marking the pointers in this source object
90 assert(is_aligned(_total_bytes, sizeof(address)), "must be");
91 src_info->set_ptrmap_start(_total_bytes / sizeof(address));
92 _total_bytes = align_up(_total_bytes + (uintx)src_info->size_in_bytes(), sizeof(address));
93 src_info->set_ptrmap_end(_total_bytes / sizeof(address));
94
95 BitMap::idx_t bitmap_size_needed = BitMap::idx_t(src_info->ptrmap_end());
96 if (_ptrmap.size() <= bitmap_size_needed) {
97 _ptrmap.resize((bitmap_size_needed + 1) * 2);
98 }
99 }
100
101 void ArchiveBuilder::SourceObjList::remember_embedded_pointer(SourceObjInfo* src_info, MetaspaceClosure::Ref* ref) {
102 // src_obj contains a pointer. Remember the location of this pointer in _ptrmap,
103 // so that we can copy/relocate it later.
104 src_info->set_has_embedded_pointer();
105 address src_obj = src_info->source_addr();
106 address* field_addr = ref->addr();
107 assert(src_info->ptrmap_start() < _total_bytes, "sanity");
108 assert(src_info->ptrmap_end() <= _total_bytes, "sanity");
109 assert(*field_addr != nullptr, "should have checked");
110
111 intx field_offset_in_bytes = ((address)field_addr) - src_obj;
112 DEBUG_ONLY(int src_obj_size = src_info->size_in_bytes();)
113 assert(field_offset_in_bytes >= 0, "must be");
114 assert(field_offset_in_bytes + intx(sizeof(intptr_t)) <= intx(src_obj_size), "must be");
115 assert(is_aligned(field_offset_in_bytes, sizeof(address)), "must be");
116
117 BitMap::idx_t idx = BitMap::idx_t(src_info->ptrmap_start() + (uintx)(field_offset_in_bytes / sizeof(address)));
118 _ptrmap.set_bit(BitMap::idx_t(idx));
119 }
120
121 class RelocateEmbeddedPointers : public BitMapClosure {
122 ArchiveBuilder* _builder;
123 address _buffered_obj;
124 BitMap::idx_t _start_idx;
125 public:
126 RelocateEmbeddedPointers(ArchiveBuilder* builder, address buffered_obj, BitMap::idx_t start_idx) :
127 _builder(builder), _buffered_obj(buffered_obj), _start_idx(start_idx) {}
128
129 bool do_bit(BitMap::idx_t bit_offset) {
130 size_t field_offset = size_t(bit_offset - _start_idx) * sizeof(address);
131 address* ptr_loc = (address*)(_buffered_obj + field_offset);
132
133 address old_p = *ptr_loc;
134 address new_p = _builder->get_buffered_addr(old_p);
135
136 log_trace(cds)("Ref: [" PTR_FORMAT "] -> " PTR_FORMAT " => " PTR_FORMAT,
137 p2i(ptr_loc), p2i(old_p), p2i(new_p));
138
139 ArchivePtrMarker::set_and_mark_pointer(ptr_loc, new_p);
140 return true; // keep iterating the bitmap
141 }
142 };
143
144 void ArchiveBuilder::SourceObjList::relocate(int i, ArchiveBuilder* builder) {
145 SourceObjInfo* src_info = objs()->at(i);
146 assert(src_info->should_copy(), "must be");
147 BitMap::idx_t start = BitMap::idx_t(src_info->ptrmap_start()); // inclusive
148 BitMap::idx_t end = BitMap::idx_t(src_info->ptrmap_end()); // exclusive
149
150 RelocateEmbeddedPointers relocator(builder, src_info->buffered_addr(), start);
151 _ptrmap.iterate(&relocator, start, end);
152 }
153
154 ArchiveBuilder::ArchiveBuilder() :
155 _current_dump_region(nullptr),
156 _buffer_bottom(nullptr),
157 _requested_static_archive_bottom(nullptr),
158 _requested_static_archive_top(nullptr),
159 _requested_dynamic_archive_bottom(nullptr),
160 _requested_dynamic_archive_top(nullptr),
161 _mapped_static_archive_bottom(nullptr),
162 _mapped_static_archive_top(nullptr),
163 _buffer_to_requested_delta(0),
164 _pz_region("pz", MAX_SHARED_DELTA), // protection zone -- used only during dumping; does NOT exist in cds archive.
165 _rw_region("rw", MAX_SHARED_DELTA),
166 _ro_region("ro", MAX_SHARED_DELTA),
167 _ac_region("ac", MAX_SHARED_DELTA),
168 _ptrmap(mtClassShared),
169 _rw_ptrmap(mtClassShared),
170 _ro_ptrmap(mtClassShared),
171 _rw_src_objs(),
172 _ro_src_objs(),
173 _src_obj_table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE),
174 _buffered_to_src_table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE),
175 _total_heap_region_size(0)
176 {
177 _klasses = new (mtClassShared) GrowableArray<Klass*>(4 * K, mtClassShared);
178 _symbols = new (mtClassShared) GrowableArray<Symbol*>(256 * K, mtClassShared);
179 _entropy_seed = 0x12345678;
180 assert(_current == nullptr, "must be");
181 _current = this;
182 }
183
184 ArchiveBuilder::~ArchiveBuilder() {
185 assert(_current == this, "must be");
186 _current = nullptr;
187
188 for (int i = 0; i < _symbols->length(); i++) {
189 _symbols->at(i)->decrement_refcount();
190 }
191
192 delete _klasses;
193 delete _symbols;
194 if (_shared_rs.is_reserved()) {
195 MemoryReserver::release(_shared_rs);
196 }
197
198 AOTArtifactFinder::dispose();
199 }
200
201 // Returns a deterministic sequence of pseudo random numbers. The main purpose is NOT
202 // for randomness but to get good entropy for the identity_hash() of archived Symbols,
203 // while keeping the contents of static CDS archives deterministic to ensure
204 // reproducibility of JDK builds.
205 int ArchiveBuilder::entropy() {
206 assert(SafepointSynchronize::is_at_safepoint(), "needed to ensure deterministic sequence");
207 _entropy_seed = os::next_random(_entropy_seed);
208 return static_cast<int>(_entropy_seed);
209 }
210
211 class GatherKlassesAndSymbols : public UniqueMetaspaceClosure {
212 ArchiveBuilder* _builder;
213
214 public:
215 GatherKlassesAndSymbols(ArchiveBuilder* builder) : _builder(builder) {}
216
217 virtual bool do_unique_ref(Ref* ref, bool read_only) {
218 return _builder->gather_klass_and_symbol(ref, read_only);
219 }
220 };
221
222 bool ArchiveBuilder::gather_klass_and_symbol(MetaspaceClosure::Ref* ref, bool read_only) {
223 if (ref->obj() == nullptr) {
224 return false;
225 }
226 if (get_follow_mode(ref) != make_a_copy) {
227 return false;
228 }
229 if (ref->msotype() == MetaspaceObj::ClassType) {
230 Klass* klass = (Klass*)ref->obj();
231 assert(klass->is_klass(), "must be");
232 if (!is_excluded(klass)) {
233 _klasses->append(klass);
234 if (klass->is_hidden()) {
235 assert(klass->is_instance_klass(), "must be");
236 }
237 }
238 } else if (ref->msotype() == MetaspaceObj::SymbolType) {
239 // Make sure the symbol won't be GC'ed while we are dumping the archive.
240 Symbol* sym = (Symbol*)ref->obj();
241 sym->increment_refcount();
242 _symbols->append(sym);
243 }
244
245 return true; // recurse
246 }
247
248 void ArchiveBuilder::gather_klasses_and_symbols() {
249 ResourceMark rm;
250
251 AOTArtifactFinder::initialize();
252 AOTArtifactFinder::find_artifacts();
253
254 log_info(cds)("Gathering classes and symbols ... ");
255 GatherKlassesAndSymbols doit(this);
256 iterate_roots(&doit);
257 #if INCLUDE_CDS_JAVA_HEAP
258 if (CDSConfig::is_dumping_full_module_graph()) {
259 ClassLoaderDataShared::iterate_symbols(&doit);
260 }
261 #endif
262 doit.finish();
263
264 if (CDSConfig::is_dumping_static_archive()) {
265 // To ensure deterministic contents in the static archive, we need to ensure that
266 // we iterate the MetaspaceObjs in a deterministic order. It doesn't matter where
267 // the MetaspaceObjs are located originally, as they are copied sequentially into
268 // the archive during the iteration.
269 //
270 // The only issue here is that the symbol table and the system directories may be
271 // randomly ordered, so we copy the symbols and klasses into two arrays and sort
272 // them deterministically.
273 //
274 // During -Xshare:dump, the order of Symbol creation is strictly determined by
275 // the SharedClassListFile (class loading is done in a single thread and the JIT
276 // is disabled). Also, Symbols are allocated in monotonically increasing addresses
277 // (see Symbol::operator new(size_t, int)). So if we iterate the Symbols by
278 // ascending address order, we ensure that all Symbols are copied into deterministic
279 // locations in the archive.
280 //
281 // TODO: in the future, if we want to produce deterministic contents in the
282 // dynamic archive, we might need to sort the symbols alphabetically (also see
283 // DynamicArchiveBuilder::sort_methods()).
284 log_info(cds)("Sorting symbols ... ");
285 _symbols->sort(compare_symbols_by_address);
286 sort_klasses();
287 }
288
289 AOTClassLinker::add_candidates();
290 }
291
292 int ArchiveBuilder::compare_symbols_by_address(Symbol** a, Symbol** b) {
293 if (a[0] < b[0]) {
294 return -1;
295 } else {
296 assert(a[0] > b[0], "Duplicated symbol %s unexpected", (*a)->as_C_string());
297 return 1;
298 }
299 }
300
301 int ArchiveBuilder::compare_klass_by_name(Klass** a, Klass** b) {
302 return a[0]->name()->fast_compare(b[0]->name());
303 }
304
305 void ArchiveBuilder::sort_klasses() {
306 log_info(cds)("Sorting classes ... ");
307 _klasses->sort(compare_klass_by_name);
308 }
309
310 address ArchiveBuilder::reserve_buffer() {
311 // AOTCodeCache::max_aot_code_size() accounts for aot code region.
312 size_t buffer_size = LP64_ONLY(CompressedClassSpaceSize) NOT_LP64(256 * M) + AOTCodeCache::max_aot_code_size();
313 ReservedSpace rs = MemoryReserver::reserve(buffer_size,
314 MetaspaceShared::core_region_alignment(),
315 os::vm_page_size(),
316 mtNone);
317 if (!rs.is_reserved()) {
318 log_error(cds)("Failed to reserve %zu bytes of output buffer.", buffer_size);
319 MetaspaceShared::unrecoverable_writing_error();
320 }
321
322 // buffer_bottom is the lowest address of the 2 core regions (rw, ro) when
323 // we are copying the class metadata into the buffer.
324 address buffer_bottom = (address)rs.base();
325 log_info(cds)("Reserved output buffer space at " PTR_FORMAT " [%zu bytes]",
326 p2i(buffer_bottom), buffer_size);
327 _shared_rs = rs;
328
329 _buffer_bottom = buffer_bottom;
330
331 if (CDSConfig::is_dumping_static_archive()) {
332 _current_dump_region = &_pz_region;
333 } else {
334 _current_dump_region = &_rw_region;
335 }
336 _current_dump_region->init(&_shared_rs, &_shared_vs);
337
338 ArchivePtrMarker::initialize(&_ptrmap, &_shared_vs);
339
340 // The bottom of the static archive should be mapped at this address by default.
341 _requested_static_archive_bottom = (address)MetaspaceShared::requested_base_address();
342
343 // The bottom of the archive (that I am writing now) should be mapped at this address by default.
344 address my_archive_requested_bottom;
345
346 if (CDSConfig::is_dumping_static_archive()) {
347 my_archive_requested_bottom = _requested_static_archive_bottom;
348 } else {
349 _mapped_static_archive_bottom = (address)MetaspaceObj::shared_metaspace_base();
350 _mapped_static_archive_top = (address)MetaspaceObj::shared_metaspace_top();
351 assert(_mapped_static_archive_top >= _mapped_static_archive_bottom, "must be");
352 size_t static_archive_size = _mapped_static_archive_top - _mapped_static_archive_bottom;
353
354 // At run time, we will mmap the dynamic archive at my_archive_requested_bottom
355 _requested_static_archive_top = _requested_static_archive_bottom + static_archive_size;
356 my_archive_requested_bottom = align_up(_requested_static_archive_top, MetaspaceShared::core_region_alignment());
357
358 _requested_dynamic_archive_bottom = my_archive_requested_bottom;
359 }
360
361 _buffer_to_requested_delta = my_archive_requested_bottom - _buffer_bottom;
362
363 address my_archive_requested_top = my_archive_requested_bottom + buffer_size;
364 if (my_archive_requested_bottom < _requested_static_archive_bottom ||
365 my_archive_requested_top <= _requested_static_archive_bottom) {
366 // Size overflow.
367 log_error(cds)("my_archive_requested_bottom = " INTPTR_FORMAT, p2i(my_archive_requested_bottom));
368 log_error(cds)("my_archive_requested_top = " INTPTR_FORMAT, p2i(my_archive_requested_top));
369 log_error(cds)("SharedBaseAddress (" INTPTR_FORMAT ") is too high. "
370 "Please rerun java -Xshare:dump with a lower value", p2i(_requested_static_archive_bottom));
371 MetaspaceShared::unrecoverable_writing_error();
372 }
373
374 if (CDSConfig::is_dumping_static_archive()) {
375 // We don't want any valid object to be at the very bottom of the archive.
376 // See ArchivePtrMarker::mark_pointer().
377 _pz_region.allocate(MetaspaceShared::protection_zone_size());
378 start_dump_region(&_rw_region);
379 }
380
381 return buffer_bottom;
382 }
383
384 void ArchiveBuilder::iterate_sorted_roots(MetaspaceClosure* it) {
385 int num_symbols = _symbols->length();
386 for (int i = 0; i < num_symbols; i++) {
387 it->push(_symbols->adr_at(i));
388 }
389
390 int num_klasses = _klasses->length();
391 for (int i = 0; i < num_klasses; i++) {
392 it->push(_klasses->adr_at(i));
393 }
394
395 iterate_roots(it);
396 }
397
398 class GatherSortedSourceObjs : public MetaspaceClosure {
399 ArchiveBuilder* _builder;
400
401 public:
402 GatherSortedSourceObjs(ArchiveBuilder* builder) : _builder(builder) {}
403
404 virtual bool do_ref(Ref* ref, bool read_only) {
405 return _builder->gather_one_source_obj(ref, read_only);
406 }
407 };
408
409 bool ArchiveBuilder::gather_one_source_obj(MetaspaceClosure::Ref* ref, bool read_only) {
410 address src_obj = ref->obj();
411 if (src_obj == nullptr) {
412 return false;
413 }
414
415 remember_embedded_pointer_in_enclosing_obj(ref);
416 if (RegeneratedClasses::has_been_regenerated(src_obj)) {
417 // No need to copy it. We will later relocate it to point to the regenerated klass/method.
418 return false;
419 }
420
421 FollowMode follow_mode = get_follow_mode(ref);
422 SourceObjInfo src_info(ref, read_only, follow_mode);
423 bool created;
424 SourceObjInfo* p = _src_obj_table.put_if_absent(src_obj, src_info, &created);
425 if (created) {
426 if (_src_obj_table.maybe_grow()) {
427 log_info(cds, hashtables)("Expanded _src_obj_table table to %d", _src_obj_table.table_size());
428 }
429 }
430
431 #ifdef ASSERT
432 if (ref->msotype() == MetaspaceObj::MethodType) {
433 Method* m = (Method*)ref->obj();
434 assert(!RegeneratedClasses::has_been_regenerated((address)m->method_holder()),
435 "Should not archive methods in a class that has been regenerated");
436 }
437 #endif
438
439 assert(p->read_only() == src_info.read_only(), "must be");
440
441 if (created && src_info.should_copy()) {
442 if (read_only) {
443 _ro_src_objs.append(p);
444 } else {
445 _rw_src_objs.append(p);
446 }
447 return true; // Need to recurse into this ref only if we are copying it
448 } else {
449 return false;
450 }
451 }
452
453 void ArchiveBuilder::record_regenerated_object(address orig_src_obj, address regen_src_obj) {
454 // Record the fact that orig_src_obj has been replaced by regen_src_obj. All calls to get_buffered_addr(orig_src_obj)
455 // should return the same value as get_buffered_addr(regen_src_obj).
456 SourceObjInfo* p = _src_obj_table.get(regen_src_obj);
457 assert(p != nullptr, "regenerated object should always be dumped");
458 SourceObjInfo orig_src_info(orig_src_obj, p);
459 bool created;
460 _src_obj_table.put_if_absent(orig_src_obj, orig_src_info, &created);
461 assert(created, "We shouldn't have archived the original copy of a regenerated object");
462 }
463
464 // Remember that we have a pointer inside ref->enclosing_obj() that points to ref->obj()
465 void ArchiveBuilder::remember_embedded_pointer_in_enclosing_obj(MetaspaceClosure::Ref* ref) {
466 assert(ref->obj() != nullptr, "should have checked");
467
468 address enclosing_obj = ref->enclosing_obj();
469 if (enclosing_obj == nullptr) {
470 return;
471 }
472
473 // We are dealing with 3 addresses:
474 // address o = ref->obj(): We have found an object whose address is o.
475 // address* mpp = ref->mpp(): The object o is pointed to by a pointer whose address is mpp.
476 // I.e., (*mpp == o)
477 // enclosing_obj : If non-null, it is the object which has a field that points to o.
478 // mpp is the address if that field.
479 //
480 // Example: We have an array whose first element points to a Method:
481 // Method* o = 0x0000abcd;
482 // Array<Method*>* enclosing_obj = 0x00001000;
483 // enclosing_obj->at_put(0, o);
484 //
485 // We the MetaspaceClosure iterates on the very first element of this array, we have
486 // ref->obj() == 0x0000abcd (the Method)
487 // ref->mpp() == 0x00001008 (the location of the first element in the array)
488 // ref->enclosing_obj() == 0x00001000 (the Array that contains the Method)
489 //
490 // We use the above information to mark the bitmap to indicate that there's a pointer on address 0x00001008.
491 SourceObjInfo* src_info = _src_obj_table.get(enclosing_obj);
492 if (src_info == nullptr || !src_info->should_copy()) {
493 // source objects of point_to_it/set_to_null types are not copied
494 // so we don't need to remember their pointers.
495 } else {
496 if (src_info->read_only()) {
497 _ro_src_objs.remember_embedded_pointer(src_info, ref);
498 } else {
499 _rw_src_objs.remember_embedded_pointer(src_info, ref);
500 }
501 }
502 }
503
504 void ArchiveBuilder::gather_source_objs() {
505 ResourceMark rm;
506 log_info(cds)("Gathering all archivable objects ... ");
507 gather_klasses_and_symbols();
508 GatherSortedSourceObjs doit(this);
509 iterate_sorted_roots(&doit);
510 doit.finish();
511 }
512
513 bool ArchiveBuilder::is_excluded(Klass* klass) {
514 if (klass->is_instance_klass()) {
515 InstanceKlass* ik = InstanceKlass::cast(klass);
516 return SystemDictionaryShared::is_excluded_class(ik);
517 } else if (klass->is_objArray_klass()) {
518 Klass* bottom = ObjArrayKlass::cast(klass)->bottom_klass();
519 if (CDSConfig::is_dumping_dynamic_archive() && MetaspaceShared::is_shared_static(bottom)) {
520 // The bottom class is in the static archive so it's clearly not excluded.
521 return false;
522 } else if (bottom->is_instance_klass()) {
523 return SystemDictionaryShared::is_excluded_class(InstanceKlass::cast(bottom));
524 }
525 }
526
527 return false;
528 }
529
530 ArchiveBuilder::FollowMode ArchiveBuilder::get_follow_mode(MetaspaceClosure::Ref *ref) {
531 address obj = ref->obj();
532 if (CDSConfig::is_dumping_dynamic_archive() && MetaspaceShared::is_in_shared_metaspace(obj)) {
533 // Don't dump existing shared metadata again.
534 return point_to_it;
535 } else if (ref->msotype() == MetaspaceObj::MethodDataType ||
536 ref->msotype() == MetaspaceObj::MethodCountersType) {
537 return set_to_null;
538 } else if (ref->msotype() == MetaspaceObj::AdapterHandlerEntryType) {
539 if (AOTCodeCache::is_dumping_adapters()) {
540 AdapterHandlerEntry* entry = (AdapterHandlerEntry*)ref->obj();
541 return AdapterHandlerLibrary::is_abstract_method_adapter(entry) ? set_to_null : make_a_copy;
542 } else {
543 return set_to_null;
544 }
545 } else {
546 if (ref->msotype() == MetaspaceObj::ClassType) {
547 Klass* klass = (Klass*)ref->obj();
548 assert(klass->is_klass(), "must be");
549 if (is_excluded(klass)) {
550 ResourceMark rm;
551 log_debug(cds, dynamic)("Skipping class (excluded): %s", klass->external_name());
552 return set_to_null;
553 }
554 }
555
556 return make_a_copy;
557 }
558 }
559
560 void ArchiveBuilder::start_dump_region(DumpRegion* next) {
561 current_dump_region()->pack(next);
562 _current_dump_region = next;
563 }
564
565 char* ArchiveBuilder::ro_strdup(const char* s) {
566 char* archived_str = ro_region_alloc((int)strlen(s) + 1);
567 strcpy(archived_str, s);
568 return archived_str;
569 }
570
571 // The objects that have embedded pointers will sink
572 // towards the end of the list. This ensures we have a maximum
573 // number of leading zero bits in the relocation bitmap.
574 int ArchiveBuilder::compare_src_objs(SourceObjInfo** a, SourceObjInfo** b) {
575 if ((*a)->has_embedded_pointer() && !(*b)->has_embedded_pointer()) {
576 return 1;
577 } else if (!(*a)->has_embedded_pointer() && (*b)->has_embedded_pointer()) {
578 return -1;
579 } else {
580 // This is necessary to keep the sorting order stable. Otherwise the
581 // archive's contents may not be deterministic.
582 return (*a)->id() - (*b)->id();
583 }
584 }
585
586 void ArchiveBuilder::sort_metadata_objs() {
587 _rw_src_objs.objs()->sort(compare_src_objs);
588 _ro_src_objs.objs()->sort(compare_src_objs);
589 }
590
591 void ArchiveBuilder::dump_rw_metadata() {
592 ResourceMark rm;
593 log_info(cds)("Allocating RW objects ... ");
594 make_shallow_copies(&_rw_region, &_rw_src_objs);
595
596 #if INCLUDE_CDS_JAVA_HEAP
597 if (CDSConfig::is_dumping_full_module_graph()) {
598 // Archive the ModuleEntry's and PackageEntry's of the 3 built-in loaders
599 char* start = rw_region()->top();
600 ClassLoaderDataShared::allocate_archived_tables();
601 alloc_stats()->record_modules(rw_region()->top() - start, /*read_only*/false);
602 }
603 #endif
604 }
605
606 void ArchiveBuilder::dump_ro_metadata() {
607 ResourceMark rm;
608 log_info(cds)("Allocating RO objects ... ");
609
610 start_dump_region(&_ro_region);
611 make_shallow_copies(&_ro_region, &_ro_src_objs);
612
613 #if INCLUDE_CDS_JAVA_HEAP
614 if (CDSConfig::is_dumping_full_module_graph()) {
615 char* start = ro_region()->top();
616 ClassLoaderDataShared::init_archived_tables();
617 alloc_stats()->record_modules(ro_region()->top() - start, /*read_only*/true);
618 }
619 #endif
620
621 RegeneratedClasses::record_regenerated_objects();
622 }
623
624 void ArchiveBuilder::make_shallow_copies(DumpRegion *dump_region,
625 const ArchiveBuilder::SourceObjList* src_objs) {
626 for (int i = 0; i < src_objs->objs()->length(); i++) {
627 make_shallow_copy(dump_region, src_objs->objs()->at(i));
628 }
629 log_info(cds)("done (%d objects)", src_objs->objs()->length());
630 }
631
632 void ArchiveBuilder::make_shallow_copy(DumpRegion *dump_region, SourceObjInfo* src_info) {
633 address src = src_info->source_addr();
634 int bytes = src_info->size_in_bytes();
635 char* dest;
636 char* oldtop;
637 char* newtop;
638
639 oldtop = dump_region->top();
640 if (src_info->msotype() == MetaspaceObj::ClassType) {
641 // Allocate space for a pointer directly in front of the future InstanceKlass, so
642 // we can do a quick lookup from InstanceKlass* -> RunTimeClassInfo*
643 // without building another hashtable. See RunTimeClassInfo::get_for()
644 // in systemDictionaryShared.cpp.
645 Klass* klass = (Klass*)src;
646 if (klass->is_instance_klass()) {
647 SystemDictionaryShared::validate_before_archiving(InstanceKlass::cast(klass));
648 dump_region->allocate(sizeof(address));
649 }
650 // Allocate space for the future InstanceKlass with proper alignment
651 const size_t alignment =
652 #ifdef _LP64
653 UseCompressedClassPointers ?
654 nth_bit(ArchiveBuilder::precomputed_narrow_klass_shift()) :
655 SharedSpaceObjectAlignment;
656 #else
657 SharedSpaceObjectAlignment;
658 #endif
659 dest = dump_region->allocate(bytes, alignment);
660 } else {
661 dest = dump_region->allocate(bytes);
662 }
663 newtop = dump_region->top();
664
665 memcpy(dest, src, bytes);
666
667 // Update the hash of buffered sorted symbols for static dump so that the symbols have deterministic contents
668 if (CDSConfig::is_dumping_static_archive() && (src_info->msotype() == MetaspaceObj::SymbolType)) {
669 Symbol* buffered_symbol = (Symbol*)dest;
670 assert(((Symbol*)src)->is_permanent(), "archived symbols must be permanent");
671 buffered_symbol->update_identity_hash();
672 }
673
674 {
675 bool created;
676 _buffered_to_src_table.put_if_absent((address)dest, src, &created);
677 assert(created, "must be");
678 if (_buffered_to_src_table.maybe_grow()) {
679 log_info(cds, hashtables)("Expanded _buffered_to_src_table table to %d", _buffered_to_src_table.table_size());
680 }
681 }
682
683 intptr_t* archived_vtable = CppVtables::get_archived_vtable(src_info->msotype(), (address)dest);
684 if (archived_vtable != nullptr) {
685 *(address*)dest = (address)archived_vtable;
686 ArchivePtrMarker::mark_pointer((address*)dest);
687 }
688
689 log_trace(cds)("Copy: " PTR_FORMAT " ==> " PTR_FORMAT " %d", p2i(src), p2i(dest), bytes);
690 src_info->set_buffered_addr((address)dest);
691
692 _alloc_stats.record(src_info->msotype(), int(newtop - oldtop), src_info->read_only());
693
694 DEBUG_ONLY(_alloc_stats.verify((int)dump_region->used(), src_info->read_only()));
695 }
696
697 // This is used by code that hand-assembles data structures, such as the LambdaProxyClassKey, that are
698 // not handled by MetaspaceClosure.
699 void ArchiveBuilder::write_pointer_in_buffer(address* ptr_location, address src_addr) {
700 assert(is_in_buffer_space(ptr_location), "must be");
701 if (src_addr == nullptr) {
702 *ptr_location = nullptr;
703 ArchivePtrMarker::clear_pointer(ptr_location);
704 } else {
705 *ptr_location = get_buffered_addr(src_addr);
706 ArchivePtrMarker::mark_pointer(ptr_location);
707 }
708 }
709
710 void ArchiveBuilder::mark_and_relocate_to_buffered_addr(address* ptr_location) {
711 assert(*ptr_location != nullptr, "sanity");
712 if (!is_in_mapped_static_archive(*ptr_location)) {
713 *ptr_location = get_buffered_addr(*ptr_location);
714 }
715 ArchivePtrMarker::mark_pointer(ptr_location);
716 }
717
718 bool ArchiveBuilder::has_been_archived(address src_addr) const {
719 SourceObjInfo* p = _src_obj_table.get(src_addr);
720 return (p != nullptr);
721 }
722
723 bool ArchiveBuilder::has_been_buffered(address src_addr) const {
724 if (RegeneratedClasses::has_been_regenerated(src_addr) ||
725 _src_obj_table.get(src_addr) == nullptr ||
726 get_buffered_addr(src_addr) == nullptr) {
727 return false;
728 } else {
729 return true;
730 }
731 }
732
733 address ArchiveBuilder::get_buffered_addr(address src_addr) const {
734 SourceObjInfo* p = _src_obj_table.get(src_addr);
735 assert(p != nullptr, "src_addr " INTPTR_FORMAT " is used but has not been archived",
736 p2i(src_addr));
737
738 return p->buffered_addr();
739 }
740
741 address ArchiveBuilder::get_source_addr(address buffered_addr) const {
742 assert(is_in_buffer_space(buffered_addr), "must be");
743 address* src_p = _buffered_to_src_table.get(buffered_addr);
744 assert(src_p != nullptr && *src_p != nullptr, "must be");
745 return *src_p;
746 }
747
748 void ArchiveBuilder::relocate_embedded_pointers(ArchiveBuilder::SourceObjList* src_objs) {
749 for (int i = 0; i < src_objs->objs()->length(); i++) {
750 src_objs->relocate(i, this);
751 }
752 }
753
754 void ArchiveBuilder::relocate_metaspaceobj_embedded_pointers() {
755 log_info(cds)("Relocating embedded pointers in core regions ... ");
756 relocate_embedded_pointers(&_rw_src_objs);
757 relocate_embedded_pointers(&_ro_src_objs);
758 }
759
760 #define ADD_COUNT(x) \
761 x += 1; \
762 x ## _a += aotlinked ? 1 : 0; \
763 x ## _i += inited ? 1 : 0;
764
765 #define DECLARE_INSTANCE_KLASS_COUNTER(x) \
766 int x = 0; \
767 int x ## _a = 0; \
768 int x ## _i = 0;
769
770 void ArchiveBuilder::make_klasses_shareable() {
771 DECLARE_INSTANCE_KLASS_COUNTER(num_instance_klasses);
772 DECLARE_INSTANCE_KLASS_COUNTER(num_boot_klasses);
773 DECLARE_INSTANCE_KLASS_COUNTER(num_vm_klasses);
774 DECLARE_INSTANCE_KLASS_COUNTER(num_platform_klasses);
775 DECLARE_INSTANCE_KLASS_COUNTER(num_app_klasses);
776 DECLARE_INSTANCE_KLASS_COUNTER(num_old_klasses);
777 DECLARE_INSTANCE_KLASS_COUNTER(num_hidden_klasses);
778 DECLARE_INSTANCE_KLASS_COUNTER(num_enum_klasses);
779 DECLARE_INSTANCE_KLASS_COUNTER(num_unregistered_klasses);
780 int num_unlinked_klasses = 0;
781 int num_obj_array_klasses = 0;
782 int num_type_array_klasses = 0;
783
784 int boot_unlinked = 0;
785 int platform_unlinked = 0;
786 int app_unlinked = 0;
787 int unreg_unlinked = 0;
788
789 for (int i = 0; i < klasses()->length(); i++) {
790 // Some of the code in ConstantPool::remove_unshareable_info() requires the classes
791 // to be in linked state, so it must be call here before the next loop, which returns
792 // all classes to unlinked state.
793 Klass* k = get_buffered_addr(klasses()->at(i));
794 if (k->is_instance_klass()) {
795 InstanceKlass::cast(k)->constants()->remove_unshareable_info();
796 }
797 }
798
799 for (int i = 0; i < klasses()->length(); i++) {
800 const char* type;
801 const char* unlinked = "";
802 const char* kind = "";
803 const char* hidden = "";
804 const char* old = "";
805 const char* generated = "";
806 const char* aotlinked_msg = "";
807 const char* inited_msg = "";
808 Klass* k = get_buffered_addr(klasses()->at(i));
809 bool inited = false;
810 k->remove_java_mirror();
811 #ifdef _LP64
812 if (UseCompactObjectHeaders) {
813 Klass* requested_k = to_requested(k);
814 address narrow_klass_base = _requested_static_archive_bottom; // runtime encoding base == runtime mapping start
815 const int narrow_klass_shift = precomputed_narrow_klass_shift();
816 narrowKlass nk = CompressedKlassPointers::encode_not_null_without_asserts(requested_k, narrow_klass_base, narrow_klass_shift);
817 k->set_prototype_header(markWord::prototype().set_narrow_klass(nk));
818 }
819 #endif //_LP64
820 if (k->is_objArray_klass()) {
821 // InstanceKlass and TypeArrayKlass will in turn call remove_unshareable_info
822 // on their array classes.
823 num_obj_array_klasses ++;
824 type = "array";
825 } else if (k->is_typeArray_klass()) {
826 num_type_array_klasses ++;
827 type = "array";
828 k->remove_unshareable_info();
829 } else {
830 assert(k->is_instance_klass(), " must be");
831 InstanceKlass* ik = InstanceKlass::cast(k);
832 InstanceKlass* src_ik = get_source_addr(ik);
833 bool aotlinked = AOTClassLinker::is_candidate(src_ik);
834 inited = ik->has_aot_initialized_mirror();
835 ADD_COUNT(num_instance_klasses);
836 if (CDSConfig::is_dumping_dynamic_archive()) {
837 // For static dump, class loader type are already set.
838 ik->assign_class_loader_type();
839 }
840 if (ik->is_hidden()) {
841 ADD_COUNT(num_hidden_klasses);
842 hidden = " hidden";
843 oop loader = k->class_loader();
844 if (loader == nullptr) {
845 type = "boot";
846 ADD_COUNT(num_boot_klasses);
847 } else if (loader == SystemDictionary::java_platform_loader()) {
848 type = "plat";
849 ADD_COUNT(num_platform_klasses);
850 } else if (loader == SystemDictionary::java_system_loader()) {
851 type = "app";
852 ADD_COUNT(num_app_klasses);
853 } else {
854 type = "bad";
855 assert(0, "shouldn't happen");
856 }
857 if (CDSConfig::is_dumping_method_handles()) {
858 assert(HeapShared::is_archivable_hidden_klass(ik), "sanity");
859 } else {
860 // Legacy CDS support for lambda proxies
861 CDS_JAVA_HEAP_ONLY(assert(HeapShared::is_lambda_proxy_klass(ik), "sanity");)
862 }
863 } else if (ik->is_shared_boot_class()) {
864 type = "boot";
865 ADD_COUNT(num_boot_klasses);
866 } else if (ik->is_shared_platform_class()) {
867 type = "plat";
868 ADD_COUNT(num_platform_klasses);
869 } else if (ik->is_shared_app_class()) {
870 type = "app";
871 ADD_COUNT(num_app_klasses);
872 } else {
873 assert(ik->is_shared_unregistered_class(), "must be");
874 type = "unreg";
875 ADD_COUNT(num_unregistered_klasses);
876 }
877
878 if (AOTClassLinker::is_vm_class(src_ik)) {
879 ADD_COUNT(num_vm_klasses);
880 }
881
882 if (!ik->is_linked()) {
883 num_unlinked_klasses ++;
884 unlinked = " unlinked";
885 if (ik->is_shared_boot_class()) {
886 boot_unlinked ++;
887 } else if (ik->is_shared_platform_class()) {
888 platform_unlinked ++;
889 } else if (ik->is_shared_app_class()) {
890 app_unlinked ++;
891 } else {
892 unreg_unlinked ++;
893 }
894 }
895
896 if (ik->is_interface()) {
897 kind = " interface";
898 } else if (src_ik->is_enum_subclass()) {
899 kind = " enum";
900 ADD_COUNT(num_enum_klasses);
901 }
902
903 if (!ik->can_be_verified_at_dumptime()) {
904 ADD_COUNT(num_old_klasses);
905 old = " old";
906 }
907
908 if (ik->is_generated_shared_class()) {
909 generated = " generated";
910 }
911 if (aotlinked) {
912 aotlinked_msg = " aot-linked";
913 }
914 if (inited) {
915 if (InstanceKlass::cast(k)->static_field_size() == 0) {
916 inited_msg = " inited (no static fields)";
917 } else {
918 inited_msg = " inited";
919 }
920 }
921
922 MetaspaceShared::rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread::current(), ik);
923 ik->remove_unshareable_info();
924 }
925
926 if (log_is_enabled(Debug, cds, class)) {
927 ResourceMark rm;
928 log_debug(cds, class)("klasses[%5d] = " PTR_FORMAT " %-5s %s%s%s%s%s%s%s%s", i,
929 p2i(to_requested(k)), type, k->external_name(),
930 kind, hidden, old, unlinked, generated, aotlinked_msg, inited_msg);
931 }
932 }
933
934 #define STATS_FORMAT "= %5d, aot-linked = %5d, inited = %5d"
935 #define STATS_PARAMS(x) num_ ## x, num_ ## x ## _a, num_ ## x ## _i
936
937 log_info(cds)("Number of classes %d", num_instance_klasses + num_obj_array_klasses + num_type_array_klasses);
938 log_info(cds)(" instance classes " STATS_FORMAT, STATS_PARAMS(instance_klasses));
939 log_info(cds)(" boot " STATS_FORMAT, STATS_PARAMS(boot_klasses));
940 log_info(cds)(" vm " STATS_FORMAT, STATS_PARAMS(vm_klasses));
941 log_info(cds)(" platform " STATS_FORMAT, STATS_PARAMS(platform_klasses));
942 log_info(cds)(" app " STATS_FORMAT, STATS_PARAMS(app_klasses));
943 log_info(cds)(" unregistered " STATS_FORMAT, STATS_PARAMS(unregistered_klasses));
944 log_info(cds)(" (enum) " STATS_FORMAT, STATS_PARAMS(enum_klasses));
945 log_info(cds)(" (hidden) " STATS_FORMAT, STATS_PARAMS(hidden_klasses));
946 log_info(cds)(" (old) " STATS_FORMAT, STATS_PARAMS(old_klasses));
947 log_info(cds)(" (unlinked) = %5d, boot = %d, plat = %d, app = %d, unreg = %d",
948 num_unlinked_klasses, boot_unlinked, platform_unlinked, app_unlinked, unreg_unlinked);
949 log_info(cds)(" obj array classes = %5d", num_obj_array_klasses);
950 log_info(cds)(" type array classes = %5d", num_type_array_klasses);
951 log_info(cds)(" symbols = %5d", _symbols->length());
952
953 #undef STATS_FORMAT
954 #undef STATS_PARAMS
955
956 DynamicArchive::make_array_klasses_shareable();
957 }
958
959 void ArchiveBuilder::serialize_dynamic_archivable_items(SerializeClosure* soc) {
960 SymbolTable::serialize_shared_table_header(soc, false);
961 SystemDictionaryShared::serialize_dictionary_headers(soc, false);
962 DynamicArchive::serialize_array_klasses(soc);
963 AOTLinkedClassBulkLoader::serialize(soc, false);
964 }
965
966 uintx ArchiveBuilder::buffer_to_offset(address p) const {
967 address requested_p = to_requested(p);
968 assert(requested_p >= _requested_static_archive_bottom, "must be");
969 return requested_p - _requested_static_archive_bottom;
970 }
971
972 uintx ArchiveBuilder::any_to_offset(address p) const {
973 if (is_in_mapped_static_archive(p)) {
974 assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
975 return p - _mapped_static_archive_bottom;
976 }
977 if (!is_in_buffer_space(p)) {
978 // p must be a "source" address
979 p = get_buffered_addr(p);
980 }
981 return buffer_to_offset(p);
982 }
983
984 address ArchiveBuilder::offset_to_buffered_address(u4 offset) const {
985 address requested_addr = _requested_static_archive_bottom + offset;
986 address buffered_addr = requested_addr - _buffer_to_requested_delta;
987 assert(is_in_buffer_space(buffered_addr), "bad offset");
988 return buffered_addr;
989 }
990
991 void ArchiveBuilder::start_ac_region() {
992 ro_region()->pack();
993 start_dump_region(&_ac_region);
994 }
995
996 void ArchiveBuilder::end_ac_region() {
997 _ac_region.pack();
998 }
999
1000 #if INCLUDE_CDS_JAVA_HEAP
1001 narrowKlass ArchiveBuilder::get_requested_narrow_klass(Klass* k) {
1002 assert(CDSConfig::is_dumping_heap(), "sanity");
1003 k = get_buffered_klass(k);
1004 Klass* requested_k = to_requested(k);
1005 const int narrow_klass_shift = ArchiveBuilder::precomputed_narrow_klass_shift();
1006 #ifdef ASSERT
1007 const size_t klass_alignment = MAX2(SharedSpaceObjectAlignment, (size_t)nth_bit(narrow_klass_shift));
1008 assert(is_aligned(k, klass_alignment), "Klass " PTR_FORMAT " misaligned.", p2i(k));
1009 #endif
1010 address narrow_klass_base = _requested_static_archive_bottom; // runtime encoding base == runtime mapping start
1011 // Note: use the "raw" version of encode that takes explicit narrow klass base and shift. Don't use any
1012 // of the variants that do sanity checks, nor any of those that use the current - dump - JVM's encoding setting.
1013 return CompressedKlassPointers::encode_not_null_without_asserts(requested_k, narrow_klass_base, narrow_klass_shift);
1014 }
1015 #endif // INCLUDE_CDS_JAVA_HEAP
1016
1017 // RelocateBufferToRequested --- Relocate all the pointers in rw/ro,
1018 // so that the archive can be mapped to the "requested" location without runtime relocation.
1019 //
1020 // - See ArchiveBuilder header for the definition of "buffer", "mapped" and "requested"
1021 // - ArchivePtrMarker::ptrmap() marks all the pointers in the rw/ro regions
1022 // - Every pointer must have one of the following values:
1023 // [a] nullptr:
1024 // No relocation is needed. Remove this pointer from ptrmap so we don't need to
1025 // consider it at runtime.
1026 // [b] Points into an object X which is inside the buffer:
1027 // Adjust this pointer by _buffer_to_requested_delta, so it points to X
1028 // when the archive is mapped at the requested location.
1029 // [c] Points into an object Y which is inside mapped static archive:
1030 // - This happens only during dynamic dump
1031 // - Adjust this pointer by _mapped_to_requested_static_archive_delta,
1032 // so it points to Y when the static archive is mapped at the requested location.
1033 template <bool STATIC_DUMP>
1034 class RelocateBufferToRequested : public BitMapClosure {
1035 ArchiveBuilder* _builder;
1036 address _buffer_bottom;
1037 intx _buffer_to_requested_delta;
1038 intx _mapped_to_requested_static_archive_delta;
1039 size_t _max_non_null_offset;
1040
1041 public:
1042 RelocateBufferToRequested(ArchiveBuilder* builder) {
1043 _builder = builder;
1044 _buffer_bottom = _builder->buffer_bottom();
1045 _buffer_to_requested_delta = builder->buffer_to_requested_delta();
1046 _mapped_to_requested_static_archive_delta = builder->requested_static_archive_bottom() - builder->mapped_static_archive_bottom();
1047 _max_non_null_offset = 0;
1048
1049 address bottom = _builder->buffer_bottom();
1050 address top = _builder->buffer_top();
1051 address new_bottom = bottom + _buffer_to_requested_delta;
1052 address new_top = top + _buffer_to_requested_delta;
1053 log_debug(cds)("Relocating archive from [" INTPTR_FORMAT " - " INTPTR_FORMAT "] to "
1054 "[" INTPTR_FORMAT " - " INTPTR_FORMAT "]",
1055 p2i(bottom), p2i(top),
1056 p2i(new_bottom), p2i(new_top));
1057 }
1058
1059 bool do_bit(size_t offset) {
1060 address* p = (address*)_buffer_bottom + offset;
1061 assert(_builder->is_in_buffer_space(p), "pointer must live in buffer space");
1062
1063 if (*p == nullptr) {
1064 // todo -- clear bit, etc
1065 ArchivePtrMarker::ptrmap()->clear_bit(offset);
1066 } else {
1067 if (STATIC_DUMP) {
1068 assert(_builder->is_in_buffer_space(*p), "old pointer must point inside buffer space");
1069 *p += _buffer_to_requested_delta;
1070 assert(_builder->is_in_requested_static_archive(*p), "new pointer must point inside requested archive");
1071 } else {
1072 if (_builder->is_in_buffer_space(*p)) {
1073 *p += _buffer_to_requested_delta;
1074 // assert is in requested dynamic archive
1075 } else {
1076 assert(_builder->is_in_mapped_static_archive(*p), "old pointer must point inside buffer space or mapped static archive");
1077 *p += _mapped_to_requested_static_archive_delta;
1078 assert(_builder->is_in_requested_static_archive(*p), "new pointer must point inside requested archive");
1079 }
1080 }
1081 _max_non_null_offset = offset;
1082 }
1083
1084 return true; // keep iterating
1085 }
1086
1087 void doit() {
1088 ArchivePtrMarker::ptrmap()->iterate(this);
1089 ArchivePtrMarker::compact(_max_non_null_offset);
1090 }
1091 };
1092
1093 #ifdef _LP64
1094 int ArchiveBuilder::precomputed_narrow_klass_shift() {
1095 // Legacy Mode:
1096 // We use 32 bits for narrowKlass, which should cover the full 4G Klass range. Shift can be 0.
1097 // CompactObjectHeader Mode:
1098 // narrowKlass is much smaller, and we use the highest possible shift value to later get the maximum
1099 // Klass encoding range.
1100 //
1101 // Note that all of this may change in the future, if we decide to correct the pre-calculated
1102 // narrow Klass IDs at archive load time.
1103 assert(UseCompressedClassPointers, "Only needed for compressed class pointers");
1104 return UseCompactObjectHeaders ? CompressedKlassPointers::max_shift() : 0;
1105 }
1106 #endif // _LP64
1107
1108 void ArchiveBuilder::relocate_to_requested() {
1109 if (!ro_region()->is_packed()) {
1110 ro_region()->pack();
1111 }
1112 size_t my_archive_size = buffer_top() - buffer_bottom();
1113
1114 if (CDSConfig::is_dumping_static_archive()) {
1115 _requested_static_archive_top = _requested_static_archive_bottom + my_archive_size;
1116 RelocateBufferToRequested<true> patcher(this);
1117 patcher.doit();
1118 } else {
1119 assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
1120 _requested_dynamic_archive_top = _requested_dynamic_archive_bottom + my_archive_size;
1121 RelocateBufferToRequested<false> patcher(this);
1122 patcher.doit();
1123 }
1124 }
1125
1126 // Write detailed info to a mapfile to analyze contents of the archive.
1127 // static dump:
1128 // java -Xshare:dump -Xlog:cds+map=trace:file=cds.map:none:filesize=0
1129 // dynamic dump:
1130 // java -cp MyApp.jar -XX:ArchiveClassesAtExit=MyApp.jsa \
1131 // -Xlog:cds+map=trace:file=cds.map:none:filesize=0 MyApp
1132 //
1133 // We need to do some address translation because the buffers used at dump time may be mapped to
1134 // a different location at runtime. At dump time, the buffers may be at arbitrary locations
1135 // picked by the OS. At runtime, we try to map at a fixed location (SharedBaseAddress). For
1136 // consistency, we log everything using runtime addresses.
1137 class ArchiveBuilder::CDSMapLogger : AllStatic {
1138 static intx buffer_to_runtime_delta() {
1139 // Translate the buffers used by the RW/RO regions to their eventual (requested) locations
1140 // at runtime.
1141 return ArchiveBuilder::current()->buffer_to_requested_delta();
1142 }
1143
1144 // rw/ro regions only
1145 static void log_metaspace_region(const char* name, DumpRegion* region,
1146 const ArchiveBuilder::SourceObjList* src_objs) {
1147 address region_base = address(region->base());
1148 address region_top = address(region->top());
1149 log_region(name, region_base, region_top, region_base + buffer_to_runtime_delta());
1150 log_metaspace_objects(region, src_objs);
1151 }
1152
1153 #define _LOG_PREFIX PTR_FORMAT ": @@ %-17s %d"
1154
1155 static void log_klass(Klass* k, address runtime_dest, const char* type_name, int bytes, Thread* current) {
1156 ResourceMark rm(current);
1157 log_debug(cds, map)(_LOG_PREFIX " %s",
1158 p2i(runtime_dest), type_name, bytes, k->external_name());
1159 }
1160 static void log_method(Method* m, address runtime_dest, const char* type_name, int bytes, Thread* current) {
1161 ResourceMark rm(current);
1162 log_debug(cds, map)(_LOG_PREFIX " %s",
1163 p2i(runtime_dest), type_name, bytes, m->external_name());
1164 }
1165
1166 // rw/ro regions only
1167 static void log_metaspace_objects(DumpRegion* region, const ArchiveBuilder::SourceObjList* src_objs) {
1168 address last_obj_base = address(region->base());
1169 address last_obj_end = address(region->base());
1170 address region_end = address(region->end());
1171 Thread* current = Thread::current();
1172 for (int i = 0; i < src_objs->objs()->length(); i++) {
1173 SourceObjInfo* src_info = src_objs->at(i);
1174 address src = src_info->source_addr();
1175 address dest = src_info->buffered_addr();
1176 log_as_hex(last_obj_base, dest, last_obj_base + buffer_to_runtime_delta());
1177 address runtime_dest = dest + buffer_to_runtime_delta();
1178 int bytes = src_info->size_in_bytes();
1179
1180 MetaspaceObj::Type type = src_info->msotype();
1181 const char* type_name = MetaspaceObj::type_name(type);
1182
1183 switch (type) {
1184 case MetaspaceObj::ClassType:
1185 log_klass((Klass*)src, runtime_dest, type_name, bytes, current);
1186 break;
1187 case MetaspaceObj::ConstantPoolType:
1188 log_klass(((ConstantPool*)src)->pool_holder(),
1189 runtime_dest, type_name, bytes, current);
1190 break;
1191 case MetaspaceObj::ConstantPoolCacheType:
1192 log_klass(((ConstantPoolCache*)src)->constant_pool()->pool_holder(),
1193 runtime_dest, type_name, bytes, current);
1194 break;
1195 case MetaspaceObj::MethodType:
1196 log_method((Method*)src, runtime_dest, type_name, bytes, current);
1197 break;
1198 case MetaspaceObj::ConstMethodType:
1199 log_method(((ConstMethod*)src)->method(), runtime_dest, type_name, bytes, current);
1200 break;
1201 case MetaspaceObj::SymbolType:
1202 {
1203 ResourceMark rm(current);
1204 Symbol* s = (Symbol*)src;
1205 log_debug(cds, map)(_LOG_PREFIX " %s", p2i(runtime_dest), type_name, bytes,
1206 s->as_quoted_ascii());
1207 }
1208 break;
1209 default:
1210 log_debug(cds, map)(_LOG_PREFIX, p2i(runtime_dest), type_name, bytes);
1211 break;
1212 }
1213
1214 last_obj_base = dest;
1215 last_obj_end = dest + bytes;
1216 }
1217
1218 log_as_hex(last_obj_base, last_obj_end, last_obj_base + buffer_to_runtime_delta());
1219 if (last_obj_end < region_end) {
1220 log_debug(cds, map)(PTR_FORMAT ": @@ Misc data %zu bytes",
1221 p2i(last_obj_end + buffer_to_runtime_delta()),
1222 size_t(region_end - last_obj_end));
1223 log_as_hex(last_obj_end, region_end, last_obj_end + buffer_to_runtime_delta());
1224 }
1225 }
1226
1227 #undef _LOG_PREFIX
1228
1229 // Log information about a region, whose address at dump time is [base .. top). At
1230 // runtime, this region will be mapped to requested_base. requested_base is nullptr if this
1231 // region will be mapped at os-selected addresses (such as the bitmap region), or will
1232 // be accessed with os::read (the header).
1233 //
1234 // Note: across -Xshare:dump runs, base may be different, but requested_base should
1235 // be the same as the archive contents should be deterministic.
1236 static void log_region(const char* name, address base, address top, address requested_base) {
1237 size_t size = top - base;
1238 base = requested_base;
1239 if (requested_base == nullptr) {
1240 top = (address)size;
1241 } else {
1242 top = requested_base + size;
1243 }
1244 log_info(cds, map)("[%-18s " PTR_FORMAT " - " PTR_FORMAT " %9zu bytes]",
1245 name, p2i(base), p2i(top), size);
1246 }
1247
1248 #if INCLUDE_CDS_JAVA_HEAP
1249 static void log_heap_region(ArchiveHeapInfo* heap_info) {
1250 MemRegion r = heap_info->buffer_region();
1251 address start = address(r.start()); // start of the current oop inside the buffer
1252 address end = address(r.end());
1253 log_region("heap", start, end, ArchiveHeapWriter::buffered_addr_to_requested_addr(start));
1254
1255 LogStreamHandle(Info, cds, map) st;
1256
1257 HeapRootSegments segments = heap_info->heap_root_segments();
1258 assert(segments.base_offset() == 0, "Sanity");
1259
1260 for (size_t seg_idx = 0; seg_idx < segments.count(); seg_idx++) {
1261 address requested_start = ArchiveHeapWriter::buffered_addr_to_requested_addr(start);
1262 st.print_cr(PTR_FORMAT ": Heap roots segment [%d]",
1263 p2i(requested_start), segments.size_in_elems(seg_idx));
1264 start += segments.size_in_bytes(seg_idx);
1265 }
1266 log_heap_roots();
1267
1268 while (start < end) {
1269 size_t byte_size;
1270 oop source_oop = ArchiveHeapWriter::buffered_addr_to_source_obj(start);
1271 address requested_start = ArchiveHeapWriter::buffered_addr_to_requested_addr(start);
1272 st.print(PTR_FORMAT ": @@ Object ", p2i(requested_start));
1273
1274 if (source_oop != nullptr) {
1275 // This is a regular oop that got archived.
1276 // Don't print the requested addr again as we have just printed it at the beginning of the line.
1277 // Example:
1278 // 0x00000007ffd27938: @@ Object (0xfffa4f27) java.util.HashMap
1279 print_oop_info_cr(&st, source_oop, /*print_requested_addr=*/false);
1280 byte_size = source_oop->size() * BytesPerWord;
1281 } else if ((byte_size = ArchiveHeapWriter::get_filler_size_at(start)) > 0) {
1282 // We have a filler oop, which also does not exist in BufferOffsetToSourceObjectTable.
1283 // Example:
1284 // 0x00000007ffc3ffd8: @@ Object filler 40 bytes
1285 st.print_cr("filler %zu bytes", byte_size);
1286 } else {
1287 ShouldNotReachHere();
1288 }
1289
1290 address oop_end = start + byte_size;
1291 log_as_hex(start, oop_end, requested_start, /*is_heap=*/true);
1292
1293 if (source_oop != nullptr) {
1294 log_oop_details(heap_info, source_oop, /*buffered_addr=*/start);
1295 }
1296 start = oop_end;
1297 }
1298 }
1299
1300 // ArchivedFieldPrinter is used to print the fields of archived objects. We can't
1301 // use _source_obj->print_on(), because we want to print the oop fields
1302 // in _source_obj with their requested addresses using print_oop_info_cr().
1303 class ArchivedFieldPrinter : public FieldClosure {
1304 ArchiveHeapInfo* _heap_info;
1305 outputStream* _st;
1306 oop _source_obj;
1307 address _buffered_addr;
1308 public:
1309 ArchivedFieldPrinter(ArchiveHeapInfo* heap_info, outputStream* st, oop src_obj, address buffered_addr) :
1310 _heap_info(heap_info), _st(st), _source_obj(src_obj), _buffered_addr(buffered_addr) {}
1311
1312 void do_field(fieldDescriptor* fd) {
1313 _st->print(" - ");
1314 BasicType ft = fd->field_type();
1315 switch (ft) {
1316 case T_ARRAY:
1317 case T_OBJECT:
1318 {
1319 fd->print_on(_st); // print just the name and offset
1320 oop obj = _source_obj->obj_field(fd->offset());
1321 if (java_lang_Class::is_instance(obj)) {
1322 obj = HeapShared::scratch_java_mirror(obj);
1323 }
1324 print_oop_info_cr(_st, obj);
1325 }
1326 break;
1327 default:
1328 if (ArchiveHeapWriter::is_marked_as_native_pointer(_heap_info, _source_obj, fd->offset())) {
1329 print_as_native_pointer(fd);
1330 } else {
1331 fd->print_on_for(_st, cast_to_oop(_buffered_addr)); // name, offset, value
1332 _st->cr();
1333 }
1334 }
1335 }
1336
1337 void print_as_native_pointer(fieldDescriptor* fd) {
1338 LP64_ONLY(assert(fd->field_type() == T_LONG, "must be"));
1339 NOT_LP64 (assert(fd->field_type() == T_INT, "must be"));
1340
1341 // We have a field that looks like an integer, but it's actually a pointer to a MetaspaceObj.
1342 address source_native_ptr = (address)
1343 LP64_ONLY(_source_obj->long_field(fd->offset()))
1344 NOT_LP64( _source_obj->int_field (fd->offset()));
1345 ArchiveBuilder* builder = ArchiveBuilder::current();
1346
1347 // The value of the native pointer at runtime.
1348 address requested_native_ptr = builder->to_requested(builder->get_buffered_addr(source_native_ptr));
1349
1350 // The address of _source_obj at runtime
1351 oop requested_obj = ArchiveHeapWriter::source_obj_to_requested_obj(_source_obj);
1352 // The address of this field in the requested space
1353 assert(requested_obj != nullptr, "Attempting to load field from null oop");
1354 address requested_field_addr = cast_from_oop<address>(requested_obj) + fd->offset();
1355
1356 fd->print_on(_st);
1357 _st->print_cr(PTR_FORMAT " (marked metadata pointer @" PTR_FORMAT " )",
1358 p2i(requested_native_ptr), p2i(requested_field_addr));
1359 }
1360 };
1361
1362 // Print the fields of instanceOops, or the elements of arrayOops
1363 static void log_oop_details(ArchiveHeapInfo* heap_info, oop source_oop, address buffered_addr) {
1364 LogStreamHandle(Trace, cds, map, oops) st;
1365 if (st.is_enabled()) {
1366 Klass* source_klass = source_oop->klass();
1367 ArchiveBuilder* builder = ArchiveBuilder::current();
1368 Klass* requested_klass = builder->to_requested(builder->get_buffered_addr(source_klass));
1369
1370 st.print(" - klass: ");
1371 source_klass->print_value_on(&st);
1372 st.print(" " PTR_FORMAT, p2i(requested_klass));
1373 st.cr();
1374
1375 if (source_oop->is_typeArray()) {
1376 TypeArrayKlass::cast(source_klass)->oop_print_elements_on(typeArrayOop(source_oop), &st);
1377 } else if (source_oop->is_objArray()) {
1378 objArrayOop source_obj_array = objArrayOop(source_oop);
1379 for (int i = 0; i < source_obj_array->length(); i++) {
1380 st.print(" -%4d: ", i);
1381 oop obj = source_obj_array->obj_at(i);
1382 if (java_lang_Class::is_instance(obj)) {
1383 obj = HeapShared::scratch_java_mirror(obj);
1384 }
1385 print_oop_info_cr(&st, obj);
1386 }
1387 } else {
1388 st.print_cr(" - fields (%zu words):", source_oop->size());
1389 ArchivedFieldPrinter print_field(heap_info, &st, source_oop, buffered_addr);
1390 InstanceKlass::cast(source_klass)->print_nonstatic_fields(&print_field);
1391
1392 if (java_lang_Class::is_instance(source_oop)) {
1393 oop scratch_mirror = source_oop;
1394 st.print(" - signature: ");
1395 print_class_signature_for_mirror(&st, scratch_mirror);
1396 st.cr();
1397
1398 Klass* src_klass = java_lang_Class::as_Klass(scratch_mirror);
1399 if (src_klass != nullptr && src_klass->is_instance_klass()) {
1400 oop rr = HeapShared::scratch_resolved_references(InstanceKlass::cast(src_klass)->constants());
1401 st.print(" - archived_resolved_references: ");
1402 print_oop_info_cr(&st, rr);
1403
1404 // We need to print the fields in the scratch_mirror, not the original mirror.
1405 // (if a class is not aot-initialized, static fields in its scratch mirror will be cleared).
1406 assert(scratch_mirror == HeapShared::scratch_java_mirror(src_klass->java_mirror()), "sanity");
1407 st.print_cr("- ---- static fields (%d):", java_lang_Class::static_oop_field_count(scratch_mirror));
1408 InstanceKlass::cast(src_klass)->do_local_static_fields(&print_field);
1409 }
1410 }
1411 }
1412 }
1413 }
1414
1415 static void print_class_signature_for_mirror(outputStream* st, oop scratch_mirror) {
1416 assert(java_lang_Class::is_instance(scratch_mirror), "sanity");
1417 if (java_lang_Class::is_primitive(scratch_mirror)) {
1418 for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
1419 BasicType bt = (BasicType)i;
1420 if (!is_reference_type(bt) && scratch_mirror == HeapShared::scratch_java_mirror(bt)) {
1421 oop orig_mirror = Universe::java_mirror(bt);
1422 java_lang_Class::print_signature(orig_mirror, st);
1423 return;
1424 }
1425 }
1426 ShouldNotReachHere();
1427 }
1428 java_lang_Class::print_signature(scratch_mirror, st);
1429 }
1430
1431 static void log_heap_roots() {
1432 LogStreamHandle(Trace, cds, map, oops) st;
1433 if (st.is_enabled()) {
1434 for (int i = 0; i < HeapShared::pending_roots()->length(); i++) {
1435 st.print("roots[%4d]: ", i);
1436 print_oop_info_cr(&st, HeapShared::pending_roots()->at(i));
1437 }
1438 }
1439 }
1440
1441 // Example output:
1442 // - The first number is the requested address (if print_requested_addr == true)
1443 // - The second number is the narrowOop version of the requested address (if UseCompressedOops == true)
1444 // 0x00000007ffc7e840 (0xfff8fd08) java.lang.Class Ljava/util/Array;
1445 // 0x00000007ffc000f8 (0xfff8001f) [B length: 11
1446 static void print_oop_info_cr(outputStream* st, oop source_oop, bool print_requested_addr = true) {
1447 if (source_oop == nullptr) {
1448 st->print_cr("null");
1449 } else {
1450 ResourceMark rm;
1451 oop requested_obj = ArchiveHeapWriter::source_obj_to_requested_obj(source_oop);
1452 if (print_requested_addr) {
1453 st->print(PTR_FORMAT " ", p2i(requested_obj));
1454 }
1455 if (UseCompressedOops) {
1456 st->print("(0x%08x) ", CompressedOops::narrow_oop_value(requested_obj));
1457 }
1458 if (source_oop->is_array()) {
1459 int array_len = arrayOop(source_oop)->length();
1460 st->print_cr("%s length: %d", source_oop->klass()->external_name(), array_len);
1461 } else {
1462 st->print("%s", source_oop->klass()->external_name());
1463
1464 if (java_lang_String::is_instance(source_oop)) {
1465 st->print(" ");
1466 java_lang_String::print(source_oop, st);
1467 } else if (java_lang_Class::is_instance(source_oop)) {
1468 oop scratch_mirror = source_oop;
1469
1470 st->print(" ");
1471 print_class_signature_for_mirror(st, scratch_mirror);
1472
1473 Klass* src_klass = java_lang_Class::as_Klass(scratch_mirror);
1474 if (src_klass != nullptr && src_klass->is_instance_klass()) {
1475 InstanceKlass* buffered_klass =
1476 ArchiveBuilder::current()->get_buffered_addr(InstanceKlass::cast(src_klass));
1477 if (buffered_klass->has_aot_initialized_mirror()) {
1478 st->print(" (aot-inited)");
1479 }
1480 }
1481 }
1482 st->cr();
1483 }
1484 }
1485 }
1486 #endif // INCLUDE_CDS_JAVA_HEAP
1487
1488 // Log all the data [base...top). Pretend that the base address
1489 // will be mapped to requested_base at run-time.
1490 static void log_as_hex(address base, address top, address requested_base, bool is_heap = false) {
1491 assert(top >= base, "must be");
1492
1493 LogStreamHandle(Trace, cds, map) lsh;
1494 if (lsh.is_enabled()) {
1495 int unitsize = sizeof(address);
1496 if (is_heap && UseCompressedOops) {
1497 // This makes the compressed oop pointers easier to read, but
1498 // longs and doubles will be split into two words.
1499 unitsize = sizeof(narrowOop);
1500 }
1501 os::print_hex_dump(&lsh, base, top, unitsize, /* print_ascii=*/true, /* bytes_per_line=*/32, requested_base);
1502 }
1503 }
1504
1505 static void log_header(FileMapInfo* mapinfo) {
1506 LogStreamHandle(Info, cds, map) lsh;
1507 if (lsh.is_enabled()) {
1508 mapinfo->print(&lsh);
1509 }
1510 }
1511
1512 public:
1513 static void log(ArchiveBuilder* builder, FileMapInfo* mapinfo,
1514 ArchiveHeapInfo* heap_info,
1515 char* bitmap, size_t bitmap_size_in_bytes) {
1516 log_info(cds, map)("%s CDS archive map for %s", CDSConfig::is_dumping_static_archive() ? "Static" : "Dynamic", mapinfo->full_path());
1517
1518 address header = address(mapinfo->header());
1519 address header_end = header + mapinfo->header()->header_size();
1520 log_region("header", header, header_end, nullptr);
1521 log_header(mapinfo);
1522 log_as_hex(header, header_end, nullptr);
1523
1524 DumpRegion* rw_region = &builder->_rw_region;
1525 DumpRegion* ro_region = &builder->_ro_region;
1526
1527 log_metaspace_region("rw region", rw_region, &builder->_rw_src_objs);
1528 log_metaspace_region("ro region", ro_region, &builder->_ro_src_objs);
1529
1530 address bitmap_end = address(bitmap + bitmap_size_in_bytes);
1531 log_region("bitmap", address(bitmap), bitmap_end, nullptr);
1532 log_as_hex((address)bitmap, bitmap_end, nullptr);
1533
1534 #if INCLUDE_CDS_JAVA_HEAP
1535 if (heap_info->is_used()) {
1536 log_heap_region(heap_info);
1537 }
1538 #endif
1539
1540 log_info(cds, map)("[End of CDS archive map]");
1541 }
1542 }; // end ArchiveBuilder::CDSMapLogger
1543
1544 void ArchiveBuilder::print_stats() {
1545 _alloc_stats.print_stats(int(_ro_region.used()), int(_rw_region.used()));
1546 }
1547
1548 void ArchiveBuilder::write_archive(FileMapInfo* mapinfo, ArchiveHeapInfo* heap_info) {
1549 // Make sure NUM_CDS_REGIONS (exported in cds.h) agrees with
1550 // MetaspaceShared::n_regions (internal to hotspot).
1551 assert(NUM_CDS_REGIONS == MetaspaceShared::n_regions, "sanity");
1552
1553 write_region(mapinfo, MetaspaceShared::rw, &_rw_region, /*read_only=*/false,/*allow_exec=*/false);
1554 write_region(mapinfo, MetaspaceShared::ro, &_ro_region, /*read_only=*/true, /*allow_exec=*/false);
1555 write_region(mapinfo, MetaspaceShared::ac, &_ac_region, /*read_only=*/false,/*allow_exec=*/false);
1556
1557 // Split pointer map into read-write and read-only bitmaps
1558 ArchivePtrMarker::initialize_rw_ro_maps(&_rw_ptrmap, &_ro_ptrmap);
1559
1560 size_t bitmap_size_in_bytes;
1561 char* bitmap = mapinfo->write_bitmap_region(ArchivePtrMarker::rw_ptrmap(), ArchivePtrMarker::ro_ptrmap(), heap_info,
1562 bitmap_size_in_bytes);
1563
1564 if (heap_info->is_used()) {
1565 _total_heap_region_size = mapinfo->write_heap_region(heap_info);
1566 }
1567
1568 print_region_stats(mapinfo, heap_info);
1569
1570 mapinfo->set_requested_base((char*)MetaspaceShared::requested_base_address());
1571 mapinfo->set_header_crc(mapinfo->compute_header_crc());
1572 // After this point, we should not write any data into mapinfo->header() since this
1573 // would corrupt its checksum we have calculated before.
1574 mapinfo->write_header();
1575 mapinfo->close();
1576
1577 if (log_is_enabled(Info, cds)) {
1578 log_info(cds)("Full module graph = %s", CDSConfig::is_dumping_full_module_graph() ? "enabled" : "disabled");
1579 print_stats();
1580 }
1581
1582 if (log_is_enabled(Info, cds, map)) {
1583 CDSMapLogger::log(this, mapinfo, heap_info,
1584 bitmap, bitmap_size_in_bytes);
1585 }
1586 CDS_JAVA_HEAP_ONLY(HeapShared::destroy_archived_object_cache());
1587 FREE_C_HEAP_ARRAY(char, bitmap);
1588 }
1589
1590 void ArchiveBuilder::write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region, bool read_only, bool allow_exec) {
1591 mapinfo->write_region(region_idx, dump_region->base(), dump_region->used(), read_only, allow_exec);
1592 }
1593
1594 void ArchiveBuilder::print_region_stats(FileMapInfo *mapinfo, ArchiveHeapInfo* heap_info) {
1595 // Print statistics of all the regions
1596 const size_t bitmap_used = mapinfo->region_at(MetaspaceShared::bm)->used();
1597 const size_t bitmap_reserved = mapinfo->region_at(MetaspaceShared::bm)->used_aligned();
1598 const size_t total_reserved = _ro_region.reserved() + _rw_region.reserved() +
1599 bitmap_reserved +
1600 _total_heap_region_size;
1601 const size_t total_bytes = _ro_region.used() + _rw_region.used() +
1602 bitmap_used +
1603 _total_heap_region_size;
1604 const double total_u_perc = percent_of(total_bytes, total_reserved);
1605
1606 _rw_region.print(total_reserved);
1607 _ro_region.print(total_reserved);
1608 _ac_region.print(total_reserved);
1609
1610 print_bitmap_region_stats(bitmap_used, total_reserved);
1611
1612 if (heap_info->is_used()) {
1613 print_heap_region_stats(heap_info, total_reserved);
1614 }
1615
1616 log_debug(cds)("total : %9zu [100.0%% of total] out of %9zu bytes [%5.1f%% used]",
1617 total_bytes, total_reserved, total_u_perc);
1618 }
1619
1620 void ArchiveBuilder::print_bitmap_region_stats(size_t size, size_t total_size) {
1621 log_debug(cds)("bm space: %9zu [ %4.1f%% of total] out of %9zu bytes [100.0%% used]",
1622 size, size/double(total_size)*100.0, size);
1623 }
1624
1625 void ArchiveBuilder::print_heap_region_stats(ArchiveHeapInfo *info, size_t total_size) {
1626 char* start = info->buffer_start();
1627 size_t size = info->buffer_byte_size();
1628 char* top = start + size;
1629 log_debug(cds)("hp space: %9zu [ %4.1f%% of total] out of %9zu bytes [100.0%% used] at " INTPTR_FORMAT,
1630 size, size/double(total_size)*100.0, size, p2i(start));
1631 }
1632
1633 void ArchiveBuilder::report_out_of_space(const char* name, size_t needed_bytes) {
1634 // This is highly unlikely to happen on 64-bits because we have reserved a 4GB space.
1635 // On 32-bit we reserve only 256MB so you could run out of space with 100,000 classes
1636 // or so.
1637 _rw_region.print_out_of_space_msg(name, needed_bytes);
1638 _ro_region.print_out_of_space_msg(name, needed_bytes);
1639
1640 log_error(cds)("Unable to allocate from '%s' region: Please reduce the number of shared classes.", name);
1641 MetaspaceShared::unrecoverable_writing_error();
1642 }
--- EOF ---