1 /* 2 * Copyright (c) 2013, 2015, Red Hat, Inc. and/or its affiliates. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 #include "precompiled.hpp" 25 #include "memory/allocation.hpp" 26 27 #include "gc/shared/gcTimer.hpp" 28 #include "gc/shared/gcTraceTime.inline.hpp" 29 #include "gc/shared/parallelCleaning.hpp" 30 31 #include "gc/shenandoah/brooksPointer.hpp" 32 #include "gc/shenandoah/shenandoahBarrierSet.hpp" 33 #include "gc/shenandoah/shenandoahCollectionSet.hpp" 34 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp" 35 #include "gc/shenandoah/shenandoahConcurrentMark.hpp" 36 #include "gc/shenandoah/shenandoahConcurrentMark.inline.hpp" 37 #include "gc/shenandoah/shenandoahConcurrentThread.hpp" 38 #include "gc/shenandoah/shenandoahFreeSet.hpp" 39 #include "gc/shenandoah/shenandoahHeap.inline.hpp" 40 #include "gc/shenandoah/shenandoahHeapRegion.hpp" 41 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp" 42 #include "gc/shenandoah/shenandoahHumongous.hpp" 43 #include "gc/shenandoah/shenandoahMarkCompact.hpp" 44 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp" 45 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp" 46 #include "gc/shenandoah/shenandoahPartialGC.hpp" 47 #include "gc/shenandoah/shenandoahRootProcessor.hpp" 48 #include "gc/shenandoah/vm_operations_shenandoah.hpp" 49 50 #include "runtime/vmThread.hpp" 51 #include "services/mallocTracker.hpp" 52 53 SCMUpdateRefsClosure::SCMUpdateRefsClosure() : _heap(ShenandoahHeap::heap()) {} 54 55 #ifdef ASSERT 56 template <class T> 57 void AssertToSpaceClosure::do_oop_nv(T* p) { 58 T o = oopDesc::load_heap_oop(p); 59 if (! oopDesc::is_null(o)) { 60 oop obj = oopDesc::decode_heap_oop_not_null(o); 61 assert(oopDesc::unsafe_equals(obj, ShenandoahBarrierSet::resolve_oop_static_not_null(obj)), 62 "need to-space object here obj: "PTR_FORMAT" , rb(obj): "PTR_FORMAT", p: "PTR_FORMAT, 63 p2i(obj), p2i(ShenandoahBarrierSet::resolve_oop_static_not_null(obj)), p2i(p)); 64 } 65 } 66 67 void AssertToSpaceClosure::do_oop(narrowOop* p) { do_oop_nv(p); } 68 void AssertToSpaceClosure::do_oop(oop* p) { do_oop_nv(p); } 69 #endif 70 71 const char* ShenandoahHeap::name() const { 72 return "Shenandoah"; 73 } 74 75 void ShenandoahHeap::print_heap_locations(HeapWord* start, HeapWord* end) { 76 HeapWord* cur = NULL; 77 for (cur = start; cur < end; cur++) { 78 tty->print_cr(PTR_FORMAT" : "PTR_FORMAT, p2i(cur), p2i(*((HeapWord**) cur))); 79 } 80 } 81 82 class ShenandoahPretouchTask : public AbstractGangTask { 83 private: 84 ShenandoahHeapRegionSet* _regions; 85 const size_t _bitmap_size; 86 const size_t _page_size; 87 char* _bitmap0_base; 88 char* _bitmap1_base; 89 public: 90 ShenandoahPretouchTask(ShenandoahHeapRegionSet* regions, 91 char* bitmap0_base, char* bitmap1_base, size_t bitmap_size, 92 size_t page_size) : 93 AbstractGangTask("Shenandoah PreTouch", 94 Universe::is_fully_initialized() ? GCId::current_raw() : 95 // During VM initialization there is 96 // no GC cycle that this task can be 97 // associated with. 98 GCId::undefined()), 99 _bitmap0_base(bitmap0_base), 100 _bitmap1_base(bitmap1_base), 101 _regions(regions), 102 _bitmap_size(bitmap_size), 103 _page_size(page_size) { 104 _regions->clear_current_index(); 105 }; 106 107 virtual void work(uint worker_id) { 108 ShenandoahHeapRegion* r = _regions->claim_next(); 109 while (r != NULL) { 110 log_trace(gc, heap)("Pretouch region " SIZE_FORMAT ": " PTR_FORMAT " -> " PTR_FORMAT, 111 r->region_number(), p2i(r->bottom()), p2i(r->end())); 112 os::pretouch_memory(r->bottom(), r->end(), _page_size); 113 114 size_t start = r->region_number() * ShenandoahHeapRegion::region_size_bytes() / CMBitMap::heap_map_factor(); 115 size_t end = (r->region_number() + 1) * ShenandoahHeapRegion::region_size_bytes() / CMBitMap::heap_map_factor(); 116 assert (end <= _bitmap_size, "end is sane: " SIZE_FORMAT " < " SIZE_FORMAT, end, _bitmap_size); 117 118 log_trace(gc, heap)("Pretouch bitmap under region " SIZE_FORMAT ": " PTR_FORMAT " -> " PTR_FORMAT, 119 r->region_number(), p2i(_bitmap0_base + start), p2i(_bitmap0_base + end)); 120 os::pretouch_memory(_bitmap0_base + start, _bitmap0_base + end, _page_size); 121 122 log_trace(gc, heap)("Pretouch bitmap under region " SIZE_FORMAT ": " PTR_FORMAT " -> " PTR_FORMAT, 123 r->region_number(), p2i(_bitmap1_base + start), p2i(_bitmap1_base + end)); 124 os::pretouch_memory(_bitmap1_base + start, _bitmap1_base + end, _page_size); 125 126 r = _regions->claim_next(); 127 } 128 } 129 }; 130 131 jint ShenandoahHeap::initialize() { 132 CollectedHeap::pre_initialize(); 133 134 BrooksPointer::initial_checks(); 135 136 size_t init_byte_size = collector_policy()->initial_heap_byte_size(); 137 size_t max_byte_size = collector_policy()->max_heap_byte_size(); 138 139 Universe::check_alignment(max_byte_size, 140 ShenandoahHeapRegion::region_size_bytes(), 141 "shenandoah heap"); 142 Universe::check_alignment(init_byte_size, 143 ShenandoahHeapRegion::region_size_bytes(), 144 "shenandoah heap"); 145 146 ReservedSpace heap_rs = Universe::reserve_heap(max_byte_size, 147 Arguments::conservative_max_heap_alignment()); 148 initialize_reserved_region((HeapWord*)heap_rs.base(), (HeapWord*) (heap_rs.base() + heap_rs.size())); 149 150 set_barrier_set(new ShenandoahBarrierSet(this)); 151 ReservedSpace pgc_rs = heap_rs.first_part(max_byte_size); 152 _storage.initialize(pgc_rs, init_byte_size); 153 154 _num_regions = init_byte_size / ShenandoahHeapRegion::region_size_bytes(); 155 _max_regions = max_byte_size / ShenandoahHeapRegion::region_size_bytes(); 156 _initialSize = _num_regions * ShenandoahHeapRegion::region_size_bytes(); 157 size_t regionSizeWords = ShenandoahHeapRegion::region_size_bytes() / HeapWordSize; 158 assert(init_byte_size == _initialSize, "tautology"); 159 _ordered_regions = new ShenandoahHeapRegionSet(_max_regions); 160 _collection_set = new ShenandoahCollectionSet(_max_regions); 161 _free_regions = new ShenandoahFreeSet(_max_regions); 162 163 // Initialize fast collection set test structure. 164 _in_cset_fast_test_length = _max_regions; 165 _in_cset_fast_test_base = 166 NEW_C_HEAP_ARRAY(bool, _in_cset_fast_test_length, mtGC); 167 _in_cset_fast_test = _in_cset_fast_test_base - 168 ((uintx) pgc_rs.base() >> ShenandoahHeapRegion::region_size_shift()); 169 170 _next_top_at_mark_starts_base = 171 NEW_C_HEAP_ARRAY(HeapWord*, _max_regions, mtGC); 172 _next_top_at_mark_starts = _next_top_at_mark_starts_base - 173 ((uintx) pgc_rs.base() >> ShenandoahHeapRegion::region_size_shift()); 174 175 _complete_top_at_mark_starts_base = 176 NEW_C_HEAP_ARRAY(HeapWord*, _max_regions, mtGC); 177 _complete_top_at_mark_starts = _complete_top_at_mark_starts_base - 178 ((uintx) pgc_rs.base() >> ShenandoahHeapRegion::region_size_shift()); 179 180 size_t i = 0; 181 for (i = 0; i < _num_regions; i++) { 182 _in_cset_fast_test_base[i] = false; // Not in cset 183 HeapWord* bottom = (HeapWord*) pgc_rs.base() + regionSizeWords * i; 184 _complete_top_at_mark_starts_base[i] = bottom; 185 _next_top_at_mark_starts_base[i] = bottom; 186 } 187 188 { 189 ShenandoahHeapLock lock(this); 190 for (i = 0; i < _num_regions; i++) { 191 ShenandoahHeapRegion* current = new ShenandoahHeapRegion(this, (HeapWord*) pgc_rs.base() + 192 regionSizeWords * i, regionSizeWords, i); 193 _free_regions->add_region(current); 194 _ordered_regions->add_region(current); 195 } 196 } 197 assert(((size_t) _ordered_regions->active_regions()) == _num_regions, ""); 198 _first_region = _ordered_regions->get(0); 199 assert((((size_t) base()) & 200 (ShenandoahHeapRegion::region_size_bytes() - 1)) == 0, 201 "misaligned heap: "PTR_FORMAT, p2i(base())); 202 203 if (log_is_enabled(Trace, gc, region)) { 204 ResourceMark rm; 205 outputStream* out = Log(gc, region)::trace_stream(); 206 log_trace(gc, region)("All Regions"); 207 _ordered_regions->print(out); 208 log_trace(gc, region)("Free Regions"); 209 _free_regions->print(out); 210 } 211 212 // The call below uses stuff (the SATB* things) that are in G1, but probably 213 // belong into a shared location. 214 JavaThread::satb_mark_queue_set().initialize(SATB_Q_CBL_mon, 215 SATB_Q_FL_lock, 216 20 /*G1SATBProcessCompletedThreshold */, 217 Shared_SATB_Q_lock); 218 219 // Reserve space for prev and next bitmap. 220 _bitmap_size = CMBitMap::compute_size(heap_rs.size()); 221 _heap_region = MemRegion((HeapWord*) heap_rs.base(), heap_rs.size() / HeapWordSize); 222 223 size_t page_size = UseLargePages ? (size_t)os::large_page_size() : (size_t)os::vm_page_size(); 224 225 ReservedSpace bitmap0(_bitmap_size, page_size); 226 os::commit_memory_or_exit(bitmap0.base(), bitmap0.size(), false, "couldn't allocate mark bitmap"); 227 MemTracker::record_virtual_memory_type(bitmap0.base(), mtGC); 228 MemRegion bitmap_region0 = MemRegion((HeapWord*) bitmap0.base(), bitmap0.size() / HeapWordSize); 229 230 ReservedSpace bitmap1(_bitmap_size, page_size); 231 os::commit_memory_or_exit(bitmap1.base(), bitmap1.size(), false, "couldn't allocate mark bitmap"); 232 MemTracker::record_virtual_memory_type(bitmap1.base(), mtGC); 233 MemRegion bitmap_region1 = MemRegion((HeapWord*) bitmap1.base(), bitmap1.size() / HeapWordSize); 234 235 if (ShenandoahVerify || (UseShenandoahMatrix && VerifyShenandoahMatrix)) { 236 ReservedSpace verify_bitmap(_bitmap_size, page_size); 237 os::commit_memory_or_exit(verify_bitmap.base(), verify_bitmap.size(), false, 238 "couldn't allocate verification bitmap"); 239 MemTracker::record_virtual_memory_type(verify_bitmap.base(), mtGC); 240 MemRegion verify_bitmap_region = MemRegion((HeapWord *) verify_bitmap.base(), verify_bitmap.size() / HeapWordSize); 241 _verification_bit_map.initialize(_heap_region, verify_bitmap_region); 242 } 243 244 if (ShenandoahAlwaysPreTouch) { 245 assert (!AlwaysPreTouch, "Should have been overridden"); 246 247 // For NUMA, it is important to pre-touch the storage under bitmaps with worker threads, 248 // before initialize() below zeroes it with initializing thread. For any given region, 249 // we touch the region and the corresponding bitmaps from the same thread. 250 251 log_info(gc, heap)("Parallel pretouch " SIZE_FORMAT " regions with " SIZE_FORMAT " byte pages", 252 _ordered_regions->count(), page_size); 253 ShenandoahPretouchTask cl(_ordered_regions, bitmap0.base(), bitmap1.base(), _bitmap_size, page_size); 254 _workers->run_task(&cl); 255 } 256 257 _mark_bit_map0.initialize(_heap_region, bitmap_region0); 258 _complete_mark_bit_map = &_mark_bit_map0; 259 260 _mark_bit_map1.initialize(_heap_region, bitmap_region1); 261 _next_mark_bit_map = &_mark_bit_map1; 262 263 _connection_matrix = new ShenandoahConnectionMatrix(_max_regions); 264 _partial_gc = new ShenandoahPartialGC(this, _max_regions); 265 266 _monitoring_support = new ShenandoahMonitoringSupport(this); 267 268 _concurrent_gc_thread = new ShenandoahConcurrentThread(); 269 270 ShenandoahMarkCompact::initialize(); 271 272 return JNI_OK; 273 } 274 275 ShenandoahHeap::ShenandoahHeap(ShenandoahCollectorPolicy* policy) : 276 CollectedHeap(), 277 _shenandoah_policy(policy), 278 _concurrent_mark_in_progress(0), 279 _evacuation_in_progress(0), 280 _full_gc_in_progress(false), 281 _update_refs_in_progress(false), 282 _free_regions(NULL), 283 _collection_set(NULL), 284 _bytes_allocated_since_cm(0), 285 _bytes_allocated_during_cm(0), 286 _allocated_last_gc(0), 287 _used_start_gc(0), 288 _max_workers(MAX2(ConcGCThreads, ParallelGCThreads)), 289 _ref_processor(NULL), 290 _in_cset_fast_test(NULL), 291 _in_cset_fast_test_base(NULL), 292 _next_top_at_mark_starts(NULL), 293 _next_top_at_mark_starts_base(NULL), 294 _complete_top_at_mark_starts(NULL), 295 _complete_top_at_mark_starts_base(NULL), 296 _mark_bit_map0(), 297 _mark_bit_map1(), 298 _connection_matrix(NULL), 299 _cancelled_concgc(false), 300 _need_update_refs(false), 301 _need_reset_bitmaps(false), 302 _heap_lock(0), 303 #ifdef ASSERT 304 _heap_lock_owner(NULL), 305 #endif 306 _gc_timer(new (ResourceObj::C_HEAP, mtGC) ConcurrentGCTimer()) 307 308 { 309 log_info(gc, init)("Parallel GC threads: "UINT32_FORMAT, ParallelGCThreads); 310 log_info(gc, init)("Concurrent GC threads: "UINT32_FORMAT, ConcGCThreads); 311 log_info(gc, init)("Parallel reference processing enabled: %s", BOOL_TO_STR(ParallelRefProcEnabled)); 312 313 _scm = new ShenandoahConcurrentMark(); 314 _used = 0; 315 316 _max_workers = MAX2(_max_workers, 1U); 317 _workers = new ShenandoahWorkGang("Shenandoah GC Threads", _max_workers, 318 /* are_GC_task_threads */true, 319 /* are_ConcurrentGC_threads */false); 320 if (_workers == NULL) { 321 vm_exit_during_initialization("Failed necessary allocation."); 322 } else { 323 _workers->initialize_workers(); 324 } 325 } 326 327 class ResetNextBitmapTask : public AbstractGangTask { 328 private: 329 ShenandoahHeapRegionSet* _regions; 330 331 public: 332 ResetNextBitmapTask(ShenandoahHeapRegionSet* regions) : 333 AbstractGangTask("Parallel Reset Bitmap Task"), 334 _regions(regions) { 335 _regions->clear_current_index(); 336 } 337 338 void work(uint worker_id) { 339 ShenandoahHeapRegion* region = _regions->claim_next(); 340 ShenandoahHeap* heap = ShenandoahHeap::heap(); 341 while (region != NULL) { 342 HeapWord* bottom = region->bottom(); 343 HeapWord* top = heap->next_top_at_mark_start(region->bottom()); 344 if (top > bottom) { 345 heap->next_mark_bit_map()->clear_range_large(MemRegion(bottom, top)); 346 } 347 region = _regions->claim_next(); 348 } 349 } 350 }; 351 352 void ShenandoahHeap::reset_next_mark_bitmap(WorkGang* workers) { 353 ResetNextBitmapTask task = ResetNextBitmapTask(_ordered_regions); 354 workers->run_task(&task); 355 } 356 357 class ResetCompleteBitmapTask : public AbstractGangTask { 358 private: 359 ShenandoahHeapRegionSet* _regions; 360 361 public: 362 ResetCompleteBitmapTask(ShenandoahHeapRegionSet* regions) : 363 AbstractGangTask("Parallel Reset Bitmap Task"), 364 _regions(regions) { 365 _regions->clear_current_index(); 366 } 367 368 void work(uint worker_id) { 369 ShenandoahHeapRegion* region = _regions->claim_next(); 370 ShenandoahHeap* heap = ShenandoahHeap::heap(); 371 while (region != NULL) { 372 HeapWord* bottom = region->bottom(); 373 HeapWord* top = heap->complete_top_at_mark_start(region->bottom()); 374 if (top > bottom) { 375 heap->complete_mark_bit_map()->clear_range_large(MemRegion(bottom, top)); 376 } 377 region = _regions->claim_next(); 378 } 379 } 380 }; 381 382 void ShenandoahHeap::reset_complete_mark_bitmap(WorkGang* workers) { 383 ResetCompleteBitmapTask task = ResetCompleteBitmapTask(_ordered_regions); 384 workers->run_task(&task); 385 } 386 387 bool ShenandoahHeap::is_next_bitmap_clear() { 388 HeapWord* start = _ordered_regions->bottom(); 389 HeapWord* end = _ordered_regions->end(); 390 return _next_mark_bit_map->getNextMarkedWordAddress(start, end) == end; 391 } 392 393 bool ShenandoahHeap::is_complete_bitmap_clear_range(HeapWord* start, HeapWord* end) { 394 return _complete_mark_bit_map->getNextMarkedWordAddress(start, end) == end; 395 } 396 397 void ShenandoahHeap::print_on(outputStream* st) const { 398 st->print("Shenandoah Heap"); 399 st->print(" total = " SIZE_FORMAT " K, used " SIZE_FORMAT " K ", capacity()/ K, used() /K); 400 st->print(" [" PTR_FORMAT ", " PTR_FORMAT ") ", 401 p2i(reserved_region().start()), 402 p2i(reserved_region().end())); 403 st->print("Region size = " SIZE_FORMAT "K ", ShenandoahHeapRegion::region_size_bytes() / K); 404 if (_concurrent_mark_in_progress) { 405 st->print("marking "); 406 } 407 if (_evacuation_in_progress) { 408 st->print("evacuating "); 409 } 410 if (cancelled_concgc()) { 411 st->print("cancelled "); 412 } 413 st->print("\n"); 414 415 // Adapted from VirtualSpace::print_on(), which is non-PRODUCT only 416 st->print ("Virtual space:"); 417 if (_storage.special()) st->print(" (pinned in memory)"); 418 st->cr(); 419 st->print_cr(" - committed: " SIZE_FORMAT, _storage.committed_size()); 420 st->print_cr(" - reserved: " SIZE_FORMAT, _storage.reserved_size()); 421 st->print_cr(" - [low, high]: [" INTPTR_FORMAT ", " INTPTR_FORMAT "]", p2i(_storage.low()), p2i(_storage.high())); 422 st->print_cr(" - [low_b, high_b]: [" INTPTR_FORMAT ", " INTPTR_FORMAT "]", p2i(_storage.low_boundary()), p2i(_storage.high_boundary())); 423 424 if (Verbose) { 425 print_heap_regions(st); 426 } 427 } 428 429 class InitGCLABClosure : public ThreadClosure { 430 public: 431 void do_thread(Thread* thread) { 432 thread->gclab().initialize(true); 433 } 434 }; 435 436 void ShenandoahHeap::post_initialize() { 437 if (UseTLAB) { 438 // This is a very tricky point in VM lifetime. We cannot easily call Threads::threads_do 439 // here, because some system threads (VMThread, WatcherThread, etc) are not yet available. 440 // Their initialization should be handled separately. Is we miss some threads here, 441 // then any other TLAB-related activity would fail with asserts. 442 443 InitGCLABClosure init_gclabs; 444 { 445 MutexLocker ml(Threads_lock); 446 for (JavaThread *thread = Threads::first(); thread != NULL; thread = thread->next()) { 447 init_gclabs.do_thread(thread); 448 } 449 } 450 gc_threads_do(&init_gclabs); 451 452 // gclab can not be initialized early during VM startup, as it can not determinate its max_size. 453 // Now, we will let WorkGang to initialize gclab when new worker is created. 454 _workers->set_initialize_gclab(); 455 } 456 457 _scm->initialize(_max_workers); 458 459 ref_processing_init(); 460 } 461 462 class CalculateUsedRegionClosure : public ShenandoahHeapRegionClosure { 463 size_t sum; 464 public: 465 466 CalculateUsedRegionClosure() { 467 sum = 0; 468 } 469 470 bool doHeapRegion(ShenandoahHeapRegion* r) { 471 sum = sum + r->used(); 472 return false; 473 } 474 475 size_t getResult() { return sum;} 476 }; 477 478 size_t ShenandoahHeap::calculateUsed() { 479 CalculateUsedRegionClosure cl; 480 heap_region_iterate(&cl); 481 return cl.getResult(); 482 } 483 484 void ShenandoahHeap::verify_heap_size_consistency() { 485 486 assert(calculateUsed() == used(), 487 "heap used size must be consistent heap-used: "SIZE_FORMAT" regions-used: "SIZE_FORMAT, used(), calculateUsed()); 488 } 489 490 size_t ShenandoahHeap::used() const { 491 OrderAccess::acquire(); 492 return _used; 493 } 494 495 void ShenandoahHeap::increase_used(size_t bytes) { 496 assert_heaplock_or_safepoint(); 497 _used += bytes; 498 } 499 500 void ShenandoahHeap::set_used(size_t bytes) { 501 assert_heaplock_or_safepoint(); 502 _used = bytes; 503 } 504 505 void ShenandoahHeap::decrease_used(size_t bytes) { 506 assert_heaplock_or_safepoint(); 507 assert(_used >= bytes, "never decrease heap size by more than we've left"); 508 _used -= bytes; 509 } 510 511 size_t ShenandoahHeap::capacity() const { 512 return _num_regions * ShenandoahHeapRegion::region_size_bytes(); 513 } 514 515 bool ShenandoahHeap::is_maximal_no_gc() const { 516 Unimplemented(); 517 return true; 518 } 519 520 size_t ShenandoahHeap::max_capacity() const { 521 return _max_regions * ShenandoahHeapRegion::region_size_bytes(); 522 } 523 524 size_t ShenandoahHeap::min_capacity() const { 525 return _initialSize; 526 } 527 528 VirtualSpace* ShenandoahHeap::storage() const { 529 return (VirtualSpace*) &_storage; 530 } 531 532 bool ShenandoahHeap::is_in(const void* p) const { 533 HeapWord* heap_base = (HeapWord*) base(); 534 HeapWord* last_region_end = heap_base + (ShenandoahHeapRegion::region_size_bytes() / HeapWordSize) * _num_regions; 535 return p >= heap_base && p < last_region_end; 536 } 537 538 bool ShenandoahHeap::is_scavengable(const void* p) { 539 return true; 540 } 541 542 HeapWord* ShenandoahHeap::allocate_from_gclab_slow(Thread* thread, size_t size) { 543 // Retain tlab and allocate object in shared space if 544 // the amount free in the tlab is too large to discard. 545 if (thread->gclab().free() > thread->gclab().refill_waste_limit()) { 546 thread->gclab().record_slow_allocation(size); 547 return NULL; 548 } 549 550 // Discard gclab and allocate a new one. 551 // To minimize fragmentation, the last GCLAB may be smaller than the rest. 552 size_t new_gclab_size = thread->gclab().compute_size(size); 553 554 thread->gclab().clear_before_allocation(); 555 556 if (new_gclab_size == 0) { 557 return NULL; 558 } 559 560 // Allocate a new GCLAB... 561 HeapWord* obj = allocate_new_gclab(new_gclab_size); 562 if (obj == NULL) { 563 return NULL; 564 } 565 566 if (ZeroTLAB) { 567 // ..and clear it. 568 Copy::zero_to_words(obj, new_gclab_size); 569 } else { 570 // ...and zap just allocated object. 571 #ifdef ASSERT 572 // Skip mangling the space corresponding to the object header to 573 // ensure that the returned space is not considered parsable by 574 // any concurrent GC thread. 575 size_t hdr_size = oopDesc::header_size(); 576 Copy::fill_to_words(obj + hdr_size, new_gclab_size - hdr_size, badHeapWordVal); 577 #endif // ASSERT 578 } 579 thread->gclab().fill(obj, obj + size, new_gclab_size); 580 return obj; 581 } 582 583 HeapWord* ShenandoahHeap::allocate_new_tlab(size_t word_size) { 584 return allocate_new_tlab(word_size, false); 585 } 586 587 HeapWord* ShenandoahHeap::allocate_new_gclab(size_t word_size) { 588 return allocate_new_tlab(word_size, true); 589 } 590 591 HeapWord* ShenandoahHeap::allocate_new_tlab(size_t word_size, bool evacuating) { 592 HeapWord* result = allocate_memory(word_size, evacuating); 593 594 if (result != NULL) { 595 assert(! in_collection_set(result), "Never allocate in dirty region"); 596 _bytes_allocated_since_cm += word_size * HeapWordSize; 597 598 log_develop_trace(gc, tlab)("allocating new tlab of size "SIZE_FORMAT" at addr "PTR_FORMAT, word_size, p2i(result)); 599 600 } 601 return result; 602 } 603 604 ShenandoahHeap* ShenandoahHeap::heap() { 605 CollectedHeap* heap = Universe::heap(); 606 assert(heap != NULL, "Unitialized access to ShenandoahHeap::heap()"); 607 assert(heap->kind() == CollectedHeap::ShenandoahHeap, "not a shenandoah heap"); 608 return (ShenandoahHeap*) heap; 609 } 610 611 ShenandoahHeap* ShenandoahHeap::heap_no_check() { 612 CollectedHeap* heap = Universe::heap(); 613 return (ShenandoahHeap*) heap; 614 } 615 616 HeapWord* ShenandoahHeap::allocate_memory_work(size_t word_size) { 617 618 ShenandoahHeapLock heap_lock(this); 619 620 HeapWord* result = allocate_memory_under_lock(word_size); 621 size_t grow_by = (word_size * HeapWordSize + ShenandoahHeapRegion::region_size_bytes() - 1) / ShenandoahHeapRegion::region_size_bytes(); 622 623 while (result == NULL && _num_regions + grow_by <= _max_regions) { 624 grow_heap_by(grow_by); 625 result = allocate_memory_under_lock(word_size); 626 } 627 628 return result; 629 } 630 631 HeapWord* ShenandoahHeap::allocate_memory(size_t word_size, bool evacuating) { 632 HeapWord* result = NULL; 633 result = allocate_memory_work(word_size); 634 635 if (!evacuating) { 636 // Allocation failed, try full-GC, then retry allocation. 637 // 638 // It might happen that one of the threads requesting allocation would unblock 639 // way later after full-GC happened, only to fail the second allocation, because 640 // other threads have already depleted the free storage. In this case, a better 641 // strategy would be to try full-GC again. 642 // 643 // Lacking the way to detect progress from "collect" call, we are left with blindly 644 // retrying for some bounded number of times. 645 // TODO: Poll if Full GC made enough progress to warrant retry. 646 int tries = 0; 647 while ((result == NULL) && (tries++ < ShenandoahFullGCTries)) { 648 log_debug(gc)("[" PTR_FORMAT " Failed to allocate " SIZE_FORMAT " bytes, doing full GC, try %d", 649 p2i(Thread::current()), word_size * HeapWordSize, tries); 650 collect(GCCause::_allocation_failure); 651 result = allocate_memory_work(word_size); 652 } 653 } 654 655 // Only update monitoring counters when not calling from a write-barrier. 656 // Otherwise we might attempt to grab the Service_lock, which we must 657 // not do when coming from a write-barrier (because the thread might 658 // already hold the Compile_lock). 659 if (! evacuating) { 660 monitoring_support()->update_counters(); 661 } 662 663 log_develop_trace(gc, alloc)("allocate memory chunk of size "SIZE_FORMAT" at addr "PTR_FORMAT " by thread %d ", 664 word_size, p2i(result), Thread::current()->osthread()->thread_id()); 665 666 return result; 667 } 668 669 HeapWord* ShenandoahHeap::allocate_memory_under_lock(size_t word_size) { 670 assert_heaplock_owned_by_current_thread(); 671 672 if (word_size * HeapWordSize > ShenandoahHeapRegion::region_size_bytes()) { 673 return allocate_large_memory(word_size); 674 } 675 676 // Not enough memory in free region set. 677 // Coming out of full GC, it is possible that there is not 678 // free region available, so current_index may not be valid. 679 if (word_size * HeapWordSize > _free_regions->capacity()) return NULL; 680 681 ShenandoahHeapRegion* my_current_region = _free_regions->current_no_humongous(); 682 683 if (my_current_region == NULL) { 684 return NULL; // No more room to make a new region. OOM. 685 } 686 assert(my_current_region != NULL, "should have a region at this point"); 687 688 #ifdef ASSERT 689 if (in_collection_set(my_current_region)) { 690 print_heap_regions(); 691 } 692 #endif 693 assert(! in_collection_set(my_current_region), "never get targetted regions in free-lists"); 694 assert(! my_current_region->is_humongous(), "never attempt to allocate from humongous object regions"); 695 696 HeapWord* result = my_current_region->allocate(word_size); 697 698 while (result == NULL) { 699 // 2nd attempt. Try next region. 700 _free_regions->increase_used(my_current_region->free()); 701 ShenandoahHeapRegion* next_region = _free_regions->next_no_humongous(); 702 assert(next_region != my_current_region, "must not get current again"); 703 my_current_region = next_region; 704 705 if (my_current_region == NULL) { 706 return NULL; // No more room to make a new region. OOM. 707 } 708 assert(my_current_region != NULL, "should have a region at this point"); 709 assert(! in_collection_set(my_current_region), "never get targetted regions in free-lists"); 710 assert(! my_current_region->is_humongous(), "never attempt to allocate from humongous object regions"); 711 result = my_current_region->allocate(word_size); 712 } 713 714 my_current_region->increase_live_data_words(word_size); 715 increase_used(word_size * HeapWordSize); 716 _free_regions->increase_used(word_size * HeapWordSize); 717 return result; 718 } 719 720 HeapWord* ShenandoahHeap::allocate_large_memory(size_t words) { 721 assert_heaplock_owned_by_current_thread(); 722 723 size_t required_regions = ShenandoahHumongous::required_regions(words * HeapWordSize); 724 if (required_regions > _max_regions) return NULL; 725 726 ShenandoahHeapRegion* r = _free_regions->allocate_contiguous(required_regions); 727 728 HeapWord* result = NULL; 729 730 if (r != NULL) { 731 result = r->bottom(); 732 733 log_debug(gc, humongous)("allocating humongous object of size: "SIZE_FORMAT" KB at location "PTR_FORMAT" in start region "SIZE_FORMAT, 734 (words * HeapWordSize) / K, p2i(result), r->region_number()); 735 } else { 736 log_debug(gc, humongous)("allocating humongous object of size: "SIZE_FORMAT" KB at location "PTR_FORMAT" failed", 737 (words * HeapWordSize) / K, p2i(result)); 738 } 739 740 741 return result; 742 743 } 744 745 HeapWord* ShenandoahHeap::mem_allocate(size_t size, 746 bool* gc_overhead_limit_was_exceeded) { 747 748 HeapWord* filler = allocate_memory(size + BrooksPointer::word_size(), false); 749 HeapWord* result = filler + BrooksPointer::word_size(); 750 if (filler != NULL) { 751 BrooksPointer::initialize(oop(result)); 752 _bytes_allocated_since_cm += size * HeapWordSize; 753 754 assert(! in_collection_set(result), "never allocate in targetted region"); 755 return result; 756 } else { 757 /* 758 tty->print_cr("Out of memory. Requested number of words: "SIZE_FORMAT" used heap: "INT64_FORMAT", bytes allocated since last CM: "INT64_FORMAT, 759 size, used(), _bytes_allocated_since_cm); 760 { 761 print_heap_regions(); 762 tty->print("Printing "SIZE_FORMAT" free regions:\n", _free_regions->count()); 763 _free_regions->print(); 764 } 765 */ 766 return NULL; 767 } 768 } 769 770 class ParallelEvacuateRegionObjectClosure : public ObjectClosure { 771 private: 772 ShenandoahHeap* _heap; 773 Thread* _thread; 774 public: 775 ParallelEvacuateRegionObjectClosure(ShenandoahHeap* heap) : 776 _heap(heap), _thread(Thread::current()) { 777 } 778 779 void do_object(oop p) { 780 781 log_develop_trace(gc, compaction)("Calling ParallelEvacuateRegionObjectClosure on "PTR_FORMAT" of size %d\n", p2i((HeapWord*) p), p->size()); 782 783 assert(_heap->is_marked_complete(p), "expect only marked objects"); 784 if (oopDesc::unsafe_equals(p, ShenandoahBarrierSet::resolve_oop_static_not_null(p))) { 785 bool evac; 786 _heap->evacuate_object(p, _thread, evac); 787 } 788 } 789 }; 790 791 #ifdef ASSERT 792 class VerifyEvacuatedObjectClosure : public ObjectClosure { 793 794 public: 795 796 void do_object(oop p) { 797 if (ShenandoahHeap::heap()->is_marked_complete(p)) { 798 oop p_prime = oopDesc::bs()->read_barrier(p); 799 assert(! oopDesc::unsafe_equals(p, p_prime), "Should point to evacuated copy"); 800 if (p->klass() != p_prime->klass()) { 801 tty->print_cr("copy has different class than original:"); 802 p->klass()->print_on(tty); 803 p_prime->klass()->print_on(tty); 804 } 805 assert(p->klass() == p_prime->klass(), "Should have the same class p: "PTR_FORMAT", p_prime: "PTR_FORMAT, p2i(p), p2i(p_prime)); 806 // assert(p->mark() == p_prime->mark(), "Should have the same mark"); 807 assert(p->size() == p_prime->size(), "Should be the same size"); 808 assert(oopDesc::unsafe_equals(p_prime, oopDesc::bs()->read_barrier(p_prime)), "One forward once"); 809 } 810 } 811 }; 812 813 void ShenandoahHeap::verify_evacuated_region(ShenandoahHeapRegion* from_region) { 814 VerifyEvacuatedObjectClosure verify_evacuation; 815 marked_object_iterate(from_region, &verify_evacuation); 816 } 817 #endif 818 819 void ShenandoahHeap::parallel_evacuate_region(ShenandoahHeapRegion* from_region) { 820 821 assert(from_region->has_live(), "all-garbage regions are reclaimed earlier"); 822 823 ParallelEvacuateRegionObjectClosure evacuate_region(this); 824 825 marked_object_iterate(from_region, &evacuate_region); 826 827 #ifdef ASSERT 828 if (ShenandoahVerify && ! cancelled_concgc()) { 829 verify_evacuated_region(from_region); 830 } 831 #endif 832 } 833 834 class ParallelEvacuationTask : public AbstractGangTask { 835 private: 836 ShenandoahHeap* _sh; 837 ShenandoahCollectionSet* _cs; 838 839 public: 840 ParallelEvacuationTask(ShenandoahHeap* sh, 841 ShenandoahCollectionSet* cs) : 842 AbstractGangTask("Parallel Evacuation Task"), 843 _cs(cs), 844 _sh(sh) {} 845 846 void work(uint worker_id) { 847 848 ShenandoahHeapRegion* from_hr = _cs->claim_next(); 849 850 while (from_hr != NULL) { 851 log_develop_trace(gc, region)("Thread "INT32_FORMAT" claimed Heap Region "SIZE_FORMAT, 852 worker_id, 853 from_hr->region_number()); 854 855 assert(from_hr->has_live(), "all-garbage regions are reclaimed early"); 856 _sh->parallel_evacuate_region(from_hr); 857 858 if (_sh->cancelled_concgc()) { 859 log_develop_trace(gc, region)("Cancelled concgc while evacuating region " SIZE_FORMAT "\n", from_hr->region_number()); 860 break; 861 } 862 from_hr = _cs->claim_next(); 863 } 864 } 865 }; 866 867 class RecycleDirtyRegionsClosure: public ShenandoahHeapRegionClosure { 868 private: 869 ShenandoahHeap* _heap; 870 size_t _bytes_reclaimed; 871 public: 872 RecycleDirtyRegionsClosure() : _heap(ShenandoahHeap::heap()) {} 873 874 bool doHeapRegion(ShenandoahHeapRegion* r) { 875 876 assert (! _heap->cancelled_concgc(), "no recycling after cancelled marking"); 877 878 if (_heap->in_collection_set(r)) { 879 log_develop_trace(gc, region)("Recycling region " SIZE_FORMAT ":", r->region_number()); 880 _heap->decrease_used(r->used()); 881 _bytes_reclaimed += r->used(); 882 r->recycle(); 883 } 884 885 return false; 886 } 887 size_t bytes_reclaimed() { return _bytes_reclaimed;} 888 void clear_bytes_reclaimed() {_bytes_reclaimed = 0;} 889 }; 890 891 void ShenandoahHeap::recycle_dirty_regions() { 892 shenandoahPolicy()->record_peak_occupancy(); 893 RecycleDirtyRegionsClosure cl; 894 cl.clear_bytes_reclaimed(); 895 896 heap_region_iterate(&cl); 897 898 _shenandoah_policy->record_bytes_reclaimed(cl.bytes_reclaimed()); 899 if (! cancelled_concgc()) { 900 clear_cset_fast_test(); 901 } 902 } 903 904 ShenandoahFreeSet* ShenandoahHeap::free_regions() { 905 return _free_regions; 906 } 907 908 void ShenandoahHeap::print_heap_regions(outputStream* st) const { 909 _ordered_regions->print(st); 910 } 911 912 class PrintAllRefsOopClosure: public ExtendedOopClosure { 913 private: 914 int _index; 915 const char* _prefix; 916 917 public: 918 PrintAllRefsOopClosure(const char* prefix) : _index(0), _prefix(prefix) {} 919 920 private: 921 template <class T> 922 inline void do_oop_work(T* p) { 923 oop o = oopDesc::load_decode_heap_oop(p); 924 if (o != NULL) { 925 if (ShenandoahHeap::heap()->is_in(o) && o->is_oop()) { 926 tty->print_cr("%s "INT32_FORMAT" ("PTR_FORMAT")-> "PTR_FORMAT" (marked: %s) (%s "PTR_FORMAT")", 927 _prefix, _index, 928 p2i(p), p2i(o), 929 BOOL_TO_STR(ShenandoahHeap::heap()->is_marked_complete(o)), 930 o->klass()->internal_name(), p2i(o->klass())); 931 } else { 932 tty->print_cr("%s "INT32_FORMAT" ("PTR_FORMAT" dirty -> "PTR_FORMAT" (not in heap, possibly corrupted or dirty)", 933 _prefix, _index, 934 p2i(p), p2i(o)); 935 } 936 } else { 937 tty->print_cr("%s "INT32_FORMAT" ("PTR_FORMAT") -> "PTR_FORMAT, _prefix, _index, p2i(p), p2i((HeapWord*) o)); 938 } 939 _index++; 940 } 941 942 public: 943 void do_oop(oop* p) { 944 do_oop_work(p); 945 } 946 947 void do_oop(narrowOop* p) { 948 do_oop_work(p); 949 } 950 951 }; 952 953 class PrintAllRefsObjectClosure : public ObjectClosure { 954 const char* _prefix; 955 956 public: 957 PrintAllRefsObjectClosure(const char* prefix) : _prefix(prefix) {} 958 959 void do_object(oop p) { 960 if (ShenandoahHeap::heap()->is_in(p)) { 961 tty->print_cr("%s object "PTR_FORMAT" (marked: %s) (%s "PTR_FORMAT") refers to:", 962 _prefix, p2i(p), 963 BOOL_TO_STR(ShenandoahHeap::heap()->is_marked_complete(p)), 964 p->klass()->internal_name(), p2i(p->klass())); 965 PrintAllRefsOopClosure cl(_prefix); 966 p->oop_iterate(&cl); 967 } 968 } 969 }; 970 971 void ShenandoahHeap::print_all_refs(const char* prefix) { 972 tty->print_cr("printing all references in the heap"); 973 tty->print_cr("root references:"); 974 975 ensure_parsability(false); 976 977 PrintAllRefsOopClosure cl(prefix); 978 roots_iterate(&cl); 979 980 tty->print_cr("heap references:"); 981 PrintAllRefsObjectClosure cl2(prefix); 982 object_iterate(&cl2); 983 } 984 985 class VerifyAfterMarkingOopClosure: public ExtendedOopClosure { 986 private: 987 ShenandoahHeap* _heap; 988 989 public: 990 VerifyAfterMarkingOopClosure() : 991 _heap(ShenandoahHeap::heap()) { } 992 993 private: 994 template <class T> 995 inline void do_oop_work(T* p) { 996 oop o = oopDesc::load_decode_heap_oop(p); 997 if (o != NULL) { 998 if (! _heap->is_marked_complete(o)) { 999 _heap->print_heap_regions(); 1000 _heap->print_all_refs("post-mark"); 1001 tty->print_cr("oop not marked, although referrer is marked: "PTR_FORMAT": in_heap: %s, is_marked: %s", 1002 p2i((HeapWord*) o), BOOL_TO_STR(_heap->is_in(o)), BOOL_TO_STR(_heap->is_marked_complete(o))); 1003 _heap->print_heap_locations((HeapWord*) o, (HeapWord*) o + o->size()); 1004 1005 tty->print_cr("oop class: %s", o->klass()->internal_name()); 1006 if (_heap->is_in(p)) { 1007 oop referrer = oop(_heap->heap_region_containing(p)->block_start_const(p)); 1008 tty->print_cr("Referrer starts at addr "PTR_FORMAT, p2i((HeapWord*) referrer)); 1009 referrer->print(); 1010 _heap->print_heap_locations((HeapWord*) referrer, (HeapWord*) referrer + referrer->size()); 1011 } 1012 tty->print_cr("heap region containing object:"); 1013 _heap->heap_region_containing(o)->print(); 1014 tty->print_cr("heap region containing referrer:"); 1015 _heap->heap_region_containing(p)->print(); 1016 tty->print_cr("heap region containing forwardee:"); 1017 _heap->heap_region_containing(oopDesc::bs()->read_barrier(o))->print(); 1018 } 1019 assert(o->is_oop(), "oop must be an oop"); 1020 assert(Metaspace::contains(o->klass()), "klass pointer must go to metaspace"); 1021 if (! oopDesc::unsafe_equals(o, oopDesc::bs()->read_barrier(o))) { 1022 tty->print_cr("oops has forwardee: p: "PTR_FORMAT" (%s), o = "PTR_FORMAT" (%s), new-o: "PTR_FORMAT" (%s)", 1023 p2i(p), 1024 BOOL_TO_STR(_heap->in_collection_set(p)), 1025 p2i(o), 1026 BOOL_TO_STR(_heap->in_collection_set(o)), 1027 p2i((HeapWord*) oopDesc::bs()->read_barrier(o)), 1028 BOOL_TO_STR(_heap->in_collection_set(oopDesc::bs()->read_barrier(o)))); 1029 tty->print_cr("oop class: %s", o->klass()->internal_name()); 1030 } 1031 assert(oopDesc::unsafe_equals(o, oopDesc::bs()->read_barrier(o)), "oops must not be forwarded"); 1032 assert(! _heap->in_collection_set(o), "references must not point to dirty heap regions"); 1033 assert(_heap->is_marked_complete(o), "live oops must be marked current"); 1034 } 1035 } 1036 1037 public: 1038 void do_oop(oop* p) { 1039 do_oop_work(p); 1040 } 1041 1042 void do_oop(narrowOop* p) { 1043 do_oop_work(p); 1044 } 1045 1046 }; 1047 1048 void ShenandoahHeap::verify_heap_after_marking() { 1049 1050 verify_heap_size_consistency(); 1051 1052 log_trace(gc)("verifying heap after marking"); 1053 1054 VerifyAfterMarkingOopClosure cl; 1055 roots_iterate(&cl); 1056 ObjectToOopClosure objs(&cl); 1057 object_iterate(&objs); 1058 } 1059 1060 1061 void ShenandoahHeap::reclaim_humongous_region_at(ShenandoahHeapRegion* r) { 1062 assert(r->is_humongous_start(), "reclaim regions starting with the first one"); 1063 1064 oop humongous_obj = oop(r->bottom() + BrooksPointer::word_size()); 1065 size_t size = humongous_obj->size() + BrooksPointer::word_size(); 1066 size_t required_regions = ShenandoahHumongous::required_regions(size * HeapWordSize); 1067 size_t index = r->region_number(); 1068 1069 1070 assert(!r->has_live(), "liveness must be zero"); 1071 1072 for(size_t i = 0; i < required_regions; i++) { 1073 1074 ShenandoahHeapRegion* region = _ordered_regions->get(index++); 1075 1076 assert((region->is_humongous_start() || region->is_humongous_continuation()), 1077 "expect correct humongous start or continuation"); 1078 1079 if (log_is_enabled(Debug, gc, humongous)) { 1080 log_debug(gc, humongous)("reclaiming "SIZE_FORMAT" humongous regions for object of size: "SIZE_FORMAT" words", required_regions, size); 1081 ResourceMark rm; 1082 outputStream* out = Log(gc, humongous)::debug_stream(); 1083 region->print_on(out); 1084 } 1085 1086 region->recycle(); 1087 ShenandoahHeap::heap()->decrease_used(ShenandoahHeapRegion::region_size_bytes()); 1088 } 1089 } 1090 1091 class ShenandoahReclaimHumongousRegionsClosure : public ShenandoahHeapRegionClosure { 1092 1093 bool doHeapRegion(ShenandoahHeapRegion* r) { 1094 ShenandoahHeap* heap = ShenandoahHeap::heap(); 1095 1096 if (r->is_humongous_start()) { 1097 oop humongous_obj = oop(r->bottom() + BrooksPointer::word_size()); 1098 if (! heap->is_marked_complete(humongous_obj)) { 1099 1100 heap->reclaim_humongous_region_at(r); 1101 } 1102 } 1103 return false; 1104 } 1105 }; 1106 1107 #ifdef ASSERT 1108 class CheckCollectionSetClosure: public ShenandoahHeapRegionClosure { 1109 bool doHeapRegion(ShenandoahHeapRegion* r) { 1110 assert(! ShenandoahHeap::heap()->in_collection_set(r), "Should have been cleared by now"); 1111 return false; 1112 } 1113 }; 1114 #endif 1115 1116 void ShenandoahHeap::prepare_for_concurrent_evacuation() { 1117 assert(_ordered_regions->get(0)->region_number() == 0, "FIXME CHF. FIXME CHF!"); 1118 1119 log_develop_trace(gc)("Thread %d started prepare_for_concurrent_evacuation", Thread::current()->osthread()->thread_id()); 1120 1121 if (!cancelled_concgc()) { 1122 1123 recycle_dirty_regions(); 1124 1125 ensure_parsability(true); 1126 1127 if (UseShenandoahMatrix && PrintShenandoahMatrix) { 1128 outputStream* log = Log(gc)::info_stream(); 1129 connection_matrix()->print_on(log); 1130 } 1131 1132 if (ShenandoahVerify || (UseShenandoahMatrix && VerifyShenandoahMatrix)) { 1133 verify_heap_reachable_at_safepoint(); 1134 } 1135 1136 #ifdef ASSERT 1137 if (ShenandoahVerify) { 1138 verify_heap_after_marking(); 1139 } 1140 #endif 1141 1142 // NOTE: This needs to be done during a stop the world pause, because 1143 // putting regions into the collection set concurrently with Java threads 1144 // will create a race. In particular, acmp could fail because when we 1145 // resolve the first operand, the containing region might not yet be in 1146 // the collection set, and thus return the original oop. When the 2nd 1147 // operand gets resolved, the region could be in the collection set 1148 // and the oop gets evacuated. If both operands have originally been 1149 // the same, we get false negatives. 1150 1151 { 1152 ShenandoahHeapLock lock(this); 1153 _collection_set->clear(); 1154 _free_regions->clear(); 1155 1156 ShenandoahReclaimHumongousRegionsClosure reclaim; 1157 heap_region_iterate(&reclaim); 1158 1159 #ifdef ASSERT 1160 CheckCollectionSetClosure ccsc; 1161 _ordered_regions->heap_region_iterate(&ccsc); 1162 #endif 1163 1164 _shenandoah_policy->choose_collection_set(_collection_set); 1165 1166 _shenandoah_policy->choose_free_set(_free_regions); 1167 } 1168 1169 _bytes_allocated_since_cm = 0; 1170 1171 Universe::update_heap_info_at_gc(); 1172 } 1173 } 1174 1175 1176 class RetireTLABClosure : public ThreadClosure { 1177 private: 1178 bool _retire; 1179 1180 public: 1181 RetireTLABClosure(bool retire) : _retire(retire) { 1182 } 1183 1184 void do_thread(Thread* thread) { 1185 thread->gclab().make_parsable(_retire); 1186 } 1187 }; 1188 1189 void ShenandoahHeap::ensure_parsability(bool retire_tlabs) { 1190 if (UseTLAB) { 1191 CollectedHeap::ensure_parsability(retire_tlabs); 1192 RetireTLABClosure cl(retire_tlabs); 1193 Threads::threads_do(&cl); 1194 } 1195 } 1196 1197 class ShenandoahEvacuateUpdateRootsClosure: public ExtendedOopClosure { 1198 private: 1199 ShenandoahHeap* _heap; 1200 Thread* _thread; 1201 public: 1202 ShenandoahEvacuateUpdateRootsClosure() : 1203 _heap(ShenandoahHeap::heap()), _thread(Thread::current()) { 1204 } 1205 1206 private: 1207 template <class T> 1208 void do_oop_work(T* p) { 1209 assert(_heap->is_evacuation_in_progress(), "Only do this when evacuation is in progress"); 1210 1211 T o = oopDesc::load_heap_oop(p); 1212 if (! oopDesc::is_null(o)) { 1213 oop obj = oopDesc::decode_heap_oop_not_null(o); 1214 if (_heap->in_collection_set(obj)) { 1215 assert(_heap->is_marked_complete(obj), "only evacuate marked objects %d %d", 1216 _heap->is_marked_complete(obj), _heap->is_marked_complete(ShenandoahBarrierSet::resolve_oop_static_not_null(obj))); 1217 oop resolved = ShenandoahBarrierSet::resolve_oop_static_not_null(obj); 1218 if (oopDesc::unsafe_equals(resolved, obj)) { 1219 bool evac; 1220 resolved = _heap->evacuate_object(obj, _thread, evac); 1221 } 1222 oopDesc::encode_store_heap_oop(p, resolved); 1223 } 1224 } 1225 #ifdef ASSERT 1226 else { 1227 // tty->print_cr("not updating root at: "PTR_FORMAT" with object: "PTR_FORMAT", is_in_heap: %s, is_in_cset: %s, is_marked: %s", 1228 // p2i(p), 1229 // p2i((HeapWord*) obj), 1230 // BOOL_TO_STR(_heap->is_in(obj)), 1231 // BOOL_TO_STR(_heap->in_cset_fast_test(obj)), 1232 // BOOL_TO_STR(_heap->is_marked_complete(obj))); 1233 } 1234 #endif 1235 } 1236 1237 public: 1238 void do_oop(oop* p) { 1239 do_oop_work(p); 1240 } 1241 void do_oop(narrowOop* p) { 1242 do_oop_work(p); 1243 } 1244 }; 1245 1246 class ShenandoahEvacuateUpdateRootsTask : public AbstractGangTask { 1247 ShenandoahRootEvacuator* _rp; 1248 public: 1249 1250 ShenandoahEvacuateUpdateRootsTask(ShenandoahRootEvacuator* rp) : 1251 AbstractGangTask("Shenandoah evacuate and update roots"), 1252 _rp(rp) 1253 { 1254 // Nothing else to do. 1255 } 1256 1257 void work(uint worker_id) { 1258 ShenandoahEvacuateUpdateRootsClosure cl; 1259 MarkingCodeBlobClosure blobsCl(&cl, CodeBlobToOopClosure::FixRelocations); 1260 1261 _rp->process_evacuate_roots(&cl, &blobsCl, worker_id); 1262 } 1263 }; 1264 1265 class ShenandoahFixRootsTask : public AbstractGangTask { 1266 ShenandoahRootEvacuator* _rp; 1267 public: 1268 1269 ShenandoahFixRootsTask(ShenandoahRootEvacuator* rp) : 1270 AbstractGangTask("Shenandoah update roots"), 1271 _rp(rp) 1272 { 1273 // Nothing else to do. 1274 } 1275 1276 void work(uint worker_id) { 1277 SCMUpdateRefsClosure cl; 1278 MarkingCodeBlobClosure blobsCl(&cl, CodeBlobToOopClosure::FixRelocations); 1279 1280 _rp->process_evacuate_roots(&cl, &blobsCl, worker_id); 1281 } 1282 }; 1283 void ShenandoahHeap::evacuate_and_update_roots() { 1284 1285 COMPILER2_PRESENT(DerivedPointerTable::clear()); 1286 1287 assert(SafepointSynchronize::is_at_safepoint(), "Only iterate roots while world is stopped"); 1288 1289 { 1290 ShenandoahRootEvacuator rp(this, workers()->active_workers(), ShenandoahCollectorPolicy::init_evac); 1291 ShenandoahEvacuateUpdateRootsTask roots_task(&rp); 1292 workers()->run_task(&roots_task); 1293 } 1294 1295 COMPILER2_PRESENT(DerivedPointerTable::update_pointers()); 1296 1297 if (cancelled_concgc()) { 1298 // If initial evacuation has been cancelled, we need to update all references 1299 // after all workers have finished. Otherwise we might run into the following problem: 1300 // GC thread 1 cannot allocate anymore, thus evacuation fails, leaves from-space ptr of object X. 1301 // GC thread 2 evacuates the same object X to to-space 1302 // which leaves a truly dangling from-space reference in the first root oop*. This must not happen. 1303 // clear() and update_pointers() must always be called in pairs, 1304 // cannot nest with above clear()/update_pointers(). 1305 COMPILER2_PRESENT(DerivedPointerTable::clear()); 1306 ShenandoahRootEvacuator rp(this, workers()->active_workers(), ShenandoahCollectorPolicy::init_evac); 1307 ShenandoahFixRootsTask update_roots_task(&rp); 1308 workers()->run_task(&update_roots_task); 1309 COMPILER2_PRESENT(DerivedPointerTable::update_pointers()); 1310 } 1311 1312 #ifdef ASSERT 1313 { 1314 AssertToSpaceClosure cl; 1315 CodeBlobToOopClosure code_cl(&cl, !CodeBlobToOopClosure::FixRelocations); 1316 ShenandoahRootEvacuator rp(this, 1); 1317 rp.process_evacuate_roots(&cl, &code_cl, 0); 1318 } 1319 #endif 1320 } 1321 1322 1323 void ShenandoahHeap::do_evacuation() { 1324 1325 parallel_evacuate(); 1326 1327 if (ShenandoahVerify && ! cancelled_concgc()) { 1328 VM_ShenandoahVerifyHeapAfterEvacuation verify_after_evacuation; 1329 if (Thread::current()->is_VM_thread()) { 1330 verify_after_evacuation.doit(); 1331 } else { 1332 VMThread::execute(&verify_after_evacuation); 1333 } 1334 } 1335 1336 } 1337 1338 void ShenandoahHeap::parallel_evacuate() { 1339 log_develop_trace(gc)("starting parallel_evacuate"); 1340 1341 _shenandoah_policy->record_phase_start(ShenandoahCollectorPolicy::conc_evac); 1342 1343 if (log_is_enabled(Trace, gc, region)) { 1344 ResourceMark rm; 1345 outputStream *out = Log(gc, region)::trace_stream(); 1346 out->print("Printing all available regions"); 1347 print_heap_regions(out); 1348 } 1349 1350 if (log_is_enabled(Trace, gc, cset)) { 1351 ResourceMark rm; 1352 outputStream *out = Log(gc, cset)::trace_stream(); 1353 out->print("Printing collection set which contains "SIZE_FORMAT" regions:\n", _collection_set->count()); 1354 _collection_set->print(out); 1355 1356 out->print("Printing free set which contains "SIZE_FORMAT" regions:\n", _free_regions->count()); 1357 _free_regions->print(out); 1358 } 1359 1360 ParallelEvacuationTask evacuationTask = ParallelEvacuationTask(this, _collection_set); 1361 1362 1363 workers()->run_task(&evacuationTask); 1364 1365 if (log_is_enabled(Trace, gc, cset)) { 1366 ResourceMark rm; 1367 outputStream *out = Log(gc, cset)::trace_stream(); 1368 out->print("Printing postgc collection set which contains "SIZE_FORMAT" regions:\n", 1369 _collection_set->count()); 1370 1371 _collection_set->print(out); 1372 1373 out->print("Printing postgc free regions which contain "SIZE_FORMAT" free regions:\n", 1374 _free_regions->count()); 1375 _free_regions->print(out); 1376 1377 } 1378 1379 if (log_is_enabled(Trace, gc, region)) { 1380 ResourceMark rm; 1381 outputStream *out = Log(gc, region)::trace_stream(); 1382 out->print_cr("all regions after evacuation:"); 1383 print_heap_regions(out); 1384 } 1385 1386 _shenandoah_policy->record_phase_end(ShenandoahCollectorPolicy::conc_evac); 1387 } 1388 1389 class VerifyEvacuationClosure: public ExtendedOopClosure { 1390 private: 1391 ShenandoahHeap* _heap; 1392 ShenandoahHeapRegion* _from_region; 1393 1394 public: 1395 VerifyEvacuationClosure(ShenandoahHeapRegion* from_region) : 1396 _heap(ShenandoahHeap::heap()), _from_region(from_region) { } 1397 private: 1398 template <class T> 1399 inline void do_oop_work(T* p) { 1400 oop heap_oop = oopDesc::load_decode_heap_oop(p); 1401 if (! oopDesc::is_null(heap_oop)) { 1402 guarantee(! _from_region->is_in(heap_oop), "no references to from-region allowed after evacuation: "PTR_FORMAT, p2i((HeapWord*) heap_oop)); 1403 } 1404 } 1405 1406 public: 1407 void do_oop(oop* p) { 1408 do_oop_work(p); 1409 } 1410 1411 void do_oop(narrowOop* p) { 1412 do_oop_work(p); 1413 } 1414 1415 }; 1416 1417 void ShenandoahHeap::roots_iterate(OopClosure* cl) { 1418 1419 assert(SafepointSynchronize::is_at_safepoint(), "Only iterate roots while world is stopped"); 1420 1421 CodeBlobToOopClosure blobsCl(cl, false); 1422 CLDToOopClosure cldCl(cl); 1423 1424 ShenandoahRootProcessor rp(this, 1); 1425 rp.process_all_roots(cl, NULL, &cldCl, &blobsCl, 0); 1426 } 1427 1428 void ShenandoahHeap::verify_evacuation(ShenandoahHeapRegion* from_region) { 1429 1430 VerifyEvacuationClosure rootsCl(from_region); 1431 roots_iterate(&rootsCl); 1432 1433 } 1434 1435 bool ShenandoahHeap::supports_tlab_allocation() const { 1436 return true; 1437 } 1438 1439 1440 size_t ShenandoahHeap::unsafe_max_tlab_alloc(Thread *thread) const { 1441 size_t idx = _free_regions->current_index(); 1442 ShenandoahHeapRegion* current = _free_regions->get_or_null(idx); 1443 if (current == NULL) { 1444 return 0; 1445 } else if (current->free() > MinTLABSize) { 1446 // Current region has enough space left, can use it. 1447 return current->free(); 1448 } else { 1449 // No more space in current region, we will take next free region 1450 // on the next TLAB allocation. 1451 return ShenandoahHeapRegion::region_size_bytes(); 1452 } 1453 } 1454 1455 size_t ShenandoahHeap::max_tlab_size() const { 1456 return ShenandoahHeapRegion::region_size_bytes(); 1457 } 1458 1459 class ResizeGCLABClosure : public ThreadClosure { 1460 public: 1461 void do_thread(Thread* thread) { 1462 thread->gclab().resize(); 1463 } 1464 }; 1465 1466 void ShenandoahHeap::resize_all_tlabs() { 1467 CollectedHeap::resize_all_tlabs(); 1468 1469 ResizeGCLABClosure cl; 1470 Threads::threads_do(&cl); 1471 } 1472 1473 class AccumulateStatisticsGCLABClosure : public ThreadClosure { 1474 public: 1475 void do_thread(Thread* thread) { 1476 thread->gclab().accumulate_statistics(); 1477 thread->gclab().initialize_statistics(); 1478 } 1479 }; 1480 1481 void ShenandoahHeap::accumulate_statistics_all_gclabs() { 1482 AccumulateStatisticsGCLABClosure cl; 1483 Threads::threads_do(&cl); 1484 } 1485 1486 bool ShenandoahHeap::can_elide_tlab_store_barriers() const { 1487 return true; 1488 } 1489 1490 oop ShenandoahHeap::new_store_pre_barrier(JavaThread* thread, oop new_obj) { 1491 // Overridden to do nothing. 1492 return new_obj; 1493 } 1494 1495 bool ShenandoahHeap::can_elide_initializing_store_barrier(oop new_obj) { 1496 return true; 1497 } 1498 1499 bool ShenandoahHeap::card_mark_must_follow_store() const { 1500 return false; 1501 } 1502 1503 void ShenandoahHeap::collect(GCCause::Cause cause) { 1504 assert(cause != GCCause::_gc_locker, "no JNI critical callback"); 1505 if (GCCause::is_user_requested_gc(cause)) { 1506 if (! DisableExplicitGC) { 1507 _concurrent_gc_thread->do_full_gc(cause); 1508 } 1509 } else if (cause == GCCause::_allocation_failure) { 1510 collector_policy()->set_should_clear_all_soft_refs(true); 1511 _concurrent_gc_thread->do_full_gc(cause); 1512 } 1513 } 1514 1515 void ShenandoahHeap::do_full_collection(bool clear_all_soft_refs) { 1516 //assert(false, "Shouldn't need to do full collections"); 1517 } 1518 1519 AdaptiveSizePolicy* ShenandoahHeap::size_policy() { 1520 Unimplemented(); 1521 return NULL; 1522 1523 } 1524 1525 CollectorPolicy* ShenandoahHeap::collector_policy() const { 1526 return _shenandoah_policy; 1527 } 1528 1529 1530 HeapWord* ShenandoahHeap::block_start(const void* addr) const { 1531 Space* sp = heap_region_containing(addr); 1532 if (sp != NULL) { 1533 return sp->block_start(addr); 1534 } 1535 return NULL; 1536 } 1537 1538 size_t ShenandoahHeap::block_size(const HeapWord* addr) const { 1539 Space* sp = heap_region_containing(addr); 1540 assert(sp != NULL, "block_size of address outside of heap"); 1541 return sp->block_size(addr); 1542 } 1543 1544 bool ShenandoahHeap::block_is_obj(const HeapWord* addr) const { 1545 Space* sp = heap_region_containing(addr); 1546 return sp->block_is_obj(addr); 1547 } 1548 1549 jlong ShenandoahHeap::millis_since_last_gc() { 1550 return 0; 1551 } 1552 1553 void ShenandoahHeap::prepare_for_verify() { 1554 if (SafepointSynchronize::is_at_safepoint() || ! UseTLAB) { 1555 ensure_parsability(false); 1556 } 1557 } 1558 1559 void ShenandoahHeap::print_gc_threads_on(outputStream* st) const { 1560 workers()->print_worker_threads_on(st); 1561 } 1562 1563 void ShenandoahHeap::gc_threads_do(ThreadClosure* tcl) const { 1564 workers()->threads_do(tcl); 1565 } 1566 1567 void ShenandoahHeap::print_tracing_info() const { 1568 if (log_is_enabled(Info, gc, stats)) { 1569 ResourceMark rm; 1570 outputStream* out = Log(gc, stats)::info_stream(); 1571 _shenandoah_policy->print_tracing_info(out); 1572 } 1573 } 1574 1575 class ShenandoahVerifyRootsClosure: public ExtendedOopClosure { 1576 private: 1577 ShenandoahHeap* _heap; 1578 VerifyOption _vo; 1579 bool _failures; 1580 public: 1581 // _vo == UsePrevMarking -> use "prev" marking information, 1582 // _vo == UseNextMarking -> use "next" marking information, 1583 // _vo == UseMarkWord -> use mark word from object header. 1584 ShenandoahVerifyRootsClosure(VerifyOption vo) : 1585 _heap(ShenandoahHeap::heap()), 1586 _vo(vo), 1587 _failures(false) { } 1588 1589 bool failures() { return _failures; } 1590 1591 private: 1592 template <class T> 1593 inline void do_oop_work(T* p) { 1594 oop obj = oopDesc::load_decode_heap_oop(p); 1595 if (! oopDesc::is_null(obj) && ! obj->is_oop()) { 1596 { // Just for debugging. 1597 tty->print_cr("Root location "PTR_FORMAT 1598 "verified "PTR_FORMAT, p2i(p), p2i((void*) obj)); 1599 // obj->print_on(tty); 1600 } 1601 } 1602 guarantee(obj->is_oop_or_null(), "is oop or null"); 1603 } 1604 1605 public: 1606 void do_oop(oop* p) { 1607 do_oop_work(p); 1608 } 1609 1610 void do_oop(narrowOop* p) { 1611 do_oop_work(p); 1612 } 1613 1614 }; 1615 1616 class ShenandoahVerifyHeapClosure: public ObjectClosure { 1617 private: 1618 ShenandoahVerifyRootsClosure _rootsCl; 1619 public: 1620 ShenandoahVerifyHeapClosure(ShenandoahVerifyRootsClosure rc) : 1621 _rootsCl(rc) {}; 1622 1623 void do_object(oop p) { 1624 _rootsCl.do_oop(&p); 1625 } 1626 }; 1627 1628 void ShenandoahHeap::verify(VerifyOption vo) { 1629 if (SafepointSynchronize::is_at_safepoint() || ! UseTLAB) { 1630 1631 ShenandoahVerifyRootsClosure rootsCl(vo); 1632 1633 assert(Thread::current()->is_VM_thread(), 1634 "Expected to be executed serially by the VM thread at this point"); 1635 1636 roots_iterate(&rootsCl); 1637 1638 bool failures = rootsCl.failures(); 1639 log_trace(gc)("verify failures: %s", BOOL_TO_STR(failures)); 1640 1641 ShenandoahVerifyHeapClosure heapCl(rootsCl); 1642 1643 object_iterate(&heapCl); 1644 // TODO: Implement rest of it. 1645 } else { 1646 tty->print("(SKIPPING roots, heapRegions, remset) "); 1647 } 1648 } 1649 size_t ShenandoahHeap::tlab_capacity(Thread *thr) const { 1650 return _free_regions->capacity(); 1651 } 1652 1653 class ShenandoahIterateObjectClosureRegionClosure: public ShenandoahHeapRegionClosure { 1654 ObjectClosure* _cl; 1655 public: 1656 ShenandoahIterateObjectClosureRegionClosure(ObjectClosure* cl) : _cl(cl) {} 1657 bool doHeapRegion(ShenandoahHeapRegion* r) { 1658 ShenandoahHeap::heap()->marked_object_iterate(r, _cl); 1659 return false; 1660 } 1661 }; 1662 1663 void ShenandoahHeap::object_iterate(ObjectClosure* cl) { 1664 ShenandoahIterateObjectClosureRegionClosure blk(cl); 1665 heap_region_iterate(&blk, false, true); 1666 } 1667 1668 class ShenandoahSafeObjectIterateAdjustPtrsClosure : public MetadataAwareOopClosure { 1669 private: 1670 ShenandoahHeap* _heap; 1671 1672 public: 1673 ShenandoahSafeObjectIterateAdjustPtrsClosure() : _heap(ShenandoahHeap::heap()) {} 1674 1675 private: 1676 template <class T> 1677 inline void do_oop_work(T* p) { 1678 T o = oopDesc::load_heap_oop(p); 1679 if (!oopDesc::is_null(o)) { 1680 oop obj = oopDesc::decode_heap_oop_not_null(o); 1681 oopDesc::encode_store_heap_oop(p, BrooksPointer::forwardee(obj)); 1682 } 1683 } 1684 public: 1685 void do_oop(oop* p) { 1686 do_oop_work(p); 1687 } 1688 void do_oop(narrowOop* p) { 1689 do_oop_work(p); 1690 } 1691 }; 1692 1693 class ShenandoahSafeObjectIterateAndUpdate : public ObjectClosure { 1694 private: 1695 ObjectClosure* _cl; 1696 public: 1697 ShenandoahSafeObjectIterateAndUpdate(ObjectClosure *cl) : _cl(cl) {} 1698 1699 virtual void do_object(oop obj) { 1700 assert (oopDesc::unsafe_equals(obj, BrooksPointer::forwardee(obj)), 1701 "avoid double-counting: only non-forwarded objects here"); 1702 1703 // Fix up the ptrs. 1704 ShenandoahSafeObjectIterateAdjustPtrsClosure adjust_ptrs; 1705 obj->oop_iterate(&adjust_ptrs); 1706 1707 // Can reply the object now: 1708 _cl->do_object(obj); 1709 } 1710 }; 1711 1712 void ShenandoahHeap::safe_object_iterate(ObjectClosure* cl) { 1713 assert(SafepointSynchronize::is_at_safepoint(), "safe iteration is only available during safepoints"); 1714 1715 // Safe iteration does objects only with correct references. 1716 // This is why we skip dirty regions that have stale copies of objects, 1717 // and fix up the pointers in the returned objects. 1718 1719 ShenandoahSafeObjectIterateAndUpdate safe_cl(cl); 1720 ShenandoahIterateObjectClosureRegionClosure blk(&safe_cl); 1721 heap_region_iterate(&blk, 1722 /* skip_dirty_regions = */ true, 1723 /* skip_humongous_continuations = */ true); 1724 1725 _need_update_refs = false; // already updated the references 1726 } 1727 1728 // Apply blk->doHeapRegion() on all committed regions in address order, 1729 // terminating the iteration early if doHeapRegion() returns true. 1730 void ShenandoahHeap::heap_region_iterate(ShenandoahHeapRegionClosure* blk, bool skip_dirty_regions, bool skip_humongous_continuation) const { 1731 for (size_t i = 0; i < _num_regions; i++) { 1732 ShenandoahHeapRegion* current = _ordered_regions->get(i); 1733 if (skip_humongous_continuation && current->is_humongous_continuation()) { 1734 continue; 1735 } 1736 if (skip_dirty_regions && in_collection_set(current)) { 1737 continue; 1738 } 1739 if (blk->doHeapRegion(current)) { 1740 return; 1741 } 1742 } 1743 } 1744 1745 class ClearLivenessClosure : public ShenandoahHeapRegionClosure { 1746 ShenandoahHeap* sh; 1747 public: 1748 ClearLivenessClosure(ShenandoahHeap* heap) : sh(heap) { } 1749 1750 bool doHeapRegion(ShenandoahHeapRegion* r) { 1751 r->clear_live_data(); 1752 sh->set_next_top_at_mark_start(r->bottom(), r->top()); 1753 return false; 1754 } 1755 }; 1756 1757 void ShenandoahHeap::start_concurrent_marking() { 1758 1759 shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::accumulate_stats); 1760 accumulate_statistics_all_tlabs(); 1761 shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::accumulate_stats); 1762 1763 set_concurrent_mark_in_progress(true); 1764 // We need to reset all TLABs because we'd lose marks on all objects allocated in them. 1765 if (UseTLAB) { 1766 shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::make_parsable); 1767 ensure_parsability(true); 1768 shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::make_parsable); 1769 } 1770 1771 _shenandoah_policy->record_bytes_allocated(_bytes_allocated_since_cm); 1772 _used_start_gc = used(); 1773 1774 #ifdef ASSERT 1775 if (ShenandoahDumpHeapBeforeConcurrentMark) { 1776 ensure_parsability(false); 1777 print_all_refs("pre-mark"); 1778 } 1779 #endif 1780 1781 shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::clear_liveness); 1782 ClearLivenessClosure clc(this); 1783 heap_region_iterate(&clc); 1784 shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::clear_liveness); 1785 1786 if (UseShenandoahMatrix) { 1787 connection_matrix()->clear_all(); 1788 } 1789 // print_all_refs("pre -mark"); 1790 1791 // oopDesc::_debug = true; 1792 1793 // Make above changes visible to worker threads 1794 OrderAccess::fence(); 1795 1796 shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::scan_roots); 1797 concurrentMark()->init_mark_roots(); 1798 shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::scan_roots); 1799 1800 // print_all_refs("pre-mark2"); 1801 } 1802 1803 class VerifyAfterEvacuationClosure : public ExtendedOopClosure { 1804 1805 ShenandoahHeap* _sh; 1806 1807 public: 1808 VerifyAfterEvacuationClosure() : _sh ( ShenandoahHeap::heap() ) {} 1809 1810 template<class T> void do_oop_nv(T* p) { 1811 T heap_oop = oopDesc::load_heap_oop(p); 1812 if (!oopDesc::is_null(heap_oop)) { 1813 oop obj = oopDesc::decode_heap_oop_not_null(heap_oop); 1814 guarantee(_sh->in_collection_set(obj) == (! oopDesc::unsafe_equals(obj, oopDesc::bs()->read_barrier(obj))), 1815 "forwarded objects can only exist in dirty (from-space) regions is_dirty: %s, is_forwarded: %s obj-klass: %s, marked: %s", 1816 BOOL_TO_STR(_sh->in_collection_set(obj)), 1817 BOOL_TO_STR(! oopDesc::unsafe_equals(obj, oopDesc::bs()->read_barrier(obj))), 1818 obj->klass()->external_name(), 1819 BOOL_TO_STR(_sh->is_marked_complete(obj)) 1820 ); 1821 obj = oopDesc::bs()->read_barrier(obj); 1822 guarantee(! _sh->in_collection_set(obj), "forwarded oops must not point to dirty regions"); 1823 guarantee(obj->is_oop(), "is_oop"); 1824 guarantee(Metaspace::contains(obj->klass()), "klass pointer must go to metaspace"); 1825 } 1826 } 1827 1828 void do_oop(oop* p) { do_oop_nv(p); } 1829 void do_oop(narrowOop* p) { do_oop_nv(p); } 1830 1831 }; 1832 1833 void ShenandoahHeap::verify_heap_after_evacuation() { 1834 1835 verify_heap_size_consistency(); 1836 1837 ensure_parsability(false); 1838 1839 VerifyAfterEvacuationClosure cl; 1840 roots_iterate(&cl); 1841 1842 ObjectToOopClosure objs(&cl); 1843 object_iterate(&objs); 1844 1845 } 1846 1847 void ShenandoahHeap::swap_mark_bitmaps() { 1848 // Swap bitmaps. 1849 CMBitMap* tmp1 = _complete_mark_bit_map; 1850 _complete_mark_bit_map = _next_mark_bit_map; 1851 _next_mark_bit_map = tmp1; 1852 1853 // Swap top-at-mark-start pointers 1854 HeapWord** tmp2 = _complete_top_at_mark_starts; 1855 _complete_top_at_mark_starts = _next_top_at_mark_starts; 1856 _next_top_at_mark_starts = tmp2; 1857 1858 HeapWord** tmp3 = _complete_top_at_mark_starts_base; 1859 _complete_top_at_mark_starts_base = _next_top_at_mark_starts_base; 1860 _next_top_at_mark_starts_base = tmp3; 1861 } 1862 1863 class VerifyReachableHeapClosure : public ExtendedOopClosure { 1864 private: 1865 SCMObjToScanQueue* _queue; 1866 ShenandoahHeap* _heap; 1867 CMBitMap* _map; 1868 bool _check_matrix; 1869 oop _obj; 1870 public: 1871 VerifyReachableHeapClosure(SCMObjToScanQueue* queue, CMBitMap* map, bool check_matrix) : 1872 _queue(queue), _heap(ShenandoahHeap::heap()), _map(map), _check_matrix(check_matrix) {}; 1873 template <class T> 1874 void do_oop_work(T* p) { 1875 T o = oopDesc::load_heap_oop(p); 1876 if (!oopDesc::is_null(o)) { 1877 oop obj = oopDesc::decode_heap_oop_not_null(o); 1878 guarantee(check_obj_alignment(obj), "sanity"); 1879 1880 guarantee(!oopDesc::is_null(obj), "sanity"); 1881 guarantee(_heap->is_in(obj), "sanity"); 1882 1883 oop forw = BrooksPointer::forwardee(obj); 1884 guarantee(!oopDesc::is_null(forw), "sanity"); 1885 guarantee(_heap->is_in(forw), "sanity"); 1886 1887 guarantee(oopDesc::unsafe_equals(obj, forw), "should not be forwarded"); 1888 1889 if (_check_matrix) { 1890 size_t from_idx = _heap->heap_region_index_containing(p); 1891 size_t to_idx = _heap->heap_region_index_containing(obj); 1892 if (!_heap->connection_matrix()->is_connected(from_idx, to_idx)) { 1893 tty->print_cr("from-obj: "); 1894 _obj->print_on(tty); 1895 tty->print_cr("to-obj:"); 1896 obj->print_on(tty); 1897 tty->print_cr("from-obj allocated after mark: %s", BOOL_TO_STR(_heap->allocated_after_complete_mark_start((HeapWord*) _obj))); 1898 tty->print_cr("to-obj allocated after mark: %s", BOOL_TO_STR(_heap->allocated_after_complete_mark_start((HeapWord*) obj))); 1899 tty->print_cr("from-obj marked: %s", BOOL_TO_STR(_heap->is_marked_complete(_obj))); 1900 tty->print_cr("to-obj marked: %s", BOOL_TO_STR(_heap->is_marked_complete(obj))); 1901 tty->print_cr("from-idx: " SIZE_FORMAT ", to-idx: " SIZE_FORMAT, from_idx, to_idx); 1902 1903 oop fwd_from = BrooksPointer::forwardee(_obj); 1904 oop fwd_to = BrooksPointer::forwardee(obj); 1905 tty->print_cr("from-obj forwardee: " PTR_FORMAT, p2i(fwd_from)); 1906 tty->print_cr("to-obj forwardee: " PTR_FORMAT, p2i(fwd_to)); 1907 tty->print_cr("forward(from-obj) marked: %s", BOOL_TO_STR(_heap->is_marked_complete(fwd_from))); 1908 tty->print_cr("forward(to-obj) marked: %s", BOOL_TO_STR(_heap->is_marked_complete(fwd_to))); 1909 size_t fwd_from_idx = _heap->heap_region_index_containing(fwd_from); 1910 size_t fwd_to_idx = _heap->heap_region_index_containing(fwd_to); 1911 tty->print_cr("forward(from-idx): " SIZE_FORMAT ", forward(to-idx): " SIZE_FORMAT, fwd_from_idx, fwd_to_idx); 1912 tty->print_cr("forward(from) connected with forward(to)? %s", BOOL_TO_STR(_heap->connection_matrix()->is_connected(fwd_from_idx, fwd_to_idx))); 1913 } 1914 guarantee(oopDesc::unsafe_equals(ShenandoahBarrierSet::resolve_oop_static_not_null(obj), obj), "polizeilich verboten"); 1915 guarantee(_heap->connection_matrix()->is_connected(from_idx, to_idx), "must be connected"); 1916 } 1917 1918 if (_map->parMark((HeapWord*) obj)) { 1919 _queue->push(SCMTask(obj)); 1920 } 1921 } 1922 } 1923 1924 void do_oop(oop* p) { do_oop_work(p); } 1925 void do_oop(narrowOop* p) { do_oop_work(p); } 1926 void set_obj(oop o) { _obj = o; } 1927 }; 1928 1929 void ShenandoahHeap::verify_heap_reachable_at_safepoint() { 1930 guarantee(SafepointSynchronize::is_at_safepoint(), "only when nothing else happens"); 1931 guarantee(ShenandoahVerify || (UseShenandoahMatrix && VerifyShenandoahMatrix), 1932 "only when these are enabled, and bitmap is initialized in ShenandoahHeap::initialize"); 1933 1934 OrderAccess::fence(); 1935 ensure_parsability(false); 1936 1937 // Allocate temporary bitmap for storing marking wavefront: 1938 MemRegion mr = MemRegion(_verification_bit_map.startWord(), _verification_bit_map.endWord()); 1939 _verification_bit_map.clear_range_large(mr); 1940 1941 // Initialize a single queue 1942 SCMObjToScanQueue* q = new SCMObjToScanQueue(); 1943 q->initialize(); 1944 1945 // Scan root set 1946 ShenandoahRootProcessor rp(this, 1); 1947 1948 { 1949 VerifyReachableHeapClosure cl(q, &_verification_bit_map, false); 1950 CLDToOopClosure cld_cl(&cl); 1951 CodeBlobToOopClosure code_cl(&cl, ! CodeBlobToOopClosure::FixRelocations); 1952 rp.process_all_roots(&cl, &cl, &cld_cl, &code_cl, 0); 1953 } 1954 1955 // Finish the scan 1956 { 1957 VerifyReachableHeapClosure cl(q, &_verification_bit_map, UseShenandoahMatrix && VerifyShenandoahMatrix); 1958 SCMTask task; 1959 while ((q->pop_buffer(task) || 1960 q->pop_local(task) || 1961 q->pop_overflow(task))) { 1962 oop obj = task.obj(); 1963 assert(!oopDesc::is_null(obj), "must not be null"); 1964 cl.set_obj(obj); 1965 obj->oop_iterate(&cl); 1966 } 1967 } 1968 1969 // Clean up! 1970 delete(q); 1971 } 1972 1973 void ShenandoahHeap::stop_concurrent_marking() { 1974 assert(concurrent_mark_in_progress(), "How else could we get here?"); 1975 if (! cancelled_concgc()) { 1976 // If we needed to update refs, and concurrent marking has been cancelled, 1977 // we need to finish updating references. 1978 set_need_update_refs(false); 1979 swap_mark_bitmaps(); 1980 } 1981 set_concurrent_mark_in_progress(false); 1982 1983 if (log_is_enabled(Trace, gc, region)) { 1984 ResourceMark rm; 1985 outputStream* out = Log(gc, region)::trace_stream(); 1986 print_heap_regions(out); 1987 } 1988 1989 } 1990 1991 void ShenandoahHeap::set_concurrent_mark_in_progress(bool in_progress) { 1992 _concurrent_mark_in_progress = in_progress ? 1 : 0; 1993 JavaThread::satb_mark_queue_set().set_active_all_threads(in_progress, !in_progress); 1994 } 1995 1996 void ShenandoahHeap::set_evacuation_in_progress_concurrently(bool in_progress) { 1997 // Note: it is important to first release the _evacuation_in_progress flag here, 1998 // so that Java threads can get out of oom_during_evacuation() and reach a safepoint, 1999 // in case a VM task is pending. 2000 set_evacuation_in_progress(in_progress); 2001 MutexLocker mu(Threads_lock); 2002 JavaThread::set_evacuation_in_progress_all_threads(in_progress); 2003 } 2004 2005 void ShenandoahHeap::set_evacuation_in_progress_at_safepoint(bool in_progress) { 2006 assert(SafepointSynchronize::is_at_safepoint(), "Only call this at safepoint"); 2007 set_evacuation_in_progress(in_progress); 2008 JavaThread::set_evacuation_in_progress_all_threads(in_progress); 2009 } 2010 2011 void ShenandoahHeap::set_evacuation_in_progress(bool in_progress) { 2012 _evacuation_in_progress = in_progress ? 1 : 0; 2013 OrderAccess::fence(); 2014 } 2015 2016 void ShenandoahHeap::verify_copy(oop p,oop c){ 2017 assert(! oopDesc::unsafe_equals(p, oopDesc::bs()->read_barrier(p)), "forwarded correctly"); 2018 assert(oopDesc::unsafe_equals(oopDesc::bs()->read_barrier(p), c), "verify pointer is correct"); 2019 if (p->klass() != c->klass()) { 2020 print_heap_regions(); 2021 } 2022 assert(p->klass() == c->klass(), "verify class p-size: "INT32_FORMAT" c-size: "INT32_FORMAT, p->size(), c->size()); 2023 assert(p->size() == c->size(), "verify size"); 2024 // Object may have been locked between copy and verification 2025 // assert(p->mark() == c->mark(), "verify mark"); 2026 assert(oopDesc::unsafe_equals(c, oopDesc::bs()->read_barrier(c)), "verify only forwarded once"); 2027 } 2028 2029 void ShenandoahHeap::oom_during_evacuation() { 2030 log_develop_trace(gc)("Out of memory during evacuation, cancel evacuation, schedule full GC by thread %d", 2031 Thread::current()->osthread()->thread_id()); 2032 2033 // We ran out of memory during evacuation. Cancel evacuation, and schedule a full-GC. 2034 collector_policy()->set_should_clear_all_soft_refs(true); 2035 concurrent_thread()->try_set_full_gc(); 2036 cancel_concgc(_oom_evacuation); 2037 2038 if ((! Thread::current()->is_GC_task_thread()) && (! Thread::current()->is_ConcurrentGC_thread())) { 2039 assert(! Threads_lock->owned_by_self() 2040 || SafepointSynchronize::is_at_safepoint(), "must not hold Threads_lock here"); 2041 log_warning(gc)("OOM during evacuation. Let Java thread wait until evacuation finishes."); 2042 while (_evacuation_in_progress) { // wait. 2043 Thread::current()->_ParkEvent->park(1); 2044 } 2045 } 2046 2047 } 2048 2049 HeapWord* ShenandoahHeap::tlab_post_allocation_setup(HeapWord* obj) { 2050 // Initialize Brooks pointer for the next object 2051 HeapWord* result = obj + BrooksPointer::word_size(); 2052 BrooksPointer::initialize(oop(result)); 2053 return result; 2054 } 2055 2056 uint ShenandoahHeap::oop_extra_words() { 2057 return BrooksPointer::word_size(); 2058 } 2059 2060 void ShenandoahHeap::grow_heap_by(size_t num_regions) { 2061 size_t old_num_regions = _num_regions; 2062 ensure_new_regions(num_regions); 2063 for (size_t i = 0; i < num_regions; i++) { 2064 size_t new_region_index = i + old_num_regions; 2065 HeapWord* start = ((HeapWord*) base()) + (ShenandoahHeapRegion::region_size_bytes() / HeapWordSize) * new_region_index; 2066 ShenandoahHeapRegion* new_region = new ShenandoahHeapRegion(this, start, ShenandoahHeapRegion::region_size_bytes() / HeapWordSize, new_region_index); 2067 2068 if (log_is_enabled(Trace, gc, region)) { 2069 ResourceMark rm; 2070 outputStream* out = Log(gc, region)::trace_stream(); 2071 out->print_cr("allocating new region at index: "SIZE_FORMAT, new_region_index); 2072 new_region->print_on(out); 2073 } 2074 2075 assert(_ordered_regions->active_regions() == new_region->region_number(), "must match"); 2076 _ordered_regions->add_region(new_region); 2077 _in_cset_fast_test_base[new_region_index] = false; // Not in cset 2078 _next_top_at_mark_starts_base[new_region_index] = new_region->bottom(); 2079 _complete_top_at_mark_starts_base[new_region_index] = new_region->bottom(); 2080 2081 _free_regions->add_region(new_region); 2082 } 2083 } 2084 2085 void ShenandoahHeap::ensure_new_regions(size_t new_regions) { 2086 2087 size_t num_regions = _num_regions; 2088 size_t new_num_regions = num_regions + new_regions; 2089 assert(new_num_regions <= _max_regions, "we checked this earlier"); 2090 2091 size_t expand_size = new_regions * ShenandoahHeapRegion::region_size_bytes(); 2092 log_trace(gc, region)("expanding storage by "SIZE_FORMAT_HEX" bytes, for "SIZE_FORMAT" new regions", expand_size, new_regions); 2093 bool success = _storage.expand_by(expand_size, ShenandoahAlwaysPreTouch); 2094 assert(success, "should always be able to expand by requested size"); 2095 2096 _num_regions = new_num_regions; 2097 2098 } 2099 2100 ShenandoahForwardedIsAliveClosure::ShenandoahForwardedIsAliveClosure() : 2101 _heap(ShenandoahHeap::heap_no_check()) { 2102 } 2103 2104 void ShenandoahForwardedIsAliveClosure::init(ShenandoahHeap* heap) { 2105 _heap = heap; 2106 } 2107 2108 bool ShenandoahForwardedIsAliveClosure::do_object_b(oop obj) { 2109 2110 assert(_heap != NULL, "sanity"); 2111 obj = ShenandoahBarrierSet::resolve_oop_static_not_null(obj); 2112 #ifdef ASSERT 2113 if (_heap->concurrent_mark_in_progress()) { 2114 assert(oopDesc::unsafe_equals(obj, ShenandoahBarrierSet::resolve_oop_static_not_null(obj)), "only query to-space"); 2115 } 2116 #endif 2117 assert(!oopDesc::is_null(obj), "null"); 2118 return _heap->is_marked_next(obj); 2119 } 2120 2121 void ShenandoahHeap::ref_processing_init() { 2122 MemRegion mr = reserved_region(); 2123 2124 isAlive.init(ShenandoahHeap::heap()); 2125 assert(_max_workers > 0, "Sanity"); 2126 2127 _ref_processor = 2128 new ReferenceProcessor(mr, // span 2129 ParallelRefProcEnabled, 2130 // mt processing 2131 _max_workers, 2132 // degree of mt processing 2133 true, 2134 // mt discovery 2135 _max_workers, 2136 // degree of mt discovery 2137 false, 2138 // Reference discovery is not atomic 2139 &isAlive); 2140 } 2141 2142 size_t ShenandoahHeap::num_regions() { 2143 return _num_regions; 2144 } 2145 2146 size_t ShenandoahHeap::max_regions() { 2147 return _max_regions; 2148 } 2149 2150 GCTracer* ShenandoahHeap::tracer() { 2151 return shenandoahPolicy()->tracer(); 2152 } 2153 2154 size_t ShenandoahHeap::tlab_used(Thread* thread) const { 2155 return _free_regions->used(); 2156 } 2157 2158 void ShenandoahHeap::cancel_concgc(GCCause::Cause cause) { 2159 if (try_cancel_concgc()) { 2160 log_info(gc)("Cancelling concurrent GC: %s", GCCause::to_string(cause)); 2161 _shenandoah_policy->report_concgc_cancelled(); 2162 } 2163 } 2164 2165 void ShenandoahHeap::cancel_concgc(ShenandoahCancelCause cause) { 2166 if (try_cancel_concgc()) { 2167 log_info(gc)("Cancelling concurrent GC: %s", cancel_cause_to_string(cause)); 2168 _shenandoah_policy->report_concgc_cancelled(); 2169 } 2170 } 2171 2172 const char* ShenandoahHeap::cancel_cause_to_string(ShenandoahCancelCause cause) { 2173 switch (cause) { 2174 case _oom_evacuation: 2175 return "Out of memory for evacuation"; 2176 case _vm_stop: 2177 return "Stopping VM"; 2178 default: 2179 return "Unknown"; 2180 } 2181 } 2182 2183 uint ShenandoahHeap::max_workers() { 2184 return _max_workers; 2185 } 2186 2187 void ShenandoahHeap::stop() { 2188 // The shutdown sequence should be able to terminate when GC is running. 2189 2190 // Step 1. Notify control thread that we are in shutdown. 2191 // Note that we cannot do that with stop(), because stop() is blocking and waits for the actual shutdown. 2192 // Doing stop() here would wait for the normal GC cycle to complete, never falling through to cancel below. 2193 _concurrent_gc_thread->prepare_for_graceful_shutdown(); 2194 2195 // Step 2. Notify GC workers that we are cancelling GC. 2196 cancel_concgc(_vm_stop); 2197 2198 // Step 3. Wait until GC worker exits normally. 2199 _concurrent_gc_thread->stop(); 2200 } 2201 2202 void ShenandoahHeap::unload_classes_and_cleanup_tables() { 2203 ShenandoahForwardedIsAliveClosure is_alive; 2204 // Unload classes and purge SystemDictionary. 2205 bool purged_class = SystemDictionary::do_unloading(&is_alive, true); 2206 ParallelCleaningTask unlink_task(&is_alive, true, true, _workers->active_workers(), purged_class); 2207 _workers->run_task(&unlink_task); 2208 ClassLoaderDataGraph::purge(); 2209 } 2210 2211 void ShenandoahHeap::set_need_update_refs(bool need_update_refs) { 2212 _need_update_refs = need_update_refs; 2213 } 2214 2215 //fixme this should be in heapregionset 2216 ShenandoahHeapRegion* ShenandoahHeap::next_compaction_region(const ShenandoahHeapRegion* r) { 2217 size_t region_idx = r->region_number() + 1; 2218 ShenandoahHeapRegion* next = _ordered_regions->get(region_idx); 2219 guarantee(next->region_number() == region_idx, "region number must match"); 2220 while (next->is_humongous()) { 2221 region_idx = next->region_number() + 1; 2222 next = _ordered_regions->get(region_idx); 2223 guarantee(next->region_number() == region_idx, "region number must match"); 2224 } 2225 return next; 2226 } 2227 2228 void ShenandoahHeap::set_region_in_collection_set(size_t region_index, bool b) { 2229 _in_cset_fast_test_base[region_index] = b; 2230 } 2231 2232 ShenandoahMonitoringSupport* ShenandoahHeap::monitoring_support() { 2233 return _monitoring_support; 2234 } 2235 2236 CMBitMap* ShenandoahHeap::complete_mark_bit_map() { 2237 return _complete_mark_bit_map; 2238 } 2239 2240 CMBitMap* ShenandoahHeap::next_mark_bit_map() { 2241 return _next_mark_bit_map; 2242 } 2243 2244 void ShenandoahHeap::add_free_region(ShenandoahHeapRegion* r) { 2245 _free_regions->add_region(r); 2246 } 2247 2248 void ShenandoahHeap::clear_free_regions() { 2249 _free_regions->clear(); 2250 } 2251 2252 address ShenandoahHeap::in_cset_fast_test_addr() { 2253 return (address) (ShenandoahHeap::heap()->_in_cset_fast_test); 2254 } 2255 2256 address ShenandoahHeap::cancelled_concgc_addr() { 2257 return (address) &(ShenandoahHeap::heap()->_cancelled_concgc); 2258 } 2259 2260 void ShenandoahHeap::clear_cset_fast_test() { 2261 assert(_in_cset_fast_test_base != NULL, "sanity"); 2262 memset(_in_cset_fast_test_base, false, 2263 _in_cset_fast_test_length * sizeof(bool)); 2264 } 2265 2266 size_t ShenandoahHeap::conservative_max_heap_alignment() { 2267 return ShenandoahMaxRegionSize; 2268 } 2269 2270 size_t ShenandoahHeap::bytes_allocated_since_cm() { 2271 return _bytes_allocated_since_cm; 2272 } 2273 2274 void ShenandoahHeap::set_bytes_allocated_since_cm(size_t bytes) { 2275 _bytes_allocated_since_cm = bytes; 2276 } 2277 2278 void ShenandoahHeap::set_next_top_at_mark_start(HeapWord* region_base, HeapWord* addr) { 2279 uintx index = ((uintx) region_base) >> ShenandoahHeapRegion::region_size_shift(); 2280 _next_top_at_mark_starts[index] = addr; 2281 } 2282 2283 HeapWord* ShenandoahHeap::next_top_at_mark_start(HeapWord* region_base) { 2284 uintx index = ((uintx) region_base) >> ShenandoahHeapRegion::region_size_shift(); 2285 return _next_top_at_mark_starts[index]; 2286 } 2287 2288 void ShenandoahHeap::set_complete_top_at_mark_start(HeapWord* region_base, HeapWord* addr) { 2289 uintx index = ((uintx) region_base) >> ShenandoahHeapRegion::region_size_shift(); 2290 _complete_top_at_mark_starts[index] = addr; 2291 } 2292 2293 HeapWord* ShenandoahHeap::complete_top_at_mark_start(HeapWord* region_base) { 2294 uintx index = ((uintx) region_base) >> ShenandoahHeapRegion::region_size_shift(); 2295 return _complete_top_at_mark_starts[index]; 2296 } 2297 2298 void ShenandoahHeap::set_full_gc_in_progress(bool in_progress) { 2299 _full_gc_in_progress = in_progress; 2300 } 2301 2302 bool ShenandoahHeap::is_full_gc_in_progress() const { 2303 return _full_gc_in_progress; 2304 } 2305 2306 void ShenandoahHeap::set_update_refs_in_progress(bool in_progress) { 2307 _update_refs_in_progress = in_progress; 2308 } 2309 2310 bool ShenandoahHeap::is_update_refs_in_progress() const { 2311 return _update_refs_in_progress; 2312 } 2313 2314 class NMethodOopInitializer : public OopClosure { 2315 private: 2316 ShenandoahHeap* _heap; 2317 public: 2318 NMethodOopInitializer() : _heap(ShenandoahHeap::heap()) { 2319 } 2320 2321 private: 2322 template <class T> 2323 inline void do_oop_work(T* p) { 2324 T o = oopDesc::load_heap_oop(p); 2325 if (! oopDesc::is_null(o)) { 2326 oop obj1 = oopDesc::decode_heap_oop_not_null(o); 2327 oop obj2 = oopDesc::bs()->write_barrier(obj1); 2328 if (! oopDesc::unsafe_equals(obj1, obj2)) { 2329 oopDesc::encode_store_heap_oop(p, obj2); 2330 } 2331 } 2332 } 2333 2334 public: 2335 void do_oop(oop* o) { 2336 do_oop_work(o); 2337 } 2338 void do_oop(narrowOop* o) { 2339 do_oop_work(o); 2340 } 2341 }; 2342 2343 void ShenandoahHeap::register_nmethod(nmethod* nm) { 2344 NMethodOopInitializer init; 2345 nm->oops_do(&init); 2346 nm->fix_oop_relocations(); 2347 } 2348 2349 void ShenandoahHeap::unregister_nmethod(nmethod* nm) { 2350 } 2351 2352 void ShenandoahHeap::pin_object(oop o) { 2353 heap_region_containing(o)->pin(); 2354 } 2355 2356 void ShenandoahHeap::unpin_object(oop o) { 2357 heap_region_containing(o)->unpin(); 2358 } 2359 2360 2361 GCTimer* ShenandoahHeap::gc_timer() const { 2362 return _gc_timer; 2363 } 2364 2365 class ShenandoahCountGarbageClosure : public ShenandoahHeapRegionClosure { 2366 private: 2367 size_t _garbage; 2368 public: 2369 ShenandoahCountGarbageClosure() : _garbage(0) { 2370 } 2371 2372 bool doHeapRegion(ShenandoahHeapRegion* r) { 2373 if (! r->is_humongous() && ! r->is_pinned() && ! r->in_collection_set()) { 2374 _garbage += r->garbage(); 2375 } 2376 return false; 2377 } 2378 2379 size_t garbage() { 2380 return _garbage; 2381 } 2382 }; 2383 2384 size_t ShenandoahHeap::garbage() { 2385 ShenandoahCountGarbageClosure cl; 2386 heap_region_iterate(&cl); 2387 return cl.garbage(); 2388 } 2389 2390 ShenandoahConnectionMatrix* ShenandoahHeap::connection_matrix() { 2391 return _connection_matrix; 2392 } 2393 2394 ShenandoahPartialGC* ShenandoahHeap::partial_gc() { 2395 return _partial_gc; 2396 } 2397 2398 void ShenandoahHeap::do_partial_collection() { 2399 partial_gc()->do_partial_collection(); 2400 } 2401 2402 template<class T> 2403 class ShenandoahUpdateHeapRefsTask : public AbstractGangTask { 2404 private: 2405 T cl; 2406 ShenandoahHeap* _heap; 2407 ShenandoahHeapRegionSet* _regions; 2408 2409 public: 2410 ShenandoahUpdateHeapRefsTask(ShenandoahHeapRegionSet* regions) : 2411 AbstractGangTask("Concurrent Update References Task"), 2412 cl(T()), 2413 _heap(ShenandoahHeap::heap()), 2414 _regions(regions) { 2415 } 2416 2417 void work(uint worker_id) { 2418 ShenandoahHeapRegion* r = _regions->claim_next(); 2419 while (r != NULL) { 2420 if (! _heap->in_collection_set(r) && 2421 ! r->is_empty()) { 2422 _heap->marked_object_oop_safe_iterate(r, &cl); 2423 } else if (_heap->in_collection_set(r)) { 2424 HeapWord* bottom = r->bottom(); 2425 HeapWord* top = _heap->complete_top_at_mark_start(r->bottom()); 2426 if (top > bottom) { 2427 _heap->complete_mark_bit_map()->clear_range_large(MemRegion(bottom, top)); 2428 } 2429 } 2430 if (_heap->cancelled_concgc()) { 2431 return; 2432 } 2433 r = _regions->claim_next(); 2434 } 2435 } 2436 }; 2437 2438 void ShenandoahHeap::update_heap_references(ShenandoahHeapRegionSet* update_regions) { 2439 if (UseShenandoahMatrix) { 2440 ShenandoahUpdateHeapRefsTask<ShenandoahUpdateHeapRefsMatrixClosure> task(update_regions); 2441 workers()->run_task(&task); 2442 } else { 2443 ShenandoahUpdateHeapRefsTask<ShenandoahUpdateHeapRefsClosure> task(update_regions); 2444 workers()->run_task(&task); 2445 } 2446 } 2447 2448 void ShenandoahHeap::concurrent_update_heap_references() { 2449 _shenandoah_policy->record_phase_start(ShenandoahCollectorPolicy::conc_update_refs); 2450 ShenandoahHeapRegionSet* update_regions = regions(); 2451 update_regions->clear_current_index(); 2452 update_heap_references(update_regions); 2453 _shenandoah_policy->record_phase_end(ShenandoahCollectorPolicy::conc_update_refs); 2454 } 2455 2456 void ShenandoahHeap::prepare_update_refs() { 2457 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 2458 set_evacuation_in_progress_at_safepoint(false); 2459 set_update_refs_in_progress(true); 2460 ensure_parsability(true); 2461 if (UseShenandoahMatrix) { 2462 connection_matrix()->clear_all(); 2463 } 2464 for (uint i = 0; i < _num_regions; i++) { 2465 ShenandoahHeapRegion* r = _ordered_regions->get(i); 2466 r->set_concurrent_iteration_safe_limit(r->top()); 2467 } 2468 } 2469 2470 void ShenandoahHeap::finish_update_refs() { 2471 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 2472 2473 if (cancelled_concgc()) { 2474 // Finish updating references where we left off. 2475 clear_cancelled_concgc(); 2476 ShenandoahHeapRegionSet* update_regions = regions(); 2477 update_heap_references(update_regions); 2478 } 2479 2480 assert(! cancelled_concgc(), "Should have been done right before"); 2481 concurrentMark()->update_roots(ShenandoahCollectorPolicy::final_update_refs_roots); 2482 recycle_dirty_regions(); 2483 set_need_update_refs(false); 2484 2485 if (ShenandoahVerify) { 2486 verify_update_refs(); 2487 } 2488 2489 { 2490 // Rebuild the free set 2491 ShenandoahHeapLock hl(this); 2492 _free_regions->clear(); 2493 size_t end = _ordered_regions->active_regions(); 2494 for (size_t i = 0; i < end; i++) { 2495 ShenandoahHeapRegion* r = _ordered_regions->get(i); 2496 if (!r->is_humongous()) { 2497 assert (!in_collection_set(r), "collection set should be clear"); 2498 _free_regions->add_region(r); 2499 } 2500 } 2501 } 2502 set_update_refs_in_progress(false); 2503 } 2504 2505 class ShenandoahVerifyUpdateRefsClosure : public ExtendedOopClosure { 2506 private: 2507 template <class T> 2508 void do_oop_work(T* p) { 2509 T o = oopDesc::load_heap_oop(p); 2510 if (! oopDesc::is_null(o)) { 2511 oop obj = oopDesc::decode_heap_oop_not_null(o); 2512 guarantee(oopDesc::unsafe_equals(obj, ShenandoahBarrierSet::resolve_oop_static_not_null(obj)), 2513 "must not be forwarded"); 2514 } 2515 } 2516 public: 2517 void do_oop(oop* p) { do_oop_work(p); } 2518 void do_oop(narrowOop* p) { do_oop_work(p); } 2519 }; 2520 2521 void ShenandoahHeap::verify_update_refs() { 2522 2523 ensure_parsability(false); 2524 2525 ShenandoahVerifyUpdateRefsClosure cl; 2526 2527 // Verify roots. 2528 { 2529 CodeBlobToOopClosure blobsCl(&cl, false); 2530 CLDToOopClosure cldCl(&cl); 2531 ShenandoahRootProcessor rp(this, 1); 2532 rp.process_all_roots(&cl, &cl, &cldCl, &blobsCl, 0); 2533 } 2534 2535 // Verify heap. 2536 for (uint i = 0; i < num_regions(); i++) { 2537 ShenandoahHeapRegion* r = regions()->get(i); 2538 marked_object_oop_iterate(r, &cl); 2539 } 2540 } 2541 2542 #ifdef ASSERT 2543 void ShenandoahHeap::assert_heaplock_owned_by_current_thread() { 2544 assert(_heap_lock == locked, "must be locked"); 2545 assert(_heap_lock_owner == Thread::current(), "must be owned by current thread"); 2546 } 2547 2548 void ShenandoahHeap::assert_heaplock_or_safepoint() { 2549 Thread* thr = Thread::current(); 2550 assert((_heap_lock == locked && _heap_lock_owner == thr) || 2551 (SafepointSynchronize::is_at_safepoint() && thr->is_VM_thread()), 2552 "must own heap lock or by VM thread at safepoint"); 2553 } 2554 2555 #endif