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 if (UseCompactObjectHeaders) { 224 narrowKlass nk = ArchiveBuilder::current()->get_requested_narrow_klass(k); 225 oopDesc::release_set_mark(mem, markWord::prototype().set_narrow_klass(nk)); 226 } else { 227 oopDesc::set_mark(mem, markWord::prototype()); 228 oopDesc::release_set_klass(mem, k); 229 } 230 } 231 { 232 // This is copied from ObjArrayAllocator::initialize 233 arrayOopDesc::set_length(mem, length); 234 } 235 236 objArrayOop arrayOop = objArrayOop(cast_to_oop(mem)); 237 for (int i = 0; i < length; i++) { 238 // Do not use arrayOop->obj_at_put(i, o) as arrayOop is outside of the real heap! 239 oop o = roots->at(i); 240 if (UseCompressedOops) { 241 * arrayOop->obj_at_addr<narrowOop>(i) = CompressedOops::encode(o); 242 } else { 243 * arrayOop->obj_at_addr<oop>(i) = o; 244 } 245 } 246 log_info(cds)("archived obj roots[%d] = " SIZE_FORMAT " bytes, klass = %p, obj = %p", length, byte_size, k, mem); 247 248 _heap_roots_bottom = _buffer_top; 249 _buffer_top = new_top; 250 } 251 252 void ArchiveHeapWriter::copy_source_objs_to_buffer(GrowableArrayCHeap<oop, mtClassShared>* roots) { 253 copy_source_objs_to_buffer_by_region(/*copy_open_region=*/true); 254 copy_roots_to_buffer(roots); 255 _open_top = _buffer_top; 256 257 // Align the closed region to the next G1 region 258 _buffer_top = _closed_bottom = align_up(_buffer_top, HeapRegion::GrainBytes); 259 copy_source_objs_to_buffer_by_region(/*copy_open_region=*/false); 260 _closed_top = _buffer_top; 261 262 log_info(cds, heap)("Size of open region = " SIZE_FORMAT " bytes", _open_top - _open_bottom); 263 log_info(cds, heap)("Size of closed region = " SIZE_FORMAT " bytes", _closed_top - _closed_bottom); 264 } 265 266 void ArchiveHeapWriter::copy_source_objs_to_buffer_by_region(bool copy_open_region) { 267 for (int i = 0; i < _source_objs->length(); i++) { 268 oop src_obj = _source_objs->at(i); 269 HeapShared::CachedOopInfo* info = HeapShared::archived_object_cache()->get(src_obj); 270 assert(info != nullptr, "must be"); 271 if (info->in_open_region() == copy_open_region) { 272 // For region-based collectors such as G1, we need to make sure that we don't have 273 // an object that can possible span across two regions. 274 size_t buffer_offset = copy_one_source_obj_to_buffer(src_obj); 275 info->set_buffer_offset(buffer_offset); 276 277 _buffer_offset_to_source_obj_table->put(buffer_offset, src_obj); 278 } 279 } 280 } 281 282 size_t ArchiveHeapWriter::filler_array_byte_size(int length) { 283 size_t byte_size = objArrayOopDesc::object_size(length) * HeapWordSize; 284 return byte_size; 285 } 286 287 int ArchiveHeapWriter::filler_array_length(size_t fill_bytes) { 288 assert(is_object_aligned(fill_bytes), "must be"); 289 size_t elemSize = (UseCompressedOops ? sizeof(narrowOop) : sizeof(oop)); 290 291 int initial_length = to_array_length(fill_bytes / elemSize); 292 for (int length = initial_length; length >= 0; length --) { 293 size_t array_byte_size = filler_array_byte_size(length); 294 if (array_byte_size == fill_bytes) { 295 return length; 296 } 297 } 298 299 ShouldNotReachHere(); 300 return -1; 301 } 302 303 void ArchiveHeapWriter::init_filler_array_at_buffer_top(int array_length, size_t fill_bytes) { 304 assert(UseCompressedClassPointers, "Archived heap only supported for compressed klasses"); 305 Klass* oak = Universe::objectArrayKlassObj(); // already relocated to point to archived klass 306 HeapWord* mem = offset_to_buffered_address<HeapWord*>(_buffer_top); 307 memset(mem, 0, fill_bytes); 308 narrowKlass nk = ArchiveBuilder::current()->get_requested_narrow_klass(oak); 309 if (UseCompactObjectHeaders) { 310 oopDesc::release_set_mark(mem, markWord::prototype().set_narrow_klass(nk)); 311 } else { 312 oopDesc::set_mark(mem, markWord::prototype()); 313 cast_to_oop(mem)->set_narrow_klass(nk); 314 } 315 arrayOopDesc::set_length(mem, array_length); 316 } 317 318 void ArchiveHeapWriter::maybe_fill_gc_region_gap(size_t required_byte_size) { 319 // We fill only with arrays (so we don't need to use a single HeapWord filler if the 320 // leftover space is smaller than a zero-sized array object). Therefore, we need to 321 // make sure there's enough space of min_filler_byte_size in the current region after 322 // required_byte_size has been allocated. If not, fill the remainder of the current 323 // region. 324 size_t min_filler_byte_size = filler_array_byte_size(0); 325 size_t new_top = _buffer_top + required_byte_size + min_filler_byte_size; 326 327 const size_t cur_min_region_bottom = align_down(_buffer_top, MIN_GC_REGION_ALIGNMENT); 328 const size_t next_min_region_bottom = align_down(new_top, MIN_GC_REGION_ALIGNMENT); 329 330 if (cur_min_region_bottom != next_min_region_bottom) { 331 // Make sure that no objects span across MIN_GC_REGION_ALIGNMENT. This way 332 // we can map the region in any region-based collector. 333 assert(next_min_region_bottom > cur_min_region_bottom, "must be"); 334 assert(next_min_region_bottom - cur_min_region_bottom == MIN_GC_REGION_ALIGNMENT, 335 "no buffered object can be larger than %d bytes", MIN_GC_REGION_ALIGNMENT); 336 337 const size_t filler_end = next_min_region_bottom; 338 const size_t fill_bytes = filler_end - _buffer_top; 339 assert(fill_bytes > 0, "must be"); 340 ensure_buffer_space(filler_end); 341 342 int array_length = filler_array_length(fill_bytes); 343 log_info(cds, heap)("Inserting filler obj array of %d elements (" SIZE_FORMAT " bytes total) @ buffer offset " SIZE_FORMAT, 344 array_length, fill_bytes, _buffer_top); 345 init_filler_array_at_buffer_top(array_length, fill_bytes); 346 347 _buffer_top = filler_end; 348 } 349 } 350 351 size_t ArchiveHeapWriter::copy_one_source_obj_to_buffer(oop src_obj) { 352 assert(!is_too_large_to_archive(src_obj), "already checked"); 353 size_t byte_size = src_obj->size() * HeapWordSize; 354 assert(byte_size > 0, "no zero-size objects"); 355 356 maybe_fill_gc_region_gap(byte_size); 357 358 size_t new_top = _buffer_top + byte_size; 359 assert(new_top > _buffer_top, "no wrap around"); 360 361 size_t cur_min_region_bottom = align_down(_buffer_top, MIN_GC_REGION_ALIGNMENT); 362 size_t next_min_region_bottom = align_down(new_top, MIN_GC_REGION_ALIGNMENT); 363 assert(cur_min_region_bottom == next_min_region_bottom, "no object should cross minimal GC region boundaries"); 364 365 ensure_buffer_space(new_top); 366 367 address from = cast_from_oop<address>(src_obj); 368 address to = offset_to_buffered_address<address>(_buffer_top); 369 assert(is_object_aligned(_buffer_top), "sanity"); 370 assert(is_object_aligned(byte_size), "sanity"); 371 memcpy(to, from, byte_size); 372 373 size_t buffered_obj_offset = _buffer_top; 374 _buffer_top = new_top; 375 376 return buffered_obj_offset; 377 } 378 379 void ArchiveHeapWriter::set_requested_address_for_regions(GrowableArray<MemRegion>* closed_regions, 380 GrowableArray<MemRegion>* open_regions) { 381 assert(closed_regions->length() == 0, "must be"); 382 assert(open_regions->length() == 0, "must be"); 383 384 assert(UseG1GC, "must be"); 385 address heap_end = (address)G1CollectedHeap::heap()->reserved().end(); 386 log_info(cds, heap)("Heap end = %p", heap_end); 387 388 size_t closed_region_byte_size = _closed_top - _closed_bottom; 389 size_t open_region_byte_size = _open_top - _open_bottom; 390 assert(closed_region_byte_size > 0, "must archived at least one object for closed region!"); 391 assert(open_region_byte_size > 0, "must archived at least one object for open region!"); 392 393 // The following two asserts are ensured by copy_source_objs_to_buffer_by_region(). 394 assert(is_aligned(_closed_bottom, HeapRegion::GrainBytes), "sanity"); 395 assert(is_aligned(_open_bottom, HeapRegion::GrainBytes), "sanity"); 396 397 _requested_closed_region_bottom = align_down(heap_end - closed_region_byte_size, HeapRegion::GrainBytes); 398 _requested_open_region_bottom = _requested_closed_region_bottom - (_closed_bottom - _open_bottom); 399 400 assert(is_aligned(_requested_closed_region_bottom, HeapRegion::GrainBytes), "sanity"); 401 assert(is_aligned(_requested_open_region_bottom, HeapRegion::GrainBytes), "sanity"); 402 403 _requested_open_region_top = _requested_open_region_bottom + (_open_top - _open_bottom); 404 _requested_closed_region_top = _requested_closed_region_bottom + (_closed_top - _closed_bottom); 405 406 assert(_requested_open_region_top <= _requested_closed_region_bottom, "no overlap"); 407 408 closed_regions->append(MemRegion(offset_to_buffered_address<HeapWord*>(_closed_bottom), 409 offset_to_buffered_address<HeapWord*>(_closed_top))); 410 open_regions->append( MemRegion(offset_to_buffered_address<HeapWord*>(_open_bottom), 411 offset_to_buffered_address<HeapWord*>(_open_top))); 412 } 413 414 // Oop relocation 415 416 template <typename T> T* ArchiveHeapWriter::requested_addr_to_buffered_addr(T* p) { 417 assert(is_in_requested_regions(cast_to_oop(p)), "must be"); 418 419 address addr = address(p); 420 assert(addr >= _requested_open_region_bottom, "must be"); 421 size_t offset = addr - _requested_open_region_bottom; 422 return offset_to_buffered_address<T*>(offset); 423 } 424 425 template <typename T> oop ArchiveHeapWriter::load_source_oop_from_buffer(T* buffered_addr) { 426 oop o = load_oop_from_buffer(buffered_addr); 427 assert(!in_buffer(cast_from_oop<address>(o)), "must point to source oop"); 428 return o; 429 } 430 431 template <typename T> void ArchiveHeapWriter::store_requested_oop_in_buffer(T* buffered_addr, 432 oop request_oop) { 433 assert(is_in_requested_regions(request_oop), "must be"); 434 store_oop_in_buffer(buffered_addr, request_oop); 435 } 436 437 void ArchiveHeapWriter::store_oop_in_buffer(oop* buffered_addr, oop requested_obj) { 438 // Make heap content deterministic. See comments inside HeapShared::to_requested_address. 439 *buffered_addr = HeapShared::to_requested_address(requested_obj); 440 } 441 442 void ArchiveHeapWriter::store_oop_in_buffer(narrowOop* buffered_addr, oop requested_obj) { 443 // Note: HeapShared::to_requested_address() is not necessary because 444 // the heap always starts at a deterministic address with UseCompressedOops==true. 445 narrowOop val = CompressedOops::encode_not_null(requested_obj); 446 *buffered_addr = val; 447 } 448 449 oop ArchiveHeapWriter::load_oop_from_buffer(oop* buffered_addr) { 450 return *buffered_addr; 451 } 452 453 oop ArchiveHeapWriter::load_oop_from_buffer(narrowOop* buffered_addr) { 454 return CompressedOops::decode(*buffered_addr); 455 } 456 457 template <typename T> void ArchiveHeapWriter::relocate_field_in_buffer(T* field_addr_in_buffer) { 458 oop source_referent = load_source_oop_from_buffer<T>(field_addr_in_buffer); 459 if (!CompressedOops::is_null(source_referent)) { 460 oop request_referent = source_obj_to_requested_obj(source_referent); 461 store_requested_oop_in_buffer<T>(field_addr_in_buffer, request_referent); 462 mark_oop_pointer<T>(field_addr_in_buffer); 463 } 464 } 465 466 template <typename T> void ArchiveHeapWriter::mark_oop_pointer(T* buffered_addr) { 467 T* request_p = (T*)(buffered_addr_to_requested_addr((address)buffered_addr)); 468 ResourceBitMap* oopmap; 469 address requested_region_bottom; 470 471 if (request_p >= (T*)_requested_closed_region_bottom) { 472 assert(request_p < (T*)_requested_closed_region_top, "sanity"); 473 oopmap = _closed_oopmap; 474 requested_region_bottom = _requested_closed_region_bottom; 475 } else { 476 assert(request_p >= (T*)_requested_open_region_bottom, "sanity"); 477 assert(request_p < (T*)_requested_open_region_top, "sanity"); 478 oopmap = _open_oopmap; 479 requested_region_bottom = _requested_open_region_bottom; 480 } 481 482 // Mark the pointer in the oopmap 483 T* region_bottom = (T*)requested_region_bottom; 484 assert(request_p >= region_bottom, "must be"); 485 BitMap::idx_t idx = request_p - region_bottom; 486 assert(idx < oopmap->size(), "overflow"); 487 oopmap->set_bit(idx); 488 } 489 490 void ArchiveHeapWriter::update_header_for_requested_obj(oop requested_obj, oop src_obj, Klass* src_klass) { 491 assert(UseCompressedClassPointers, "Archived heap only supported for compressed klasses"); 492 narrowKlass nk = ArchiveBuilder::current()->get_requested_narrow_klass(src_klass); 493 address buffered_addr = requested_addr_to_buffered_addr(cast_from_oop<address>(requested_obj)); 494 495 oop fake_oop = cast_to_oop(buffered_addr); 496 fake_oop->set_narrow_klass(nk); 497 498 // We need to retain the identity_hash, because it may have been used by some hashtables 499 // in the shared heap. This also has the side effect of pre-initializing the 500 // identity_hash for all shared objects, so they are less likely to be written 501 // into during run time, increasing the potential of memory sharing. 502 if (src_obj != nullptr) { 503 int src_hash = src_obj->identity_hash(); 504 if (UseCompactObjectHeaders) { 505 fake_oop->set_mark(markWord::prototype().set_narrow_klass(nk).copy_set_hash(src_hash)); 506 } else { 507 fake_oop->set_mark(markWord::prototype().copy_set_hash(src_hash)); 508 } 509 assert(fake_oop->mark().is_unlocked(), "sanity"); 510 511 DEBUG_ONLY(int archived_hash = fake_oop->identity_hash()); 512 assert(src_hash == archived_hash, "Different hash codes: original %x, archived %x", src_hash, archived_hash); 513 } 514 } 515 516 // Relocate an element in the buffered copy of HeapShared::roots() 517 template <typename T> void ArchiveHeapWriter::relocate_root_at(oop requested_roots, int index) { 518 size_t offset = (size_t)((objArrayOop)requested_roots)->obj_at_offset<T>(index); 519 relocate_field_in_buffer<T>((T*)(buffered_heap_roots_addr() + offset)); 520 } 521 522 class ArchiveHeapWriter::EmbeddedOopRelocator: public BasicOopIterateClosure { 523 oop _src_obj; 524 address _buffered_obj; 525 526 public: 527 EmbeddedOopRelocator(oop src_obj, address buffered_obj) : 528 _src_obj(src_obj), _buffered_obj(buffered_obj) {} 529 530 void do_oop(narrowOop *p) { EmbeddedOopRelocator::do_oop_work(p); } 531 void do_oop( oop *p) { EmbeddedOopRelocator::do_oop_work(p); } 532 533 private: 534 template <class T> void do_oop_work(T *p) { 535 size_t field_offset = pointer_delta(p, _src_obj, sizeof(char)); 536 ArchiveHeapWriter::relocate_field_in_buffer<T>((T*)(_buffered_obj + field_offset)); 537 } 538 }; 539 540 // Update all oop fields embedded in the buffered objects 541 void ArchiveHeapWriter::relocate_embedded_oops(GrowableArrayCHeap<oop, mtClassShared>* roots, 542 GrowableArray<ArchiveHeapBitmapInfo>* closed_bitmaps, 543 GrowableArray<ArchiveHeapBitmapInfo>* open_bitmaps) { 544 size_t oopmap_unit = (UseCompressedOops ? sizeof(narrowOop) : sizeof(oop)); 545 size_t closed_region_byte_size = _closed_top - _closed_bottom; 546 size_t open_region_byte_size = _open_top - _open_bottom; 547 ResourceBitMap closed_oopmap(closed_region_byte_size / oopmap_unit); 548 ResourceBitMap open_oopmap (open_region_byte_size / oopmap_unit); 549 550 _closed_oopmap = &closed_oopmap; 551 _open_oopmap = &open_oopmap; 552 553 auto iterator = [&] (oop src_obj, HeapShared::CachedOopInfo& info) { 554 oop requested_obj = requested_obj_from_buffer_offset(info.buffer_offset()); 555 update_header_for_requested_obj(requested_obj, src_obj, src_obj->klass()); 556 557 address buffered_obj = offset_to_buffered_address<address>(info.buffer_offset()); 558 EmbeddedOopRelocator relocator(src_obj, buffered_obj); 559 560 src_obj->oop_iterate(&relocator); 561 }; 562 HeapShared::archived_object_cache()->iterate_all(iterator); 563 564 // Relocate HeapShared::roots(), which is created in copy_roots_to_buffer() and 565 // doesn't have a corresponding src_obj, so we can't use EmbeddedOopRelocator on it. 566 oop requested_roots = requested_obj_from_buffer_offset(_heap_roots_bottom); 567 update_header_for_requested_obj(requested_roots, nullptr, Universe::objectArrayKlassObj()); 568 int length = roots != nullptr ? roots->length() : 0; 569 for (int i = 0; i < length; i++) { 570 if (UseCompressedOops) { 571 relocate_root_at<narrowOop>(requested_roots, i); 572 } else { 573 relocate_root_at<oop>(requested_roots, i); 574 } 575 } 576 577 closed_bitmaps->append(make_bitmap_info(&closed_oopmap, /*is_open=*/false, /*is_oopmap=*/true)); 578 open_bitmaps ->append(make_bitmap_info(&open_oopmap, /*is_open=*/false, /*is_oopmap=*/true)); 579 580 closed_bitmaps->append(compute_ptrmap(/*is_open=*/false)); 581 open_bitmaps ->append(compute_ptrmap(/*is_open=*/true)); 582 583 _closed_oopmap = nullptr; 584 _open_oopmap = nullptr; 585 } 586 587 ArchiveHeapBitmapInfo ArchiveHeapWriter::make_bitmap_info(ResourceBitMap* bitmap, bool is_open, bool is_oopmap) { 588 size_t size_in_bits = bitmap->size(); 589 size_t size_in_bytes; 590 uintptr_t* buffer; 591 592 if (size_in_bits > 0) { 593 size_in_bytes = bitmap->size_in_bytes(); 594 buffer = (uintptr_t*)NEW_C_HEAP_ARRAY(char, size_in_bytes, mtInternal); 595 bitmap->write_to(buffer, size_in_bytes); 596 } else { 597 size_in_bytes = 0; 598 buffer = nullptr; 599 } 600 601 log_info(cds, heap)("%s @ " INTPTR_FORMAT " (" SIZE_FORMAT_W(6) " bytes) for %s heap region", 602 is_oopmap ? "Oopmap" : "Ptrmap", 603 p2i(buffer), size_in_bytes, 604 is_open? "open" : "closed"); 605 606 ArchiveHeapBitmapInfo info; 607 info._map = (address)buffer; 608 info._size_in_bits = size_in_bits; 609 info._size_in_bytes = size_in_bytes; 610 611 return info; 612 } 613 614 void ArchiveHeapWriter::mark_native_pointer(oop src_obj, int field_offset) { 615 Metadata* ptr = src_obj->metadata_field_acquire(field_offset); 616 if (ptr != nullptr) { 617 NativePointerInfo info; 618 info._src_obj = src_obj; 619 info._field_offset = field_offset; 620 _native_pointers->append(info); 621 } 622 } 623 624 ArchiveHeapBitmapInfo ArchiveHeapWriter::compute_ptrmap(bool is_open) { 625 int num_non_null_ptrs = 0; 626 Metadata** bottom = (Metadata**) (is_open ? _requested_open_region_bottom: _requested_closed_region_bottom); 627 Metadata** top = (Metadata**) (is_open ? _requested_open_region_top: _requested_closed_region_top); // exclusive 628 ResourceBitMap ptrmap(top - bottom); 629 630 for (int i = 0; i < _native_pointers->length(); i++) { 631 NativePointerInfo info = _native_pointers->at(i); 632 oop src_obj = info._src_obj; 633 int field_offset = info._field_offset; 634 HeapShared::CachedOopInfo* p = HeapShared::archived_object_cache()->get(src_obj); 635 if (p->in_open_region() == is_open) { 636 // requested_field_addr = the address of this field in the requested space 637 oop requested_obj = requested_obj_from_buffer_offset(p->buffer_offset()); 638 Metadata** requested_field_addr = (Metadata**)(cast_from_oop<address>(requested_obj) + field_offset); 639 assert(bottom <= requested_field_addr && requested_field_addr < top, "range check"); 640 641 // Mark this field in the bitmap 642 BitMap::idx_t idx = requested_field_addr - bottom; 643 ptrmap.set_bit(idx); 644 num_non_null_ptrs ++; 645 646 // Set the native pointer to the requested address of the metadata (at runtime, the metadata will have 647 // this address if the RO/RW regions are mapped at the default location). 648 649 Metadata** buffered_field_addr = requested_addr_to_buffered_addr(requested_field_addr); 650 Metadata* native_ptr = *buffered_field_addr; 651 assert(native_ptr != nullptr, "sanity"); 652 653 address buffered_native_ptr = ArchiveBuilder::current()->get_buffered_addr((address)native_ptr); 654 address requested_native_ptr = ArchiveBuilder::current()->to_requested(buffered_native_ptr); 655 *buffered_field_addr = (Metadata*)requested_native_ptr; 656 } 657 } 658 659 log_info(cds, heap)("compute_ptrmap: marked %d non-null native pointers for %s heap region", 660 num_non_null_ptrs, is_open ? "open" : "closed"); 661 662 if (num_non_null_ptrs == 0) { 663 ResourceBitMap empty; 664 return make_bitmap_info(&empty, is_open, /*is_oopmap=*/ false); 665 } else { 666 return make_bitmap_info(&ptrmap, is_open, /*is_oopmap=*/ false); 667 } 668 } 669 670 #endif // INCLUDE_CDS_JAVA_HEAP