1 /* 2 * Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "cds/archiveBuilder.hpp" 27 #include "cds/archiveHeapLoader.inline.hpp" 28 #include "cds/archiveUtils.hpp" 29 #include "cds/cdsConfig.hpp" 30 #include "cds/classListParser.hpp" 31 #include "cds/classListWriter.hpp" 32 #include "cds/dynamicArchive.hpp" 33 #include "cds/filemap.hpp" 34 #include "cds/heapShared.hpp" 35 #include "cds/metaspaceShared.hpp" 36 #include "classfile/systemDictionaryShared.hpp" 37 #include "classfile/vmClasses.hpp" 38 #include "interpreter/bootstrapInfo.hpp" 39 #include "memory/metaspaceUtils.hpp" 40 #include "memory/resourceArea.hpp" 41 #include "oops/compressedOops.inline.hpp" 42 #include "oops/klass.inline.hpp" 43 #include "runtime/arguments.hpp" 44 #include "utilities/bitMap.inline.hpp" 45 #include "utilities/debug.hpp" 46 #include "utilities/formatBuffer.hpp" 47 #include "utilities/globalDefinitions.hpp" 48 #include "utilities/spinYield.hpp" 49 50 CHeapBitMap* ArchivePtrMarker::_ptrmap = nullptr; 51 CHeapBitMap* ArchivePtrMarker::_rw_ptrmap = nullptr; 52 CHeapBitMap* ArchivePtrMarker::_ro_ptrmap = nullptr; 53 VirtualSpace* ArchivePtrMarker::_vs; 54 55 bool ArchivePtrMarker::_compacted; 56 57 void ArchivePtrMarker::initialize(CHeapBitMap* ptrmap, VirtualSpace* vs) { 58 assert(_ptrmap == nullptr, "initialize only once"); 59 assert(_rw_ptrmap == nullptr, "initialize only once"); 60 assert(_ro_ptrmap == nullptr, "initialize only once"); 61 _vs = vs; 62 _compacted = false; 63 _ptrmap = ptrmap; 64 65 // Use this as initial guesstimate. We should need less space in the 66 // archive, but if we're wrong the bitmap will be expanded automatically. 67 size_t estimated_archive_size = MetaspaceGC::capacity_until_GC(); 68 // But set it smaller in debug builds so we always test the expansion code. 69 // (Default archive is about 12MB). 70 DEBUG_ONLY(estimated_archive_size = 6 * M); 71 72 // We need one bit per pointer in the archive. 73 _ptrmap->initialize(estimated_archive_size / sizeof(intptr_t)); 74 } 75 76 void ArchivePtrMarker::initialize_rw_ro_maps(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap) { 77 address* rw_bottom = (address*)ArchiveBuilder::current()->rw_region()->base(); 78 address* ro_bottom = (address*)ArchiveBuilder::current()->ro_region()->base(); 79 80 _rw_ptrmap = rw_ptrmap; 81 _ro_ptrmap = ro_ptrmap; 82 83 size_t rw_size = ArchiveBuilder::current()->rw_region()->used() / sizeof(address); 84 size_t ro_size = ArchiveBuilder::current()->ro_region()->used() / sizeof(address); 85 // ro_start is the first bit in _ptrmap that covers the pointer that would sit at ro_bottom. 86 // E.g., if rw_bottom = (address*)100 87 // ro_bottom = (address*)116 88 // then for 64-bit platform: 89 // ro_start = ro_bottom - rw_bottom = (116 - 100) / sizeof(address) = 2; 90 size_t ro_start = ro_bottom - rw_bottom; 91 92 // Note: ptrmap is big enough only to cover the last pointer in ro_region. 93 // See ArchivePtrMarker::compact() 94 _rw_ptrmap->initialize(rw_size); 95 _ro_ptrmap->initialize(_ptrmap->size() - ro_start); 96 97 for (size_t rw_bit = 0; rw_bit < _rw_ptrmap->size(); rw_bit++) { 98 _rw_ptrmap->at_put(rw_bit, _ptrmap->at(rw_bit)); 99 } 100 101 for(size_t ro_bit = ro_start; ro_bit < _ptrmap->size(); ro_bit++) { 102 _ro_ptrmap->at_put(ro_bit-ro_start, _ptrmap->at(ro_bit)); 103 } 104 assert(_ptrmap->size() - ro_start == _ro_ptrmap->size(), "must be"); 105 } 106 107 void ArchivePtrMarker::mark_pointer(address* ptr_loc) { 108 assert(_ptrmap != nullptr, "not initialized"); 109 assert(!_compacted, "cannot mark anymore"); 110 111 if (ptr_base() <= ptr_loc && ptr_loc < ptr_end()) { 112 address value = *ptr_loc; 113 // We don't want any pointer that points to very bottom of the archive, otherwise when 114 // MetaspaceShared::default_base_address()==0, we can't distinguish between a pointer 115 // to nothing (null) vs a pointer to an objects that happens to be at the very bottom 116 // of the archive. 117 assert(value != (address)ptr_base(), "don't point to the bottom of the archive"); 118 119 if (value != nullptr) { 120 assert(uintx(ptr_loc) % sizeof(intptr_t) == 0, "pointers must be stored in aligned addresses"); 121 size_t idx = ptr_loc - ptr_base(); 122 if (_ptrmap->size() <= idx) { 123 _ptrmap->resize((idx + 1) * 2); 124 } 125 assert(idx < _ptrmap->size(), "must be"); 126 _ptrmap->set_bit(idx); 127 //tty->print_cr("Marking pointer [" PTR_FORMAT "] -> " PTR_FORMAT " @ " SIZE_FORMAT_W(5), p2i(ptr_loc), p2i(*ptr_loc), idx); 128 } 129 } 130 } 131 132 void ArchivePtrMarker::clear_pointer(address* ptr_loc) { 133 assert(_ptrmap != nullptr, "not initialized"); 134 assert(!_compacted, "cannot clear anymore"); 135 136 assert(ptr_base() <= ptr_loc && ptr_loc < ptr_end(), "must be"); 137 assert(uintx(ptr_loc) % sizeof(intptr_t) == 0, "pointers must be stored in aligned addresses"); 138 size_t idx = ptr_loc - ptr_base(); 139 assert(idx < _ptrmap->size(), "cannot clear pointers that have not been marked"); 140 _ptrmap->clear_bit(idx); 141 //tty->print_cr("Clearing pointer [" PTR_FORMAT "] -> " PTR_FORMAT " @ " SIZE_FORMAT_W(5), p2i(ptr_loc), p2i(*ptr_loc), idx); 142 } 143 144 class ArchivePtrBitmapCleaner: public BitMapClosure { 145 CHeapBitMap* _ptrmap; 146 address* _ptr_base; 147 address _relocatable_base; 148 address _relocatable_end; 149 size_t _max_non_null_offset; 150 151 public: 152 ArchivePtrBitmapCleaner(CHeapBitMap* ptrmap, address* ptr_base, address relocatable_base, address relocatable_end) : 153 _ptrmap(ptrmap), _ptr_base(ptr_base), 154 _relocatable_base(relocatable_base), _relocatable_end(relocatable_end), _max_non_null_offset(0) {} 155 156 bool do_bit(size_t offset) { 157 address* ptr_loc = _ptr_base + offset; 158 address ptr_value = *ptr_loc; 159 if (ptr_value != nullptr) { 160 assert(_relocatable_base <= ptr_value && ptr_value < _relocatable_end, "do not point to arbitrary locations!"); 161 if (_max_non_null_offset < offset) { 162 _max_non_null_offset = offset; 163 } 164 } else { 165 _ptrmap->clear_bit(offset); 166 DEBUG_ONLY(log_trace(cds, reloc)("Clearing pointer [" PTR_FORMAT "] -> null @ " SIZE_FORMAT_W(9), p2i(ptr_loc), offset)); 167 } 168 169 return true; 170 } 171 172 size_t max_non_null_offset() const { return _max_non_null_offset; } 173 }; 174 175 void ArchivePtrMarker::compact(address relocatable_base, address relocatable_end) { 176 assert(!_compacted, "cannot compact again"); 177 ArchivePtrBitmapCleaner cleaner(_ptrmap, ptr_base(), relocatable_base, relocatable_end); 178 _ptrmap->iterate(&cleaner); 179 compact(cleaner.max_non_null_offset()); 180 } 181 182 void ArchivePtrMarker::compact(size_t max_non_null_offset) { 183 assert(!_compacted, "cannot compact again"); 184 _ptrmap->resize(max_non_null_offset + 1); 185 _compacted = true; 186 } 187 188 char* DumpRegion::expand_top_to(char* newtop) { 189 assert(is_allocatable(), "must be initialized and not packed"); 190 assert(newtop >= _top, "must not grow backwards"); 191 if (newtop > _end) { 192 ArchiveBuilder::current()->report_out_of_space(_name, newtop - _top); 193 ShouldNotReachHere(); 194 } 195 196 commit_to(newtop); 197 _top = newtop; 198 199 if (_max_delta > 0) { 200 uintx delta = ArchiveBuilder::current()->buffer_to_offset((address)(newtop-1)); 201 if (delta > _max_delta) { 202 // This is just a sanity check and should not appear in any real world usage. This 203 // happens only if you allocate more than 2GB of shared objects and would require 204 // millions of shared classes. 205 log_error(cds)("Out of memory in the CDS archive: Please reduce the number of shared classes."); 206 MetaspaceShared::unrecoverable_writing_error(); 207 } 208 } 209 210 return _top; 211 } 212 213 void DumpRegion::commit_to(char* newtop) { 214 assert(CDSConfig::is_dumping_archive(), "sanity"); 215 char* base = _rs->base(); 216 size_t need_committed_size = newtop - base; 217 size_t has_committed_size = _vs->committed_size(); 218 if (need_committed_size < has_committed_size) { 219 return; 220 } 221 222 size_t min_bytes = need_committed_size - has_committed_size; 223 size_t preferred_bytes = 1 * M; 224 size_t uncommitted = _vs->reserved_size() - has_committed_size; 225 226 size_t commit = MAX2(min_bytes, preferred_bytes); 227 commit = MIN2(commit, uncommitted); 228 assert(commit <= uncommitted, "sanity"); 229 230 if (!_vs->expand_by(commit, false)) { 231 log_error(cds)("Failed to expand shared space to " SIZE_FORMAT " bytes", 232 need_committed_size); 233 MetaspaceShared::unrecoverable_writing_error(); 234 } 235 236 const char* which; 237 if (_rs->base() == (char*)MetaspaceShared::symbol_rs_base()) { 238 which = "symbol"; 239 } else { 240 which = "shared"; 241 } 242 log_debug(cds)("Expanding %s spaces by " SIZE_FORMAT_W(7) " bytes [total " SIZE_FORMAT_W(9) " bytes ending at %p]", 243 which, commit, _vs->actual_committed_size(), _vs->high()); 244 } 245 246 char* DumpRegion::allocate(size_t num_bytes, size_t alignment) { 247 // Always align to at least minimum alignment 248 alignment = MAX2(SharedSpaceObjectAlignment, alignment); 249 char* p = (char*)align_up(_top, alignment); 250 char* newtop = p + align_up(num_bytes, (size_t)SharedSpaceObjectAlignment); 251 expand_top_to(newtop); 252 memset(p, 0, newtop - p); 253 return p; 254 } 255 256 void DumpRegion::append_intptr_t(intptr_t n, bool need_to_mark) { 257 assert(is_aligned(_top, sizeof(intptr_t)), "bad alignment"); 258 intptr_t *p = (intptr_t*)_top; 259 char* newtop = _top + sizeof(intptr_t); 260 expand_top_to(newtop); 261 *p = n; 262 if (need_to_mark) { 263 ArchivePtrMarker::mark_pointer(p); 264 } 265 } 266 267 void DumpRegion::print(size_t total_bytes) const { 268 log_debug(cds)("%s space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used] at " INTPTR_FORMAT, 269 _name, used(), percent_of(used(), total_bytes), reserved(), percent_of(used(), reserved()), 270 p2i(ArchiveBuilder::current()->to_requested(_base))); 271 } 272 273 void DumpRegion::print_out_of_space_msg(const char* failing_region, size_t needed_bytes) { 274 log_error(cds)("[%-8s] " PTR_FORMAT " - " PTR_FORMAT " capacity =%9d, allocated =%9d", 275 _name, p2i(_base), p2i(_top), int(_end - _base), int(_top - _base)); 276 if (strcmp(_name, failing_region) == 0) { 277 log_error(cds)(" required = %d", int(needed_bytes)); 278 } 279 } 280 281 void DumpRegion::init(ReservedSpace* rs, VirtualSpace* vs) { 282 _rs = rs; 283 _vs = vs; 284 // Start with 0 committed bytes. The memory will be committed as needed. 285 if (!_vs->initialize(*_rs, 0)) { 286 fatal("Unable to allocate memory for shared space"); 287 } 288 _base = _top = _rs->base(); 289 _end = _rs->end(); 290 } 291 292 void DumpRegion::pack(DumpRegion* next) { 293 assert(!is_packed(), "sanity"); 294 _end = (char*)align_up(_top, MetaspaceShared::core_region_alignment()); 295 _is_packed = true; 296 if (next != nullptr) { 297 next->_rs = _rs; 298 next->_vs = _vs; 299 next->_base = next->_top = this->_end; 300 next->_end = _rs->end(); 301 } 302 } 303 304 void WriteClosure::do_ptr(void** p) { 305 // Write ptr into the archive; ptr can be: 306 // (a) null -> written as 0 307 // (b) a "buffered" address -> written as is 308 // (c) a "source" address -> convert to "buffered" and write 309 // The common case is (c). E.g., when writing the vmClasses into the archive. 310 // We have (b) only when we don't have a corresponding source object. E.g., 311 // the archived c++ vtable entries. 312 address ptr = *(address*)p; 313 if (ptr != nullptr && !ArchiveBuilder::current()->is_in_buffer_space(ptr)) { 314 ptr = ArchiveBuilder::current()->get_buffered_addr(ptr); 315 } 316 // null pointers do not need to be converted to offsets 317 if (ptr != nullptr) { 318 ptr = (address)ArchiveBuilder::current()->buffer_to_offset(ptr); 319 } 320 _dump_region->append_intptr_t((intptr_t)ptr, false); 321 } 322 323 void ReadClosure::do_ptr(void** p) { 324 assert(*p == nullptr, "initializing previous initialized pointer."); 325 intptr_t obj = nextPtr(); 326 assert(obj >= 0, "sanity."); 327 *p = (obj != 0) ? (void*)(_base_address + obj) : (void*)obj; 328 } 329 330 void ReadClosure::do_u4(u4* p) { 331 intptr_t obj = nextPtr(); 332 *p = (u4)(uintx(obj)); 333 } 334 335 void ReadClosure::do_int(int* p) { 336 intptr_t obj = nextPtr(); 337 *p = (int)(intx(obj)); 338 } 339 340 void ReadClosure::do_bool(bool* p) { 341 intptr_t obj = nextPtr(); 342 *p = (bool)(uintx(obj)); 343 } 344 345 void ReadClosure::do_tag(int tag) { 346 int old_tag; 347 old_tag = (int)(intptr_t)nextPtr(); 348 // do_int(&old_tag); 349 assert(tag == old_tag, "tag doesn't match (%d, expected %d)", old_tag, tag); 350 FileMapInfo::assert_mark(tag == old_tag); 351 } 352 353 void ArchiveUtils::log_to_classlist(BootstrapInfo* bootstrap_specifier, TRAPS) { 354 if (ClassListWriter::is_enabled()) { 355 if (SystemDictionaryShared::is_supported_invokedynamic(bootstrap_specifier)) { 356 const constantPoolHandle& pool = bootstrap_specifier->pool(); 357 if (SystemDictionaryShared::is_builtin_loader(pool->pool_holder()->class_loader_data())) { 358 // Currently lambda proxy classes are supported only for the built-in loaders. 359 ResourceMark rm(THREAD); 360 int pool_index = bootstrap_specifier->bss_index(); 361 ClassListWriter w; 362 w.stream()->print("%s %s", ClassListParser::lambda_proxy_tag(), pool->pool_holder()->name()->as_C_string()); 363 CDSIndyInfo cii; 364 ClassListParser::populate_cds_indy_info(pool, pool_index, &cii, CHECK); 365 GrowableArray<const char*>* indy_items = cii.items(); 366 for (int i = 0; i < indy_items->length(); i++) { 367 w.stream()->print(" %s", indy_items->at(i)); 368 } 369 w.stream()->cr(); 370 } 371 } 372 } 373 } 374 375 bool ArchiveUtils::has_aot_initialized_mirror(InstanceKlass* src_ik) { 376 if (SystemDictionaryShared::is_excluded_class(src_ik)) { 377 assert(!ArchiveBuilder::current()->has_been_buffered(src_ik), "sanity"); 378 return false; 379 } 380 return ArchiveBuilder::current()->get_buffered_addr(src_ik)->has_aot_initialized_mirror(); 381 } 382 383 size_t HeapRootSegments::size_in_bytes(size_t seg_idx) { 384 assert(seg_idx < _count, "In range"); 385 return objArrayOopDesc::object_size(size_in_elems(seg_idx)) * HeapWordSize; 386 } 387 388 int HeapRootSegments::size_in_elems(size_t seg_idx) { 389 assert(seg_idx < _count, "In range"); 390 if (seg_idx != _count - 1) { 391 return _max_size_in_elems; 392 } else { 393 // Last slice, leftover 394 return _roots_count % _max_size_in_elems; 395 } 396 } 397 398 size_t HeapRootSegments::segment_offset(size_t seg_idx) { 399 assert(seg_idx < _count, "In range"); 400 return _base_offset + seg_idx * _max_size_in_bytes; 401 } 402 403 ArchiveWorkers::ArchiveWorkers() : 404 _end_semaphore(0), 405 _num_workers(max_workers()), 406 _started_workers(0), 407 _finish_tokens(0), 408 _state(UNUSED), 409 _task(nullptr) {} 410 411 ArchiveWorkers::~ArchiveWorkers() { 412 assert(Atomic::load(&_state) != WORKING, "Should not be working"); 413 } 414 415 int ArchiveWorkers::max_workers() { 416 // The pool is used for short-lived bursty tasks. We do not want to spend 417 // too much time creating and waking up threads unnecessarily. Plus, we do 418 // not want to overwhelm large machines. This is why we want to be very 419 // conservative about the number of workers actually needed. 420 return MAX2(0, log2i_graceful(os::active_processor_count())); 421 } 422 423 bool ArchiveWorkers::is_parallel() { 424 return _num_workers > 0; 425 } 426 427 void ArchiveWorkers::start_worker_if_needed() { 428 while (true) { 429 int cur = Atomic::load(&_started_workers); 430 if (cur >= _num_workers) { 431 return; 432 } 433 if (Atomic::cmpxchg(&_started_workers, cur, cur + 1, memory_order_relaxed) == cur) { 434 new ArchiveWorkerThread(this); 435 return; 436 } 437 } 438 } 439 440 void ArchiveWorkers::run_task(ArchiveWorkerTask* task) { 441 assert(Atomic::load(&_state) == UNUSED, "Should be unused yet"); 442 assert(Atomic::load(&_task) == nullptr, "Should not have running tasks"); 443 Atomic::store(&_state, WORKING); 444 445 if (is_parallel()) { 446 run_task_multi(task); 447 } else { 448 run_task_single(task); 449 } 450 451 assert(Atomic::load(&_state) == WORKING, "Should be working"); 452 Atomic::store(&_state, SHUTDOWN); 453 } 454 455 void ArchiveWorkers::run_task_single(ArchiveWorkerTask* task) { 456 // Single thread needs no chunking. 457 task->configure_max_chunks(1); 458 459 // Execute the task ourselves, as there are no workers. 460 task->work(0, 1); 461 } 462 463 void ArchiveWorkers::run_task_multi(ArchiveWorkerTask* task) { 464 // Multiple threads can work with multiple chunks. 465 task->configure_max_chunks(_num_workers * CHUNKS_PER_WORKER); 466 467 // Set up the run and publish the task. Issue one additional finish token 468 // to cover the semaphore shutdown path, see below. 469 Atomic::store(&_finish_tokens, _num_workers + 1); 470 Atomic::release_store(&_task, task); 471 472 // Kick off pool startup by starting a single worker, and proceed 473 // immediately to executing the task locally. 474 start_worker_if_needed(); 475 476 // Execute the task ourselves, while workers are catching up. 477 // This allows us to hide parts of task handoff latency. 478 task->run(); 479 480 // Done executing task locally, wait for any remaining workers to complete. 481 // Once all workers report, we can proceed to termination. To do this safely, 482 // we need to make sure every worker has left. A spin-wait alone would suffice, 483 // but we do not want to burn cycles on it. A semaphore alone would not be safe, 484 // since workers can still be inside it as we proceed from wait here. So we block 485 // on semaphore first, and then spin-wait for all workers to terminate. 486 _end_semaphore.wait(); 487 SpinYield spin; 488 while (Atomic::load(&_finish_tokens) != 0) { 489 spin.wait(); 490 } 491 492 OrderAccess::fence(); 493 494 assert(Atomic::load(&_finish_tokens) == 0, "All tokens are consumed"); 495 } 496 497 void ArchiveWorkers::run_as_worker() { 498 assert(is_parallel(), "Should be in parallel mode"); 499 500 ArchiveWorkerTask* task = Atomic::load_acquire(&_task); 501 task->run(); 502 503 // All work done in threads should be visible to caller. 504 OrderAccess::fence(); 505 506 // Signal the pool the work is complete, and we are exiting. 507 // Worker cannot do anything else with the pool after this. 508 if (Atomic::sub(&_finish_tokens, 1, memory_order_relaxed) == 1) { 509 // Last worker leaving. Notify the pool it can unblock to spin-wait. 510 // Then consume the last token and leave. 511 _end_semaphore.signal(); 512 int last = Atomic::sub(&_finish_tokens, 1, memory_order_relaxed); 513 assert(last == 0, "Should be"); 514 } 515 } 516 517 void ArchiveWorkerTask::run() { 518 while (true) { 519 int chunk = Atomic::load(&_chunk); 520 if (chunk >= _max_chunks) { 521 return; 522 } 523 if (Atomic::cmpxchg(&_chunk, chunk, chunk + 1, memory_order_relaxed) == chunk) { 524 assert(0 <= chunk && chunk < _max_chunks, "Sanity"); 525 work(chunk, _max_chunks); 526 } 527 } 528 } 529 530 void ArchiveWorkerTask::configure_max_chunks(int max_chunks) { 531 if (_max_chunks == 0) { 532 _max_chunks = max_chunks; 533 } 534 } 535 536 ArchiveWorkerThread::ArchiveWorkerThread(ArchiveWorkers* pool) : NamedThread(), _pool(pool) { 537 set_name("ArchiveWorkerThread"); 538 if (os::create_thread(this, os::os_thread)) { 539 os::start_thread(this); 540 } else { 541 vm_exit_during_initialization("Unable to create archive worker", 542 os::native_thread_creation_failed_msg()); 543 } 544 } 545 546 void ArchiveWorkerThread::run() { 547 // Avalanche startup: each worker starts two others. 548 _pool->start_worker_if_needed(); 549 _pool->start_worker_if_needed(); 550 551 // Set ourselves up. 552 os::set_priority(this, NearMaxPriority); 553 554 // Work. 555 _pool->run_as_worker(); 556 } 557 558 void ArchiveWorkerThread::post_run() { 559 this->NamedThread::post_run(); 560 delete this; 561 }