1 /* 2 * Copyright (c) 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/archiveHeapWriter.hpp" 27 #include "cds/filemap.hpp" 28 #include "cds/heapShared.hpp" 29 #include "gc/shared/collectedHeap.hpp" 30 #include "memory/iterator.inline.hpp" 31 #include "memory/oopFactory.hpp" 32 #include "memory/universe.hpp" 33 #include "oops/compressedOops.hpp" 34 #include "oops/oop.inline.hpp" 35 #include "oops/objArrayOop.inline.hpp" 36 #include "oops/oopHandle.inline.hpp" 37 #include "oops/typeArrayKlass.hpp" 38 #include "oops/typeArrayOop.hpp" 39 #include "runtime/java.hpp" 40 #include "runtime/mutexLocker.hpp" 41 #include "utilities/bitMap.inline.hpp" 42 43 #if INCLUDE_G1GC 44 #include "gc/g1/g1CollectedHeap.hpp" 45 #include "gc/g1/heapRegion.hpp" 46 #endif 47 48 #if INCLUDE_CDS_JAVA_HEAP 49 50 51 GrowableArrayCHeap<u1, mtClassShared>* ArchiveHeapWriter::_buffer; 52 53 // The following are offsets from buffer_bottom() 54 size_t ArchiveHeapWriter::_buffer_top; 55 size_t ArchiveHeapWriter::_open_bottom; 56 size_t ArchiveHeapWriter::_open_top; 57 size_t ArchiveHeapWriter::_closed_bottom; 58 size_t ArchiveHeapWriter::_closed_top; 59 size_t ArchiveHeapWriter::_heap_roots_bottom; 60 61 size_t ArchiveHeapWriter::_heap_roots_word_size; 62 63 address ArchiveHeapWriter::_requested_open_region_bottom; 64 address ArchiveHeapWriter::_requested_open_region_top; 65 address ArchiveHeapWriter::_requested_closed_region_bottom; 66 address ArchiveHeapWriter::_requested_closed_region_top; 67 68 ResourceBitMap* ArchiveHeapWriter::_closed_oopmap; 69 ResourceBitMap* ArchiveHeapWriter::_open_oopmap; 70 71 GrowableArrayCHeap<ArchiveHeapWriter::NativePointerInfo, mtClassShared>* ArchiveHeapWriter::_native_pointers; 72 GrowableArrayCHeap<oop, mtClassShared>* ArchiveHeapWriter::_source_objs; 73 74 ArchiveHeapWriter::BufferOffsetToSourceObjectTable* 75 ArchiveHeapWriter::_buffer_offset_to_source_obj_table = nullptr; 76 77 void ArchiveHeapWriter::init() { 78 if (HeapShared::can_write()) { 79 Universe::heap()->collect(GCCause::_java_lang_system_gc); 80 81 _buffer_offset_to_source_obj_table = new BufferOffsetToSourceObjectTable(); 82 83 _requested_open_region_bottom = nullptr; 84 _requested_open_region_top = nullptr; 85 _requested_closed_region_bottom = nullptr; 86 _requested_closed_region_top = nullptr; 87 88 _native_pointers = new GrowableArrayCHeap<NativePointerInfo, mtClassShared>(2048); 89 _source_objs = new GrowableArrayCHeap<oop, mtClassShared>(10000); 90 91 guarantee(UseG1GC, "implementation limitation"); 92 guarantee(MIN_GC_REGION_ALIGNMENT <= /*G1*/HeapRegion::min_region_size_in_words() * HeapWordSize, "must be"); 93 } 94 } 95 96 void ArchiveHeapWriter::add_source_obj(oop src_obj) { 97 _source_objs->append(src_obj); 98 } 99 100 // For the time being, always support two regions (to be strictly compatible with existing G1 101 // mapping code. We might eventually use a single region (JDK-8298048). 102 void ArchiveHeapWriter::write(GrowableArrayCHeap<oop, mtClassShared>* roots, 103 GrowableArray<MemRegion>* closed_regions, GrowableArray<MemRegion>* open_regions, 104 GrowableArray<ArchiveHeapBitmapInfo>* closed_bitmaps, 105 GrowableArray<ArchiveHeapBitmapInfo>* open_bitmaps) { 106 assert(HeapShared::can_write(), "sanity"); 107 allocate_buffer(); 108 copy_source_objs_to_buffer(roots); 109 set_requested_address_for_regions(closed_regions, open_regions); 110 relocate_embedded_oops(roots, closed_bitmaps, open_bitmaps); 111 } 112 113 bool ArchiveHeapWriter::is_too_large_to_archive(oop o) { 114 return is_too_large_to_archive(o->size()); 115 } 116 117 bool ArchiveHeapWriter::is_string_too_large_to_archive(oop string) { 118 typeArrayOop value = java_lang_String::value_no_keepalive(string); 119 return is_too_large_to_archive(value); 120 } 121 122 bool ArchiveHeapWriter::is_too_large_to_archive(size_t size) { 123 assert(size > 0, "no zero-size object"); 124 assert(size * HeapWordSize > size, "no overflow"); 125 static_assert(MIN_GC_REGION_ALIGNMENT > 0, "must be positive"); 126 127 size_t byte_size = size * HeapWordSize; 128 if (byte_size > size_t(MIN_GC_REGION_ALIGNMENT)) { 129 return true; 130 } else { 131 return false; 132 } 133 } 134 135 // Various lookup functions between source_obj, buffered_obj and requested_obj 136 bool ArchiveHeapWriter::is_in_requested_regions(oop o) { 137 assert(_requested_open_region_bottom != nullptr, "do not call before this is initialized"); 138 assert(_requested_closed_region_bottom != nullptr, "do not call before this is initialized"); 139 140 address a = cast_from_oop<address>(o); 141 return (_requested_open_region_bottom <= a && a < _requested_open_region_top) || 142 (_requested_closed_region_bottom <= a && a < _requested_closed_region_top); 143 } 144 145 oop ArchiveHeapWriter::requested_obj_from_buffer_offset(size_t offset) { 146 oop req_obj = cast_to_oop(_requested_open_region_bottom + offset); 147 assert(is_in_requested_regions(req_obj), "must be"); 148 return req_obj; 149 } 150 151 oop ArchiveHeapWriter::source_obj_to_requested_obj(oop src_obj) { 152 assert(DumpSharedSpaces, "dump-time only"); 153 HeapShared::CachedOopInfo* p = HeapShared::archived_object_cache()->get(src_obj); 154 if (p != nullptr) { 155 return requested_obj_from_buffer_offset(p->buffer_offset()); 156 } else { 157 return nullptr; 158 } 159 } 160 161 oop ArchiveHeapWriter::buffered_addr_to_source_obj(address buffered_addr) { 162 oop* p = _buffer_offset_to_source_obj_table->get(buffered_address_to_offset(buffered_addr)); 163 if (p != nullptr) { 164 return *p; 165 } else { 166 return nullptr; 167 } 168 } 169 170 address ArchiveHeapWriter::buffered_addr_to_requested_addr(address buffered_addr) { 171 return _requested_open_region_bottom + buffered_address_to_offset(buffered_addr); 172 } 173 174 oop ArchiveHeapWriter::heap_roots_requested_address() { 175 return requested_obj_from_buffer_offset(_heap_roots_bottom); 176 } 177 178 address ArchiveHeapWriter::heap_region_requested_bottom(int heap_region_idx) { 179 assert(_buffer != nullptr, "must be initialized"); 180 switch (heap_region_idx) { 181 case MetaspaceShared::first_closed_heap_region: 182 return _requested_closed_region_bottom; 183 case MetaspaceShared::first_open_heap_region: 184 return _requested_open_region_bottom; 185 default: 186 ShouldNotReachHere(); 187 return nullptr; 188 } 189 } 190 191 void ArchiveHeapWriter::allocate_buffer() { 192 int initial_buffer_size = 100000; 193 _buffer = new GrowableArrayCHeap<u1, mtClassShared>(initial_buffer_size); 194 _open_bottom = _buffer_top = 0; 195 ensure_buffer_space(1); // so that buffer_bottom() works 196 } 197 198 void ArchiveHeapWriter::ensure_buffer_space(size_t min_bytes) { 199 // We usually have very small heaps. If we get a huge one it's probably caused by a bug. 200 guarantee(min_bytes <= max_jint, "we dont support archiving more than 2G of objects"); 201 _buffer->at_grow(to_array_index(min_bytes)); 202 } 203 204 void ArchiveHeapWriter::copy_roots_to_buffer(GrowableArrayCHeap<oop, mtClassShared>* roots) { 205 Klass* k = Universe::objectArrayKlassObj(); // already relocated to point to archived klass 206 int length = roots != nullptr ? roots->length() : 0; 207 _heap_roots_word_size = objArrayOopDesc::object_size(length); 208 size_t byte_size = _heap_roots_word_size * HeapWordSize; 209 if (byte_size >= MIN_GC_REGION_ALIGNMENT) { 210 log_error(cds, heap)("roots array is too large. Please reduce the number of classes"); 211 vm_exit(1); 212 } 213 214 maybe_fill_gc_region_gap(byte_size); 215 216 size_t new_top = _buffer_top + byte_size; 217 ensure_buffer_space(new_top); 218 219 HeapWord* mem = offset_to_buffered_address<HeapWord*>(_buffer_top); 220 memset(mem, 0, byte_size); 221 { 222 // This is copied from MemAllocator::finish 223 oopDesc::set_mark(mem, markWord::prototype()); 224 oopDesc::release_set_klass(mem, k); 225 } 226 { 227 // This is copied from ObjArrayAllocator::initialize 228 arrayOopDesc::set_length(mem, length); 229 } 230 231 objArrayOop arrayOop = objArrayOop(cast_to_oop(mem)); 232 for (int i = 0; i < length; i++) { 233 // Do not use arrayOop->obj_at_put(i, o) as arrayOop is outside of the real heap! 234 oop o = roots->at(i); 235 if (UseCompressedOops) { 236 * arrayOop->obj_at_addr<narrowOop>(i) = CompressedOops::encode(o); 237 } else { 238 * arrayOop->obj_at_addr<oop>(i) = o; 239 } 240 } 241 log_info(cds)("archived obj roots[%d] = " SIZE_FORMAT " bytes, klass = %p, obj = %p", length, byte_size, k, mem); 242 243 _heap_roots_bottom = _buffer_top; 244 _buffer_top = new_top; 245 } 246 247 void ArchiveHeapWriter::copy_source_objs_to_buffer(GrowableArrayCHeap<oop, mtClassShared>* roots) { 248 copy_source_objs_to_buffer_by_region(/*copy_open_region=*/true); 249 copy_roots_to_buffer(roots); 250 _open_top = _buffer_top; 251 252 // Align the closed region to the next G1 region 253 _buffer_top = _closed_bottom = align_up(_buffer_top, HeapRegion::GrainBytes); 254 copy_source_objs_to_buffer_by_region(/*copy_open_region=*/false); 255 _closed_top = _buffer_top; 256 257 log_info(cds, heap)("Size of open region = " SIZE_FORMAT " bytes", _open_top - _open_bottom); 258 log_info(cds, heap)("Size of closed region = " SIZE_FORMAT " bytes", _closed_top - _closed_bottom); 259 } 260 261 void ArchiveHeapWriter::copy_source_objs_to_buffer_by_region(bool copy_open_region) { 262 for (int i = 0; i < _source_objs->length(); i++) { 263 oop src_obj = _source_objs->at(i); 264 HeapShared::CachedOopInfo* info = HeapShared::archived_object_cache()->get(src_obj); 265 assert(info != nullptr, "must be"); 266 if (info->in_open_region() == copy_open_region) { 267 // For region-based collectors such as G1, we need to make sure that we don't have 268 // an object that can possible span across two regions. 269 size_t buffer_offset = copy_one_source_obj_to_buffer(src_obj); 270 info->set_buffer_offset(buffer_offset); 271 272 _buffer_offset_to_source_obj_table->put(buffer_offset, src_obj); 273 } 274 } 275 } 276 277 size_t ArchiveHeapWriter::filler_array_byte_size(int length) { 278 size_t byte_size = objArrayOopDesc::object_size(length) * HeapWordSize; 279 return byte_size; 280 } 281 282 int ArchiveHeapWriter::filler_array_length(size_t fill_bytes) { 283 assert(is_object_aligned(fill_bytes), "must be"); 284 size_t elemSize = (UseCompressedOops ? sizeof(narrowOop) : sizeof(oop)); 285 286 int initial_length = to_array_length(fill_bytes / elemSize); 287 for (int length = initial_length; length >= 0; length --) { 288 size_t array_byte_size = filler_array_byte_size(length); 289 if (array_byte_size == fill_bytes) { 290 return length; 291 } 292 } 293 294 ShouldNotReachHere(); 295 return -1; 296 } 297 298 void ArchiveHeapWriter::init_filler_array_at_buffer_top(int array_length, size_t fill_bytes) { 299 assert(UseCompressedClassPointers, "Archived heap only supported for compressed klasses"); 300 Klass* oak = Universe::objectArrayKlassObj(); // already relocated to point to archived klass 301 HeapWord* mem = offset_to_buffered_address<HeapWord*>(_buffer_top); 302 memset(mem, 0, fill_bytes); 303 oopDesc::set_mark(mem, markWord::prototype()); 304 narrowKlass nk = ArchiveBuilder::current()->get_requested_narrow_klass(oak); 305 cast_to_oop(mem)->set_narrow_klass(nk); 306 arrayOopDesc::set_length(mem, array_length); 307 } 308 309 void ArchiveHeapWriter::maybe_fill_gc_region_gap(size_t required_byte_size) { 310 // We fill only with arrays (so we don't need to use a single HeapWord filler if the 311 // leftover space is smaller than a zero-sized array object). Therefore, we need to 312 // make sure there's enough space of min_filler_byte_size in the current region after 313 // required_byte_size has been allocated. If not, fill the remainder of the current 314 // region. 315 size_t min_filler_byte_size = filler_array_byte_size(0); 316 size_t new_top = _buffer_top + required_byte_size + min_filler_byte_size; 317 318 const size_t cur_min_region_bottom = align_down(_buffer_top, MIN_GC_REGION_ALIGNMENT); 319 const size_t next_min_region_bottom = align_down(new_top, MIN_GC_REGION_ALIGNMENT); 320 321 if (cur_min_region_bottom != next_min_region_bottom) { 322 // Make sure that no objects span across MIN_GC_REGION_ALIGNMENT. This way 323 // we can map the region in any region-based collector. 324 assert(next_min_region_bottom > cur_min_region_bottom, "must be"); 325 assert(next_min_region_bottom - cur_min_region_bottom == MIN_GC_REGION_ALIGNMENT, 326 "no buffered object can be larger than %d bytes", MIN_GC_REGION_ALIGNMENT); 327 328 const size_t filler_end = next_min_region_bottom; 329 const size_t fill_bytes = filler_end - _buffer_top; 330 assert(fill_bytes > 0, "must be"); 331 ensure_buffer_space(filler_end); 332 333 int array_length = filler_array_length(fill_bytes); 334 log_info(cds, heap)("Inserting filler obj array of %d elements (" SIZE_FORMAT " bytes total) @ buffer offset " SIZE_FORMAT, 335 array_length, fill_bytes, _buffer_top); 336 init_filler_array_at_buffer_top(array_length, fill_bytes); 337 338 _buffer_top = filler_end; 339 } 340 } 341 342 size_t ArchiveHeapWriter::copy_one_source_obj_to_buffer(oop src_obj) { 343 assert(!is_too_large_to_archive(src_obj), "already checked"); 344 size_t byte_size = src_obj->size() * HeapWordSize; 345 assert(byte_size > 0, "no zero-size objects"); 346 347 maybe_fill_gc_region_gap(byte_size); 348 349 size_t new_top = _buffer_top + byte_size; 350 assert(new_top > _buffer_top, "no wrap around"); 351 352 size_t cur_min_region_bottom = align_down(_buffer_top, MIN_GC_REGION_ALIGNMENT); 353 size_t next_min_region_bottom = align_down(new_top, MIN_GC_REGION_ALIGNMENT); 354 assert(cur_min_region_bottom == next_min_region_bottom, "no object should cross minimal GC region boundaries"); 355 356 ensure_buffer_space(new_top); 357 358 address from = cast_from_oop<address>(src_obj); 359 address to = offset_to_buffered_address<address>(_buffer_top); 360 assert(is_object_aligned(_buffer_top), "sanity"); 361 assert(is_object_aligned(byte_size), "sanity"); 362 memcpy(to, from, byte_size); 363 364 size_t buffered_obj_offset = _buffer_top; 365 _buffer_top = new_top; 366 367 return buffered_obj_offset; 368 } 369 370 void ArchiveHeapWriter::set_requested_address_for_regions(GrowableArray<MemRegion>* closed_regions, 371 GrowableArray<MemRegion>* open_regions) { 372 assert(closed_regions->length() == 0, "must be"); 373 assert(open_regions->length() == 0, "must be"); 374 375 assert(UseG1GC, "must be"); 376 address heap_end = (address)G1CollectedHeap::heap()->reserved().end(); 377 log_info(cds, heap)("Heap end = %p", heap_end); 378 379 size_t closed_region_byte_size = _closed_top - _closed_bottom; 380 size_t open_region_byte_size = _open_top - _open_bottom; 381 assert(closed_region_byte_size > 0, "must archived at least one object for closed region!"); 382 assert(open_region_byte_size > 0, "must archived at least one object for open region!"); 383 384 // The following two asserts are ensured by copy_source_objs_to_buffer_by_region(). 385 assert(is_aligned(_closed_bottom, HeapRegion::GrainBytes), "sanity"); 386 assert(is_aligned(_open_bottom, HeapRegion::GrainBytes), "sanity"); 387 388 _requested_closed_region_bottom = align_down(heap_end - closed_region_byte_size, HeapRegion::GrainBytes); 389 _requested_open_region_bottom = _requested_closed_region_bottom - (_closed_bottom - _open_bottom); 390 391 assert(is_aligned(_requested_closed_region_bottom, HeapRegion::GrainBytes), "sanity"); 392 assert(is_aligned(_requested_open_region_bottom, HeapRegion::GrainBytes), "sanity"); 393 394 _requested_open_region_top = _requested_open_region_bottom + (_open_top - _open_bottom); 395 _requested_closed_region_top = _requested_closed_region_bottom + (_closed_top - _closed_bottom); 396 397 assert(_requested_open_region_top <= _requested_closed_region_bottom, "no overlap"); 398 399 closed_regions->append(MemRegion(offset_to_buffered_address<HeapWord*>(_closed_bottom), 400 offset_to_buffered_address<HeapWord*>(_closed_top))); 401 open_regions->append( MemRegion(offset_to_buffered_address<HeapWord*>(_open_bottom), 402 offset_to_buffered_address<HeapWord*>(_open_top))); 403 } 404 405 // Oop relocation 406 407 template <typename T> T* ArchiveHeapWriter::requested_addr_to_buffered_addr(T* p) { 408 assert(is_in_requested_regions(cast_to_oop(p)), "must be"); 409 410 address addr = address(p); 411 assert(addr >= _requested_open_region_bottom, "must be"); 412 size_t offset = addr - _requested_open_region_bottom; 413 return offset_to_buffered_address<T*>(offset); 414 } 415 416 template <typename T> oop ArchiveHeapWriter::load_source_oop_from_buffer(T* buffered_addr) { 417 oop o = load_oop_from_buffer(buffered_addr); 418 assert(!in_buffer(cast_from_oop<address>(o)), "must point to source oop"); 419 return o; 420 } 421 422 template <typename T> void ArchiveHeapWriter::store_requested_oop_in_buffer(T* buffered_addr, 423 oop request_oop) { 424 assert(is_in_requested_regions(request_oop), "must be"); 425 store_oop_in_buffer(buffered_addr, request_oop); 426 } 427 428 void ArchiveHeapWriter::store_oop_in_buffer(oop* buffered_addr, oop requested_obj) { 429 // Make heap content deterministic. See comments inside HeapShared::to_requested_address. 430 *buffered_addr = HeapShared::to_requested_address(requested_obj); 431 } 432 433 void ArchiveHeapWriter::store_oop_in_buffer(narrowOop* buffered_addr, oop requested_obj) { 434 // Note: HeapShared::to_requested_address() is not necessary because 435 // the heap always starts at a deterministic address with UseCompressedOops==true. 436 narrowOop val = CompressedOops::encode_not_null(requested_obj); 437 *buffered_addr = val; 438 } 439 440 oop ArchiveHeapWriter::load_oop_from_buffer(oop* buffered_addr) { 441 return *buffered_addr; 442 } 443 444 oop ArchiveHeapWriter::load_oop_from_buffer(narrowOop* buffered_addr) { 445 return CompressedOops::decode(*buffered_addr); 446 } 447 448 template <typename T> void ArchiveHeapWriter::relocate_field_in_buffer(T* field_addr_in_buffer) { 449 oop source_referent = load_source_oop_from_buffer<T>(field_addr_in_buffer); 450 if (!CompressedOops::is_null(source_referent)) { 451 oop request_referent = source_obj_to_requested_obj(source_referent); 452 store_requested_oop_in_buffer<T>(field_addr_in_buffer, request_referent); 453 mark_oop_pointer<T>(field_addr_in_buffer); 454 } 455 } 456 457 template <typename T> void ArchiveHeapWriter::mark_oop_pointer(T* buffered_addr) { 458 T* request_p = (T*)(buffered_addr_to_requested_addr((address)buffered_addr)); 459 ResourceBitMap* oopmap; 460 address requested_region_bottom; 461 462 if (request_p >= (T*)_requested_closed_region_bottom) { 463 assert(request_p < (T*)_requested_closed_region_top, "sanity"); 464 oopmap = _closed_oopmap; 465 requested_region_bottom = _requested_closed_region_bottom; 466 } else { 467 assert(request_p >= (T*)_requested_open_region_bottom, "sanity"); 468 assert(request_p < (T*)_requested_open_region_top, "sanity"); 469 oopmap = _open_oopmap; 470 requested_region_bottom = _requested_open_region_bottom; 471 } 472 473 // Mark the pointer in the oopmap 474 T* region_bottom = (T*)requested_region_bottom; 475 assert(request_p >= region_bottom, "must be"); 476 BitMap::idx_t idx = request_p - region_bottom; 477 assert(idx < oopmap->size(), "overflow"); 478 oopmap->set_bit(idx); 479 } 480 481 void ArchiveHeapWriter::update_header_for_requested_obj(oop requested_obj, oop src_obj, Klass* src_klass) { 482 assert(UseCompressedClassPointers, "Archived heap only supported for compressed klasses"); 483 narrowKlass nk = ArchiveBuilder::current()->get_requested_narrow_klass(src_klass); 484 address buffered_addr = requested_addr_to_buffered_addr(cast_from_oop<address>(requested_obj)); 485 486 oop fake_oop = cast_to_oop(buffered_addr); 487 fake_oop->set_narrow_klass(nk); 488 489 // We need to retain the identity_hash, because it may have been used by some hashtables 490 // in the shared heap. This also has the side effect of pre-initializing the 491 // identity_hash for all shared objects, so they are less likely to be written 492 // into during run time, increasing the potential of memory sharing. 493 if (src_obj != nullptr) { 494 int src_hash = src_obj->identity_hash(); 495 fake_oop->set_mark(markWord::prototype().copy_set_hash(src_hash)); 496 assert(fake_oop->mark().is_unlocked(), "sanity"); 497 498 DEBUG_ONLY(int archived_hash = fake_oop->identity_hash()); 499 assert(src_hash == archived_hash, "Different hash codes: original %x, archived %x", src_hash, archived_hash); 500 } 501 } 502 503 // Relocate an element in the buffered copy of HeapShared::roots() 504 template <typename T> void ArchiveHeapWriter::relocate_root_at(oop requested_roots, int index) { 505 size_t offset = (size_t)((objArrayOop)requested_roots)->obj_at_offset<T>(index); 506 relocate_field_in_buffer<T>((T*)(buffered_heap_roots_addr() + offset)); 507 } 508 509 class ArchiveHeapWriter::EmbeddedOopRelocator: public BasicOopIterateClosure { 510 oop _src_obj; 511 address _buffered_obj; 512 513 public: 514 EmbeddedOopRelocator(oop src_obj, address buffered_obj) : 515 _src_obj(src_obj), _buffered_obj(buffered_obj) {} 516 517 void do_oop(narrowOop *p) { EmbeddedOopRelocator::do_oop_work(p); } 518 void do_oop( oop *p) { EmbeddedOopRelocator::do_oop_work(p); } 519 520 private: 521 template <class T> void do_oop_work(T *p) { 522 size_t field_offset = pointer_delta(p, _src_obj, sizeof(char)); 523 ArchiveHeapWriter::relocate_field_in_buffer<T>((T*)(_buffered_obj + field_offset)); 524 } 525 }; 526 527 // Update all oop fields embedded in the buffered objects 528 void ArchiveHeapWriter::relocate_embedded_oops(GrowableArrayCHeap<oop, mtClassShared>* roots, 529 GrowableArray<ArchiveHeapBitmapInfo>* closed_bitmaps, 530 GrowableArray<ArchiveHeapBitmapInfo>* open_bitmaps) { 531 size_t oopmap_unit = (UseCompressedOops ? sizeof(narrowOop) : sizeof(oop)); 532 size_t closed_region_byte_size = _closed_top - _closed_bottom; 533 size_t open_region_byte_size = _open_top - _open_bottom; 534 ResourceBitMap closed_oopmap(closed_region_byte_size / oopmap_unit); 535 ResourceBitMap open_oopmap (open_region_byte_size / oopmap_unit); 536 537 _closed_oopmap = &closed_oopmap; 538 _open_oopmap = &open_oopmap; 539 540 auto iterator = [&] (oop src_obj, HeapShared::CachedOopInfo& info) { 541 oop requested_obj = requested_obj_from_buffer_offset(info.buffer_offset()); 542 update_header_for_requested_obj(requested_obj, src_obj, src_obj->klass()); 543 544 address buffered_obj = offset_to_buffered_address<address>(info.buffer_offset()); 545 EmbeddedOopRelocator relocator(src_obj, buffered_obj); 546 547 src_obj->oop_iterate(&relocator); 548 }; 549 HeapShared::archived_object_cache()->iterate_all(iterator); 550 551 // Relocate HeapShared::roots(), which is created in copy_roots_to_buffer() and 552 // doesn't have a corresponding src_obj, so we can't use EmbeddedOopRelocator on it. 553 oop requested_roots = requested_obj_from_buffer_offset(_heap_roots_bottom); 554 update_header_for_requested_obj(requested_roots, nullptr, Universe::objectArrayKlassObj()); 555 int length = roots != nullptr ? roots->length() : 0; 556 for (int i = 0; i < length; i++) { 557 if (UseCompressedOops) { 558 relocate_root_at<narrowOop>(requested_roots, i); 559 } else { 560 relocate_root_at<oop>(requested_roots, i); 561 } 562 } 563 564 closed_bitmaps->append(make_bitmap_info(&closed_oopmap, /*is_open=*/false, /*is_oopmap=*/true)); 565 open_bitmaps ->append(make_bitmap_info(&open_oopmap, /*is_open=*/false, /*is_oopmap=*/true)); 566 567 closed_bitmaps->append(compute_ptrmap(/*is_open=*/false)); 568 open_bitmaps ->append(compute_ptrmap(/*is_open=*/true)); 569 570 _closed_oopmap = nullptr; 571 _open_oopmap = nullptr; 572 } 573 574 ArchiveHeapBitmapInfo ArchiveHeapWriter::make_bitmap_info(ResourceBitMap* bitmap, bool is_open, bool is_oopmap) { 575 size_t size_in_bits = bitmap->size(); 576 size_t size_in_bytes; 577 uintptr_t* buffer; 578 579 if (size_in_bits > 0) { 580 size_in_bytes = bitmap->size_in_bytes(); 581 buffer = (uintptr_t*)NEW_C_HEAP_ARRAY(char, size_in_bytes, mtInternal); 582 bitmap->write_to(buffer, size_in_bytes); 583 } else { 584 size_in_bytes = 0; 585 buffer = nullptr; 586 } 587 588 log_info(cds, heap)("%s @ " INTPTR_FORMAT " (" SIZE_FORMAT_W(6) " bytes) for %s heap region", 589 is_oopmap ? "Oopmap" : "Ptrmap", 590 p2i(buffer), size_in_bytes, 591 is_open? "open" : "closed"); 592 593 ArchiveHeapBitmapInfo info; 594 info._map = (address)buffer; 595 info._size_in_bits = size_in_bits; 596 info._size_in_bytes = size_in_bytes; 597 598 return info; 599 } 600 601 void ArchiveHeapWriter::mark_native_pointer(oop src_obj, int field_offset) { 602 Metadata* ptr = src_obj->metadata_field_acquire(field_offset); 603 if (ptr != nullptr) { 604 NativePointerInfo info; 605 info._src_obj = src_obj; 606 info._field_offset = field_offset; 607 _native_pointers->append(info); 608 } 609 } 610 611 ArchiveHeapBitmapInfo ArchiveHeapWriter::compute_ptrmap(bool is_open) { 612 int num_non_null_ptrs = 0; 613 Metadata** bottom = (Metadata**) (is_open ? _requested_open_region_bottom: _requested_closed_region_bottom); 614 Metadata** top = (Metadata**) (is_open ? _requested_open_region_top: _requested_closed_region_top); // exclusive 615 ResourceBitMap ptrmap(top - bottom); 616 617 for (int i = 0; i < _native_pointers->length(); i++) { 618 NativePointerInfo info = _native_pointers->at(i); 619 oop src_obj = info._src_obj; 620 int field_offset = info._field_offset; 621 HeapShared::CachedOopInfo* p = HeapShared::archived_object_cache()->get(src_obj); 622 if (p->in_open_region() == is_open) { 623 // requested_field_addr = the address of this field in the requested space 624 oop requested_obj = requested_obj_from_buffer_offset(p->buffer_offset()); 625 Metadata** requested_field_addr = (Metadata**)(cast_from_oop<address>(requested_obj) + field_offset); 626 assert(bottom <= requested_field_addr && requested_field_addr < top, "range check"); 627 628 // Mark this field in the bitmap 629 BitMap::idx_t idx = requested_field_addr - bottom; 630 ptrmap.set_bit(idx); 631 num_non_null_ptrs ++; 632 633 // Set the native pointer to the requested address of the metadata (at runtime, the metadata will have 634 // this address if the RO/RW regions are mapped at the default location). 635 636 Metadata** buffered_field_addr = requested_addr_to_buffered_addr(requested_field_addr); 637 Metadata* native_ptr = *buffered_field_addr; 638 assert(native_ptr != nullptr, "sanity"); 639 640 address buffered_native_ptr = ArchiveBuilder::current()->get_buffered_addr((address)native_ptr); 641 address requested_native_ptr = ArchiveBuilder::current()->to_requested(buffered_native_ptr); 642 *buffered_field_addr = (Metadata*)requested_native_ptr; 643 } 644 } 645 646 log_info(cds, heap)("compute_ptrmap: marked %d non-null native pointers for %s heap region", 647 num_non_null_ptrs, is_open ? "open" : "closed"); 648 649 if (num_non_null_ptrs == 0) { 650 ResourceBitMap empty; 651 return make_bitmap_info(&empty, is_open, /*is_oopmap=*/ false); 652 } else { 653 return make_bitmap_info(&ptrmap, is_open, /*is_oopmap=*/ false); 654 } 655 } 656 657 #endif // INCLUDE_CDS_JAVA_HEAP