1 /* 2 * Copyright (c) 2015, 2019, 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 #include "precompiled.hpp" 25 #include "gc/shared/gcArguments.hpp" 26 #include "gc/shared/oopStorage.hpp" 27 #include "gc/z/zAddress.hpp" 28 #include "gc/z/zGlobals.hpp" 29 #include "gc/z/zHeap.inline.hpp" 30 #include "gc/z/zHeapIterator.hpp" 31 #include "gc/z/zList.inline.hpp" 32 #include "gc/z/zLock.inline.hpp" 33 #include "gc/z/zMark.inline.hpp" 34 #include "gc/z/zOopClosures.inline.hpp" 35 #include "gc/z/zPage.inline.hpp" 36 #include "gc/z/zPageTable.inline.hpp" 37 #include "gc/z/zRelocationSet.inline.hpp" 38 #include "gc/z/zResurrection.hpp" 39 #include "gc/z/zRootsIterator.hpp" 40 #include "gc/z/zStat.hpp" 41 #include "gc/z/zTask.hpp" 42 #include "gc/z/zThread.hpp" 43 #include "gc/z/zTracer.inline.hpp" 44 #include "gc/z/zVirtualMemory.inline.hpp" 45 #include "gc/z/zWorkers.inline.hpp" 46 #include "logging/log.hpp" 47 #include "memory/resourceArea.hpp" 48 #include "oops/oop.inline.hpp" 49 #include "runtime/arguments.hpp" 50 #include "runtime/safepoint.hpp" 51 #include "runtime/thread.hpp" 52 #include "utilities/align.hpp" 53 #include "utilities/debug.hpp" 54 55 static const ZStatSampler ZSamplerHeapUsedBeforeMark("Memory", "Heap Used Before Mark", ZStatUnitBytes); 56 static const ZStatSampler ZSamplerHeapUsedAfterMark("Memory", "Heap Used After Mark", ZStatUnitBytes); 57 static const ZStatSampler ZSamplerHeapUsedBeforeRelocation("Memory", "Heap Used Before Relocation", ZStatUnitBytes); 58 static const ZStatSampler ZSamplerHeapUsedAfterRelocation("Memory", "Heap Used After Relocation", ZStatUnitBytes); 59 static const ZStatCounter ZCounterUndoPageAllocation("Memory", "Undo Page Allocation", ZStatUnitOpsPerSecond); 60 static const ZStatCounter ZCounterOutOfMemory("Memory", "Out Of Memory", ZStatUnitOpsPerSecond); 61 62 ZHeap* ZHeap::_heap = NULL; 63 64 ZHeap::ZHeap() : 65 _workers(), 66 _object_allocator(_workers.nworkers()), 67 _page_allocator(heap_min_size(), heap_initial_size(), heap_max_size(), heap_max_reserve_size()), 68 _page_table(), 69 _forwarding_table(), 70 _mark(&_workers, &_page_table), 71 _reference_processor(&_workers), 72 _weak_roots_processor(&_workers), 73 _relocate(&_workers), 74 _relocation_set(), 75 _unload(&_workers), 76 _serviceability(heap_min_size(), heap_max_size()) { 77 // Install global heap instance 78 assert(_heap == NULL, "Already initialized"); 79 _heap = this; 80 81 // Update statistics 82 ZStatHeap::set_at_initialize(heap_max_size(), heap_max_reserve_size()); 83 } 84 85 size_t ZHeap::heap_min_size() const { 86 return MinHeapSize; 87 } 88 89 size_t ZHeap::heap_initial_size() const { 90 return InitialHeapSize; 91 } 92 93 size_t ZHeap::heap_max_size() const { 94 return MaxHeapSize; 95 } 96 97 size_t ZHeap::heap_max_reserve_size() const { 98 // Reserve one small page per worker plus one shared medium page. This is still just 99 // an estimate and doesn't guarantee that we can't run out of memory during relocation. 100 const size_t max_reserve_size = (_workers.nworkers() * ZPageSizeSmall) + ZPageSizeMedium; 101 return MIN2(max_reserve_size, heap_max_size()); 102 } 103 104 bool ZHeap::is_initialized() const { 105 return _page_allocator.is_initialized() && _mark.is_initialized(); 106 } 107 108 size_t ZHeap::min_capacity() const { 109 return _page_allocator.min_capacity(); 110 } 111 112 size_t ZHeap::max_capacity() const { 113 return _page_allocator.max_capacity(); 114 } 115 116 size_t ZHeap::current_max_capacity() const { 117 return _page_allocator.current_max_capacity(); 118 } 119 120 size_t ZHeap::capacity() const { 121 return _page_allocator.capacity(); 122 } 123 124 size_t ZHeap::max_reserve() const { 125 return _page_allocator.max_reserve(); 126 } 127 128 size_t ZHeap::used_high() const { 129 return _page_allocator.used_high(); 130 } 131 132 size_t ZHeap::used_low() const { 133 return _page_allocator.used_low(); 134 } 135 136 size_t ZHeap::used() const { 137 return _page_allocator.used(); 138 } 139 140 size_t ZHeap::unused() const { 141 return _page_allocator.unused(); 142 } 143 144 size_t ZHeap::allocated() const { 145 return _page_allocator.allocated(); 146 } 147 148 size_t ZHeap::reclaimed() const { 149 return _page_allocator.reclaimed(); 150 } 151 152 size_t ZHeap::tlab_capacity() const { 153 return capacity(); 154 } 155 156 size_t ZHeap::tlab_used() const { 157 return _object_allocator.used(); 158 } 159 160 size_t ZHeap::max_tlab_size() const { 161 return ZObjectSizeLimitSmall; 162 } 163 164 size_t ZHeap::unsafe_max_tlab_alloc() const { 165 size_t size = _object_allocator.remaining(); 166 167 if (size < MinTLABSize) { 168 // The remaining space in the allocator is not enough to 169 // fit the smallest possible TLAB. This means that the next 170 // TLAB allocation will force the allocator to get a new 171 // backing page anyway, which in turn means that we can then 172 // fit the largest possible TLAB. 173 size = max_tlab_size(); 174 } 175 176 return MIN2(size, max_tlab_size()); 177 } 178 179 bool ZHeap::is_in(uintptr_t addr) const { 180 if (addr < ZAddressReservedStart || addr >= ZAddressReservedEnd) { 181 return false; 182 } 183 184 const ZPage* const page = _page_table.get(addr); 185 if (page != NULL) { 186 return page->is_in(addr); 187 } 188 189 return false; 190 } 191 192 uintptr_t ZHeap::block_start(uintptr_t addr) const { 193 const ZPage* const page = _page_table.get(addr); 194 return page->block_start(addr); 195 } 196 197 bool ZHeap::block_is_obj(uintptr_t addr) const { 198 const ZPage* const page = _page_table.get(addr); 199 return page->block_is_obj(addr); 200 } 201 202 uint ZHeap::nconcurrent_worker_threads() const { 203 return _workers.nconcurrent(); 204 } 205 206 uint ZHeap::nconcurrent_no_boost_worker_threads() const { 207 return _workers.nconcurrent_no_boost(); 208 } 209 210 void ZHeap::set_boost_worker_threads(bool boost) { 211 _workers.set_boost(boost); 212 } 213 214 void ZHeap::worker_threads_do(ThreadClosure* tc) const { 215 _workers.threads_do(tc); 216 } 217 218 void ZHeap::print_worker_threads_on(outputStream* st) const { 219 _workers.print_threads_on(st); 220 } 221 222 void ZHeap::out_of_memory() { 223 ResourceMark rm; 224 225 ZStatInc(ZCounterOutOfMemory); 226 log_info(gc)("Out Of Memory (%s)", Thread::current()->name()); 227 } 228 229 ZPage* ZHeap::alloc_page(uint8_t type, size_t size, ZAllocationFlags flags) { 230 ZPage* const page = _page_allocator.alloc_page(type, size, flags); 231 if (page != NULL) { 232 // Insert page table entry 233 _page_table.insert(page); 234 } 235 236 return page; 237 } 238 239 void ZHeap::undo_alloc_page(ZPage* page) { 240 assert(page->is_allocating(), "Invalid page state"); 241 242 ZStatInc(ZCounterUndoPageAllocation); 243 log_trace(gc)("Undo page allocation, thread: " PTR_FORMAT " (%s), page: " PTR_FORMAT ", size: " SIZE_FORMAT, 244 ZThread::id(), ZThread::name(), p2i(page), page->size()); 245 246 free_page(page, false /* reclaimed */); 247 } 248 249 void ZHeap::free_page(ZPage* page, bool reclaimed) { 250 // Remove page table entry 251 _page_table.remove(page); 252 253 // Free page 254 _page_allocator.free_page(page, reclaimed); 255 } 256 257 uint64_t ZHeap::uncommit(uint64_t delay) { 258 return _page_allocator.uncommit(delay); 259 } 260 261 void ZHeap::before_flip() { 262 if (ZVerifyViews) { 263 // Unmap all pages 264 _page_allocator.debug_unmap_all_pages(); 265 } 266 } 267 268 void ZHeap::after_flip() { 269 if (ZVerifyViews) { 270 // Map all pages 271 ZPageTableIterator iter(&_page_table); 272 for (ZPage* page; iter.next(&page);) { 273 _page_allocator.debug_map_page(page); 274 } 275 _page_allocator.debug_map_cached_pages(); 276 } 277 } 278 279 void ZHeap::flip_to_marked() { 280 before_flip(); 281 ZAddress::flip_to_marked(); 282 after_flip(); 283 } 284 285 void ZHeap::flip_to_remapped() { 286 before_flip(); 287 ZAddress::flip_to_remapped(); 288 after_flip(); 289 } 290 291 void ZHeap::mark_start() { 292 assert(SafepointSynchronize::is_at_safepoint(), "Should be at safepoint"); 293 294 // Update statistics 295 ZStatSample(ZSamplerHeapUsedBeforeMark, used()); 296 297 // Flip address view 298 flip_to_marked(); 299 300 // Retire allocating pages 301 _object_allocator.retire_pages(); 302 303 // Reset allocated/reclaimed/used statistics 304 _page_allocator.reset_statistics(); 305 306 // Reset encountered/dropped/enqueued statistics 307 _reference_processor.reset_statistics(); 308 309 // Enter mark phase 310 ZGlobalPhase = ZPhaseMark; 311 312 // Reset marking information and mark roots 313 _mark.start(); 314 315 // Update statistics 316 ZStatHeap::set_at_mark_start(capacity(), used()); 317 } 318 319 void ZHeap::mark(bool initial) { 320 _mark.mark(initial); 321 } 322 323 void ZHeap::mark_flush_and_free(Thread* thread) { 324 _mark.flush_and_free(thread); 325 } 326 327 bool ZHeap::mark_end() { 328 assert(SafepointSynchronize::is_at_safepoint(), "Should be at safepoint"); 329 330 // Try end marking 331 if (!_mark.end()) { 332 // Marking not completed, continue concurrent mark 333 return false; 334 } 335 336 // Enter mark completed phase 337 ZGlobalPhase = ZPhaseMarkCompleted; 338 339 // Update statistics 340 ZStatSample(ZSamplerHeapUsedAfterMark, used()); 341 ZStatHeap::set_at_mark_end(capacity(), allocated(), used()); 342 343 // Block resurrection of weak/phantom references 344 ZResurrection::block(); 345 346 // Process weak roots 347 _weak_roots_processor.process_weak_roots(); 348 349 // Prepare to unload unused classes and code 350 _unload.prepare(); 351 352 return true; 353 } 354 355 void ZHeap::set_soft_reference_policy(bool clear) { 356 _reference_processor.set_soft_reference_policy(clear); 357 } 358 359 void ZHeap::process_non_strong_references() { 360 // Process Soft/Weak/Final/PhantomReferences 361 _reference_processor.process_references(); 362 363 // Process concurrent weak roots 364 _weak_roots_processor.process_concurrent_weak_roots(); 365 366 // Unload unused classes and code 367 _unload.unload(); 368 369 // Unblock resurrection of weak/phantom references 370 ZResurrection::unblock(); 371 372 // Enqueue Soft/Weak/Final/PhantomReferences. Note that this 373 // must be done after unblocking resurrection. Otherwise the 374 // Finalizer thread could call Reference.get() on the Finalizers 375 // that were just enqueued, which would incorrectly return null 376 // during the resurrection block window, since such referents 377 // are only Finalizable marked. 378 _reference_processor.enqueue_references(); 379 } 380 381 void ZHeap::select_relocation_set() { 382 // Do not allow pages to be deleted 383 _page_allocator.enable_deferred_delete(); 384 385 // Register relocatable pages with selector 386 ZRelocationSetSelector selector; 387 ZPageTableIterator pt_iter(&_page_table); 388 for (ZPage* page; pt_iter.next(&page);) { 389 if (!page->is_relocatable()) { 390 // Not relocatable, don't register 391 continue; 392 } 393 394 if (page->is_marked()) { 395 // Register live page 396 selector.register_live_page(page); 397 } else { 398 // Register garbage page 399 selector.register_garbage_page(page); 400 401 // Reclaim page immediately 402 free_page(page, true /* reclaimed */); 403 } 404 } 405 406 // Allow pages to be deleted 407 _page_allocator.disable_deferred_delete(); 408 409 // Select pages to relocate 410 selector.select(&_relocation_set); 411 412 // Setup forwarding table 413 ZRelocationSetIterator rs_iter(&_relocation_set); 414 for (ZForwarding* forwarding; rs_iter.next(&forwarding);) { 415 _forwarding_table.insert(forwarding); 416 } 417 418 // Update statistics 419 ZStatRelocation::set_at_select_relocation_set(selector.relocating()); 420 ZStatHeap::set_at_select_relocation_set(selector.live(), 421 selector.garbage(), 422 reclaimed()); 423 } 424 425 void ZHeap::reset_relocation_set() { 426 // Reset forwarding table 427 ZRelocationSetIterator iter(&_relocation_set); 428 for (ZForwarding* forwarding; iter.next(&forwarding);) { 429 _forwarding_table.remove(forwarding); 430 } 431 432 // Reset relocation set 433 _relocation_set.reset(); 434 } 435 436 void ZHeap::relocate_start() { 437 assert(SafepointSynchronize::is_at_safepoint(), "Should be at safepoint"); 438 439 // Finish unloading of classes and code 440 _unload.finish(); 441 442 // Flip address view 443 flip_to_remapped(); 444 445 // Enter relocate phase 446 ZGlobalPhase = ZPhaseRelocate; 447 448 // Update statistics 449 ZStatSample(ZSamplerHeapUsedBeforeRelocation, used()); 450 ZStatHeap::set_at_relocate_start(capacity(), allocated(), used()); 451 452 // Remap/Relocate roots 453 _relocate.start(); 454 } 455 456 void ZHeap::relocate() { 457 // Relocate relocation set 458 const bool success = _relocate.relocate(&_relocation_set); 459 460 // Update statistics 461 ZStatSample(ZSamplerHeapUsedAfterRelocation, used()); 462 ZStatRelocation::set_at_relocate_end(success); 463 ZStatHeap::set_at_relocate_end(capacity(), allocated(), reclaimed(), 464 used(), used_high(), used_low()); 465 } 466 467 void ZHeap::object_iterate(ObjectClosure* cl, bool visit_referents) { 468 assert(SafepointSynchronize::is_at_safepoint(), "Should be at safepoint"); 469 470 ZHeapIterator iter(visit_referents); 471 iter.objects_do(cl); 472 } 473 474 void ZHeap::serviceability_initialize() { 475 _serviceability.initialize(); 476 } 477 478 GCMemoryManager* ZHeap::serviceability_memory_manager() { 479 return _serviceability.memory_manager(); 480 } 481 482 MemoryPool* ZHeap::serviceability_memory_pool() { 483 return _serviceability.memory_pool(); 484 } 485 486 ZServiceabilityCounters* ZHeap::serviceability_counters() { 487 return _serviceability.counters(); 488 } 489 490 void ZHeap::print_on(outputStream* st) const { 491 st->print_cr(" ZHeap used " SIZE_FORMAT "M, capacity " SIZE_FORMAT "M, max capacity " SIZE_FORMAT "M", 492 used() / M, 493 capacity() / M, 494 max_capacity() / M); 495 MetaspaceUtils::print_on(st); 496 } 497 498 void ZHeap::print_extended_on(outputStream* st) const { 499 print_on(st); 500 st->cr(); 501 502 // Do not allow pages to be deleted 503 _page_allocator.enable_deferred_delete(); 504 505 // Print all pages 506 ZPageTableIterator iter(&_page_table); 507 for (ZPage* page; iter.next(&page);) { 508 page->print_on(st); 509 } 510 511 // Allow pages to be deleted 512 _page_allocator.enable_deferred_delete(); 513 514 st->cr(); 515 } 516 517 class ZVerifyRootsTask : public ZTask { 518 private: 519 ZStatTimerDisable _disable; 520 ZRootsIterator _strong_roots; 521 ZWeakRootsIterator _weak_roots; 522 523 public: 524 ZVerifyRootsTask() : 525 ZTask("ZVerifyRootsTask"), 526 _disable(), 527 _strong_roots(), 528 _weak_roots() {} 529 530 virtual void work() { 531 ZStatTimerDisable disable; 532 ZVerifyOopClosure cl; 533 _strong_roots.oops_do(&cl); 534 _weak_roots.oops_do(&cl); 535 } 536 }; 537 538 void ZHeap::verify() { 539 // Heap verification can only be done between mark end and 540 // relocate start. This is the only window where all oop are 541 // good and the whole heap is in a consistent state. 542 guarantee(ZGlobalPhase == ZPhaseMarkCompleted, "Invalid phase"); 543 544 { 545 ZVerifyRootsTask task; 546 _workers.run_parallel(&task); 547 } 548 549 { 550 ZVerifyObjectClosure cl; 551 object_iterate(&cl, false /* visit_referents */); 552 } 553 }