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/archiveHeapWriter.hpp"
26 #include "cds/cdsConfig.hpp"
27 #include "cds/filemap.hpp"
28 #include "cds/heapShared.hpp"
29 #include "classfile/javaClasses.hpp"
30 #include "classfile/modules.hpp"
31 #include "classfile/systemDictionary.hpp"
32 #include "gc/shared/collectedHeap.hpp"
33 #include "memory/iterator.inline.hpp"
34 #include "memory/oopFactory.hpp"
35 #include "memory/universe.hpp"
36 #include "oops/compressedOops.hpp"
37 #include "oops/objArrayOop.inline.hpp"
38 #include "oops/oop.inline.hpp"
39 #include "oops/oopHandle.inline.hpp"
40 #include "oops/typeArrayKlass.hpp"
41 #include "oops/typeArrayOop.hpp"
42 #include "runtime/java.hpp"
43 #include "runtime/mutexLocker.hpp"
44 #include "utilities/bitMap.inline.hpp"
45 #if INCLUDE_G1GC
46 #include "gc/g1/g1CollectedHeap.hpp"
47 #include "gc/g1/g1HeapRegion.hpp"
48 #endif
49
50 #if INCLUDE_CDS_JAVA_HEAP
51
52 GrowableArrayCHeap<u1, mtClassShared>* ArchiveHeapWriter::_buffer = nullptr;
53
54 // The following are offsets from buffer_bottom()
55 size_t ArchiveHeapWriter::_buffer_used;
56
57 // Heap root segments
58 HeapRootSegments ArchiveHeapWriter::_heap_root_segments;
59
60 address ArchiveHeapWriter::_requested_bottom;
61 address ArchiveHeapWriter::_requested_top;
62
63 GrowableArrayCHeap<ArchiveHeapWriter::NativePointerInfo, mtClassShared>* ArchiveHeapWriter::_native_pointers;
64 GrowableArrayCHeap<oop, mtClassShared>* ArchiveHeapWriter::_source_objs;
65 GrowableArrayCHeap<ArchiveHeapWriter::HeapObjOrder, mtClassShared>* ArchiveHeapWriter::_source_objs_order;
66
67 ArchiveHeapWriter::BufferOffsetToSourceObjectTable*
68 ArchiveHeapWriter::_buffer_offset_to_source_obj_table = nullptr;
69
70
71 typedef ResourceHashtable<
72 size_t, // offset of a filler from ArchiveHeapWriter::buffer_bottom()
73 size_t, // size of this filler (in bytes)
74 127, // prime number
75 AnyObj::C_HEAP,
76 mtClassShared> FillersTable;
77 static FillersTable* _fillers;
78 static int _num_native_ptrs = 0;
79
80 void ArchiveHeapWriter::init() {
81 if (CDSConfig::is_dumping_heap()) {
82 Universe::heap()->collect(GCCause::_java_lang_system_gc);
303 _source_objs_order->append(os);
304 }
305 log_info(cds)("computed ranks");
306 _source_objs_order->sort(compare_objs_by_oop_fields);
307 log_info(cds)("sorting heap objects done");
308 }
309
310 void ArchiveHeapWriter::copy_source_objs_to_buffer(GrowableArrayCHeap<oop, mtClassShared>* roots) {
311 // There could be multiple root segments, which we want to be aligned by region.
312 // Putting them ahead of objects makes sure we waste no space.
313 copy_roots_to_buffer(roots);
314
315 sort_source_objs();
316 for (int i = 0; i < _source_objs_order->length(); i++) {
317 int src_obj_index = _source_objs_order->at(i)._index;
318 oop src_obj = _source_objs->at(src_obj_index);
319 HeapShared::CachedOopInfo* info = HeapShared::archived_object_cache()->get(src_obj);
320 assert(info != nullptr, "must be");
321 size_t buffer_offset = copy_one_source_obj_to_buffer(src_obj);
322 info->set_buffer_offset(buffer_offset);
323
324 _buffer_offset_to_source_obj_table->put_when_absent(buffer_offset, src_obj);
325 _buffer_offset_to_source_obj_table->maybe_grow();
326
327 if (java_lang_Module::is_instance(src_obj)) {
328 Modules::check_archived_module_oop(src_obj);
329 }
330 }
331
332 log_info(cds)("Size of heap region = %zu bytes, %d objects, %d roots, %d native ptrs",
333 _buffer_used, _source_objs->length() + 1, roots->length(), _num_native_ptrs);
334 }
335
336 size_t ArchiveHeapWriter::filler_array_byte_size(int length) {
337 size_t byte_size = objArrayOopDesc::object_size(length) * HeapWordSize;
338 return byte_size;
339 }
340
341 int ArchiveHeapWriter::filler_array_length(size_t fill_bytes) {
342 assert(is_object_aligned(fill_bytes), "must be");
343 size_t elemSize = (UseCompressedOops ? sizeof(narrowOop) : sizeof(oop));
344
345 int initial_length = to_array_length(fill_bytes / elemSize);
346 for (int length = initial_length; length >= 0; length --) {
347 size_t array_byte_size = filler_array_byte_size(length);
348 if (array_byte_size == fill_bytes) {
349 return length;
350 }
351 }
352
353 ShouldNotReachHere();
402 _fillers->put(buffered_address_to_offset((address)filler), fill_bytes);
403 }
404 }
405
406 size_t ArchiveHeapWriter::get_filler_size_at(address buffered_addr) {
407 size_t* p = _fillers->get(buffered_address_to_offset(buffered_addr));
408 if (p != nullptr) {
409 assert(*p > 0, "filler must be larger than zero bytes");
410 return *p;
411 } else {
412 return 0; // buffered_addr is not a filler
413 }
414 }
415
416 template <typename T>
417 void update_buffered_object_field(address buffered_obj, int field_offset, T value) {
418 T* field_addr = cast_to_oop(buffered_obj)->field_addr<T>(field_offset);
419 *field_addr = value;
420 }
421
422 size_t ArchiveHeapWriter::copy_one_source_obj_to_buffer(oop src_obj) {
423 assert(!is_too_large_to_archive(src_obj), "already checked");
424 size_t byte_size = src_obj->size() * HeapWordSize;
425 assert(byte_size > 0, "no zero-size objects");
426
427 // For region-based collectors such as G1, the archive heap may be mapped into
428 // multiple regions. We need to make sure that we don't have an object that can possible
429 // span across two regions.
430 maybe_fill_gc_region_gap(byte_size);
431
432 size_t new_used = _buffer_used + byte_size;
433 assert(new_used > _buffer_used, "no wrap around");
434
435 size_t cur_min_region_bottom = align_down(_buffer_used, MIN_GC_REGION_ALIGNMENT);
436 size_t next_min_region_bottom = align_down(new_used, MIN_GC_REGION_ALIGNMENT);
437 assert(cur_min_region_bottom == next_min_region_bottom, "no object should cross minimal GC region boundaries");
438
439 ensure_buffer_space(new_used);
440
441 address from = cast_from_oop<address>(src_obj);
442 address to = offset_to_buffered_address<address>(_buffer_used);
730 // requested_field_addr = the address of this field in the requested space
731 oop requested_obj = requested_obj_from_buffer_offset(p->buffer_offset());
732 Metadata** requested_field_addr = (Metadata**)(cast_from_oop<address>(requested_obj) + field_offset);
733 assert(bottom <= requested_field_addr && requested_field_addr < top, "range check");
734
735 // Mark this field in the bitmap
736 BitMap::idx_t idx = requested_field_addr - bottom;
737 heap_info->ptrmap()->set_bit(idx);
738 num_non_null_ptrs ++;
739 max_idx = MAX2(max_idx, idx);
740
741 // Set the native pointer to the requested address of the metadata (at runtime, the metadata will have
742 // this address if the RO/RW regions are mapped at the default location).
743
744 Metadata** buffered_field_addr = requested_addr_to_buffered_addr(requested_field_addr);
745 Metadata* native_ptr = *buffered_field_addr;
746 guarantee(native_ptr != nullptr, "sanity");
747 guarantee(ArchiveBuilder::current()->has_been_buffered((address)native_ptr),
748 "Metadata %p should have been archived", native_ptr);
749
750 address buffered_native_ptr = ArchiveBuilder::current()->get_buffered_addr((address)native_ptr);
751 address requested_native_ptr = ArchiveBuilder::current()->to_requested(buffered_native_ptr);
752 *buffered_field_addr = (Metadata*)requested_native_ptr;
753 }
754
755 heap_info->ptrmap()->resize(max_idx + 1);
756 log_info(cds, heap)("calculate_ptrmap: marked %d non-null native pointers for heap region (%zu bits)",
757 num_non_null_ptrs, size_t(heap_info->ptrmap()->size()));
758 }
759
760 #endif // INCLUDE_CDS_JAVA_HEAP
|
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/archiveHeapWriter.hpp"
26 #include "cds/cdsConfig.hpp"
27 #include "cds/filemap.hpp"
28 #include "cds/heapShared.hpp"
29 #include "cds/regeneratedClasses.hpp"
30 #include "classfile/javaClasses.hpp"
31 #include "classfile/modules.hpp"
32 #include "classfile/systemDictionary.hpp"
33 #include "gc/shared/collectedHeap.hpp"
34 #include "memory/iterator.inline.hpp"
35 #include "memory/oopFactory.hpp"
36 #include "memory/universe.hpp"
37 #include "oops/compressedOops.hpp"
38 #include "oops/objArrayOop.inline.hpp"
39 #include "oops/oop.inline.hpp"
40 #include "oops/oopHandle.inline.hpp"
41 #include "oops/typeArrayKlass.hpp"
42 #include "oops/typeArrayOop.hpp"
43 #include "runtime/java.hpp"
44 #include "runtime/mutexLocker.hpp"
45 #include "utilities/bitMap.inline.hpp"
46 #if INCLUDE_G1GC
47 #include "gc/g1/g1CollectedHeap.hpp"
48 #include "gc/g1/g1HeapRegion.hpp"
49 #endif
50
51 #if INCLUDE_CDS_JAVA_HEAP
52
53 GrowableArrayCHeap<u1, mtClassShared>* ArchiveHeapWriter::_buffer = nullptr;
54
55 // The following are offsets from buffer_bottom()
56 size_t ArchiveHeapWriter::_buffer_used;
57
58 // Heap root segments
59 HeapRootSegments ArchiveHeapWriter::_heap_root_segments;
60
61 address ArchiveHeapWriter::_requested_bottom;
62 address ArchiveHeapWriter::_requested_top;
63
64 static size_t _num_strings = 0;
65 static size_t _string_bytes = 0;
66 static size_t _num_packages = 0;
67 static size_t _num_protection_domains = 0;
68
69 GrowableArrayCHeap<ArchiveHeapWriter::NativePointerInfo, mtClassShared>* ArchiveHeapWriter::_native_pointers;
70 GrowableArrayCHeap<oop, mtClassShared>* ArchiveHeapWriter::_source_objs;
71 GrowableArrayCHeap<ArchiveHeapWriter::HeapObjOrder, mtClassShared>* ArchiveHeapWriter::_source_objs_order;
72
73 ArchiveHeapWriter::BufferOffsetToSourceObjectTable*
74 ArchiveHeapWriter::_buffer_offset_to_source_obj_table = nullptr;
75
76
77 typedef ResourceHashtable<
78 size_t, // offset of a filler from ArchiveHeapWriter::buffer_bottom()
79 size_t, // size of this filler (in bytes)
80 127, // prime number
81 AnyObj::C_HEAP,
82 mtClassShared> FillersTable;
83 static FillersTable* _fillers;
84 static int _num_native_ptrs = 0;
85
86 void ArchiveHeapWriter::init() {
87 if (CDSConfig::is_dumping_heap()) {
88 Universe::heap()->collect(GCCause::_java_lang_system_gc);
309 _source_objs_order->append(os);
310 }
311 log_info(cds)("computed ranks");
312 _source_objs_order->sort(compare_objs_by_oop_fields);
313 log_info(cds)("sorting heap objects done");
314 }
315
316 void ArchiveHeapWriter::copy_source_objs_to_buffer(GrowableArrayCHeap<oop, mtClassShared>* roots) {
317 // There could be multiple root segments, which we want to be aligned by region.
318 // Putting them ahead of objects makes sure we waste no space.
319 copy_roots_to_buffer(roots);
320
321 sort_source_objs();
322 for (int i = 0; i < _source_objs_order->length(); i++) {
323 int src_obj_index = _source_objs_order->at(i)._index;
324 oop src_obj = _source_objs->at(src_obj_index);
325 HeapShared::CachedOopInfo* info = HeapShared::archived_object_cache()->get(src_obj);
326 assert(info != nullptr, "must be");
327 size_t buffer_offset = copy_one_source_obj_to_buffer(src_obj);
328 info->set_buffer_offset(buffer_offset);
329 assert(buffer_offset <= 0x7fffffff, "sanity");
330 HeapShared::add_to_permanent_oop_table(src_obj, (int)buffer_offset);
331
332 _buffer_offset_to_source_obj_table->put_when_absent(buffer_offset, src_obj);
333 _buffer_offset_to_source_obj_table->maybe_grow();
334
335 if (java_lang_Module::is_instance(src_obj)) {
336 Modules::check_archived_module_oop(src_obj);
337 }
338 }
339
340 log_info(cds)("Size of heap region = %zu bytes, %d objects, %d roots, %d native ptrs",
341 _buffer_used, _source_objs->length() + 1, roots->length(), _num_native_ptrs);
342 log_info(cds)(" strings = %8zu (%zu bytes)", _num_strings, _string_bytes);
343 log_info(cds)(" packages = %8zu", _num_packages);
344 log_info(cds)(" protection domains = %8zu", _num_protection_domains);
345 }
346
347 size_t ArchiveHeapWriter::filler_array_byte_size(int length) {
348 size_t byte_size = objArrayOopDesc::object_size(length) * HeapWordSize;
349 return byte_size;
350 }
351
352 int ArchiveHeapWriter::filler_array_length(size_t fill_bytes) {
353 assert(is_object_aligned(fill_bytes), "must be");
354 size_t elemSize = (UseCompressedOops ? sizeof(narrowOop) : sizeof(oop));
355
356 int initial_length = to_array_length(fill_bytes / elemSize);
357 for (int length = initial_length; length >= 0; length --) {
358 size_t array_byte_size = filler_array_byte_size(length);
359 if (array_byte_size == fill_bytes) {
360 return length;
361 }
362 }
363
364 ShouldNotReachHere();
413 _fillers->put(buffered_address_to_offset((address)filler), fill_bytes);
414 }
415 }
416
417 size_t ArchiveHeapWriter::get_filler_size_at(address buffered_addr) {
418 size_t* p = _fillers->get(buffered_address_to_offset(buffered_addr));
419 if (p != nullptr) {
420 assert(*p > 0, "filler must be larger than zero bytes");
421 return *p;
422 } else {
423 return 0; // buffered_addr is not a filler
424 }
425 }
426
427 template <typename T>
428 void update_buffered_object_field(address buffered_obj, int field_offset, T value) {
429 T* field_addr = cast_to_oop(buffered_obj)->field_addr<T>(field_offset);
430 *field_addr = value;
431 }
432
433 void ArchiveHeapWriter::update_stats(oop src_obj) {
434 if (java_lang_String::is_instance(src_obj)) {
435 _num_strings ++;
436 _string_bytes += src_obj->size() * HeapWordSize;
437 _string_bytes += java_lang_String::value(src_obj)->size() * HeapWordSize;
438 } else {
439 Klass* k = src_obj->klass();
440 Symbol* name = k->name();
441 if (name->equals("java/lang/NamedPackage") || name->equals("java/lang/Package")) {
442 _num_packages ++;
443 } else if (name->equals("java/security/ProtectionDomain")) {
444 _num_protection_domains ++;
445 }
446 }
447 }
448
449 size_t ArchiveHeapWriter::copy_one_source_obj_to_buffer(oop src_obj) {
450 update_stats(src_obj);
451
452 assert(!is_too_large_to_archive(src_obj), "already checked");
453 size_t byte_size = src_obj->size() * HeapWordSize;
454 assert(byte_size > 0, "no zero-size objects");
455
456 // For region-based collectors such as G1, the archive heap may be mapped into
457 // multiple regions. We need to make sure that we don't have an object that can possible
458 // span across two regions.
459 maybe_fill_gc_region_gap(byte_size);
460
461 size_t new_used = _buffer_used + byte_size;
462 assert(new_used > _buffer_used, "no wrap around");
463
464 size_t cur_min_region_bottom = align_down(_buffer_used, MIN_GC_REGION_ALIGNMENT);
465 size_t next_min_region_bottom = align_down(new_used, MIN_GC_REGION_ALIGNMENT);
466 assert(cur_min_region_bottom == next_min_region_bottom, "no object should cross minimal GC region boundaries");
467
468 ensure_buffer_space(new_used);
469
470 address from = cast_from_oop<address>(src_obj);
471 address to = offset_to_buffered_address<address>(_buffer_used);
759 // requested_field_addr = the address of this field in the requested space
760 oop requested_obj = requested_obj_from_buffer_offset(p->buffer_offset());
761 Metadata** requested_field_addr = (Metadata**)(cast_from_oop<address>(requested_obj) + field_offset);
762 assert(bottom <= requested_field_addr && requested_field_addr < top, "range check");
763
764 // Mark this field in the bitmap
765 BitMap::idx_t idx = requested_field_addr - bottom;
766 heap_info->ptrmap()->set_bit(idx);
767 num_non_null_ptrs ++;
768 max_idx = MAX2(max_idx, idx);
769
770 // Set the native pointer to the requested address of the metadata (at runtime, the metadata will have
771 // this address if the RO/RW regions are mapped at the default location).
772
773 Metadata** buffered_field_addr = requested_addr_to_buffered_addr(requested_field_addr);
774 Metadata* native_ptr = *buffered_field_addr;
775 guarantee(native_ptr != nullptr, "sanity");
776 guarantee(ArchiveBuilder::current()->has_been_buffered((address)native_ptr),
777 "Metadata %p should have been archived", native_ptr);
778
779 if (RegeneratedClasses::has_been_regenerated((address)native_ptr)) {
780 native_ptr = (Metadata*)RegeneratedClasses::get_regenerated_object((address)native_ptr);
781 }
782
783 address buffered_native_ptr = ArchiveBuilder::current()->get_buffered_addr((address)native_ptr);
784 address requested_native_ptr = ArchiveBuilder::current()->to_requested(buffered_native_ptr);
785 *buffered_field_addr = (Metadata*)requested_native_ptr;
786 }
787
788 heap_info->ptrmap()->resize(max_idx + 1);
789 log_info(cds, heap)("calculate_ptrmap: marked %d non-null native pointers for heap region (%zu bits)",
790 num_non_null_ptrs, size_t(heap_info->ptrmap()->size()));
791 }
792
793 #endif // INCLUDE_CDS_JAVA_HEAP
|